多重背包问题
#背包问题VII
class Solution:
"""
@param n: the money of you
@param prices: the price of rice[i]
@param weight: the weight of rice[i]
@param amounts: the amount of rice[i]
@return: the maximum weight
"""
def backPackVII(self, n, prices, weight, amounts):
# write your code here
#nums = 0
#pp = []
#ww = []
#for i in range(len(prices)):
# sums = min(amounts[i], n // prices[i])
# nums += sums
# for j in range(sums):
# pp.append(prices[i])
# ww.append(weight[i])
#dp = [0] * (n + 1)
#for i in range(nums):
# for j in range(n, pp[i]-1, -1):
# dp[j] = max(dp[j], dp[j - pp[i]] + ww[i])
#return dp[n]
#
p = len(prices)
dp = [0] * (n + 1)
for i in range(p):
for _ in range(amounts[i]):
for j in range(n, prices[i] - 1, -1):
dp[j] = max(dp[j], dp[j - prices[i]] + weight[i])
return dp[n]