Advertisement
Josif_tepe

Untitled

Oct 24th, 2023
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int maxn = 2005;
  4. const int max_weight = 3550;
  5. const int INF = 1e9;
  6. int price[maxn], weights[maxn];
  7. int n, W;
  8. int dp[maxn][max_weight];
  9. int rec(int at, int weight_left) {
  10.     if(weight_left == 0) {
  11.         return 0;
  12.     }
  13.     if(at == -1) {
  14.         return 0;
  15.     }
  16.     if(dp[at][weight_left] != -1) {
  17.         return dp[at][weight_left];
  18.     }
  19.     int result = -INF;
  20.     result = max(result, rec(at - 1, weight_left));
  21.    
  22.     if(weight_left - weights[at] >= 0) {
  23.         result = max(result, rec(at - 1, weight_left - weights[at]) + price[at]);
  24.     }
  25.     return dp[at][weight_left] = result;
  26. }
  27. int main() {
  28.     cin >> n;
  29.     for(int i = 0; i < n; i++) {
  30.         cin >> price[i] >> weights[i];
  31.     }
  32.     cin >> W;
  33.     for(int i = 0; i < maxn; i++) {
  34.         for(int j = 0; j < max_weight; j++) {
  35.             dp[i][j] = -1;
  36.         }
  37.     }
  38.     cout << rec(n - 1, W) << endl;
  39.    
  40.    
  41.     return 0;
  42. }
  43.  
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement