#include<bits/stdc++.h>
#define ll long long int
using namespace std;
ll thup(ll W, ll v[], ll wt[], ll n)
{
ll i, j;
ll k[n+1][W+1];
for(i=0; i<=n; i++)
{
for(j=0; j<=W; j++)
{
if(i == 0 || j == 0)
k[i][j] = 0;
else if(wt[i-1] <= j)
k[i][j] = max((v[i-1]+k[i-1][j-wt[i-1]]), k[i-1][j]);
else
k[i][j] = k[i-1][j];
}
}
return k[n][W];
}
int main()
{
ll m, i, j;
while(cin >> m)
{
for(j=1; j<=m; j++)
{
ll n, value, weight, n1, W, c = 0;
cin >> n;
ll v[n], wt[n];
for(i=0; i<n; i++)
{
cin >> value >> weight;
v[i] = value;
wt[i] = weight;
}
cin >> n1;
for(i=1; i<=n1; i++)
{
cin >> W;
ll p = thup(W, v, wt, n);
c += p;
}
cout << c << endl;
}
}
return 0;
}
#define ll long long int
using namespace std;
ll thup(ll W, ll v[], ll wt[], ll n)
{
ll i, j;
ll k[n+1][W+1];
for(i=0; i<=n; i++)
{
for(j=0; j<=W; j++)
{
if(i == 0 || j == 0)
k[i][j] = 0;
else if(wt[i-1] <= j)
k[i][j] = max((v[i-1]+k[i-1][j-wt[i-1]]), k[i-1][j]);
else
k[i][j] = k[i-1][j];
}
}
return k[n][W];
}
int main()
{
ll m, i, j;
while(cin >> m)
{
for(j=1; j<=m; j++)
{
ll n, value, weight, n1, W, c = 0;
cin >> n;
ll v[n], wt[n];
for(i=0; i<n; i++)
{
cin >> value >> weight;
v[i] = value;
wt[i] = weight;
}
cin >> n1;
for(i=1; i<=n1; i++)
{
cin >> W;
ll p = thup(W, v, wt, n);
c += p;
}
cout << c << endl;
}
}
return 0;
}
Comments
Post a Comment