Buggy python submission (verdict: Runtime Error):
for i in range(n):
    b[i] = format(a[i], 'b').zfill(maxd)[::-1]
#print(b)

s = [0 for i in range(maxd)]
c = [0 for i in range(maxd)]
for i in range(maxd):
    c[i] = sum(list(map(lambda x: int(x[i]), b)))
for i in range(digitk1):
    for j in range(i):
        count = c[j]
        if count <= n/2:
            s[i] += (n - count) * 2 ** j
        else:
            s[i] += count * 2 ** j
    count = c[i]

    if int(bink[i]) == 0:
        s[i] = count * 2**i
        pass
    else:
        s[i] += count * 2 ** i

    for j in range(i+1, digitk):
        count = c[j]
        if int(bink[j]) == 0:
            s[i] += count * 2 ** j

        else:
            s[i] += (n-count) * 2 ** j

    for j in range(digitk1, maxd):
        count = c[j]
        s[i] += count * 2 ** j
print(max(s))

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = ['' for i in range(n)]
maxd = len(bin(10 ** 12 + 1))-2
bink = bin(k)[2:]
digitk = len(bink)
bink1 = bin(k+1)[2:]
digitk1 = len(bink1)
bink = bink.zfill(maxd)[::-1]
bink1 = bink1.zfill(maxd)[::-1]
for i in range(n):
    b[i] = format(a[i], 'b').zfill(maxd)[::-1]
#print(b)

s = [0 for i in range(maxd)]
c = [0 for i in range(maxd)]
for i in range(maxd):
    c[i] = sum(list(map(lambda x: int(x[i]), b)))
for i in range(digitk1):
    if bink1[i] == '1':
        for j in range(i):
            if c[j] <= n/2:
                s[i] += (n-c[j]) * 2 ** j
            else:
                s[i] += c[j] * 2 ** j

        s[i] += c[i] * 2 ** i

        for j in range(i+1, digitk1):
            if bink1[j] == '1':
                s[i] += (n-c[j]) * 2 ** j
            else:
                s[i] += c[j] * 2 ** j

    for j in range(digitk1, maxd):
        s[i] += c[j] * 2 ** j
print(max(s))

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = ['' for i in range(n)]
maxd = len(bin(10 ** 12))-2
bink = bin(k)[2:]
digitk = len(bink)
bink = bink.zfill(maxd)[::-1]
#print(bink)
for i in range(n):
    b[i] = format(a[i], 'b').zfill(maxd)[::-1]
#print(b)

s = [0 for i in range(maxd)]

for i in range(digitk):
    for j in range(i):
        count = sum(list(map(lambda x: int(x[j]), b)))
        if count <= n//2:
            s[i] += (n - count) * 2 ** j
        else:
            s[i] += count * 2 ** j

    count = sum(list(map(lambda x: int(x[i]), b)))
    s[i] += count * 2 ** i

    for j in range(i+1, maxd):
        count = sum(list(map(lambda x: int(x[j]), b)))
        if int(bink[j]) == 0:
            s[i] += count * 2 ** j

        else:
            s[i] += (n-count) * 2 ** j

print(max(s))


Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = ['' for i in range(n)]
maxd = len(bin(10 ** 12 + 1))-2
bink = bin(k)[2:]
digitk = len(bink)
bink1 = bin(k+1)[2:]
digitk1 = len(bink1)
bink = bink.zfill(maxd)[::-1]
bink1 = bink1.zfill(maxd)[::-1]
for i in range(n):
    b[i] = format(a[i], 'b').zfill(maxd)[::-1]
#print(b)

s = [0 for i in range(maxd)]
c = [0 for i in range(maxd)]
for i in range(maxd):
    c[i] = sum(list(map(lambda x: int(x[i]), b)))
for i in range(digitk1):
    if bink1[i] == '1':
        for j in range(i):
            if c[j] <= n/2:
                s[i] += (n-c[j]) * 2 ** j
            else:
                s[i] += c[j] * 2 ** j

        s[i] += c[i] * 2 ** i

        for j in range(i+1, digitk1):
            if bink1[j] == '1':
                s[i] += (n-c[j]) * 2 ** j
            else:
                s[i] += c[j] * 2 ** j

    for j in range(digitk1, maxd):
        s[i] += c[j] * 2 ** j
print(max(s))