Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- long long price[1003],weight[1003],dp[2006][300];
- long long n,W;
- long long func(long long i, long long w)
- {
- long long r1,r2;
- if(i==n+1) return 0;
- if(dp[i][w]!=0) return dp[i][w];
- if(w+weight[i]<=W)
- {
- r1=price[i]+func(i+1,w+weight[i]);
- }
- else r1=0;
- r2=func(i+1,w);
- return dp[i][w]= max(r1,r2);
- }
- int main()
- {
- int t;
- cin>>t;
- while(t--) {
- cin>>n;
- long long res=0;
- for(int i=0;i<n;i++) cin>>price[i]>>weight[i];
- int g; cin>>g;
- while(g--){
- memset(dp,0,sizeof(dp));
- cin>>W;
- res+=func(0,0);
- }
- cout<<res<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement