Buggy python submission (verdict: Runtime Error):
def calculate(j, eval, vval, ks):
    xi = max(eval)
    if xi == 0:
        return 0
    i = eval.index(xi)

    if d[i] >= n - d[i]:
        if ks[i] == '1':
            return calculate(i, eval[:i], vval[:i], ks[:i]) + sum(vval[i:])
        else:
            eval[i] = 0
            return vval[i] + calculate(j, eval, vval, ks)
    else:
        if ks[i] == '1':
            eval[i] = 0
            return vval[i] + calculate(j, eval, vval, ks)
        else:
            m = int(ks[:i + 1], 2)
            if m == 0:
                t = d[i] << (maxlen - i - 1)
                eval[i] = 0
                return t + calculate(j, eval, vval, ks)
            else:
                ks = format(m - 1, 'b')
                ks = '0'*(i - len(ks) + 1) + ks
                return calculate(i, eval[:i], vval[:i], ks[:i]) + sum(vval[i:])


if __name__ == "__main__":
    n, k = map(int, input().split())
    a = [format(int(x), 'b') for x in input().split()]

    maxlen = 40
    ks = format(k, 'b')
    ks = '0' * (maxlen - len(ks)) + ks

    d = [0] * maxlen
    for c in a:
        len_ = len(c)
        for j in range(len_):
            if c[j] == '1':
                d[j + maxlen - len_] += 1
    e = [0] * maxlen
    val = [0] * maxlen
    flag = True
    result = 0
    for j in range(maxlen):
        if flag:
            if ks[j] == '0' and d[j] == 0:
                continue
            else:
                flag = False
        e[j] = abs(2 * d[j] - n) << (maxlen - j - 1)
        if e[j] == 0:
            result += d[j] << (maxlen - j - 1)
        else:
            val[j] = max(d[j], n - d[j]) << (maxlen - j - 1)

    result += calculate(maxlen, e, val, ks)
    print(result)


Fixed submission:
from sys import stdin
n, k = map(int, stdin.readline().rstrip().split())
a = [format(int(x), 'b')[::-1] for x in stdin.readline().rstrip().split()]

d = [0] * 64
maxlen = 0
for c in a:
    len_ = len(c)
    if len_ > maxlen:
        maxlen = len_
    for j in range(len(c)):
        if c[j] == '1':
            d[j] += 1

m = format(k, 'b')[::-1]
if len(m) >= maxlen:
    maxlen = len(m)
else:
    m = m + '0' * (maxlen - len(m))

flag = False
result = 0
for j in range(maxlen - 1, -1, -1):
    if d[j] >= n - d[j]:
        result = result * 2 + d[j]
        if m[j] == '1':
            flag = True
    else:
        if flag or m[j] == '1':
            result = result * 2 + n - d[j]
        else:
            result = result * 2 + d[j]

print(result)

Buggy python submission (verdict: Runtime Error):
from sys import stdin
n, k = map(int, stdin.readline().rstrip().split())
a = [format(int(x), 'b') for x in stdin.readline().rstrip().split()]

m = format(k, 'b')
maxlen = len(m)

d = [0] * maxlen
for c in a:
    len_ = len(c)
    for j in range(len_):
        if c[j] == '1':
            d[j + maxlen - len_] += 1

flag = False
result = 0
for j in range(maxlen):
    if d[j] >= n - d[j]:
        result = result * 2 + d[j]
        if m[j] == '1':
            flag = True
    else:
        if flag or m[j] == '1':
            result = result * 2 + n - d[j]
        else:
            result = result * 2 + d[j]

print(result)

Fixed submission:
from sys import stdin
n, k = map(int, stdin.readline().rstrip().split())
a = [format(int(x), 'b')[::-1] for x in stdin.readline().rstrip().split()]

d = [0] * 64
maxlen = 0
for c in a:
    len_ = len(c)
    if len_ > maxlen:
        maxlen = len_
    for j in range(len(c)):
        if c[j] == '1':
            d[j] += 1

m = format(k, 'b')[::-1]
if len(m) >= maxlen:
    maxlen = len(m)
else:
    m = m + '0' * (maxlen - len(m))

flag = False
result = 0
for j in range(maxlen - 1, -1, -1):
    if d[j] >= n - d[j]:
        result = result * 2 + d[j]
        if m[j] == '1':
            flag = True
    else:
        if flag or m[j] == '1':
            result = result * 2 + n - d[j]
        else:
            result = result * 2 + d[j]

print(result)