0-1背包。
今天UVa出问题了?
1 # include <stdio.h>
2 # include <memory.h>
3
4 # define MAX(x,y) ((x)>(y) ? (x):(y))
5
6 int f[31];
7 int mw[101];
8 int p[1001];
9 int w[1001];
10
11 int main()
12 {
13 int k, i, v, T, maxV, N, G;
14
15 scanf("%d", &T);
16 while (T--)
17 {
18 scanf("%d", &N);
19 for (i = 1; i <= N; ++i)
20 scanf("%d%d", &p[i], &w[i]);
21 scanf("%d", &G);
22 for (i = 1; i <= G; ++i)
23 scanf("%d", &mw[i]);
24
25 maxV = 0;
26
27 for ( k = 1; k <= G; ++k)
28 {
29 memset(f, 0, sizeof(f));
30 for ( i = 1; i <= N; ++i)
31 for ( v = mw[k]; v >= w[i]; --v)
32 f[v] = MAX(f[v], f[v-w[i]]+p[i]);
33 maxV += f[mw[k]];
34 }
35
36 printf("%d\n", maxV);
37 }
38
39 return 0;
40 }