Buggy python submission (verdict: Time Limit Exceeded):
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 for j in range(w+1)] for i in range(n+1)]
for i in range(n,-1,-1):
    for j in range(0,w+1,1):
        if i==n or j==0:
            dp[i][j]=0
        elif li[i][0]>j:
            dp[i][j]=dp[i+1][j]
        else:
            dp[i][j]=max(dp[i+1][j-li[i][0]]+li[i][1],dp[i+1][j])
print(dp[i][w])

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 python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(1000000)
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)

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))

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 python submission (verdict: Time Limit Exceeded):
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()

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 python 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()

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 python 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()

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 python submission (verdict: Time Limit Exceeded):
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])

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 python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(1000000)
def recur(li,i,n,w,dp):
    if i==(n-1):
        if w<=li[i][0]:
            return li[i][1],li[i][1]
        else:
            return 0,0
    if w==0:
        return li[i][1],li[i][1]
    mx=li[i][1]
    for j in range(i+1,n):
        if (w-li[i][0])>=li[j][0]:
            if dp[j][w-li[i][0]]==[-1,-1]:
                val=recur(li,j,n,w-li[i][0],dp)
                dp[j][w-li[i][0]]=val
            else:
                val=dp[j][w-li[i][0]]
            mx=max(mx,val[0]+li[i][1])
    if dp[i+1][w]==[-1,-1]:
        val=recur(li,i+1,n,w,dp)
        dp[i+1][w]=val
    else:
        val=dp[i+1][w]
    overall=max(mx,val[1])
    #print(mx,overall)
    return mx,overall

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)]
print(recur(li,0,n,w,dp)[1])

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()