fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define MAX_N 100
  6. #define MAX_W 1000
  7. int cnt=0;
  8.  
  9. int n;
  10. int dp[MAX_W+1];
  11. int weight[MAX_N+1];
  12. int cost[MAX_N+1];
  13. int CAP;
  14. int func(int i,int w)
  15. {
  16. cnt++;
  17. if(i==n+1) return 0;
  18. if(dp[w]!=-1) return dp[w];
  19.  
  20. int profit1=0,profit2=0;
  21. if(w+weight[i]<=CAP)
  22. profit1=cost[i]+func(i+1,w+weight[i]);
  23.  
  24. profit2=func(i+1,w);
  25.  
  26. dp[w]=max(profit1,profit2);
  27. return dp[w];
  28. }
  29.  
  30. int main()
  31. {
  32.  
  33. memset(dp,-1,sizeof(dp));
  34. int i,j,k,l;
  35. scanf("%d %d",&n,&CAP);
  36. for(i=1;i<=n;i++)
  37. {
  38. scanf("%d %d",&weight[i],&cost[i]);
  39. }
  40.  
  41. printf("%d",func(1,0));
  42.  
  43. }
  44.  
  45. //4 6
  46. //4 5
  47. //3 5
  48. //1 5
  49. //3 6
  50. //answer: 11
  51.  
Success #stdin #stdout 0s 3104KB
stdin
4 6
4 5
3 5
1 5
3 6
stdout
15