Advertisement
Waliul

uva-10130 (dp)

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