Greeting everyone, I'm trying to solve 0/1 Knapsack problem using the Dynamic Programming Top-Down Approach. I'm pretty sure that most of my logic is correct, my code is compiling successfully. But, it's not giving the proper/correct output that is needed. For Instance, suppose weight[] has inputs as 10,20,30 and it's corresponding value[] has 60,100,120. The max weight that the Knapsack can hold onto is 50. The max profit should be 220, but my code is giving me the answer 280 instead. Please help me, here's my piece of code:-
#include<bits/stdc++.h>
using namespace std;
void knapsack(vector<int>& weight, vector<int>& value, int w, int n){
vector<vector<int>> t;
for(int i=0;i<n+1;++i){
vector<int> temp;
for(int j=0;j<w+1;++j){
int x =0;
temp.push_back(x);
}
t.push_back(temp);
temp.clear();
}
for(int i=1;i<n+1;++i){
for(int j=1;j<w+1;++j){
if(weight[i-1]<=w){
t[i][j] = max(value[i-1]+t[i-1][w-weight[i-1]], t[i-1][j]);
}
else{
t[i][j] = t[i-1][j];
}
}
}
cout<<"Max Profit: "<<t[n][w];
// return final;
// vector<int> oneDimVector;
// for(int i = 0; i < n+1; i++){
// for(int j = 0; j < w+1; j++){
// oneDimVector.push_back(t[i][j]);
// }
// }
// vector<int>::iterator maxElement;
// maxElement = max_element(oneDimVector.begin(), oneDimVector.end());
// cout<<"Max Profit: "<<*maxElement;
}
int main(){
int n;
int w;//Total weight of knapsack
cin>>n;
cin>>w;
vector<int> weight;
vector<int> value;
for(int i=0;i<n;++i){
int x;
cin>>x;
weight.push_back(x);
}
for(int i=0;i<n;++i){
int x;
cin>>x;
value.push_back(x);
}
knapsack(weight,value,w,n);
}