Buggy python submission (verdict: Runtime Error):
#import sys
#stdin = sys.stdin

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

from itertools import accumulate

n,w = li()
wi,vi = li()
w0 = wi
v_list = [[0] for _ in range(4)]
v_list[0].append(vi)

for _ in range(n-1):
    wi,vi = li()
    v_list[wi-w0].append(vi)
    
for i in range(4):
    v_list[i].sort(reverse=True)
    v_list[i] = list(accumulate(v_list[i]))
    
ans = 0
res = w
for w1 in range(len(v_list[0])):
    
    if w - w0*(w1) < 0:
        pass
    else:
        res1 = w - w0*(w1)
        ans1 = v_list[0][w1]
        ans = max(ans, ans1)
    

    for w2 in range(len(v_list[1])):

        if res1 - (w0+1)*(w2) < 0:
            pass
        
        else:
            res2 = res1 - (w0+1)*(w2)
            ans2 = ans1 + v_list[1][w2]
            ans = max(ans, ans2)

        
        
        for w3 in range(len(v_list[2])):
            
            if res2 - (w0+2)*(w3) < 0:
                pass
            
            else:
                res3 = res2 - (w0+2)*(w3)
                ans3 = ans2 + v_list[2][w3]
                ans = max(ans, ans3)
                
            
            for w4 in range(len(v_list[3])):

                if res3 - (w0+3)*(w4) < 0:
                    pass
                
                else:
                    res4 = res3 - (w0+3)*(w4)
                    ans4 = ans3 + v_list[3][w4]
                    ans = max(ans, ans4)

                
print(ans)

Fixed submission:

from collections import defaultdict

N,W = map(int, input().split())
WV = []
for i in range(N):
    WV.append(list(map(int, input().split())))

dp = defaultdict(int)

dp[0] = 0

for w,v in WV:
    for nw, nv in list(dp.items()):
        if nw+w <= W:
            dp[nw+w] = max(dp[nw+w], nv+v)
            
print(max(dp.values()))

Buggy python submission (verdict: Time Limit Exceeded):
N,W = map(int, input().split())
WV = [[0,0]]
for i in range(N):
    WV.append(list(map(int, input().split())))

W1 = WV[0][0]
for i in range(N):
    WV[i][0] -= W1
    

dp = [[0 for _ in range(W+1)] for _ in range(N+1)]

W = W-N*W1

for n in range(1,N+1):
    for w in range(W+1):
        if w<WV[n][0]:
            dp[n][w] = dp[n-1][w]
        else:
            dp[n][w] = max(dp[n-1][w],
                           dp[n-1][w-WV[n][0]]+WV[n][1])

print(dp[N][W])

Fixed submission:

from collections import defaultdict

N,W = map(int, input().split())
WV = []
for i in range(N):
    WV.append(list(map(int, input().split())))

dp = defaultdict(int)

dp[0] = 0

for w,v in WV:
    for nw, nv in list(dp.items()):
        if nw+w <= W:
            dp[nw+w] = max(dp[nw+w], nv+v)
            
print(max(dp.values()))

Buggy python submission (verdict: Time Limit Exceeded):

N,W = map(int, input().split())
WV = [[0,0]]
for i in range(N):
    WV.append(list(map(int, input().split())))

W1 = WV[0][0]
for i in range(N):
    WV[i][0] -= W1
    

dp = [[0 for _ in range(W+1)] for _ in range(N+1)]

for n in range(1,N+1):
    for w in range(W+1):
        if w<WV[n][0]:
            dp[n][w] = dp[n-1][w]
        else:
            dp[n][w] = max(dp[n-1][w],
                           dp[n-1][w-WV[n][0]]+WV[n][1])

print(dp[N][W])

Fixed submission:

from collections import defaultdict

N,W = map(int, input().split())
WV = []
for i in range(N):
    WV.append(list(map(int, input().split())))

dp = defaultdict(int)

dp[0] = 0

for w,v in WV:
    for nw, nv in list(dp.items()):
        if nw+w <= W:
            dp[nw+w] = max(dp[nw+w], nv+v)
            
print(max(dp.values()))