fork(1) download
  1. #include <stdio.h>
  2. #define MAXV 1000
  3. int dp[MAXV];
  4.  
  5. int main (){
  6. int N,V;
  7. scanf("%d %d",&N,&V);
  8. int a[1000]; //体积
  9. int b[1000]; //价值
  10. int i,j;
  11. for (i=1;i<=N;i++){
  12. scanf("%d %d",&a[i],&b[i]);
  13. }
  14.  
  15. //完全背包:j从小到大
  16. for(i = 1; i <= N; i++){
  17. for(j = a[i]; j <= V; j++){
  18. if(dp[j - a[i]] + b[i] > dp[j]){
  19. dp[j] = dp[j - a[i]] + b[i];
  20. }
  21. }
  22. }
  23. printf("%d", dp[V]);
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0s 5300KB
stdin
3 7
2 3
6 7
1 4
stdout
28