Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdout,stdin
def knapsack(C,n,wei,val,mat):
    if(C==0 or n==0):
        return 0
    else:
        if(C>=wei[n-1]):
            a=mat[C-wei[n-1]][n-1]
            b=mat[C][n-1]
            if(a==-1):
                a=knapsack(C-wei[n-1],n-1,wei,val,mat)
                mat[C-wei[n-1]][n-1]=a
            if(b==-1):
                b=knapsack(C,n-1,wei,val,mat)
                mat[C][n-1]=b
            mat[C][n]=max(a+val[n-1],b)
            return (max(a+val[n-1],b))
        else:
            alp=matrix[C][n-1]
            if(alp==-1):
                alp=knapsack(C,n-1,wei,val,mat)
                mat[C][n-1]=alp
                mat[C][n]=alp
            return(alp)
N,W=map(int,stdin.readline().split())
matrix=[[-1 for i in range(0,N+1)]for j in range(0,W+1)]
for i in range(0,len(matrix)):
    matrix[i][0]=0
for i in range(0,len(matrix[0])):
    matrix[0][i]=0
weights=[]
values=[]
for i in range(0,N):
    w,v=map(int,stdin.readline().split())
    weights.append(w)
    values.append(v)
knapsack(W,N,weights,values,matrix)
stdout.write(str(matrix[W][N]))

Fixed submission:
from sys import stdin,stdout
N,W=map(int,stdin.readline().split())
matrix=[[-1 for i in range(0,W+1)]for j in range(0,N+1)]
for i in range(0,len(matrix)):
    matrix[i][0]=0
for i in range(0,len(matrix[0])):
    matrix[0][i]=0
weights=[]
values=[]
for i in range(0,N):
    w,v=map(int,stdin.readline().split())
    weights.append(w)
    values.append(v)
for item in range(1,N+1):
    for weight in range(1,W+1):
        if(weights[item-1]<=weight):
            matrix[item][weight]=max(values[item-1]+matrix[item-1][weight-weights[item-1]],matrix[item-1][weight])
        else:
            matrix[item][weight]=matrix[item-1][weight]
print(matrix[N][W])

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdout,stdin
def knapsack(C,n,wei,val,mat):
    if(C==0 or n==0):
        return 0
    else:
        if(C>=wei[n-1]):
            a=mat[C-wei[n-1]][n-1]
            b=mat[C][n-1]
            if(a==-1):
                a=knapsack(C-wei[n-1],n-1,wei,val,mat)
                mat[C-wei[n-1]][n-1]=a
            if(b==-1):
                b=knapsack(C,n-1,wei,val,mat)
                mat[C][n-1]=b
            mat[C][n]=max(a+val[n-1],b)
            return (max(a+val[n-1],b))
        else:
            alp=matrix[C][n-1]
            if(alp==-1):
                alp=knapsack(C,n-1,wei,val,mat)
                mat[C][n-1]=alp
                mat[C][n]=alp
            return(alp)
N,W=map(int,stdin.readline().split())
matrix=[[-1 for i in range(0,N+1)]for j in range(0,W+1)]
for i in range(0,len(matrix)):
    matrix[i][0]=0
for i in range(0,len(matrix[0])):
    matrix[0][i]=0
weights=[]
values=[]
for i in range(0,N):
    w,v=map(int,stdin.readline().split())
    weights.append(w)
    values.append(v)
knapsack(W,N,weights,values,matrix)
stdout.write(str(matrix[W][N]))

Fixed submission:
from sys import stdin,stdout
N,W=map(int,stdin.readline().split())
matrix=[[-1 for i in range(0,W+1)]for j in range(0,N+1)]
for i in range(0,len(matrix)):
    matrix[i][0]=0
for i in range(0,len(matrix[0])):
    matrix[0][i]=0
weights=[]
values=[]
for i in range(0,N):
    w,v=map(int,stdin.readline().split())
    weights.append(w)
    values.append(v)
