Problem p02975 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 7)

debug = True
debug = False


def dprint(*objects):
    if debug == True:
        print(*objects)


def solve():
    N = int(input())
    a_list = list(map(int, input().split()))
    a_list.sort()

    import collections
    c = collections.Counter(a_list)
    keys = sorted(c.keys())
    nk = len(keys)

    dprint(c)
    dprint(keys)

    # a[0] からスタート
    found = False
    start = keys[0]
    ccand = []
    rcand = []
    for i in range(0, nk):

        # 同じ数字を使う場合はcountに気を付ける
        if i == 0 and c[keys[i]] <= 1:
            continue

        center = keys[i]
        max = start + center

        for j in range(i, nk):
            right = keys[j]

            # 同じ数字を使う場合はcountに気を付ける
            if i == j and i == 0 and c[keys[i]] <= 2:
                continue
            if i == j and c[keys[i]] <= 1:
                continue

            if right > max:
                # これより大きいところにはない
                break

            if right == start ^ center:
                found = True
                ccand.append(center)
                rcand.append(right)
                # break
        #
        # if found == True:
        #     break

    if found == False:
        dprint("first pair search failed")
        print("No")
        return

    dprint("first set created")
    dprint(ccand, rcand)
    center = ccand[-1]
    right = rcand[-1]

    c[start] = c[start] - 1
    c[center] = c[center] - 1
    c[right] = c[right] - 1

    # これで、0, second, third が決まったので、あとは順次決める
    left = center
    center = right
    dprint(start, left, center)

    cnt = 3
    while True:
        # N個作ったら終了
        if cnt == N:
            break

        # 次の数字
        right = left ^ center

        # 検索
        rcount = c.get(right, -1)
        if rcount <= 0:
            dprint("break rcount:", rcount)
            print("No")
            return

        dprint("found", right, rcount)

        # update
        c[right] = c[right] - 1
        left = center
        center = right

        cnt += 1
        dprint("next", cnt, left, center)

    if start ^ left == center:
        print("Yes")
    else:
        dprint("last check failed", start, left, center)
        print("No")


solve()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 7)

debug = True
debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

def solve():
    N = int(input())
    a_list = list(map(int, input().split()))

    import collections
    c = collections.Counter(a_list)
    keys = sorted(c.keys())
    nk = len(keys)

    dprint(c)
    dprint(keys)

    # a[0] からスタート
    found = False
    start = keys[0]

    cand_center = []
    cand_right = []

    for i in range(1, nk):
        center = keys[i]
        max = start + center

        for j in range(i+1, nk):
            right = keys[j]
            if right > max:
                # これより大きいところにはない
                dprint("break by max condition")
                break

            if right == start ^ center:
                cand_center.append(center)
                cand_right.append(right)

    if len(cand_center) == 0:
        print("No")
        return

    if len(cand_center) > 1:
        raise ValueError("majika")

    center = cand_center[0]
    right = cand_right[0]
    c[start] = c[start] - 1
    c[center] = c[center] - 1
    c[right] = c[right] - 1

    # これで、0, second, third が決まったので、あとは順次決める
    left = center
    center = right
    dprint(start, left, center)

    cnt = 3
    while True:
        # N個作ったら終了
        if cnt == N:
            break

        # 次の数字
        right = left ^ center

        # 検索
        rcount = c.get(right, -1)
        if rcount <= 0:
            dprint("break", rcount)
            print("No")
            return

        dprint("found", right, rcount)

        # update
        c[right] = c[right] - 1
        left = center
        center = right

        cnt += 1
        dprint("next", cnt, left, center)

    if start ^ left == center:
        print("Yes")
    else:
        dprint("break", start, left, center)
        print("No")


solve()