-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKNAPSACK.py
More file actions
28 lines (25 loc) · 831 Bytes
/
KNAPSACK.py
File metadata and controls
28 lines (25 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
s, n = tuple(map(int, input().split()))
v = []
w = []
for _ in range(n):
wv_new = tuple(map(int, input().split()))
w.append(wv_new[0])
v.append(wv_new[1])
knapsack_memo = {}
def knapsack_solve(i, w_remaining):
# i is the number of items avaliable
if (i, w_remaining) in knapsack_memo:
return knapsack_memo[(i, w_remaining)]
if i == 0:
knapsack_memo[(i, w_remaining)] = 0
return 0
if w_remaining == 0:
knapsack_memo[(i, w_remaining)] = 0
return 0
if w[i - 1] > w_remaining:
knapsack_memo[(i, w_remaining)] = knapsack_solve(i - 1, w_remaining)
return knapsack_memo[(i, w_remaining)]
else:
knapsack_memo[(i, w_remaining)] = max(knapsack_solve(i - 1, w_remaining), knapsack_solve(i - 1, w_remaining - w[i - 1]) + v[i - 1])
return knapsack_memo[(i, w_remaining)]
print(knapsack_solve(n, s))