plz check the code in the chat box
One test not passing plz check
#include<bits/stdc++.h> using namespace std; int knap(int *size,int *val,int n,int c,int dp[][1000]){ if(c<0){ return 0; } if(c==0||n==0){ return 0; } if(dp[n][c]!=-1){ return dp[n][c]; } int a=0,b=0; if(c>=size[0]) a=knap(size,val,n,c-size[0],dp)+val[0]; b=knap(size+1,val+1,n-1,c,dp); int x=max(a,b); dp[n][c]=x; return x; } int main() { int n,c; cin>>n>>c; int size[n],val[n]; for(int i=0;i<n; i++){ cin>>size[i]; } for(int i=0;i<n; i++){ cin>>val[i]; } int dp[1000][1000]; for(int i=0;i<1000;i++){ for(int j=0;j<1000;j++) dp[i][j]=-1; } cout<<knap(size,val,n,c,dp); return 0; }
check now->
prepare dp upto 1000 X 1000 so size should be atlest 1001 X 1001
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.