当前位置: 代码迷 >> 综合 >> 洛谷 P1060 [NOIP2006 普及组] 开心的金明
  详细解决方案

洛谷 P1060 [NOIP2006 普及组] 开心的金明

热度:97   发布时间:2023-12-22 07:40:38.0

题目链接:

[NOIP2006 普及组] 开心的金明 - 洛谷https://www.luogu.com.cn/problem/P1060

【分析】标准的01背包问题。

n, m = [int(i) for i in input().split()]
v, p = [0] * m, [0] * m
for i in range(m):v[i], p[i] = [int(j) for j in input().split()]
dp = []
for i in range(m + 1):dp.append([0] * (n + 1))
ans = 0
for i in range(m):for j in range(n):w = v[i]if j >= w:dp[i + 1][j] = max(dp[i][j], dp[i][j - w] + w * p[i])else:dp[i + 1][j] = dp[i][j]ans = max(ans, dp[i + 1][j])
print(ans)