洛谷 P1048 采药
采药
01背包模板题。
#include <iostream>
#include <cstdio>
using namespace std;
//Mystery_Sky
//一维01背包模板
#define M 1000
int f[M], c[M], w[M];
int ans, v, m;
int main() {
scanf("%d%d", &v, &m);
for(int i = 1; i <= m; i++) scanf("%d%d", &c[i], &w[i]);
for(int i = 1; i <= m; i++)
for(int j = v; j >= c[i]; j--) {
f[j] = max(f[j], f[j-c[i]]+w[i]);
}
printf("%d\n", f[v]);
return 0;
}
唯愿,青春不辜负梦想,未来星辰闪耀
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步