I'm trying to solve this problem however several testcases fail. I'm using a memo table to called arr to optimize recursion.
What may I have done wrong?
s=list(map(int,input().split()))
n=s[0]
W=s[1]
value=[-1]
weight=[-1]
for i in range(n):
t=list(map(int,input().split()))
weight.append(t[0])
value.append(t[1])
arr=[[-1]*(W+1)]*(n+1)
def KS(n,W):
if n==0 or W==0:
return 0
if arr[n][W]!=-1:
return arr[n][W]
if weight[n]>W:
answer=KS(n-1,W)
else:
answer=max(value[n]+KS(n-1,W-weight[n]),KS(n-1,W))
arr[n][W]=answer
return answer
result=KS(n,W)
print(result)