fork download
  1. #define _GLIBCXX_FILESYSTEM
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5.  
  6. int solve(vector<int>&tw,vector<int>&tv,int ix,int w)
  7. {
  8. if(w==0) return 0;
  9. if(ix==tw.size())return 0;
  10.  
  11. int ans=0;
  12. ans=solve(tw,tv,ix+1,w);
  13. if(w-tw[ix]>=0)
  14. ans=max(ans,(solve(tw,tv,ix+1,w-tw[ix])+tv[ix]));
  15.  
  16. return ans;
  17.  
  18. }
  19.  
  20. int main()
  21. {
  22. int t;
  23. cin>>t;
  24. while(t--)
  25. {
  26.  
  27. int n,w; //items numebr, total weight
  28. cin>>n>>w;
  29. vector<int> tw(n); // item weight
  30. vector<int> tv(n); // item value;
  31. for(int i=0;i<n;i++)
  32. {
  33. cin>>tw[i]>>tv[i];
  34. }
  35.  
  36. cout<<solve(tw,tv,0,w);
  37.  
  38.  
  39.  
  40.  
  41. }
  42.  
  43. }
  44.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Standard output is empty