题解 | #【模板】完全背包#
【模板】完全背包
https://www.nowcoder.com/practice/237ae40ea1e84d8980c1d5666d1c53bc
#include <algorithm> #include <cstring> #include <iostream> using namespace std; const int N = 1010; int f[N], w[N], v[N]; int main() { int n, m; 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 = v[i]; j <= m; j ++) f[j] = max(f[j], f[j - v[i]] + w[i]); cout << f[m] << endl; memset(f, -0x3f, sizeof f); f[0] = 0; for(int i = 1; i <= n; i ++) for(int j = v[i]; j <= m; j ++) if(f[j] != 0x3f) f[j] = max(f[j], f[j - v[i]] + w[i]); if(f[m] < 0) f[m] = 0; cout << f[m] << endl; return 0; }