Advertisement
Josif_tepe

Untitled

Dec 7th, 2021
148
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.  
  3. using namespace std;
  4. int n;
  5. int cost[2002], weight[2002];
  6. int dp[2002][3502];
  7. int rek(int at, int weight_left) {
  8.     if(at == n or weight_left == 0) {
  9.         return 0;
  10.     }
  11.     if(dp[at][weight_left] != -1) {
  12.         return dp[at][weight_left];
  13.     }
  14.     int maks = -2e9;
  15.     maks = max(maks, rek(at + 1, weight_left)); // sto ako ne go zememe ovoj element
  16.    
  17.     if(weight_left - weight[at] >= 0) {
  18.         maks = max(maks, rek(at + 1, weight_left - weight[at]) + cost[at]); // sto ako go zememe ovoj element
  19.     }
  20.     dp[at][weight_left] = maks;
  21.     return maks;
  22. }
  23. int main()
  24. {
  25.     cin >> n;
  26.     for(int i = 0; i < n; i++) {
  27.         cin >> cost[i] >> weight[i];
  28.     }
  29.    
  30.     int max_weight;
  31.     cin >> max_weight;
  32.     for(int i = 0; i < n; i++) {
  33.         for(int j = 0; j <= max_weight; j++) {
  34.             dp[i][j] = -1;
  35.         }
  36.     }
  37.     cout << rek(0, max_weight) << endl;
  38.        return 0;
  39. }
  40.  
  41.  
  42.  
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement