Getting TLE in testcase 0.
Code:
#include
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll int helper(int s,int n,vector &w,vector &val,vector<vector> &dp)
{
if(s==0||n<0)
return 0;
if(dp[s][n]!=INT_MIN)
return dp[s][n];
if(s<w[n])
{
return dp[s][n] = helper(s,n-1,w,val,dp);
}
else
{
// return max(max(val[n]+helper(s-w[n],n-1,w,val,dp),helper(s,n-1,w,val,dp)),val[n]+helper(s-w[n],n,w,val,dp));
return max(val[n]+helper(s-w[n],n,w,val,dp),helper(s,n-1,w,val,dp));
}
}
int main() {
int n,s;
cin>>n>>s;
vector<int> w;
vector<ll int> val;
for(int i=0;i<n;i++)
{ int temp;
cin>>temp;
w.push_back(temp);
}
ll int temp;
for(int i=0;i<n;i++)
{
cin>>temp;
val.push_back(temp);
}
vector<vector<ll int>> dp(s+1,vector<ll int>(n,INT_MIN));
cout<<helper(s,n-1,w,val,dp);
return 0;
}