fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdio>
  4.  
  5. using namespace std;
  6. #define N 500+5
  7. #define MAX 1000005
  8.  
  9. #define sci(a) scanf("%d", &a)
  10. #define sci2(a,b) scanf("%d%d", &a, &b)
  11.  
  12.  
  13. int Max(int a, int b){
  14. if(a > b)
  15. return a;
  16. else return b;
  17. }
  18. int knapsack(int w,int n, int wt[], int value[])
  19. {
  20. vector<int>result[n+5];
  21. for(int i = 0; i <=n; i++){
  22. for(int j=0; j <= w; j++){
  23. if(i==0 || j==0)
  24. result[i].push_back(0);
  25. else if( wt[i-1] <= j )
  26. result[i].push_back(Max(value[i-1]+ result[i-1][(j-wt[i-1])], result[i-1][j]));
  27. else
  28. result[i].push_back(result[i-1][j]);
  29. }
  30. }
  31. return result[n][w];
  32. }
  33. int main()
  34. {
  35. int w, n;
  36. sci2(w,n);
  37. int wt[n], value[n];
  38. for(int i=0; i<n; i++)
  39. sci2(value[i], wt[i]);
  40. printf("%d\n", knapsack(w,n, wt, value));
  41.  
  42. return 0;
  43. }
  44.  
  45.  
  46.  
Runtime error #stdin #stdout 0s 16296KB
stdin
Standard input is empty
stdout
Standard output is empty