Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    from bisect import bisect_left
    N, W = map(int, input().split())
    n1 = N//2
    n2 = N-n1
    a = [[tuple(map(int, input().split())) for _ in [0]*n1], [tuple(map(int, input().split())) for _ in [0]*n2]]
    dp = [[(0, 0) for _ in [0]*(2**n1)], [(0, 0) for _ in [0]*(2**n2)]]

    for i in range(2):
        n = len(a[i])
        cdp = dp[i]
        ca = a[i]
        append = cdp.append
        for bit in range(1, 2**n):
            v, w = 0, 0
            for j in range(n):
                if 2**j & bit:
                    v += ca[j][0]
                    w += ca[j][1]
            if w <= W:
                cdp[bit] = (w, v)
        cdp = sorted(cdp)
        dp[i] = [pair for w in [[-1]] for sa in [w.__setitem__] for pair in cdp if pair[1] > w[0] and not sa(0, pair[1])]

    result = 0
    inf = float("inf")
    l = len(dp[1])
    for w, v in dp[0]:
        i = bisect_left(dp[1], (W-w, inf))
        newv = v + dp[1][i-1][1] if i > 0 else 0
        #print(w, v, i, newv)

        if newv > result:
            result = newv

    print(result)

if __name__ == "__main__":
    solve()

Fixed submission:
import sys
from bisect import bisect_right


def meet_in_the_middle(a: list, limit: int) -> tuple:
    first_v, first_w, second_v, second_w = [], [], [], []
    for items, v_append, w_append in (
            (a[:len(a)//2], first_v.append, first_w.append),
            (a[len(a)//2:], second_v.append, second_w.append)
    ):
        enumerated = [(0, 0)]
        for v, w in items:
            enumerated += [(w+_w, v+_v) for _w, _v in enumerated]

        enumerated.sort()
        enumerated = enumerated[:bisect_right(enumerated, (limit, float("inf")))] + [(limit+1, 0)]
        max_v = -1
        for (cw, cv), (nw, nv) in zip(enumerated, enumerated[1:]):
            if cw < nw and max_v < cv:
                v_append(cv)
                w_append(cw)
                max_v = cv

    return (first_v, first_w), (second_v, second_w)


N, W = map(int, input().split())
a = [list(map(int, l.split())) for l in sys.stdin]

(first_v, first_w), (second_v, second_w) = meet_in_the_middle(a, W)
ans = 0
for v, w in zip(first_v, first_w):
    i = bisect_right(second_w, W-w)-1
    new_v = v + second_v[i]
    if ans < new_v:
        ans = new_v
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    from bisect import bisect_left
    N, W = map(int, input().split())
    n1 = N//2
    n2 = N-n1
    a = [[tuple(map(int, input().split())) for _ in [0]*n1], [tuple(map(int, input().split())) for _ in [0]*n2]]
    dp = [[(0, 0) for _ in [0]*(2**n1)], [(0, 0) for _ in [0]*(2**n2)]]

    for i in range(2):
        n = len(a[i])
        cdp = dp[i]
        ca = a[i]
        for j in range(n):
            if ca[j][1] > W:
                continue
            for bitset in range(2**j, 2**(j+1)):
                #print(bin(bitset))
                v, w = ca[j]
                for k in range(j):
                    if 2**k & bitset:
                        v += ca[k][0]
                        w += ca[k][1]
                if w <= W:
                    cdp[bitset] = (w, v)
        cdp = sorted(cdp)
        dp[i] = [pair for w in [[-1]] for sa in [w.__setitem__] for pair in cdp if pair[1] > w[0] and not sa(0, pair[1])]

    #print(dp)
    result = 0
    inf = float("inf")
    l = len(dp[1])
    for w, v in dp[0]:
        i = bisect_left(dp[1], (W-w, inf))
        newv = v + dp[1][i-1][1] if i > 0 else 0
        #print(w, v, i, newv)

        if newv > result:
            result = newv

    print(result)

if __name__ == "__main__":
    solve()

Fixed submission:
import sys
from bisect import bisect_right


def meet_in_the_middle(a: list, limit: int) -> tuple:
    first_v, first_w, second_v, second_w = [], [], [], []
    for items, v_append, w_append in (
            (a[:len(a)//2], first_v.append, first_w.append),
            (a[len(a)//2:], second_v.append, second_w.append)
    ):
        enumerated = [(0, 0)]
        for v, w in items:
            enumerated += [(w+_w, v+_v) for _w, _v in enumerated]

        enumerated.sort()
        enumerated = enumerated[:bisect_right(enumerated, (limit, float("inf")))] + [(limit+1, 0)]
        max_v = -1
        for (cw, cv), (nw, nv) in zip(enumerated, enumerated[1:]):
            if cw < nw and max_v < cv:
                v_append(cv)
                w_append(cw)
                max_v = cv

    return (first_v, first_w), (second_v, second_w)


N, W = map(int, input().split())
a = [list(map(int, l.split())) for l in sys.stdin]

(first_v, first_w), (second_v, second_w) = meet_in_the_middle(a, W)
ans = 0
for v, w in zip(first_v, first_w):
    i = bisect_right(second_w, W-w)-1
    new_v = v + second_v[i]
    if ans < new_v:
        ans = new_v
print(ans)