for item in range(1,N+1):
    for weight in range(1,W+1):
        if(weights[item-1]<=weight):
            matrix[item][weight]=max(values[item-1]+matrix[item-1][weight-weights[item-1]],matrix[item-1][weight])
        else:
            matrix[item][weight]=matrix[item-1][weight]
print(matrix[N][W])

Buggy python submission (verdict: Time Limit Exceeded):
def knapsack(C,n,wei,val,mat):
    if(C==0 or n==0):
        return 0
    else:
        if(C>=wei[n-1]):
            a=mat[C-wei[n-1]][n-1]
            b=mat[C][n-1]
            if(a==-1):
                a=knapsack(C-wei[n-1],n-1,wei,val,mat)
                mat[C-wei[n-1]][n-1]=a
            if(b==-1):
                b=knapsack(C,n-1,wei,val,mat)
                mat[C][n-1]=b
            mat[C][n]=max(a+val[n-1],b)
            return (max(a+val[n-1],b))
        else:
            alp=matrix[C][n-1]
            if(alp==-1):
                alp=knapsack(C,n-1,wei,val,mat)
                mat[C][n-1]=alp
                mat[C][n]=alp
            return (alp)
N,W=map(int,input().split())
matrix=[[-1 for i in range(0,N+1)]for j in range(0,W+1)]
for i in range(0,len(matrix)):
    matrix[i][0]=0
for i in range(0,len(matrix[0])):
    matrix[0][i]=0
weights=[]
values=[]
for i in range(0,N):
    w,v=map(int,input().split())
    weights.append(w)
    values.append(v)
knapsack(W,N,weights,values,matrix)
print(matrix[W][N])

Fixed submission:
from sys import stdin,stdout
N,W=map(int,stdin.readline().split())
matrix=[[-1 for i in range(0,W+1)]for j in range(0,N+1)]
for i in range(0,len(matrix)):
    matrix[i][0]=0
for i in range(0,len(matrix[0])):
    matrix[0][i]=0
weights=[]
values=[]
for i in range(0,N):
    w,v=map(int,stdin.readline().split())
    weights.append(w)
    values.append(v)
for item in range(1,N+1):
    for weight in range(1,W+1):
        if(weights[item-1]<=weight):
            matrix[item][weight]=max(values[item-1]+matrix[item-1][weight-weights[item-1]],matrix[item-1][weight])
        else:
            matrix[item][weight]=matrix[item-1][weight]
print(matrix[N][W])

Buggy python submission (verdict: Time Limit Exceeded):
def knapsack(C,n,wei,val):
    if(C==0 or n==0):
        return 0
    else:
        if(C>=wei[n-1]):
            return (max(knapsack(C-wei[n-1],n-1,wei,val)+val[n-1],knapsack(C,n-1,wei,val)))
        else:
            return (knapsack(C,n-1,wei,val))
N,W=map(int,input().split())
weights=[]
values=[]
for i in range(0,N):
    w,v=map(int,input().split())
    weights.append(w)
    values.append(v)
print(knapsack(W,N,weights,values))

Fixed submission:
from sys import stdin,stdout
N,W=map(int,stdin.readline().split())
matrix=[[-1 for i in range(0,W+1)]for j in range(0,N+1)]
for i in range(0,len(matrix)):
    matrix[i][0]=0
for i in range(0,len(matrix[0])):
    matrix[0][i]=0
weights=[]
values=[]
for i in range(0,N):
    w,v=map(int,stdin.readline().split())
    weights.append(w)
    values.append(v)
for item in range(1,N+1):
    for weight in range(1,W+1):
        if(weights[item-1]<=weight):
            matrix[item][weight]=max(values[item-1]+matrix[item-1][weight-weights[item-1]],matrix[item-1][weight])
        else:
            matrix[item][weight]=matrix[item-1][weight]
print(matrix[N][W])