-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10130.cpp
68 lines (55 loc) · 1.47 KB
/
10130.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <cstdio>
#include <cstring>
using namespace std;
struct object {
int w, v;
};
void sortO(object *list, int len)
{
int i = 0;
int j = len - 1;
int pivot = list[len / 2].w;
object tmpS;
if (len < 2) return;
while (true) {
while (list[i].w - pivot < 0)
++i;
while (list[j].w - pivot > 0)
--j;
if (i >= j) break;
memcpy(&tmpS, list + i, sizeof(object));
memcpy(list + i, list + j, sizeof(object));
memcpy(list + j, &tmpS, sizeof(object));
i++;
j--;
}
sortO(list, i);
sortO(list + i, len - i);
}
int main(void)
{
object objs[1001];
int times, i, j, p, lenO, lenP, people[101], dp[32], sum, max;
scanf("%d", ×);
while (times-- > 0) {
scanf("%d", &lenO);
for (i = 0; i < lenO; ++i)
scanf("%d %d", &objs[i].v, &objs[i].w);
scanf("%d", &lenP);
max = 0;
for (i = 0; i < lenP; ++i) {
scanf("%d", &people[i]);
if (max < people[i]) max = people[i];
}
sortO(objs, lenO);
memset(dp, 0, sizeof(int) * (max + 1));
for (i = 0; i < lenO; ++i)
for (j = max; j - objs[i].w > -1; --j)
if (dp[j] < objs[i].v + dp[j - objs[i].w]) dp[j] = objs[i].v + dp[j - objs[i].w];
sum = 0;
for (i = 0; i < lenP; ++i)
sum += dp[people[i]];
printf("%d\n", sum);
}
return 0;
}