AcWing 5. 多重背包问题 II---python3【书记】
原题链接
中等
作者:
AcWing书记
,
2021-04-26 20:48:47
,
所有人可见
,
阅读 2
N, volume = map(int, input().split())
dp = [0 for _ in range(volume + 1)]
volume_, price_ = [], []
for _ in range(N):
volume_input, price_input, goods_num = map(int, input().split())
k = 1
# divide group
while k <= goods_num:
volume_.append(k * volume_input)
price_.append(k * price_input)
goods_num -= k
k *= 2
if goods_num > 0:
volume_.append(volume_input * goods_num)
price_.append(price_input * goods_num)
# 转化为 01背包 问题
group_sum = len(volume_)
for group_no in range(group_sum):
for v in range(volume, volume_[group_no] - 1, -1):
dp[v] = max(dp[v], dp[v - volume_[group_no]] + price_[group_no])
print(dp[-1])