Advertisement
Josif_tepe

Untitled

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