Advertisement
Waliul

uav-10130 (recursive)

Mar 22nd, 2020
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int cap, n, cost[1005], weight[1005], dp[1005][50];
  5.  
  6. int obj(int i, int w)
  7. {
  8.     int profit1, profit2;
  9.     if(i == n + 1)
  10.     {
  11.         return 0;
  12.     }
  13.     if(w+weight[i] <= cap)
  14.     {
  15.         profit1 = cost[i] + obj(i + 1, w + weight[i]);
  16.     }
  17.  
  18.     profit2 = obj(i + 1, w);
  19.  
  20.     return max(profit1, profit2);
  21. }
  22.  
  23. int main()
  24. {
  25.     int t, p, w, g, mw, i, j, total;
  26.     cin>>t;
  27.     while(t--)
  28.     {
  29.         memset(dp, -1, sizeof(dp));
  30.         cin>>n;
  31.         total = 0;
  32.         for(i = 1; i <= n; i++)
  33.         {
  34.             cin>>cost[i];
  35.             cin>>weight[i];
  36.         }
  37.         cin>>g;
  38.         while(g--)
  39.         {
  40.             cin>>mw;
  41.             cap = mw;
  42.             total += obj(1, 0);
  43.         }
  44.         if(t == 1)
  45.         {
  46.             break;
  47.         }
  48.         cout<<total<<endl;
  49.     }
  50.     cout<<total;
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement