def knapsack(i, capacity):
if n == 1:
dp[i] = 1
return
if v_all[i][capacity] == -1:
if i == 0 or capacity == 0:
v_all[i][capacity] = 0
elif s[i] > capacity:
v_all[i][capacity] = 0
knapsack(i - 1, capacity)
else:
s1 = knapsack(i - 1, capacity - s[i]) + v[i]
s2 = knapsack(i - 1, capacity)
if s1 >= s2:
dp[i] = 1
v_all[i][capacity] = s1
else:
dp[i] = 0
v_all[i][capacity] = s2
return v_all[i][capacity]
n = 5
v = [100, 200, 300, 150, 120]
s = [300, 200, 150, 200, 100]
capacity = 500
dp = [0] * n
v_all = []
for i in range(n):
temp = [-1] * (capacity + 1)
v_all.append(temp)
knapsack(n - 1, capacity)
print(dp)