Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, W = map(int, input().split())
    VN = []
    WN = []
    i = 0
    while i < N:
        w, v = map(int, input().split())
        WN.append(w)
        VN.append(v)
        i += 1

    ans = []
    mask = 0
    while mask < 1<<N:
        i = 0
        w = 0
        v = 0
        while i < N:
            if mask & 1<<i:
                w += WN[i]
                if w > W:
                    break
                else:
                    v += VN[i]
            i += 1
        if w <= W:
            ans.append(v)
        mask += 1

    print(max(ans))

main()


Fixed submission:
def main():
    N, W = map(int, input().split())
    w1 = [0, 0]
    w2 = [0, 0]
    w3 = [0, 0]
    w4 = [0, 0]
    v1 = []
    v2 = []
    v3 = []
    v4 = []
    i = 0
    while i < N:
        w, v = map(int, input().split())
        if i == 0 or w == w1[0]:
            w1[0] = w
            w1[1] += 1
            v1.append(v)
        elif w == w1[0] + 1:
            w2[0] = w
            w2[1] += 1
            v2.append(v)
        elif w == w1[0] + 2:
            w3[0] = w
            w3[1] += 1
            v3.append(v)
        elif w == w1[0] + 3:
            w4[0] = w
            w4[1] += 1
            v4.append(v)
        i += 1

    v1.sort(reverse=True)
    v2.sort(reverse=True)
    v3.sort(reverse=True)
    v4.sort(reverse=True)

    ans = []
    for i in range(w1[1] + 1):
        for j in range(w2[1] + 1):
            for k in range(w3[1] + 1):
                for l in range(w4[1] + 1):
                    w = w1[0] * i + w2[0] * j + w3[0] * k + w4[0] * l
                    v = sum(v1[:i] + v2[:j] + v3[:k] + v4[:l])
                    if w <= W:
                        ans.append(v)

    print(max(ans))

main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, W = map(int, input().split())
    VN = []
    WN = []
    i = 0
    while i < N:
        w, v = map(int, input().split())
        WN.append(w)
        VN.append(v)
        i += 1

    ans = []
    mask = 0
    while mask < 1<<N:
        i = 0
        w = 0
        v = 0
        while i < N:
            if mask & 1<<i:
                w += WN[i]
                v += VN[i]
            i += 1
        if w <= W:
            ans.append(v)
        mask += 1

    print(max(ans))

main()


Fixed submission:
def main():
    N, W = map(int, input().split())
    w1 = [0, 0]
    w2 = [0, 0]
    w3 = [0, 0]
    w4 = [0, 0]
    v1 = []
    v2 = []
    v3 = []
    v4 = []
    i = 0
    while i < N:
        w, v = map(int, input().split())
        if i == 0 or w == w1[0]:
            w1[0] = w
            w1[1] += 1
            v1.append(v)
        elif w == w1[0] + 1:
            w2[0] = w
            w2[1] += 1
            v2.append(v)
        elif w == w1[0] + 2:
            w3[0] = w
            w3[1] += 1
            v3.append(v)
        elif w == w1[0] + 3:
            w4[0] = w
            w4[1] += 1
            v4.append(v)
        i += 1

    v1.sort(reverse=True)
    v2.sort(reverse=True)
    v3.sort(reverse=True)
    v4.sort(reverse=True)

    ans = []
    for i in range(w1[1] + 1):
        for j in range(w2[1] + 1):
            for k in range(w3[1] + 1):
                for l in range(w4[1] + 1):
                    w = w1[0] * i + w2[0] * j + w3[0] * k + w4[0] * l
                    v = sum(v1[:i] + v2[:j] + v3[:k] + v4[:l])
                    if w <= W:
                        ans.append(v)

    print(max(ans))

main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, W = map(int, input().split())
    VN = []
    WN = []
    for i in range(N):
        w, v = map(int, input().split())
        WN.append(w)
        VN.append(v)

    ans = 0
    mask = 0
    while mask < 1<<N:
        i = 0
        w = []
        v = []
        while i < N:
            if mask & 1<<i:
                w.append(WN[i])
                v.append(VN[i])
            i += 1
        if sum(w) <= W:
            ans = max(ans, sum(v))
        mask += 1

    print(ans)

main()


Fixed submission:
def main():
    N, W = map(int, input().split())
    w1 = [0, 0]
    w2 = [0, 0]
    w3 = [0, 0]
    w4 = [0, 0]
    v1 = []
    v2 = []
    v3 = []
    v4 = []
    i = 0
    while i < N:
        w, v = map(int, input().split())
        if i == 0 or w == w1[0]:
            w1[0] = w
            w1[1] += 1
            v1.append(v)
        elif w == w1[0] + 1:
            w2[0] = w
            w2[1] += 1
            v2.append(v)
        elif w == w1[0] + 2:
            w3[0] = w
            w3[1] += 1
            v3.append(v)
        elif w == w1[0] + 3:
            w4[0] = w
            w4[1] += 1
            v4.append(v)
        i += 1

    v1.sort(reverse=True)
    v2.sort(reverse=True)
    v3.sort(reverse=True)
    v4.sort(reverse=True)

    ans = []
    for i in range(w1[1] + 1):
        for j in range(w2[1] + 1):
            for k in range(w3[1] + 1):
                for l in range(w4[1] + 1):
                    w = w1[0] * i + w2[0] * j + w3[0] * k + w4[0] * l
                    v = sum(v1[:i] + v2[:j] + v3[:k] + v4[:l])
                    if w <= W:
                        ans.append(v)

    print(max(ans))

main()