fork(1) download
  1. #include <stdio.h>
  2.  
  3. #define MAX_N 100
  4. #define MAX_W 1000
  5. int n;
  6. int dp[MAX_N+1][MAX_W+1];
  7. int weight[MAX_N+1];
  8. int cost[MAX_N+1];
  9. int CAP;
  10. int func(int i,int w)
  11. {
  12. if(i==n+1) return 0;
  13. if(dp[i][w]!=-1) return dp[i][w];
  14. int profit1=0, profit2=0;
  15. if(w+weight[i]<=CAP)
  16. profit1=cost[i]+func(i+1,w+weight[i]);
  17. profit2=func(i+1,w);
  18. dp[i][w]= max(profit1,profit2);
  19. return dp[i][w];
  20. }
  21. int max(int p1, int p2)
  22. {
  23. if(p1>p2) return p1;
  24. else if(p1<=p2) return p2;
  25. }
  26. int main()
  27. {
  28. int i;
  29.  
  30. freopen("in","r",stdin);
  31. memset(dp,-1,sizeof(dp));
  32. scanf("%d%d",&n,&CAP);
  33. for(i=0;i<n;i++)
  34. {
  35. scanf("%d%d\n",&weight[i],&cost[i]);
  36. }
  37. printf("%d\n",func(1,0));
  38. }
  39.  
  40.  
Success #stdin #stdout 0s 2184KB
stdin
Standard input is empty
stdout
0