题意:
背包和
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#define LL long long
using namespace std;
const int maxn = 1e3 + 10;
const int maxw = 30 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = 4 * atan(1.0);
const double ee = exp(1.0);
int n;
int p[maxn];
int w[maxn];
int dp[maxn][maxw];
int dfs(int c)
{
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= c; j++)
{
dp[i][j] = (i == 1 ? 0 : dp[i - 1][j]);
if (w[i] <= j)
{
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + p[i]);
}
}
}
return dp[n][c];
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
int ncase;
scanf("%d", &ncase);
while (ncase--)
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &p[i], &w[i]);
}
int k;
scanf("%d", &k);
int ans = 0;
for (int i = 0; i < k; i++)
{
int x;
scanf("%d", &x);
ans += dfs(x);
}
printf("%d\n", ans);
}
return 0;
}