avp26/u11/knapsack.py
2026-12-01 20:31:35 +01:00

17 lines
425 B
Python

def maximum_value(maximum_weight, items):
dp = [0] * (maximum_weight+1)
for item in items:
item_w = item['weight']
item_v = item['value']
for w in range(maximum_weight, item_w -1, -1):
new_v = dp[w - item_w] + item_v
if new_v > dp[w]:
dp[w] = new_v
return dp[maximum_weight]
def maximum_value_with_items(maximum_weight, items):
pass