-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbottom_up.cpp
46 lines (42 loc) · 1020 Bytes
/
bottom_up.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
#include<bits/stdc++.h>
using namespace std;
int n,g;
vector<int> price, weight;
vector<vector<int>> dp;
void knapsack(int n, int w) {
dp.assign(n + 1, vector<int>(w + 1, 0));
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= w; j++) {
if(weight[i - 1] <= j) {
dp[i][j] = max(dp[i - 1][j], price[i - 1] + dp[i - 1][j - weight[i - 1]]);
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
}
int knapsack_query(int w) {
return dp[price.size()][w];
}
int main() {
int tc;
scanf("%d", &tc);
while(tc--) {
scanf("%d", &n);
price.resize(n);
weight.resize(n);
for(int i = 0; i < n; i++) {
scanf("%d %d", &price[i], &weight[i]);
}
knapsack(n, 30);
int ans = 0;
scanf("%d", &g);
while(g--) {
int mw;
scanf("%d", &mw);
ans += knapsack_query(mw);
}
printf("%d\n", ans);
}
return 0;
}