Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int,input().split()))

bin_digit = [0]*50
for ai in a:
    i = 0
    while ai>0:
        bin_digit[i] += int(bin(ai)[-1])
        i += 1
        ai = ai>>1

length_k = len(bin(k))-2
x_k = [None]*length_k
for i in range(length_k):
    if 2*bin_digit[i]>=n:
        x_k[i] = 0
    else:
        x_k[i] = 1

bin_k = list(map(int, bin(k)[-1:1:-1]))
for i in range(length_k-1,-1,-1):
    if x_k[i]>bin_k[i]:
        x_k[i] = 1-x_k[i]
    elif x_k[i]<bin_k[i]:
        break

x = sum(x_k[i]*2**i for i in range(len(x_k)))
ans = sum(x^ae for ae in a)
print(ans)


Fixed submission:
n, k = map(int, input().split())
a = list(map(int,input().split()))

bin_digit = [0]*50
for ai in a:
    i = 0
    bin_ai = bin(ai)
    for i in range(len(bin_ai)-2):
        if bin_ai[-1-i] == '1':
            bin_digit[i] += 1
        i += 1

bin_k = list(map(int, bin(k)[-1:1:-1]))
length_k = len(bin_k)
x_k = [None]*length_k
for i in range(length_k):
    if 2*bin_digit[i]>=n:
        x_k[i] = 0
    else:
        x_k[i] = 1

for i in range(length_k-1,-1,-1):
    if x_k[i]>bin_k[i]:
        x_k[i] = 1-x_k[i]
    elif x_k[i]<bin_k[i]:
        break

x = sum(x_k[i]*2**i for i in range(len(x_k)))
ans = sum(x^ae for ae in a)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int,input().split()))

bin_digit = [0]*50
for ai in a:
    i = 0
    while ai>0:
        bin_digit[i] += int(bin(ai)[-1])
        i += 1
        ai = ai>>1

length_k = len(bin(k))-2
x_k = [0]*length_k
for i in range(length_k):
    if bin_digit[i]>=n-bin_digit[i]:
        x_k[i] = 0
    else:
        x_k[i] = 1

bin_k = bin(k)[-1:1:-1]
for i in range(length_k-1,-1,-1):
    if x_k[i]>int(bin_k[i]):
        x_k[i] = 1-x_k[i]
    elif x_k[i]<int(bin_k[i]):
        break

x = sum(x_k[i]*2**i for i in range(len(x_k)))
ans = sum(x^ae for ae in a)
print(ans)


Fixed submission:
n, k = map(int, input().split())
a = list(map(int,input().split()))

bin_digit = [0]*50
for ai in a:
    i = 0
    bin_ai = bin(ai)
    for i in range(len(bin_ai)-2):
        if bin_ai[-1-i] == '1':
            bin_digit[i] += 1
        i += 1

bin_k = list(map(int, bin(k)[-1:1:-1]))
length_k = len(bin_k)
x_k = [None]*length_k
for i in range(length_k):
    if 2*bin_digit[i]>=n:
        x_k[i] = 0
    else:
        x_k[i] = 1

for i in range(length_k-1,-1,-1):
    if x_k[i]>bin_k[i]:
        x_k[i] = 1-x_k[i]
    elif x_k[i]<bin_k[i]:
        break

x = sum(x_k[i]*2**i for i in range(len(x_k)))
ans = sum(x^ae for ae in a)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
a_bin = list(map(bin, a))

bin_digit = [0]*50
for ai in a:
    i = 0
    # print(bin(ai))
    while ai>0:
        bin_digit[i] += int(bin(ai)[-1])
        i += 1
        ai = ai>>1
# print(bin_digit)

length_k = len(bin(k))-2
x_k = [0]*length_k
for i in range(length_k):
    if bin_digit[i]>=n-bin_digit[i]:
        x_k[i] = 0
    else:
        x_k[i] = 1

# print('x_k',x_k)
# print('x_k=',sum(x_k[i]*2**i for i in range(len(x_k))))

bin_k = bin(k)[-1:1:-1]
for i in range(length_k-1,-1,-1):
    if x_k[i]>int(bin_k[i]):
        x_k[i] = 1-x_k[i]
    elif x_k[i]<int(bin_k[i]):
        break

x = sum(x_k[i]*2**i for i in range(len(x_k)))
# print('x=',x)
ans = sum(x^ae for ae in a)
print(ans)


Fixed submission:
n, k = map(int, input().split())
a = list(map(int,input().split()))

bin_digit = [0]*50
for ai in a:
    i = 0
    bin_ai = bin(ai)
    for i in range(len(bin_ai)-2):
        if bin_ai[-1-i] == '1':
            bin_digit[i] += 1
        i += 1

bin_k = list(map(int, bin(k)[-1:1:-1]))
length_k = len(bin_k)
x_k = [None]*length_k
for i in range(length_k):
    if 2*bin_digit[i]>=n:
        x_k[i] = 0
    else:
        x_k[i] = 1

for i in range(length_k-1,-1,-1):
    if x_k[i]>bin_k[i]:
        x_k[i] = 1-x_k[i]
    elif x_k[i]<bin_k[i]:
        break

x = sum(x_k[i]*2**i for i in range(len(x_k)))
ans = sum(x^ae for ae in a)
print(ans)