From f4204fe6984a6f25e80ba01db3fd08424b74c714 Mon Sep 17 00:00:00 2001 From: Maksym Kurets Date: Fri, 4 Dec 2026 22:25:16 +0100 Subject: [PATCH] u11 update --- u11/knapsack.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/u11/knapsack.py b/u11/knapsack.py index 735579e..52b6459 100644 --- a/u11/knapsack.py +++ b/u11/knapsack.py @@ -31,15 +31,20 @@ def maximum_value_with_items(maximum_weight, items): v = item["value"] Novystav = dict(stav) - for currW,(currV, vibir) in stav.items(): - NEwW = currW + w - if NEwW <= maximum_weight: - NewV = currV + v - if NewV > Novystav.get(NEwW, (0, []))[0]: - Novystav[NEwW] = (NewV, vibir + [item]) - - stav = Novystav + for item in items: + w = item["weight"] + v = item["value"] + Novystav = dict(stav) + for currW, (currV, vibir) in stav.items(): + NEwW = currW + w + if NEwW <= maximum_weight: + NewV = currV + v + if NewV > Novystav.get(NEwW, (0, []))[0]: + Novystav[NEwW] = (NewV, vibir + [item]) + + stav = Novystav + BV = 0 Bvibir = [] for v, vibir in stav.values():