Advertisement
Ishu_15hu

SuperSale_UVA(1003)

Oct 14th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. long long price[1003],weight[1003],dp[2006][300];
  4. long long n,W;
  5. long long func(long long i, long long w)
  6. {
  7.     long long r1,r2;
  8.     if(i==n+1) return 0;
  9.  
  10.     if(dp[i][w]!=0) return dp[i][w];
  11.  
  12.     if(w+weight[i]<=W)
  13.     {
  14.  
  15.         r1=price[i]+func(i+1,w+weight[i]);
  16.     }
  17.  
  18.     else r1=0;
  19.  
  20.     r2=func(i+1,w);
  21.  
  22.     return dp[i][w]= max(r1,r2);
  23. }
  24.  
  25. int main()
  26. {
  27.     int t;
  28.     cin>>t;
  29.     while(t--) {
  30.             cin>>n;
  31.             long long res=0;
  32.  
  33.  
  34.             for(int i=0;i<n;i++) cin>>price[i]>>weight[i];
  35.  
  36.             int g; cin>>g;
  37.             while(g--){
  38.  
  39.             memset(dp,0,sizeof(dp));
  40.                  cin>>W;
  41.                  res+=func(0,0);
  42.             }
  43.          cout<<res<<endl;
  44.     }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement