AcWing 3. 完全背包问题
原题链接
简单
作者:
heiyou
,
2020-05-12 00:07:41
,
所有人可见
,
阅读 647
/*
f[i][j] : 所有只从前i个物品中选的集合,体积为j
状态计算:f[i][j] 划分:选0个第i个物品, 1个第i个物品,2个......k个
f[i - 1][j] f[i - 1][j - vi] + wi f[i - 1][j - vi * k] + k * w[i]
推导过程:f[i][j] = max(f[I - 1][j] , f[I - 1][j - v] + w, f[I - 1][j - 2v] + 2w)
f[i][j - v] = max(f[I - 1][j - v], f[I - 1][j - 2v] + w, f[I - 1][j - 3v] + 2w)
---> f[i][j] = max(f[i - 1][j], f[i][j - v] + w);
*/
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, m;
int v[N], w[N];
int f[N][N];
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++) cin >> v[i] >> w[i];
for(int i = 1; i <= n; i ++)
for(int j = 0; j <= m; j ++) {
f[i][j] = f[i - 1][j];
if(j >= v[i]) {
f[i][j] = max(f[i][j], f[i][j - v[i]] + w[i]);
}
}
cout << f[n][m] << endl;
return 0;
}