混合背包模板题。
#include <iostream>
#include <cstdio>
using namespace std;
//Mystery_Sky
//
#define M 100000
int c[M], w[M];
int f[M], new_c[M], new_w[M], num[M];
bool it[M];
int v, m, tot;
int main() {
scanf("%d%d", &v, &m);
for(int i = 1; i <= m; i++) scanf("%d%d%d", &c[i], &w[i], &num[i]);
for(int i = 1; i <= m; i++) {
if(num[i] == 0) {
it[++tot] = 1;
new_c[tot] = c[i];
new_w[tot] = w[i];
continue;
}
if(num[i] == 1) {
new_c[++tot] = c[i];
new_w[tot] = w[i];
continue;
}
for(int j = 1; j <= num[i]; j<<=1) {
num[i] -= j;
new_c[++tot] = c[i] * j;
new_w[tot] = w[i] * j;
}
if(num[i]) {
new_c[++tot] = c[i] * num[i];
new_w[tot] = w[i] * num[i];
num[i] = 0;
}
}
for(int i = 1; i <= tot; i++) {
if(it[i]) {
for(int j = new_c[i]; j <= v; j++)
f[j] = max(f[j], f[j-new_c[i]] + new_w[i]);
}
else
for(int j = v; j >= new_c[i]; j--)
f[j] = max(f[j], f[j-new_c[i]] + new_w[i]);
}
printf("%d
", f[v]);
return 0;
}