u11 test4

This commit is contained in:
Volodymyr Baliasnikov 2026-12-01 20:48:20 +01:00
parent 34f35fb827
commit 3e279617c5

View File

@ -1,5 +1,5 @@
def maximum_value(maximum_weight, items): def maximum_value(maximum_weight, items):
dp = [0] * (maximum_weight+1) dp = [0] * (maximum_weight + 1)
for item in items: for item in items:
item_w = item['weight'] item_w = item['weight']
@ -14,4 +14,16 @@ def maximum_value(maximum_weight, items):
def maximum_value_with_items(maximum_weight, items): def maximum_value_with_items(maximum_weight, items):
pass dp = [0] * (maximum_weight + 1)
choose = [] * (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
choose[w] = choose[w - item_w] + item_v
return dp[maximum_weight], choose[maximum_weight]