Minimum money needed

this is my code.I have tried to do this ques in 1D DP but it showing wrong answer can u pls tell me that what is the mistake and also the testcase in which it will give wrong answer so that i can dry run

#include<bits/stdc++.h>
using namespace std;
int money(int *arr, int n,int w,int cap,int *dp)
{
if(cap==0)
{
return 0;
}

if(dp[cap]!=INT_MAX)
{

   return dp[cap];

}
int ans=0;
int ans1=INT_MAX;
for(int i=1;i<=n;i++)
{
if(arr[i]!=-1 && (cap-i)>=0)
{
ans=arr[i]+money(arr,n,w,cap-i,dp);
ans1=min(ans,ans1);
}
}
dp[cap]=ans1;
return dp[cap];
}
int main()
{
int n,w;
cin>>n>>w;
int *arr=new int[n];
arr[0]=0;
for(int i=1;i<=n;i++)
{
cin>>arr[i];
}
int *dp=new int[w+1];
dp[0]=0;
for(int i=1;i<=w;i++)
{
dp[i]=INT_MAX;
}
int p=money(arr,n,w,w,dp);
if(p==INT_MAX)
{
cout<<"-1";
}
else
{
cout<<p;
}
cout<<endl;
for(int i=0;i<=w;i++)
{
cout<<dp[i]<<" ";
}
}