一开始想到的是全家能带出去的,当然啦,这样想幸好样例过不了,不然是ym了,然后想每个人做一次背包的总和,ok,样例过了,submit accepted
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXG 131
#define MAXN 1001
int p[MAXN], w[MAXN], G[MAXN], dp[MAXG];
int zero_one_knapsack(const int &n, const int &g)
{
int sum(0);
for(int i = 0; i < g; i ++) {
memset(dp, 0, sizeof(dp));
for(int j = 0; j < n; j ++) {
for(int v = G[i]; v >= w[j]; v --) {
dp[v] = max(dp[v], dp[v-w[j]]+p[j]);
}
}
sum += dp[ G[i] ];
}
return sum;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int cas, n, g;
scanf("%d", &cas);
while( cas -- ) {
scanf("%d", &n);
for(int i = 0; i < n; i ++) {
scanf("%d %d", &p[i], &w[i]);
}
scanf("%d", &g);
for(int i = 0; i < g; i ++) {
scanf("%d", &G[i]);
}
printf("%d\n", zero_one_knapsack(n, g));
}
return 0;
}
10130 SuperSale
最新推荐文章于 2020-03-02 22:37:01 发布