题解 | #[NOIP2001]装箱问题#
[NOIP2001]装箱问题
http://www.nowcoder.com/practice/55100a6608ad4656849dbd1f16d044cb
经典的0/1背包问题,与大家重温。
def ZeroOnePack(v, V):
n = len(v)
dp = [[0 for _ in range(V+1)] for _ in range(n+1)]
for i in range(1, n+1):
for j in range(1, V+1):
if j >= v[i-1]:
dp[i][j] = max(dp[i-1][j], dp[i-1][j-v[i-1]]+v[i-1])
else:
dp[i][j] = dp[i-1][j]
return V - dp[-1][-1]
V = int(input())
n = int(input())
v = []
for _ in range(n):
v.append(int(input()))
print(ZeroOnePack(v, V))