Advertisement
Josif_tepe

Untitled

Nov 3rd, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <iostream>
  2. #include<ctime>
  3. #include<map>
  4. #include<vector>
  5. #include<queue>
  6. using namespace std;
  7. int n;
  8. int values[2002], weights[2002];
  9. int maximum_weight;
  10. int dp[2002][3502];
  11. int rec(int i, int weight_left) {
  12.     if(i == n or weight_left == 0) {
  13.         return 0;
  14.     }
  15.     if(dp[i][weight_left] != -1) {
  16.         return dp[i][weight_left];
  17.     }
  18.     int result = 0;
  19.     result = max(result, rec(i + 1, weight_left));
  20.     if(weight_left - weights[i] >= 0) {
  21.         result = max(result, rec(i + 1, weight_left - weights[i]) + values[i]);
  22.     }
  23.     dp[i][weight_left] = result;
  24.     return result;
  25. }
  26. int main()
  27. {
  28.     cin >> n;
  29.     for(int i = 0; i < n; i++) {
  30.         cin >> values[i];
  31.         cin >> weights[i];
  32.     }
  33.     cin >> maximum_weight;
  34.     for(int i = 0; i < n; i++) {
  35.         for(int j = 0; j <= maximum_weight; j++) {
  36.             dp[i][j] = -1;
  37.         }
  38.     }
  39.     cout << rec(0, maximum_weight) << endl;
  40.     return 0;
  41. }
  42.  
  43.  
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement