Problem p03163 (python)

Fixed submission:
from sys import stdin, gettrace
import sys
sys.setrecursionlimit(1000000)
if not gettrace():
    def input():
        return next(stdin)[:-1]
 
 
# def input():
#    return stdin.buffer.readline()

def recursion(li,i,n,w,dp):
    if i==n or w==0:
        return 0
    if li[i][0]>w:
        if dp[i+1][w]==-1:
            x=recursion(li,i+1,n,w,dp)
            dp[i+1][w]=x            
        else:
            x=dp[i+1][w]
        return x
    else:
        if dp[i+1][w]==-1:
            x=recursion(li,i+1,n,w,dp)
            dp[i+1][w]=x            
        else:
            x=dp[i+1][w]
        if dp[i+1][w-li[i][0]]==-1:
            y=recursion(li,i+1,n,w-li[i][0],dp)
            dp[i+1][w-li[i][0]]=y
        else:
            y=dp[i+1][w-li[i][0]]            
        return max(li[i][1]+y,x)

def main():
    n,w=[int(x) for x in input().split()]
    li=[]
    for i in range(n):
        temp=[int(x) for x in input().split()]
        li.append(temp)
    dp=[[-1 for j in range(w+1)] for i in range(n+1)]
    print(recursion(li,0,n,w,dp))
 
 
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from sys import stdin, gettrace
 
if not gettrace():
    def input():
        return next(stdin)[:-1]
 
 
# def input():
#    return stdin.buffer.readline()
 
def main():
    n,w=map(int,input().split())
	li=[]
	for i in range(n):
    	temp=[int(x) for x in input().split()]
    	li.append(temp)
	dp=[[[-1,-1] for j in range(w+1)] for i in range(n)]
	dp[n-1][0]=[li[n-1][1],li[n-1][1]]
	for i in range(n-1,-1,-1):
    	for j in range(w,-1,-1):
        	if j==0:
            	dp[i][j]=li[i][1],li[i][1]
        	if i==(n-1):
            	if li[i][0]<=j:
                	dp[i][j]=li[i][1],li[i][1]
            	else:
                	dp[i][j]=0,0
        	else:
            	mx=li[i][1]
            	for k in range(i+1,n):
                	if (j-li[i][0])>=li[k][0]:
                    	val=dp[k][j-li[i][0]]
                    	mx=max(mx,val[0]+li[i][1])
            	val=dp[i+1][j]
            	overall=max(mx,val[1])
            	dp[i][j]=[mx,overall]
	print(dp[0][w][1])
 
 
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from sys import stdin, gettrace
 
if not gettrace():
    def input():
        return next(stdin)[:-1]
 
 
# def input():
#    return stdin.buffer.readline()
 
def main():
    dn,w=map(int,input().split())
	li=[]
	for i in range(n):
    	temp=[int(x) for x in input().split()]
    	li.append(temp)
	dp=[[[-1,-1] for j in range(w+1)] for i in range(n)]
	dp[n-1][0]=[li[n-1][1],li[n-1][1]]
	for i in range(n-1,-1,-1):
    	for j in range(w,-1,-1):
        	if j==0:
            	dp[i][j]=li[i][1],li[i][1]
        	if i==(n-1):
            	if li[i][0]<=j:
                	dp[i][j]=li[i][1],li[i][1]
            	else:
                	dp[i][j]=0,0
        	else:
            	mx=li[i][1]
            	for k in range(i+1,n):
                	if (j-li[i][0])>=li[k][0]:
                    	val=dp[k][j-li[i][0]]
                    	mx=max(mx,val[0]+li[i][1])
            	val=dp[i+1][j]
            	overall=max(mx,val[1])
            	dp[i][j]=[mx,overall]
	print(dp[0][w][1])
 
 
if __name__ == "__main__":
    main()