Advertisement
Josif_tepe

Untitled

Dec 14th, 2021
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int n;
  5. int cost[105], weight[105];
  6. long long dp[101][100001];
  7. long long rek(int att, int cost_left) {
  8.     if(cost_left == 0) {
  9.         return 0;
  10.     }
  11.     if(att==n)
  12.     {
  13.         return 2e9;
  14.     }
  15.     if(dp[att][cost_left] != -1) {
  16.         return dp[att][cost_left];
  17.     }
  18.     long long maks = 1e15;
  19.     maks = min(maks, rek(att + 1, cost_left)); // sto ako ne go zememe ovoj element
  20.     if(cost_left - cost[att] >= 0) {
  21.         maks = min(maks, rek(att + 1, cost_left - cost[att]) + weight[att]); // sto ako go zememe ovoj element
  22.     }
  23.     dp[att][cost_left] = maks;
  24.     return maks;
  25. }
  26. int main()
  27. {
  28.     cin >> n;
  29.     int max_weight;
  30.     cin >> max_weight;
  31.     for(int i = 0; i < n; i++) {
  32.         cin >> weight[i] >> cost[i];
  33.     }
  34.     for(int i = 0; i < n; i++) {
  35.         for(int j = 0; j <= 100000; j++) {
  36.             dp[i][j] = -1;
  37.         }
  38.     }
  39.     int rez;
  40.     for(int j = 0; j <= 100000; j++) {
  41.             if(rek(0,j)<=max_weight)
  42.             {
  43.                rez=j;
  44.             }
  45.         }
  46.     cout<<rez;
  47.        return 0;
  48. }
  49.  
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement