fork(1) download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define PP pair<int,int>
  6. int n,target;
  7. int cost[102], weight[102];
  8. int CAP;
  9.  
  10. pair<int,int> Knap(int i,int w)
  11. {
  12. if(i==n+1) return make_pair(0,w);
  13. pair<int,int> profit1, profit2;
  14.  
  15. if(w+weight[i]<=CAP) {
  16. pair<int,int> tmp = Knap(i+1, w + weight[i]);
  17. profit1 = make_pair(tmp.first+cost[i], tmp.second);
  18. }
  19. else
  20. profit1 = make_pair(0,0);
  21.  
  22. profit2=Knap(i+1,w);
  23. return max(profit1, profit2);
  24. }
  25.  
  26. int main()
  27. {
  28. int t;
  29. cin>>t;
  30. while(t--)
  31. {
  32. cin>>n;
  33. cin>>CAP;
  34.  
  35. for(int i=1; i<=n; i++)
  36. {
  37. scanf("%d %d",&weight[i],&cost[i]);
  38. }
  39. pair<int,int>P;
  40. P=Knap(1,0);
  41.  
  42. cout<<P.first<<" "<<P.second<<endl;
  43. }
  44. return 0;
  45. }
Success #stdin #stdout 0s 3144KB
stdin
1
4 10
1 120
4 280
3 150
4 200
stdout
600 9