Josif_tepe

Untitled

Oct 23rd, 2025
511
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. const int maxn = 100 + 10;
  10. const ll INF = 2e17;
  11. int n;
  12. int weights[maxn], values[maxn];
  13. ll dp[maxn][100005];
  14. ll rec(int at, int weight_capacity) {
  15.     if(at >= n) {
  16.         return 0;
  17.     }
  18.    
  19.     if(dp[at][weight_capacity] != -1) {
  20.         return dp[at][weight_capacity];
  21.     }
  22.    
  23.     ll res = -INF;
  24.    
  25.     res = max(res, rec(at + 1, weight_capacity));
  26.    
  27.     if(weight_capacity >= weights[at]) {
  28.         res = max(res, rec(at + 1, weight_capacity - weights[at]) + values[at]);
  29.     }
  30.        
  31.     dp[at][weight_capacity] = res;
  32.     return res;
  33. }
  34. int main() {
  35.    
  36.     memset(dp, -1, sizeof dp);
  37.     int W;
  38.     cin >> n >> W;
  39.    
  40.     for(int i = 0; i < n; i++) {
  41.         cin >>  weights[i] >> values[i];
  42.     }
  43.    
  44.     cout << rec(0, W) << endl;
  45.     return 0;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment