本文最后更新于 2025年3月7日 凌晨
https://www.luogu.com.cn/problem/P1048
P1048 采药
题解
01背包,纯模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include<bits/stdc++.h> using namespace std; const int N = 1010; int v[N],w[N]; int dp[N]; int n,m;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); 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--) { dp[j] = max(dp[j], dp[j - v[i]] + w[i]); } } cout << dp[m]; return 0; }
|