AcWing 5. 多重背包问题 II
原题链接
中等
作者:
润森
,
2020-09-21 22:37:08
,
所有人可见
,
阅读 471
def binary_divide(volume,price,count):
divides = []
for i in range(32):
# 从0位开始枚举
cur = 1 << i
# 如果小于枚举值,说明已经拆分完毕了
if count < cur:
# 把剩下的部分打包
divides.append((count, count * volume, count * price))
break
else:
# 否则继续拆分,打包1 << i个物品
count -= cur
divides.append((cur, cur * volume, cur * price))
return divides
n,v = map(int, input().split())
goods = []
for i in range(n):
goods.append([int(i) for i in input().split()])
new_good = []
for i in goods:
# 二进制拆分
new_good.extend(binary_divide(*i))
dp = [0 for _ in range(v+1)]
for item in new_good:
i, j = item[1], item[2]
for k in range(v - i, -1, -1):
dp[k + i] = max(dp[k + i], dp[k] + j)
print(dp[-1])