Advertisement
tumaryui

knapsack

Nov 16th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define pb push_back
  4. using namespace std;
  5. const int N = 600;
  6. int n, W, c[N], w[N];
  7. int dp[N][25020];
  8. main()
  9. {
  10. cin >> n >> W;
  11. int sc= 0;
  12. int mx = 0;
  13. for(int i = 0; i < n; i++)
  14. {
  15. cin >> w[i + 1] >> c[i + 1];
  16. mx = max(mx, c[i + 1]);
  17. sc += c[i + 1];
  18. }
  19.  
  20. dp[0][0] = 0;
  21. for(int i = 1; i < 25020; i++)
  22. {
  23. dp[0][i] = W + 1;
  24. }
  25.  
  26. for(int i = 1; i <= n; i++)
  27. {
  28. for(int j = 0; j <= sc; j++)
  29. {
  30. if(j - c[i] < 0)
  31. {
  32. dp[i][j] = dp[i - 1][j];
  33. continue;
  34. }
  35. dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - c[i]] + w[i]);
  36. }
  37. }
  38. int ans = 0;
  39. for(int i = 0; i <= sc; i++)
  40. {
  41. if(dp[n][i] <= W) ans = max(ans, i);
  42. }
  43. cout << ans;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement