Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, w = [int(x) for x in input().split()]
    itens = []
    dp = [[-1]*w for i in range(n)]
    
    for _ in range(n):
        itens.append([int(x) for x in input().split()])#item([peso, valor])

    print(knapsack(n, w, itens, dp))

def knapsack(n, w, itens, dp):

    if n == 0 or w == 0:
        return 0

    if dp[n-1][w-1] != -1:
        return dp[n-1][w-1]

    if itens[n-1][0] > w:
        dp[n-1][w-1] = knapsack(n-1, w, itens, dp)
        return dp[n-1][w-1]

    else:
        dp[n-1][w-1] = max(itens[n-1][1] + knapsack(n-1, w - itens[n-1][0], itens, dp),
                           knapsack(n-1, w, itens, dp))

        return dp[n-1][w-1]

main()

Fixed submission:
ln = input()
n, cap = ln.split()
n = int(n)
cap = int(cap)

'''
weights = [[0 for x in range(2)] for y in range(n)] 
values = [[0 for x in range(2)] for y in range(n)] 
mat = [[0 for x in range(2)] for y in range(1000000)] 
'''
weights = [0 for x in range(n)] 
values =  [0 for x in range(n)] 
mat =  [0 for x in range(1000000)] 

for i in range(n):
    ln = input()
    ln = ln.split()
    weights[i] = int(ln[0])
    values[i] = int(ln[1])
 
for i in range(n):
    for j in range(cap,weights[i]-1,-1):
        '''
        print(j)
        print(values[i])
        print(mat[j-weights[i]])
        '''
        mat[j] = max(mat[j], mat[j-weights[i]] + values[i])
 
        
print(mat[cap])

Buggy python submission (verdict: Time Limit Exceeded):
arr=[]
n,w=map(int,input().split())
for _ in range(n):
	arr.append(tuple(map(int,input().split())))
dp=[-1]*((n+1)*(w+1))
def f(pos,W):
	if(pos==n):
		return 0
	if dp[pos*(w+1)+W]!=-1:
		return dp[pos*(w+1)+W]
	res=f(pos+1,W)
	if(arr[pos][0]<=W):
		res=max(res,f(pos+1,W-arr[pos][0])+arr[pos][1])
	dp[pos*(w+1)+W]=res
	return res
print(f(0,w))

Fixed submission:
ln = input()
n, cap = ln.split()
n = int(n)
cap = int(cap)

'''
weights = [[0 for x in range(2)] for y in range(n)] 
values = [[0 for x in range(2)] for y in range(n)] 
mat = [[0 for x in range(2)] for y in range(1000000)] 
'''
weights = [0 for x in range(n)] 
values =  [0 for x in range(n)] 
mat =  [0 for x in range(1000000)] 

for i in range(n):
    ln = input()
    ln = ln.split()
    weights[i] = int(ln[0])
    values[i] = int(ln[1])
 
for i in range(n):
    for j in range(cap,weights[i]-1,-1):
        '''
        print(j)
        print(values[i])
        print(mat[j-weights[i]])
        '''
        mat[j] = max(mat[j], mat[j-weights[i]] + values[i])
 
        
print(mat[cap])

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 22 22:30:30 2020

@author: matha
"""

#global


def dp(pos, cw):
    if pos>= N or cw > W:
        return 0;
    temp = mem.get((pos, cw), None)
    if temp:
        return temp
    
    w = rec[pos][0]
    
    v1 = 0
    v2 = 0
    
    if(cw+w <= W):
        v1 = rec[pos][1]
        v1 += dp(pos+1, cw + w)
    
    v2 = dp(pos+1, cw)
    
    if(v1>v2):
        mem[(pos, cw)] = v1
        return v1
    else:
        mem[(pos, cw)] = v2
        return v2

rec = []
mem = {}

N, W = input().split()
N = int(N)
W = int(W)

for i in range(N):
    w, v = input().split()
    w = int(w)
    v = int(v)
    
    rec.append( (w, v))
    

print(dp(0, 0))
    
    



Fixed submission:
ln = input()
n, cap = ln.split()
n = int(n)
cap = int(cap)

'''
weights = [[0 for x in range(2)] for y in range(n)] 
values = [[0 for x in range(2)] for y in range(n)] 
mat = [[0 for x in range(2)] for y in range(1000000)] 
'''
weights = [0 for x in range(n)] 
values =  [0 for x in range(n)] 
mat =  [0 for x in range(1000000)] 

for i in range(n):
    ln = input()
    ln = ln.split()
    weights[i] = int(ln[0])
    values[i] = int(ln[1])
 
for i in range(n):
    for j in range(cap,weights[i]-1,-1):
        '''
        print(j)
        print(values[i])
        print(mat[j-weights[i]])
        '''
        mat[j] = max(mat[j], mat[j-weights[i]] + values[i])
 
        
print(mat[cap])