题解 | #采药#
采药
http://www.nowcoder.com/practice/d7c03b114f0541dd8e32ce9987326c16
01背包问题,用滚动数组优化
#include <iostream>
using namespace std;
const int N = 110, M = 1010;
int n, m;
int f[M];
int v[N], w[N];
int main()
{
cin >> m >> n;
for (int i = 1; i <= n; ++i)
cin >> v[i] >> w[i];
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;
return 0;
}