AcWing 5. 多重背包问题 II
原题链接
中等
作者:
松鼠爱葡萄
,
2020-09-24 11:08:30
,
所有人可见
,
阅读 528
时间复杂度 $O(nlog(s))$
#include<iostream>
using namespace std;
const int N = 1010;
int f[N];
int v[N], w[N], s[N];
int n, m;
int main() {
cin >> n >> m;
int cnt = 0;
for (int i = 1; i <= n; i++) {
int a, b, s;
cin >> a >> b >> s;
int k = 1;
while (k <= s) {
cnt++;
v[cnt] = a * k;
w[cnt] = b * k;
s -= k;
k *= 2;
}
if (s > 0) {
cnt++;
v[cnt] = a * s;
w[cnt] = b * s;
}
}
n = cnt;
for (int i = 1; i <= n; i++)
for (int j = m; j >= v[i]; j--)
f[j] = max(f[j], f[j - v[i]] + w[i]);
cout << f[m] << endl;
}