Josif_tepe

Untitled

Oct 29th, 2025
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <cstring>
  6. #include <map>
  7. using namespace std;
  8. typedef long long ll;
  9.  
  10. const int maxn = 105;
  11. const ll INF = 1e16;
  12. int n;
  13. ll W;
  14. int weights[maxn], values[maxn];
  15. ll dp[maxn][100005];
  16.  
  17. ll rec(int at, int value_left) {
  18. if(value_left == 0) {
  19.     return 0;
  20. }
  21. if(at >= n) {
  22.     return INF;
  23. }
  24.  
  25. if(dp[at][value_left] != -1) {
  26.  return dp[at][value_left];
  27. }
  28.  
  29. ll res = INF;
  30. res = min(res, rec(at + 1, value_left));
  31. if(value_left - values[at] >= 0) {
  32.     res = min(res, rec(at + 1, value_left - values[at]) + weights[at]);
  33. }
  34. dp[at][value_left] = res;
  35. return res;
  36. }
  37. int main()
  38. {
  39. memset(dp, -1, sizeof dp);
  40. cin >> n >> W;
  41.  
  42. int sum = 0;
  43. for(int i = 0; i < n; i++) {
  44.     cin >> weights[i] >> values[i];
  45.    
  46.     sum += values[i];
  47. }
  48.  
  49.  
  50. for(int i = sum; i >= 0; i--) {
  51.     if(rec(0, i) <= W) {
  52.         cout << i << endl;
  53.         break;
  54.     }
  55. }
  56. return 0;
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment