Buggy python submission (verdict: Runtime Error):
from math import log2

N, K = map(int, input().split())
A = [int(s) for s in input().split()]


def cntbit(bit):
    cnt = 0
    for a in A:
        if a & bit:
            cnt += 1
    return cnt


def getans(bink):
    if len(bink) == 1:
        if bink == "0":
            return cntbit(1)
        else:
            return N - cntbit(1)

    ans = 0
    bit = 1 << (len(bink) - 1)
    if bink[0] == "0":
        return bit * cntbit(bit) + getans(bink[1:])
    else:
        ans1 = bit * (N - cntbit(bit)) + getans(bink[1:])
        ans2 = 0
        ans2 += bit * cntbit(bit)
        for i in range(len(bink) - 1):
            bit = 1 << i
            cnt = cntbit(bit)
            ans2 += bit * max(cnt, N - cnt)
        return max(ans1, ans2)


bink = bin(K)[2:]
bink = "0" * (40 - len(bink)) + bink
print(getans(bink))


Fixed submission:
N, K = map(int, input().split())
A = [int(s) for s in input().split()]


def cntbit(bit):
    cnt = 0
    for a in A:
        if a & bit:
            cnt += 1
    return cnt


def getans(bink):
    if len(bink) == 1:
        if bink == "0":
            return cntbit(1)
        else:
            return N - cntbit(1)

    ans = 0
    bit = 1 << (len(bink) - 1)
    if bink[0] == "0":
        return bit * cntbit(bit) + getans(bink[1:])
    else:
        ans1 = bit * (N - cntbit(bit)) + getans(bink[1:])
        ans2 = 0
        ans2 += bit * cntbit(bit)
        for i in range(len(bink) - 1):
            bit = 1 << i
            cnt = cntbit(bit)
            ans2 += bit * max(cnt, N - cnt)
        return max(ans1, ans2)


bink = bin(K)[2:]
bink = "0" * (40 - len(bink)) + bink
print(getans(bink))

Buggy python submission (verdict: Time Limit Exceeded):
from math import log2

N, K = map(int, input().split())
A = [int(s) for s in input().split()]


def cntbit(bit):
    cnt = 0
    for a in A:
        if a & bit:
            cnt += 1
    return cnt


def getans(bink):
    if len(bink) == 1:
        if bink == "0":
            return cntbit(1)
        else:
            return N - cntbit(1)

    ans = 0
    bit = 1 << (len(bink) - 1)
    if bink[0] == "0":
        return bit * cntbit(bit) + getans(bink[1:])
    else:
        ans1 = bit * (N - cntbit(bit)) + getans(bink[1:])
        ans2 = 0
        ans2 += bit * cntbit(bit)
        for i in range(len(bink) - 1):
            bit = 1 << i
            cnt = cntbit(bit)
            ans2 += bit * max(cnt, N - cnt)
        return max(ans1, ans2)


bink = bin(K)[2:]
bink = "0" * (40 - len(bink)) + bink
print(getans(bink))


Fixed submission:
N, K = map(int, input().split())
A = [int(s) for s in input().split()]


def cntbit(bit):
    cnt = 0
    for a in A:
        if a & bit:
            cnt += 1
    return cnt


def getans(bink):
    if len(bink) == 1:
        if bink == "0":
            return cntbit(1)
        else:
            return N - cntbit(1)

    ans = 0
    bit = 1 << (len(bink) - 1)
    if bink[0] == "0":
        return bit * cntbit(bit) + getans(bink[1:])
    else:
        ans1 = bit * (N - cntbit(bit)) + getans(bink[1:])
        ans2 = 0
        ans2 += bit * cntbit(bit)
        for i in range(len(bink) - 1):
            bit = 1 << i
            cnt = cntbit(bit)
            ans2 += bit * max(cnt, N - cnt)
        return max(ans1, ans2)


bink = bin(K)[2:]
bink = "0" * (40 - len(bink)) + bink
print(getans(bink))