Buggy python submission (verdict: Runtime Error):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect_left

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = MAP()
A = LIST()
tmp = [0]*40
for a in A:  # tmpに各ビット毎の1の出現回数を保存
    for i in range(39, -1, -1):
        if a&(1<<i):
            tmp[k]+=1
# print(tmp)
a = 0  # K をこえないように管理する
for i in range(39,-1,-1):
	if a + (1<<i) <= K and tmp[i] < (N+1)//2:
		a += (1<<i)

# print(tmp2)

ans = 0
for i in A:
	ans += i^a
print(ans)


Fixed submission:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect_left

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = MAP()
A = LIST()
tmp = [0]*40
for a in A:  # tmpに各ビット毎の1の出現回数を保存
    for i in range(40):
        if a&(1<<i):
            tmp[i]+=1
# print(tmp)
a = 0  # K をこえないように管理する
for i in range(39,-1,-1):
	if a + (1<<i) <= K and tmp[i] < (N+1)//2:
		a += (1<<i)

# print(tmp2)

ans = 0
for i in A:
	ans += i^a
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect_left
import numpy as np

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = MAP()
A = LIST()
tmp = []
for i in A:
	if tmp == []:
		tmp = np.array([int(x) for x in list("{:b}".format(i).zfill(39))])
	else:
		tmp += np.array([int(x) for x in list("{:b}".format(i).zfill(39))])
tmp = list(tmp)
a = 0  #  K をこえないように管理する
tmp2 = [0]*39
for i in range(39,-1,-1):
	if a + (1<<(i)) <= K and tmp[38-i] < (N+1)//2:
		a += (1<<i)

# print(tmp2)

ans = 0
for i in A:
	ans += i^a
print(ans)


Fixed submission:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect_left

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = MAP()
A = LIST()
tmp = [0]*40
for a in A:  # tmpに各ビット毎の1の出現回数を保存
    for i in range(40):
        if a&(1<<i):
            tmp[i]+=1
# print(tmp)
a = 0  # K をこえないように管理する
for i in range(39,-1,-1):
	if a + (1<<i) <= K and tmp[i] < (N+1)//2:
		a += (1<<i)

# print(tmp2)

ans = 0
for i in A:
	ans += i^a
print(ans)