-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSolution.cpp
More file actions
46 lines (42 loc) · 842 Bytes
/
Solution.cpp
File metadata and controls
46 lines (42 loc) · 842 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int n;
const int N = 1003;
int price[N], weight[N];
int dp[N][35];
int f(int i, int R)
{
if(i == n || R == 0)
return 0;
if(dp[i][R] != -1)
return dp[i][R];
int pick, skip;
if(weight[i] <= R)
pick = price[i] + f(i + 1, R - weight[i]);
else
pick = 0;
skip = f(i + 1, R);
return dp[i][R] = max(pick, skip);
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d %d", &price[i], &weight[i]);
int q, ans = 0;
memset(dp, -1, sizeof dp);
scanf("%d", &q);
while(q--)
{
int x;
scanf("%d", &x);
ans += f(0, x);
}
printf("%d\n", ans);
}
return 0;
}