Buggy python submission (verdict: Runtime Error):
n,w=map(int, input().split())
lim = min(w,10**5)
dp = [0] * (lim+1)
items = []
mod = 10**5
for _ in range(n):
    ww,v = map(int, input().split())
    items.append((ww,v))
    if ww > mod:
        continue
    for i in range(ww,lim+1)[::-1]:
        # print(i)
        if dp[i-ww] + v > dp[i]:
            dp[i] = dp[i-ww] + v
    # print(dp)
if w <= 10**5:
    print(dp[-1])
    exit()
ll = w-mod
dd = [0] * (ll+1)
for ww,v in items:
    if ww <= mod:
        continue
    u = ww % mod
    for i in range(u,ll+1)[::-1]:
        if dd[i-u] + v > dd[i]:
            dd[i] = dd[i-u] + v
    # print(dp)
print(dp[-1] + dd[-1])


Fixed submission:
from itertools import accumulate
n,w=map(int, input().split())
items = {}
for _ in range(n):
    u,v = map(int, input().split())
    if u not in items:
        items[u] = []
    items[u].append(v)
cost = 0
acc = []
minkey = min(items.keys())
for i in range(4):
    if minkey + i in items:
        items[minkey + i].sort(reverse=True)
        acc.append([0] + list(accumulate(items[minkey + i])))
    else:
        acc.append([0])

ans = 0
for i in range(len(acc[0])):
    a = (i) * minkey
    if w < a:
        continue
    for j in range(len(acc[1])):
        b = (j) * (minkey+1)
        if w < a + b:
            continue
        for k in range(len(acc[2])):
            c = (k) * (minkey+2)
            if w < a + b + c:
                continue
            for l in range(len(acc[3])):
                d = (l) * (minkey+3)
                if w < a + b + c + d:
                    continue
                cost = acc[0][i] + acc[1][j] + acc[2][k] + acc[3][l]
                if cost > ans :
                    ans = cost

print(ans)

Buggy python submission (verdict: Runtime Error):
n,w=map(int, input().split())
dp = [0] * (w+1)
ans = 0
for _ in range(n):
    ww,v = map(int, input().split())
    for i in range(ww,w+1)[::-1]:
        if dp[i-ww] + v > dp[i]:
            dp[i] = dp[i-ww] + v
            if ans < dp[i]:
                ans = dp[i]
    # print(dp)
print(ans)

Fixed submission:
from itertools import accumulate
n,w=map(int, input().split())
items = {}
for _ in range(n):
    u,v = map(int, input().split())
    if u not in items:
        items[u] = []
    items[u].append(v)
cost = 0
acc = []
minkey = min(items.keys())
for i in range(4):
    if minkey + i in items:
        items[minkey + i].sort(reverse=True)
        acc.append([0] + list(accumulate(items[minkey + i])))
    else:
        acc.append([0])

ans = 0
for i in range(len(acc[0])):
    a = (i) * minkey
    if w < a:
        continue
    for j in range(len(acc[1])):
        b = (j) * (minkey+1)
        if w < a + b:
            continue
        for k in range(len(acc[2])):
            c = (k) * (minkey+2)
            if w < a + b + c:
                continue
            for l in range(len(acc[3])):
                d = (l) * (minkey+3)
                if w < a + b + c + d:
                    continue
                cost = acc[0][i] + acc[1][j] + acc[2][k] + acc[3][l]
                if cost > ans :
                    ans = cost

print(ans)

Buggy python submission (verdict: Runtime Error):
4 1
10 100
10 100
10 100
10 100

Fixed submission:
from itertools import accumulate
n,w=map(int, input().split())
items = {}
for _ in range(n):
    u,v = map(int, input().split())
    if u not in items:
        items[u] = []
    items[u].append(v)
cost = 0
acc = []
minkey = min(items.keys())
for i in range(4):
    if minkey + i in items:
        items[minkey + i].sort(reverse=True)
        acc.append([0] + list(accumulate(items[minkey + i])))
    else:
        acc.append([0])

ans = 0
for i in range(len(acc[0])):
    a = (i) * minkey
    if w < a:
        continue
    for j in range(len(acc[1])):
        b = (j) * (minkey+1)
        if w < a + b:
            continue
        for k in range(len(acc[2])):
            c = (k) * (minkey+2)
            if w < a + b + c:
                continue
            for l in range(len(acc[3])):
                d = (l) * (minkey+3)
                if w < a + b + c + d:
                    continue
                cost = acc[0][i] + acc[1][j] + acc[2][k] + acc[3][l]
                if cost > ans :
                    ans = cost

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,W = map(int, input().split())

knap = [0 for i in range(W+1)]
mono = []

for i in range(N):
    w,v = map(int, input().split())
    mono.append([w,v])
mono.sort(key = lambda x:x[1], reverse=True)
for w, v in mono:
    for i in range(w, W+1)[::-1]:
        if knap[i] < knap[i-w] + v:
            knap[i] = knap[i-w] + v
print(knap[W])


Fixed submission:
from itertools import accumulate
n,w=map(int, input().split())
items = {}
for _ in range(n):
    u,v = map(int, input().split())
    if u not in items:
        items[u] = []
    items[u].append(v)
cost = 0
acc = []
minkey = min(items.keys())
for i in range(4):
    if minkey + i in items:
        items[minkey + i].sort(reverse=True)
        acc.append([0] + list(accumulate(items[minkey + i])))
    else:
        acc.append([0])

ans = 0
for i in range(len(acc[0])):
    a = (i) * minkey
    if w < a:
        continue
    for j in range(len(acc[1])):
        b = (j) * (minkey+1)
        if w < a + b:
            continue
        for k in range(len(acc[2])):
            c = (k) * (minkey+2)
            if w < a + b + c:
                continue
            for l in range(len(acc[3])):
                d = (l) * (minkey+3)
                if w < a + b + c + d:
                    continue
                cost = acc[0][i] + acc[1][j] + acc[2][k] + acc[3][l]
                if cost > ans :
                    ans = cost

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,W = map(int, input().split())

knap = [0 for i in range(W+1)]
mono = []

for i in range(N):
    w,v = map(int, input().split())
    mono.append([w,v])
    
for w, v in mono:
    for i in range(w, W+1)[::-1]:
        knap[i] = max(knap[i], knap[i-w] + v)
print(knap[W])


Fixed submission:
from itertools import accumulate
n,w=map(int, input().split())
items = {}
for _ in range(n):
    u,v = map(int, input().split())
    if u not in items:
        items[u] = []
    items[u].append(v)
cost = 0
acc = []
minkey = min(items.keys())
for i in range(4):
    if minkey + i in items:
        items[minkey + i].sort(reverse=True)
        acc.append([0] + list(accumulate(items[minkey + i])))
    else:
        acc.append([0])

ans = 0
for i in range(len(acc[0])):
    a = (i) * minkey
    if w < a:
        continue
    for j in range(len(acc[1])):
        b = (j) * (minkey+1)
        if w < a + b:
            continue
        for k in range(len(acc[2])):
            c = (k) * (minkey+2)
            if w < a + b + c:
                continue
            for l in range(len(acc[3])):
                d = (l) * (minkey+3)
                if w < a + b + c + d:
                    continue
                cost = acc[0][i] + acc[1][j] + acc[2][k] + acc[3][l]
                if cost > ans :
                    ans = cost

print(ans)