Advertisement
Josif_tepe

Untitled

Nov 1st, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <vector>
  6. using namespace  std;
  7. typedef long long ll;
  8.  
  9. int main() {
  10.     int N, W;
  11.     cin >> N >> W;
  12.     vector<int> val(N), wt(N);
  13.     for(int i = 0; i < N; i++) {
  14.         cin >> val[i] >> wt[i];
  15.     }
  16.     vector<int> dp(W + 5, -2e9);
  17.     dp[W] = 0;
  18.    
  19.     for(int weight = W; weight >= 0; weight--) {
  20.         for(int i = 0; i < N; i++) {
  21.             if(weight - wt[i] >= 0) {
  22.                 dp[weight - wt[i]] = max(dp[weight - wt[i]], dp[weight] + val[i]);
  23.             }
  24.         }
  25.     }
  26.     int result = -2e9;
  27.     for(int i = 0; i <= W; i++) {
  28.         result = max(result, dp[i]);
  29.     }
  30.     if(result == -2e9) result = 0;
  31.     cout << result << endl;
  32.     return 0;
  33. }
  34. /*
  35.  3 8
  36.  3 30
  37.  4 50
  38.  5 60
  39.  
  40.  140: 12
  41.  139 = inf
  42.  138 = inf
  43.  
  44.  110: 9
  45.  90: 8
  46.  80: 7
  47.  60: 5
  48.  50: 4
  49.  30: 3
  50.  */
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement