AcWing 背包模型相关问题 1024. 装箱问题

'''
价值就是体积,转化为01背包问题求解
'''

v = int(input())
n = int(input())
arr = []
for i in range(n):
    val = int(input())
    arr.append((val, val))

dp = [0] * (v + 1)

for i in range(n):
    for j in range(v, -1, -1):
        if i == 0:
            dp[j] = arr[i][1] if j >= arr[i][0] else 0
        else:
            if j >= arr[i][0]:
                dp[j] = max(dp[j], arr[i][1] + dp[j - arr[i][0]])

print(v - dp[v])

 

©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页