Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- long long int dp[2000][100],value[2000],weight[2000],capacity[100];
- void knapsack(int n,int cap)
- {
- int i,j;
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=cap;j++)
- {
- if(j<weight[i])
- {
- dp[i][j]=dp[i-1][j];
- }
- else
- {
- dp[i][j]=max(dp[i-1][j-weight[i]]+value[i],dp[i-1][j]);
- }
- }
- }
- }
- int main()
- {
- freopen("in.txt","r",stdin);
- freopen("out.txt","w",stdout);
- int t,q,i,j,k,object,ans,capacity[200],max;
- cin>>t;
- while(t--)
- {
- ans=0,max=0;
- cin>>object;
- memset(value,0,sizeof(value));
- memset(weight,0,sizeof(weight));
- memset(capacity,0,sizeof(capacity));
- memset(dp,0,sizeof(dp));
- for(i=1;i<=object;i++)
- {
- cin>>value[i]>>weight[i];
- }
- cin>>q;
- for(i=1;i<=q;i++)
- {
- cin>>capacity[i];
- if(capacity[i]>max)
- max=capacity[i];
- }
- knapsack(object,max);
- for(i=1;i<=q;i++)
- {
- ans+=dp[object][capacity[i]];
- }
- cout<<ans<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement