2019-05-30 05:52:07 -07:00
|
|
|
def maximum_value(maximum_weight, items):
|
|
|
|
|
totals = [[0 for _ in range(len(items) + 1)]
|
|
|
|
|
for _ in range(maximum_weight + 1)]
|
|
|
|
|
|
|
|
|
|
for weight in range(1, maximum_weight + 1):
|
|
|
|
|
for index, item in enumerate(items, 1):
|
2019-04-04 21:03:56 +03:00
|
|
|
if item["weight"] <= weight:
|
2019-05-30 05:52:07 -07:00
|
|
|
value = item["value"] + \
|
|
|
|
|
totals[weight - item["weight"]][index - 1]
|
|
|
|
|
|
|
|
|
|
value_without_item = totals[weight][index - 1]
|
|
|
|
|
totals[weight][index] = max(value, value_without_item)
|
2019-04-04 21:03:56 +03:00
|
|
|
else:
|
2019-05-30 05:52:07 -07:00
|
|
|
totals[weight][index] = totals[weight][index - 1]
|
|
|
|
|
return totals[maximum_weight][len(items)]
|