@codestar05
Its a basic dp knapsack problem .
that is why your logic will not work.
Here is my code for reference.
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<algorithm>
#include<numeric>
using namespace std;
typedef long long int lli;
using namespace __gnu_pbds;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//typedef long long int lli;
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> multi_pbds;
//typedef tree<pair<int , int>, null_type, less<pair<int ,int>>, rb_tree_tag, tree_order_statistics_node_update> pbdsp;
//typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define nl "\n";
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define db long double
#define prq priority_queue<lli>
#define psq priority_queue<lli,vector<lli>,greater<lli>>
#define mod 1000000007
#define lb lower_bound
#define ub upper_bound
#define vlli vector<lli>
#define mslli multiset<lli>
#define inf 1e17
#define sp " "
#define pb push_back
#define pie 3.14159265358979323846
#define test lli t; cin>>t; while(t--)
int dp[101][101];
int cal(int i,int c[],int p[],int t[],int w,int n)
{
// cout<<i<<" "<<w<<endl;
if(i==n)
return 0;
if(dp[i][w]!=-1)
return dp[i][w];
int take=0;
int nottake=0;
if(w>=t[i])
{
take=(c[i]*p[i])+cal(i+1,c,p,t,w-t[i],n);
}
nottake=cal(i+1,c,p,t,w,n);
return dp[i][w]=max(take,nottake);
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
test{
int n,w;
cin>>n>>w;
// cout<<w<<endl;
int c[n],p[n],t[n];
for(int i=0;i<n;i++)
{
cin>>c[i]>>p[i]>>t[i];
}
memset(dp,-1,sizeof(dp));
// cout<<w<<endl;
cout<<cal(0,c,p,t,w,n)<<endl;
}
}