简单的背包——01背包(7)
题目来源:洛谷 P2639 [USACO09OCT]Bessie’s Weight Problem G
共五个测试点
题解
AC代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 45e3 + 5;
int h, n, w;
int f[maxn];
int main()
{
memset(f, 0, sizeof(f));
scanf("%d%d", &h, &n);
for (int i = 1; i <= n; ++i)
{
scanf("%d", &w);
for (int j = h; j >= w; --j)
{
// 干草的价值就是干草的体积
f[j] = max(f[j], f[j - w] + w);
}
}
printf("%d", f[h]);
return 0;
}