forgeplus/testcode/0_1Package_dp.py

20 lines
434 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# N件物品一个容量为V的背包
N, V = map(int, input().split())
weight = []
val = []
for _ in range(N):
wi, vi = map(int, input().split())
weight.append(wi)
val.append(vi)
dp = [[0]*(V+1) for _ in range(N+1)]
for i in range(1, N+1):
for w in range(1, V+1):
dp[i][w] = dp[i-1][w]
if w-weight[i-1] >= 0:
dp[i][w] = max(dp[i][w], dp[i-1][w-weight[i-1]]+val[i-1])
print(dp[N][V])