You are given N non-negative integers A_1, A_2, ..., A_N and another non-negative integer K.
For a integer X between 0 and K (inclusive), let f(X) = (X XOR A_1) + (X XOR A_2) + ... + (X XOR A_N).
Here, for non-negative integers a and b, a XOR b denotes the bitwise exclusive OR of a and b.
Find the maximum value of f.
What is XOR?
The bitwise exclusive OR of a and b, X, is defined as follows:
 - When X is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if, when written in base two, exactly one of A and B has 1 in the 2^k's place, and 0 otherwise.
For example, 3 XOR 5 = 6. (When written in base two: 011 XOR 101 = 110.)

-----Constraints-----
 - All values in input are integers.
 - 1 \leq N \leq 10^5
 - 0 \leq K \leq 10^{12}
 - 0 \leq A_i \leq 10^{12}

-----Input-----
Input is given from Standard Input in the following format:
N K
A_1 A_2 ... A_N

-----Output-----
Print the maximum value of f.

-----Sample Input-----
3 7
1 6 3

-----Sample Output-----
14

The maximum value is: f(4) = (4 XOR 1) + (4 XOR 6) + (4 XOR 3) = 5 + 2 + 7 = 14.

Difficulty: MEDIUM_HARD

Starter code:

Solutions:
from collections import defaultdict
(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
BMAX = 40
d = [0] * BMAX
for a in A:
	for i in range(BMAX):
		if a & 1 << i:
			d[i] += 1
dp = [[-1 for _ in range(2)] for _ in range(BMAX + 1)]
dp[0][0] = 0
for i in range(BMAX):
	now = BMAX - 1 - i
	p0 = 2 ** now * d[now]
	p1 = 2 ** now * (N - d[now])
	if K & 1 << now:
		is_one = True
	else:
		is_one = False
	if dp[i][1] != -1:
		dp[i + 1][1] = dp[i][1] + max(p1, p0)
	if dp[i][0] != -1 and is_one:
		dp[i + 1][1] = max(dp[i + 1][1], dp[i][0] + p0)
	if dp[i][0] != -1:
		dp[i + 1][0] = dp[i][0] + (p1 if is_one else p0)
print(max(dp[-1]))


(n, k) = map(int, input().split())
al = list(map(int, input().split()))
dp = [n] * 41
for i in range(n):
	i_bit = bin(al[i])[2:]
	lenb = len(i_bit)
	for j in range(lenb - 1, -1, -1):
		dp[lenb - 1 - j] -= int(i_bit[j])
temp = k
res = 0
m = n // 2
for i in range(40, -1, -1):
	if dp[i] > m and 2 ** i <= temp:
		temp -= 2 ** i
		res += 2 ** i * dp[i]
	else:
		res += 2 ** i * (n - dp[i])
print(res)


import sys
readline = sys.stdin.readline
MAX_DIGIT = 40

def main():
	(N, K) = map(int, readline().rstrip().split())
	K_bin = bin(K)[2:].zfill(MAX_DIGIT)
	A = list(map(int, readline().rstrip().split()))
	A = [bin(a)[2:].zfill(MAX_DIGIT) for a in A]
	dp = [[-1] * 2 for _ in range(MAX_DIGIT + 1)]
	dp[0][0] = 0
	mul = 2 ** (MAX_DIGIT - 1)
	for d in range(MAX_DIGIT):
		cnt = len([1 for a in A if a[d] == '1'])
		gain0 = cnt * mul
		gain1 = (N - cnt) * mul
		if dp[d][1] != -1:
			dp[d + 1][1] = max(dp[d + 1][1], dp[d][1] + max(gain0, gain1))
		if dp[d][0] != -1:
			if K_bin[d] == '1':
				dp[d + 1][1] = max(dp[d + 1][1], dp[d][0] + gain0)
		if dp[d][0] != -1:
			if K_bin[d] == '1':
				dp[d + 1][0] = max(dp[d + 1][0], dp[d][0] + gain1)
			else:
				dp[d + 1][0] = max(dp[d + 1][0], dp[d][0] + gain0)
		mul //= 2
	print(max(dp[MAX_DIGIT]))

def __starting_point():
	main()
__starting_point()


(N, K) = list(map(int, input().split()))
L = list(map(int, input().split()))
d = format(K, 'b')
dp = [[-1 for _ in range(2)] for _ in range(len(d) + 1)]
dp[0][0] = 0
for i in range(len(d))[::-1]:
	if K == 0:
		break
	index = len(d) - i - 1
	cnt = 0
	for x in L:
		if x & 1 << i:
			cnt += 1
	tmp = 2 ** i
	if dp[index][1] != -1:
		dp[index + 1][1] = max(dp[index + 1][1], dp[index][1] + max(cnt, N - cnt) * tmp)
	if d[index] == '1':
		if dp[index][0] != -1:
			dp[index + 1][1] = max(dp[index + 1][1], dp[index][0] + cnt * tmp)
		if dp[index][0] != -1:
			dp[index + 1][0] = max(dp[index + 1][0], dp[index][0] + (N - cnt) * tmp)
	elif dp[index][0] != -1:
		dp[index + 1][0] = max(dp[index + 1][0], dp[index][0] + cnt * tmp)
ans = 0
for i in range(len(d), 50):
	tmp = 2 ** i
	cnt = 0
	for x in L:
		if x & 1 << i:
			cnt += 1
	ans += cnt * tmp
print(ans + max(dp[len(d)][0], dp[len(d)][1], 0))


import numpy as np

def binstr(n):
	return np.array(list(map(int, list(bin(n)[2:].rjust(40, '0')))))
(N, K) = map(int, input().split())
A = list(map(int, input().split()))
AA = sum([binstr(i) for i in A])
X = 0
ans = 0
for i in range(40):
	if AA[i] > N - AA[i]:
		ans += AA[i] * 2 ** (39 - i)
	elif X + 2 ** (39 - i) <= K:
		ans += (N - AA[i]) * 2 ** (39 - i)
		X += 2 ** (39 - i)
	else:
		ans += AA[i] * 2 ** (39 - i)
print(ans)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
bounded = True
for i in range(40, -1, -1):
	d = 1 << i
	cnt1 = sum((d & ai > 0 for ai in a))
	cnt0 = n - cnt1
	if bounded and k & d == 0 or cnt1 >= cnt0:
		ans += cnt1 * d
		if k & d > 0:
			bounded = False
	else:
		ans += cnt0 * d
print(ans)


import numpy as np
(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
MAXBITS = 40
bits = np.ndarray((N, MAXBITS), dtype=int)
for i in range(N):
	a = A[i]
	bits[i] = np.array([a >> j & 1 for j in range(MAXBITS - 1, -1, -1)])
sm = bits[0]
for b in bits[1:]:
	sm += b
lower = False
Kbits = np.array([K >> j & 1 for j in range(MAXBITS - 1, -1, -1)])
ans = 0
for i in range(MAXBITS):
	ones = sm[i]
	zeros = N - sm[i]
	base = 2 ** (MAXBITS - i - 1)
	bit = 0
	if ones >= zeros:
		bit = 0
	elif lower or Kbits[i] == 1:
		bit = 1
	if Kbits[i] > bit:
		lower = True
	ans += ones * base if bit == 0 else zeros * base
print(ans)


(n, k) = map(int, input().split())
A = list(map(int, input().split()))
B = [0] * 40
for i in A:
	s = format(i, '40b')
	for j in range(40):
		if s[-1 - j] == '1':
			B[j] += 1
C = [0] * 40
for i in range(40):
	if B[-1 - i] < n / 2:
		C[i] = 1
	else:
		C[i] = 0
AA = []
a = 0
for i in range(40):
	if C[i] == 1:
		if a + 2 ** (39 - i) > k:
			continue
		b = a
		for j in range(i + 1, 40):
			if C[j] == 1:
				b = b + 2 ** (39 - j)
		AA.append(b)
		a = a + 2 ** (39 - i)
AA.append(a)
ans = 0
for i in AA:
	a = 0
	for j in A:
		a = a + (i ^ j)
	if a > ans:
		ans = a
print(ans)


import sys, math, collections, itertools
input = sys.stdin.readline
(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
maxA = max(A)
binK = bin(max(maxA, K))[2:]
num_one = [0] * len(binK)
for k in range(len(binK)):
	for a in A:
		num_one[len(binK) - k - 1] += a >> k & 1
ans = 0
flag = False
for k in range(len(binK)):
	if flag:
		if num_one[k] > N - num_one[k]:
			ans += pow(2, len(binK) - k - 1) * num_one[k]
		else:
			ans += pow(2, len(binK) - k - 1) * (N - num_one[k])
	elif K >> len(binK) - k - 1 & 1:
		if num_one[k] > N - num_one[k]:
			flag = True
			ans += pow(2, len(binK) - k - 1) * num_one[k]
		else:
			ans += pow(2, len(binK) - k - 1) * (N - num_one[k])
	else:
		ans += pow(2, len(binK) - k - 1) * num_one[k]
print(ans)


(N, K) = map(int, input().split())
a = list(map(int, input().split()))
c = [0] * 40
ans = 0
for aa in a:
	for k in range(40):
		if aa & 1 << k:
			c[k] += 1
x = 0
for k in range(39, -1, -1):
	if x + (1 << k) > K:
		continue
	if c[k] <= N - c[k]:
		x += 1 << k
for aa in a:
	ans += x ^ aa
print(ans)


import sys
from itertools import accumulate
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7

def resolve():
	(n, k) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	ans = list(accumulate(A))[-1]
	B = [0 for _ in range(52)]
	for i in reversed(list(range(51))):
		cnt1 = 0
		for j in range(n):
			if A[j] & 1 << i:
				cnt1 += 1
		cnt0 = n - cnt1
		if cnt0 > cnt1:
			B[i] = (1 << i) * (cnt0 - cnt1)
	pre = 0
	maxV = 0
	for i in reversed(list(range(51))):
		if k & 1 << i:
			total = 0
			for j in range(i):
				total += B[j]
			total += pre
			maxV = max(maxV, total)
			pre += B[i]
	maxV = max(maxV, pre)
	print(ans + maxV)

def __starting_point():
	resolve()
__starting_point()


(n, k) = map(int, input().split())
A = list(map(int, input().split()))
M = 40
C = [[0] * 2 for _ in range(M + 1)]
for a in A:
	for i in range(M + 1):
		C[i][a >> i & 1] += 1
dp = [[0] * 2 for _ in range(M + 1)]
dp[M][1] = -float('inf')
for i in range(M - 1, -1, -1):
	j = k >> i & 1
	dp[i][0] = dp[i + 1][0] * 2 + C[i][j ^ 1]
	dp[i][1] = dp[i + 1][1] * 2 + max(C[i])
	if j:
		dp[i][1] = max(dp[i][1], dp[i + 1][0] * 2 + C[i][1])
ans = max(dp[0])
print(ans)


(n, k) = list(map(int, input().split()))
A = list(map(int, input().split()))
k_bin = '{:040b}'.format(k)
A_bin = list(['{:040b}'.format(x) for x in A])
X = ''
first = 1
for (i, col) in enumerate(zip(*A_bin)):
	if k_bin[i] == '0' and first:
		X += '0'
		continue
	if first:
		zero_cnt = col.count('0')
		one_cnt = n - zero_cnt
		if one_cnt < zero_cnt:
			X += '1'
		else:
			first = 0
			X += '0'
		continue
	zero_cnt = col.count('0')
	one_cnt = n - zero_cnt
	if one_cnt < zero_cnt:
		X += '1'
	else:
		X += '0'
X = int(X, 2)
ans = 0
for a in A:
	ans += X ^ a
print(ans)


def submit():
	(n, k) = (int(e) for e in input().split())
	a = [int(e) for e in input().split()]
	max_bit = 40
	bit_cnt = []
	b = 0
	bit = 1
	while b <= max_bit:
		cnt = sum((e & bit > 0 for e in a))
		bit_cnt.append(cnt)
		bit <<= 1
		b += 1
	isless = False
	curr_bit = 1 << max_bit
	ans = 0
	for i in range(max_bit + 1):
		k_bit = k & curr_bit > 0
		if not isless and k_bit == 0:
			ans += bit_cnt[max_bit - i] * curr_bit
		elif not isless and k_bit == 1:
			cnt1 = bit_cnt[max_bit - i]
			cnt0 = n - cnt1
			if cnt1 > cnt0:
				ans += cnt1 * curr_bit
				isless = True
			else:
				ans += cnt0 * curr_bit
		else:
			cnt1 = bit_cnt[max_bit - i]
			cnt0 = n - cnt1
			if cnt1 > cnt0:
				ans += cnt1 * curr_bit
			else:
				ans += cnt0 * curr_bit
		curr_bit >>= 1
	print(ans)
submit()


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
L = 40
bc = [0] * L
for i in range(L):
	for a in A:
		if a & 1 << i:
			bc[i] += 1
dp = [[-1, -1] for _ in range(L + 1)]
dp[L][0] = 0
for i in range(L - 1, -1, -1):
	c = K >> i & 1
	for l in range(2):
		if dp[i + 1][l] < 0:
			continue
		for d in range(2):
			if l == 0 and d > c:
				continue
			nl = 0 if l == 0 and d == c else 1
			dp[i][nl] = max(dp[i][nl], dp[i + 1][l] + 2 ** i * (N - bc[i] if d else bc[i]))
print(max(dp[0]))


import numpy as np
(n, k) = list(map(int, input().split()))
a = np.array(input().split(), dtype=int)
n1 = [0 for i in range(40)]
g0 = [0 for i in range(40)]
g1 = [0 for i in range(40)]
ss = [0 for i in range(40)]
t2 = [1 for i in range(40)]
for i in range(40):
	n1[i] = np.count_nonzero(a & 1)
	a >>= 1
	if i > 0:
		t2[i] = t2[i - 1] * 2
	g0[i] = n1[i] * t2[i]
	g1[i] = (n - n1[i]) * t2[i]
	if n1[i] < n - n1[i]:
		ss[i] = ss[i - 1] + g1[i]
	else:
		ss[i] = ss[i - 1] + g0[i]

def DFS(ni, ki, sm):
	if ni == 0:
		if ki == 0:
			return sm + g0[0]
		else:
			return sm + ss[0]
	elif ki // t2[ni] == 0:
		return DFS(ni - 1, ki, sm + g0[ni])
	elif ki // t2[ni] > 1:
		return sm + ss[ni]
	else:
		return max(sm + g0[ni] + ss[ni - 1], DFS(ni - 1, ki - t2[ni], sm + g1[ni]))
ma = DFS(39, k, 0)
print(ma)


(n, k, *a) = map(int, open(0).read().split())
d = [0] * 41
for i in range(n):
	for j in range(41):
		d[j] += a[i] >> j & 1
f = 0
la = len(bin(max(a))) - 2
lk = len(bin(k)) - 2
b = [0] * 41
for i in range(lk - 1, -1, -1):
	if k >> i & 1 == 1:
		if d[i] < n - d[i]:
			b[i] = 1
		else:
			b[i] = 0
			f = 1
	if k >> i & 1 == 0:
		if d[i] < n - d[i] and f == 1:
			b[i] = 1
		else:
			b[i] = 0
ans = 0
for i in range(max(lk, la)):
	ans += (1 << i) * [d[i], n - d[i]][b[i]]
print(ans)


def XXOR():
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	num = len(str(bin(10 ** 12))) - 2
	ans = 0
	for i in range(num, -1, -1):
		count = 0
		for j in a:
			if j >> i & 1:
				count += 1
		c = 2 ** i
		if count < n - count and c <= k:
			ans += (n - count) * c
			k -= c
		else:
			ans += count * c
	print(ans)

def __starting_point():
	XXOR()
__starting_point()


def solve(N, K, A):
	import math
	max_K = 0
	for i in reversed(list(range(int(math.log2(K + 1)) + 1))):
		cnt = 0
		for a in A:
			if a >> i & 1:
				cnt += 1
		if cnt <= N // 2:
			if max_K + (1 << i) <= K:
				max_K += 1 << i
	ans = 0
	for a in A:
		ans += a ^ max_K
	print(ans)

def __starting_point():
	(N, K) = list(map(int, input().split()))
	A = [int(i) for i in input().split()]
	solve(N, K, A)
__starting_point()


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left, bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil, pi, factorial
from operator import itemgetter

def I():
	return int(input())

def MI():
	return map(int, input().split())

def LI():
	return list(map(int, input().split()))

def LI2():
	return [int(input()) for i in range(n)]

def MXI():
	return [[LI()] for i in range(n)]

def SI():
	return input().rstrip()

def printns(x):
	print('\n'.join(x))

def printni(x):
	print('\n'.join(list(map(str, x))))
inf = 10 ** 17
mod = 10 ** 9 + 7
(n, k) = MI()
lis = LI()
u = max(max(lis), k)
memo = 0
for i in range(45):
	x = (u >> i) % 2
	if x == 1:
		memo = i
memo += 1
table = [0 for i in range(memo)]
for i in range(n):
	for j in range(memo):
		x = (lis[i] >> j) % 2
		if x == 1:
			table[j] += 1
table2 = [[0, 0] for i in range(memo)]
for i in range(memo):
	table2[i][0] = abs(n - 2 * table[i]) * 2 ** i
	table2[i][1] = i + 1
	if n - 2 * table[i] < 0:
		table2[i][1] *= -1
table2.sort(reverse=True)
maxx = 0
ans_table = [0 for i in range(memo)]
for i in range(memo):
	if table2[i][1] > 0:
		maxx += 2 ** (table2[i][1] - 1)
		if maxx <= k:
			ans_table[table2[i][1] - 1] = 1
		else:
			ans_table[table2[i][1] - 1] = 0
			maxx -= 2 ** (table2[i][1] - 1)
	else:
		ans_table[abs(table2[i][1]) - 1] = 0
ans = 0
for i in range(memo):
	if ans_table[i] == 1:
		ans += 2 ** i * (n - table[i])
	else:
		ans += 2 ** i * table[i]
print(ans)


def f_max(X, digit):
	if X == 0:
		return sum([Ad[i] * 2 ** i for i in range(digit)])
	elif X == 2 ** digit - 1:
		return sum([max(Ad[i], N - Ad[i]) * 2 ** i for i in range(digit)])
	elif X >= 2 ** (digit - 1):
		return max((N - Ad[digit - 1]) * 2 ** (digit - 1) + f_max(X - 2 ** (digit - 1), digit - 1), Ad[digit - 1] * 2 ** (digit - 1) + f_max(2 ** (digit - 1) - 1, digit - 1))
	else:
		return Ad[digit - 1] * 2 ** (digit - 1) + f_max(X, digit - 1)
(N, K) = map(int, input().split())
(K_max, digits) = (2 * 10 ** 12, 0)
while K_max > 0:
	K_max >>= 1
	digits += 1
Ad = [0] * digits
for a in list(map(int, input().split())):
	for i in range(digits):
		Ad[i] += a % 2
		a >>= 1
print(f_max(K, digits))


(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
M = 50
l = len(str(N))
dp = [[-1] * 2 for _ in range(M + 1)]
dp[0][0] = 0
for i in range(M):
	mask = 1 << M - i - 1
	num = 0
	for j in range(N):
		if A[j] & mask:
			num += 1
	c0 = mask * num
	c1 = mask * (N - num)
	if dp[i][1] != -1:
		dp[i + 1][1] = max(dp[i + 1][1], dp[i][1] + max(c0, c1))
	if dp[i][0] != -1:
		if K & mask:
			dp[i + 1][1] = max(dp[i + 1][1], dp[i][0] + c0)
			dp[i + 1][0] = max(dp[i + 1][0], dp[i][0] + c1)
		else:
			dp[i + 1][0] = max(dp[i + 1][0], dp[i][0] + c0)
print(max(dp[M][0], dp[M][1]))


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
dp = [0] * 41
bp = [0] * 41
for i in range(N):
	o = len(bin(A[i]))
	for j in range(o - 2):
		if A[i] >> j & 1 == 1:
			dp[j] += 1
ans = 0
count = 0
for i in range(len(dp) - 1, -1, -1):
	bp[i] = N - dp[i]
	if dp[i] < bp[i] and count + pow(2, i) <= K:
		count += pow(2, i)
ans = 0
for i in range(N):
	ans += count ^ A[i]
print(ans)


def main():
	(n, k, *a) = list(map(int, open(0).read().split()))
	cnt = [sum((x >> i & 1 for x in a)) for i in range(40)]
	bits = [0 if i > n - i else 1 for i in cnt]
	base = format(k, 'b')
	l = len(base)
	tmp = []
	if sum((2 ** i for (i, b) in enumerate(bits[:l]) if b == 1)) > k:
		for i in range(l - 1, -1, -1):
			if int(base[-i - 1]) < bits[i]:
				bits[i] = 0
				tmp.append(sum((2 ** j for (j, b) in enumerate(bits[:l]) if b == 1)))
	else:
		tmp.append(sum((2 ** j for (j, b) in enumerate(bits[:l]) if b == 1)))
	ans = max((sum((i ^ x for x in a)) for i in tmp if i <= k))
	print(ans)

def __starting_point():
	main()
__starting_point()


def resolve():
	(N, K) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	dp = [[-1 for _ in range(2)] for __ in range(45)]
	dp[0][0] = 0
	for i in range(40):
		mask = 1 << 40 - i - 1
		one_num = 0
		for a in A:
			if a & mask:
				one_num += 1
		costdiff0 = mask * one_num
		costdiff1 = mask * (N - one_num)
		if dp[i][1] != -1:
			dp[i + 1][1] = max(dp[i + 1][1], dp[i][1] + max(costdiff0, costdiff1))
		if dp[i][0] != -1 and K & mask:
			dp[i + 1][1] = max(dp[i + 1][1], dp[i][0] + costdiff0)
		if dp[i][0] != -1:
			if K & mask:
				dp[i + 1][0] = max(dp[i + 1][0], dp[i][0] + costdiff1)
			else:
				dp[i + 1][0] = max(dp[i + 1][0], dp[i][0] + costdiff0)
	print(max(dp[40][0], dp[40][1]))
if '__main__' == __name__:
	resolve()


import sys
from functools import lru_cache
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007

def score(x, counter, N):
	ans = 0
	for i in range(40):
		if x & 1 << i:
			ans += (N - counter[i]) * (1 << i)
		else:
			ans += counter[i] * (1 << i)
	return ans

def main():
	(N, K, *A) = list(map(int, read().split()))
	counter = [0] * 40
	for a in A:
		for i in range(40):
			if a & 1 << i:
				counter[i] += 1
	B = [0] * 40
	for i in range(39):
		if counter[i] < N - counter[i]:
			B[i + 1] = B[i] + (1 << i)
		else:
			B[i + 1] = B[i]
	C = [0] * 40
	for i in range(39, 0, -1):
		if K & 1 << i:
			C[i - 1] = C[i] + (1 << i)
		else:
			C[i - 1] = C[i]
	ans = score(K, counter, N)
	for i in range(40):
		if K & 1 << i:
			x = B[i] + C[i]
			res = score(x, counter, N)
			if ans < res:
				ans = res
	print(ans)
	return

def __starting_point():
	main()
__starting_point()


import sys
(N, K) = map(int, sys.stdin.readline().rstrip().split())
A = list(map(int, sys.stdin.readline().rstrip().split()))
bin_k = bin(K)[2:].zfill(50)
cnt = [0] * 50
for a in A:
	bin_a = bin(a)[2:]
	for (i, b) in enumerate(bin_a[::-1]):
		if b == '1':
			cnt[i] += 1
cnt = cnt[::-1]
dp = [[0] * 2 for _ in range(50 + 1)]
ans = 0
base = 2 ** (50 - 1)
flag = False
for (i, c) in enumerate(cnt, 1):
	if flag:
		if bin_k[i - 1] == '1':
			dp[i][0] = dp[i - 1][0] + base * (N - c)
			dp[i][1] = max(dp[i - 1][0] + base * c, dp[i - 1][1] + base * c, dp[i - 1][1] + base * (N - c))
		else:
			dp[i][0] = dp[i - 1][0] + base * c
			dp[i][1] = max(dp[i - 1][1] + base * c, dp[i - 1][1] + base * (N - c))
	elif bin_k[i - 1] == '1':
		dp[i][0] = dp[i - 1][0] + base * (N - c)
		tmp1 = dp[i - 1][0] + base * c
		tmp2 = dp[i - 1][1] + base * c
		tmp3 = dp[i - 1][1] + base * (N - c)
		if tmp3 <= tmp1 and tmp3 <= tmp2:
			dp[i][1] = max(tmp1, tmp2)
			flag = True
		elif tmp3 <= tmp1:
			dp[i][1] = tmp1
			flag = True
		else:
			dp[i][1] = tmp3
	else:
		dp[i][0] = dp[i - 1][0] + base * c
		dp[i][1] = dp[i - 1][1] + base * c
	base //= 2
print(max(dp[50][0], dp[50][1]))


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
l = max(len(bin(k)[2:]), len(bin(max(a))[2:]))
c = [0] * l
for i in a:
	b_a = bin(i)[2:].zfill(l)
	for j in range(l):
		if b_a[j] == '1':
			c[j] += 1
x = 0
for i in range(l):
	e = l - i - 1
	if n - c[i] > c[i] and x + 2 ** e <= k:
		x += 2 ** e
ans = 0
for i in a:
	ans += i ^ x
print(ans)


(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
Bit = 0
while True:
	if 1 << Bit + 1 > K:
		break
	else:
		Bit += 1
X = 0
for b in range(Bit, -1, -1):
	X_ = X + (1 << b)
	if X_ > K:
		continue
	Sum = sum([X ^ a for a in A])
	Sum_ = sum([X_ ^ a for a in A])
	if Sum_ > Sum:
		X = X_
print(sum([X ^ a for a in A]))


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
cnt = 0
for i in range(41)[::-1]:
	S = sum(map(lambda x: x >> i & 1, a))
	if S > n // 2:
		m = 0
	else:
		m = 1 << i
	if S > n // 2 or m > k - cnt:
		ans += S << i
	else:
		ans += n - S << i
		cnt += m
print(ans)


from sys import stdin

def main():
	readline = stdin.readline
	(n, k) = map(int, readline().split())
	a = list(map(int, readline().split()))
	s = bin(max(k, max(a)))[2:]
	l = len(s)
	cnt_one = [0] * l
	for i in range(n):
		b = a[i]
		for j in range(l):
			if b & 1 == 1:
				cnt_one[j] += 1
			b >>= 1
	cnt_one.reverse()
	k_bin = bin(k)[2:]
	dp = [[-float('inf'), -float('inf')] for _ in range(l + 1)]
	dp[0][0] = 0
	for i in range(l):
		j = l - i
		if len(k_bin) < j:
			dp[i + 1][0] = dp[i][0] + cnt_one[i] * 2 ** (j - 1)
		elif k_bin[len(k_bin) - j] == '1':
			dp[i + 1][0] = dp[i][0] + (n - cnt_one[i]) * 2 ** (j - 1)
			if cnt_one[i] >= (n + 1) // 2:
				dp[i + 1][1] = max(dp[i][0] + cnt_one[i] * 2 ** (j - 1), dp[i][1] + cnt_one[i] * 2 ** (j - 1))
			else:
				dp[i + 1][1] = max(dp[i][0] + cnt_one[i] * 2 ** (j - 1), dp[i][1] + (n - cnt_one[i]) * 2 ** (j - 1))
		else:
			dp[i + 1][0] = dp[i][0] + cnt_one[i] * 2 ** (j - 1)
			if cnt_one[i] >= (n + 1) // 2:
				dp[i + 1][1] = dp[i][1] + cnt_one[i] * 2 ** (j - 1)
			else:
				dp[i + 1][1] = dp[i][1] + (n - cnt_one[i]) * 2 ** (j - 1)
	print(max(dp[l][0], dp[l][1]))

def __starting_point():
	main()
__starting_point()


import sys
import numpy as np
(N, K) = map(int, sys.stdin.readline().rstrip().split())
A = np.array(list(map(int, sys.stdin.readline().rstrip().split())))
cnt = []
for i in range(41):
	cnt.append(np.count_nonzero(A >> i & 1))
cnt = cnt[::-1]
dp = [[0] * 2 for _ in range(42)]
ans = 0
base = 2 ** (41 - 1)
for (i, c) in enumerate(cnt, 1):
	if K >> 41 - i & 1 == 1:
		dp[i][0] = dp[i - 1][0] + base * (N - c)
		if dp[i - 1][1] > 0:
			dp[i][1] = max(dp[i - 1][0] + base * c, dp[i - 1][1] + base * c, dp[i - 1][1] + base * (N - c))
		else:
			dp[i][1] = dp[i - 1][0] + base * c
	else:
		dp[i][0] = dp[i - 1][0] + base * c
		if dp[i - 1][1] > 0:
			dp[i][1] = max(dp[i - 1][1] + base * c, dp[i - 1][1] + base * (N - c))
	base //= 2
print(max(dp[41][0], dp[41][1]))


import sys
import math
import collections
import bisect
import copy
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
ni = lambda : int(sys.stdin.readline())
ns = lambda : list(map(int, sys.stdin.readline().split()))
na = lambda : list(map(int, sys.stdin.readline().split()))
na1 = lambda : list([int(x) - 1 for x in sys.stdin.readline().split()])

def main():
	(n, k) = ns()
	a = na()
	max_len = 41
	popcount = [0] * max_len
	for ai in a:
		cnt = 0
		while ai > 0:
			if ai & 1:
				popcount[cnt] += 1
			ai >>= 1
			cnt += 1
	popcount.reverse()
	dp1 = [0 for _ in range(max_len + 1)]
	dp2 = [-1 for _ in range(max_len + 1)]
	for i in range(max_len):
		tmp = pow(2, max_len - 1 - i)
		ki = k >> max_len - 1 - i & 1
		one = popcount[i]
		zero = n - popcount[i]
		if ki:
			dp1[i + 1] = dp1[i] + zero * tmp
			dp2[i + 1] = dp1[i] + one * tmp
			if dp2[i] != -1:
				dp2[i + 1] = max(dp2[i + 1], dp2[i] + zero * tmp, dp2[i] + one * tmp)
		else:
			dp1[i + 1] = dp1[i] + one * tmp
			if dp2[i] != -1:
				dp2[i + 1] = max(dp2[i] + zero * tmp, dp2[i] + one * tmp)
	print(max(dp1[max_len], dp2[max_len]))

def __starting_point():
	main()
__starting_point()


(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
b = 1 << 63
X = 0
ans = 0
for i in range(64):
	cnt = 0
	for a in A:
		if a & b:
			cnt += 1
	if N >= cnt * 2 and X ^ b <= K:
		X ^= b
		ans += (N - cnt) * b
	else:
		ans += cnt * b
	b >>= 1
print(ans)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
MAX = max(max(A), K)
l = len(str(bin(MAX))) - 2
AB = []
for i in range(N):
	s = str(bin(A[i]))
	temp = s[2:]
	lendif = l - len(temp)
	while lendif > 0:
		temp = '0' + temp
		lendif -= 1
	AB.append(temp)
ans = 0
now = 0
for i in range(l):
	c = 0
	d = 0
	for j in range(N):
		if AB[j][i] == '0':
			c += 1
		else:
			d += 1
	if c > d:
		if pow(2, l - 1 - i) + now > K:
			m = d
		else:
			now += pow(2, l - 1 - i)
			m = c
	else:
		m = d
	ans += pow(2, l - 1 - i) * m
print(ans)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
cnts = [0] * 46
for a in A:
	for i in range(46):
		if a & 1 << i:
			cnts[i] += 1
L = 45
dp = [[-1 for _ in range(2)] for _ in range(L + 1)]
dp[0][0] = 0
for i in range(L):
	p0 = pow(2, L - 1 - i) * cnts[L - 1 - i]
	p1 = pow(2, L - 1 - i) * (N - cnts[L - 1 - i])
	if K & 1 << L - 1 - i:
		is_one = True
	else:
		is_one = False
	if dp[i][1] != -1:
		dp[i + 1][1] = dp[i][1] + max(p0, p1)
	if dp[i][0] != -1 and is_one:
		dp[i + 1][1] = max(dp[i + 1][1], dp[i][0] + p0)
	if dp[i][0] != -1:
		if is_one:
			dp[i + 1][0] = dp[i][0] + p1
		else:
			dp[i + 1][0] = dp[i][0] + p0
print(max(dp[-1]))


import numpy as np
(n, k) = list(map(int, input().split()))
a = np.array(list(map(int, input().split())))
L = len(f'{10 ** 12:b}')
res = 0
for i in range(-1, L):
	if i != -1 and (not k >> i & 1):
		continue
	val = 0
	for j in range(L):
		d = np.count_nonzero(a >> j & 1)
		if i < j:
			if k >> j & 1:
				val += (n - d) * 2 ** j
			else:
				val += d * 2 ** j
		elif i == j:
			val += d * 2 ** j
		else:
			val += max(d, n - d) * 2 ** j
	res = max(res, val)
print(res)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
max_digit = K.bit_length()

def bi(x, max_d=max_digit):
	b = bin(x)[2:]
	if len(b) < max_d:
		return '0' * (max_d - len(b)) + b
	else:
		return b[-max_digit:]

def f(x):
	return sum(map(lambda a: a ^ x, A))
B = list(map(bi, A))
count = [0] * max_digit
ans = 0
for j in range(max_digit):
	for i in range(N):
		if B[i][j] == '1':
			count[j] += 1
	if count[j] < N / 2:
		if ans + 2 ** (max_digit - j - 1) <= K:
			ans += 2 ** (max_digit - j - 1)
		else:
			continue
print(f(ans))


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7
(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
C = [1] * 50
for i in range(50 - 1):
	C[i + 1] = 2 * C[i]
count = [0] * 50
for i in range(N):
	a = A[i]
	j = 0
	while a:
		if a % 2 == 1:
			count[j] += 1
		j += 1
		a //= 2
B = [0] * 60
k = K
j = 0
while k:
	if k % 2 == 1:
		B[j] = 1
	j += 1
	k //= 2
under = [-1] * (j + 1)
just = [0] * (j + 1)
tmp = 0
for i in range(49, j - 1, -1):
	tmp += C[i] * count[i]
for i in range(j - 1, -1, -1):
	tmp1 = count[i]
	tmp0 = N - tmp1
	if B[i] == 0:
		just[i] = just[i + 1] + C[i] * tmp1
		if under[i + 1] >= 0:
			under[i] = under[i + 1] + C[i] * max(tmp1, tmp0)
	else:
		just[i] = just[i + 1] + C[i] * tmp0
		if under[i + 1] == -1:
			under[i] = just[i + 1] + C[i] * tmp1
		if under[i + 1] >= 0:
			under[i] = under[i + 1] + C[i] * max(tmp1, tmp0)
			under[i] = max(under[i], just[i + 1] + C[i] * tmp1)
print(max(under[0], just[0]) + tmp)


(n, k) = map(int, input().split())
A = list(map(int, input().split()))
k_bin = '{:040b}'.format(k)
A_bin = list(map(lambda x: '{:040b}'.format(x), A))
dp = [[0] * 2 for _ in range(41)]
for (i, col) in enumerate(zip(*A_bin)):
	zero_cnt = col.count('0')
	one_cnt = n - zero_cnt
	p = 40 - i - 1
	if dp[i][0] != 0:
		dp[i + 1][0] = dp[i][0] + max(zero_cnt, one_cnt) * 2 ** p
	if k_bin[i] == '0':
		dp[i + 1][1] = dp[i][1] + one_cnt * 2 ** p
	else:
		dp[i + 1][0] = max(dp[i + 1][0], dp[i][1] + one_cnt * 2 ** p)
		dp[i + 1][1] = dp[i][1] + zero_cnt * 2 ** p
print(max(dp[-1]))


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
(t, ans) = (2 ** 40, 0)
while t:
	c = sum([(a[i] & t) // t for i in range(n)])
	if c >= n - c or k < t:
		ans += t * c
	else:
		ans += t * (n - c)
		k -= t
	t = t >> 1
print(ans)


import sys
input = sys.stdin.readline

def read():
	(N, K) = list(map(int, input().strip().split()))
	A = list(map(int, input().strip().split()))
	return (N, K, A)

def solve(N, K, A):
	L = max(K, max(A), 1).bit_length()
	k = [0 for i in range(L)]
	i = 0
	while K:
		k[i] += K & 1
		i += 1
		K >>= 1
	b = [0 for i in range(L)]
	for a in A:
		i = 0
		x = a
		while x:
			b[i] += x & 1
			i += 1
			x >>= 1
	k = k[::-1]
	b = b[::-1]
	dp = [[-1, -1] for i in range(L + 1)]
	dp[0][1] = 0
	for i in range(L):
		if dp[i][0] >= 0:
			dp[i + 1][0] = (dp[i][0] << 1) + max(b[i], N - b[i])
		if k[i] == 1:
			dp[i + 1][0] = max(dp[i + 1][0], (dp[i][1] << 1) + b[i])
			dp[i + 1][1] = (dp[i][1] << 1) + (N - b[i])
		else:
			dp[i + 1][1] = (dp[i][1] << 1) + b[i]
	return max(dp[L][1], dp[L][0])

def __starting_point():
	inputs = read()
	outputs = solve(*inputs)
	if outputs is not None:
		print('%s' % str(outputs))
__starting_point()


(N, K) = map(int, input().split())
As = list(map(int, input().split()))
bitK = K.bit_length()
ans = [0] * bitK
for a in As:
	for i in range(bitK):
		if not a >> i & 1:
			ans[i] += 1
ans = ans[::-1]
string = ['0'] * bitK
if bitK == 0:
	print(sum(As))
else:
	for (i, a) in enumerate(ans):
		if a >= N / 2:
			string[i] = '1'
			if int(''.join(string), 2) > K:
				string[i] = '0'
	num = int(''.join(string), 2)
	print(sum([x ^ num for x in As]))


import numpy as np
(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
MAX = max(len(bin(K)[2:]), len(bin(max(A))[2:]))
L = [[0] * MAX for _ in range(N)]
for (i, a) in enumerate(A):
	b = bin(a)[2:]
	b = b.zfill(MAX)
	for (j, s) in enumerate(b):
		if s == '1':
			L[i][j] += 1
L = np.array(L)
count = 0
ones = L.sum(axis=0)
for i in range(MAX):
	if ones[i] * 2 == N:
		continue
	elif ones[i] * 2 < N:
		add = 2 ** (MAX - 1 - i)
		if count + add <= K:
			count += add
	else:
		continue
ans = 0
for a in A:
	ans += count ^ a
print(ans)


(n, k) = list(map(int, input().split()))
A = sorted(map(int, input().split()), reverse=True)
cnt = [0 for _ in range(100)]
max_a = A[0]
k += 1
index_k = 0
index_a = 0
for i in range(100):
	tmp = 2 ** i
	if tmp > max_a:
		index_a = i - 1
		break
	for a in A:
		if a < tmp:
			break
		if a >> i & 1:
			cnt[i] += 1
for i in range(100):
	tmp = 2 ** i
	if tmp > k:
		index_k = i - 1
		break
best = [0]
for (i, c) in enumerate(cnt[:index_k + 1]):
	best.append(best[-1] + max(c, n - c) * 2 ** i)
initial = 0
if index_a > index_k:
	initial = sum([2 ** x[0] * x[1] for x in enumerate(cnt[index_k + 1:index_a + 1], index_k + 1)])
equal = [initial]
for (i, c) in enumerate(cnt[index_k::-1]):
	if k >> index_k - i & 1:
		equal.append(equal[-1] + (n - c) * 2 ** (index_k - i))
	else:
		equal.append(equal[-1] + c * 2 ** (index_k - i))
ans = 0
for i in range(index_k + 1):
	if k >> i & 1:
		ans = max(ans, best[i] + cnt[i] * 2 ** i + equal[-(i + 2)])
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
b = []
L = len(bin(k)) - 2
for x in a:
	b.append(bin(x)[2:])
	L = max(L, len(bin(x)) - 2)
for i in range(n):
	b[i] = '0' * (L - len(b[i])) + b[i]
X = 0
for i in range(L):
	if 2 ** (L - i - 1) <= k:
		one = 0
		for j in range(n):
			if b[j][i] == '1':
				one += 1
		if one < n - one:
			k -= 2 ** (L - i - 1)
			X += 2 ** (L - i - 1)
ans = 0
for x in a:
	ans += X ^ x
print(ans)


import numpy as np
(n, k) = list(map(int, input().split()))
a = np.array(list(map(int, input().split())))
L = len(f'{10 ** 12:b}')
dp = np.full((L + 1, 2), -1, dtype=np.int64)
dp[0, 0] = 0
for i in range(L):
	d = 1 << L - i - 1
	cnt = np.count_nonzero(a & d)
	val0 = d * cnt
	val1 = d * (n - cnt)
	if dp[i, 1] != -1:
		dp[i + 1, 1] = max(dp[i + 1, 1], dp[i, 1] + max(val0, val1))
	if dp[i, 0] != -1:
		if k & d:
			dp[i + 1, 1] = max(dp[i + 1, 1], dp[i, 0] + val0)
	if dp[i, 0] != -1:
		if k & d:
			dp[i + 1, 0] = max(dp[i + 1, 0], dp[i, 0] + val1)
		else:
			dp[i + 1, 0] = max(dp[i + 1, 0], dp[i, 0] + val0)
print(max(dp[L, 0], dp[L, 1]))


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
x = 0
for i in range(k.bit_length() - 1, -1, -1):
	cnt = 0
	for j in range(n):
		if a[j] >> i & 1 == 1:
			cnt += 1
	if cnt < (n + 1) // 2:
		t = x + (1 << i)
		if t <= k:
			x = t
ans = 0
for ai in a:
	ans += ai ^ x
print(ans)


import math
(N, K) = map(int, input().split())
A = list(map(int, input().split()))
D = len(bin(max(A))) - 2
O = max(len(bin(K)) - 2, D)
M = N / 2
L = [0] * D
C = [1] * O
for a in A:
	s = str(bin(a))[2:]
	l = list(s.zfill(D))
	L = [int(i) + j for (i, j) in zip(l, L)]
L = (O - D) * [0] + L
x = 0
for i in range(O):
	tmp = 0
	if L[i] < M:
		tmp = 2 ** abs(O - i - 1)
		if x + tmp <= K:
			x += tmp
ans = 0
for a in A:
	ans += x ^ a
print(ans)


import sys
readline = sys.stdin.readline
(N, K) = map(int, readline().split())
A = list((int(x) for x in readline().split()))
bits = [0] * 50
for i in range(N):
	for j in range(50):
		if A[i] >> j & 1:
			bits[j] += 1
ans = 0
tmp_K = 0
tmp_greed = 0
for i in range(50)[::-1]:
	if K >> i & 1:
		tmp_greed = bits[i] * 1 << i
		for j in range(i)[::-1]:
			tmp_greed += max(bits[j], N - bits[j]) * 1 << j
		ans = max(tmp_greed + tmp_K, ans)
		tmp_K += (N - bits[i]) * 1 << i
	else:
		tmp_K += bits[i] * 1 << i
ans = max(ans, tmp_K)
print(ans)


(N, K) = list(map(int, input().split()))
A_list = list(map(int, input().split()))
max_a = max(max(A_list), K)
max_len = 0
tmp = max_a
while tmp > 0:
	max_len += 1
	tmp //= 2
K_bits = [1 if K >> j & 1 else 0 for j in range(max_len)][::-1]
sum_1_bits = [0] * max_len
for a in A_list:
	a_bits = [j for j in range(max_len) if a >> j & 1]
	for bit in a_bits:
		sum_1_bits[bit] += 1
sum_1_bits = sum_1_bits[::-1]
dp = [[0] * max_len for i in range(2)]
border = N / 2
twice = 2 ** (max_len - 1)
highest_digit = 0
i = 0
if K == 0:
	res = 0
	for b in sum_1_bits:
		res += twice * b
		twice //= 2
	print(res)
	return
while True:
	if K_bits[i] == 1:
		highest_digit = i
		break
	dp[0][i] = dp[0][i - 1] + twice * sum_1_bits[i]
	dp[1][i] = dp[1][i - 1] + twice * sum_1_bits[i]
	twice //= 2
	i += 1
dp[0][highest_digit] = dp[0][highest_digit - 1] + twice * (N - sum_1_bits[highest_digit])
dp[1][highest_digit] = dp[1][highest_digit - 1] + twice * sum_1_bits[highest_digit]
twice //= 2
for i in range(highest_digit + 1, max_len):
	ibits = sum_1_bits[i]
	dp[1][i] = dp[1][i - 1] + twice * max(ibits, N - ibits)
	if K_bits[i] == 1:
		dp[1][i] = max(dp[1][i], dp[0][i - 1] + twice * ibits)
	k_side = ibits if K_bits[i] == 0 else N - ibits
	dp[0][i] = dp[0][i - 1] + twice * k_side
	twice //= 2
print(max(dp[0][-1], dp[1][-1]))


(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
A_max = max(A)
M = len(bin(A_max)) - 2
L = len(bin(K)) - 2
C = [[N, 0] for j in range(max(M, L))]
for i in A:
	for j in range(M):
		if i >> j & 1:
			C[j][1] += 1
			C[j][0] -= 1
P = 0
for j in range(L - 1, -1, -1):
	if C[j][0] >= C[j][1]:
		if P + 2 ** j <= K:
			P += 2 ** j
ans = 0
for i in A:
	ans += P ^ i
print(ans)


(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
i = 1
while i <= K:
	i *= 2
x = 0
while i != 0:
	if sum([1 for a in A if a & i == 0]) > N / 2:
		if x + i <= K:
			x += i
	i //= 2
print(sum([a ^ x for a in A]))


def solve():
	(N, K) = map(int, input().split())
	A = list(map(int, input().split()))
	digit = len(str(bin(K))) - 2
	digits1 = [0] * digit
	ans = 0
	for a in A:
		ans += a - a % (1 << digit)
		for i in range(digit):
			if a & 1 << i:
				digits1[i] += 1
	x = 0
	for i in range(digit - 1, -1, -1):
		if digits1[i] < N / 2 and x + (1 << i) <= K:
			x += 1 << i
			ans += (1 << i) * (N - digits1[i])
		else:
			ans += (1 << i) * digits1[i]
	return ans
print(solve())


import sys
from functools import lru_cache
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007

def main():
	(N, K, *A) = list(map(int, read().split()))
	K = list(map(int, f'{K:>040b}'))
	A2 = [list(map(int, f'{a:>040b}')) for a in A]
	B = [0] * 40
	for a in A2:
		for (i, bit) in enumerate(a):
			B[i] += bit
	for i in range(40):
		if B[i] == N - B[i]:
			B[i] = 2
		elif B[i] < N - B[i]:
			B[i] = 1
		else:
			B[i] = 0

	@lru_cache(maxsize=None)
	def rec(i, x, smaller):
		if i == 40:
			ans = 0
			for a in A:
				ans += x ^ a
			return ans
		elif smaller:
			if B[i] == 2:
				return rec(i + 1, x << 1, True)
			else:
				return rec(i + 1, (x << 1) + B[i], True)
		elif B[i] == 2:
			if K[i] == 0:
				return rec(i + 1, x << 1, False)
			else:
				return rec(i + 1, x << 1, True)
		elif K[i] == B[i]:
			return rec(i + 1, (x << 1) + K[i], False)
		elif K[i] < B[i]:
			return rec(i + 1, (x << 1) + K[i], False)
		else:
			return rec(i + 1, (x << 1) + B[i], True)
	print(rec(0, 0, False))
	return

def __starting_point():
	main()
__starting_point()


(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
bit_cnt = [0] * 40
for a in arr:
	for i in range(40):
		if a >> i & 1:
			bit_cnt[i] += 1
x = 0
for i in range(39, -1, -1):
	if x + (1 << i) <= k and bit_cnt[i] <= n - bit_cnt[i]:
		x += 1 << i
print(sum([x ^ y for y in arr]))


(n, k) = list(map(int, input().split()))
A = list(map(int, input().split()))
B = [[0] * 2 for _ in range(42)]
for a in A:
	b = format(a, 'b')
	b = list(b)
	b.reverse()
	for i in range(len(b)):
		if b[i] == '1':
			B[i][1] += 1
for i in range(42):
	if B[i][1] != 0:
		B[i][0] = n - B[i][1]
B.reverse()
temp = 0
max_ = 0
for (i, (b0, b1)) in enumerate(B):
	if b1 == 0:
		if temp + 2 ** (41 - i) <= k:
			max_ += n * 2 ** (41 - i)
			temp += 2 ** (41 - i)
	elif b0 >= b1:
		if temp + 2 ** (41 - i) <= k:
			max_ += b0 * 2 ** (41 - i)
			temp += 2 ** (41 - i)
		else:
			max_ += b1 * 2 ** (41 - i)
			temp += 0
	else:
		max_ += b1 * 2 ** (41 - i)
		temp += 0
print(max_)


MAX_DIGIT = 40
SMALL = 0
EQUAL = 1
(N, K) = map(int, input().split())
A = list(map(int, input().split()))
dp = [[-1 for _ in range(2)] for _ in range(MAX_DIGIT + 1)]
dp[0][EQUAL] = 0
for d in range(MAX_DIGIT):
	shift = MAX_DIGIT - d - 1
	bit_count = 0
	for a in A:
		if a >> shift & 1:
			bit_count += 1
	cost0 = (1 << shift) * bit_count
	cost1 = (1 << shift) * (N - bit_count)
	if dp[d][SMALL] != -1:
		dp[d + 1][SMALL] = max(dp[d + 1][SMALL], dp[d][SMALL] + max(cost0, cost1))
	if dp[d][EQUAL] != -1:
		if K >> shift & 1:
			dp[d + 1][SMALL] = max(dp[d + 1][SMALL], dp[d][EQUAL] + cost0)
	if dp[d][EQUAL] != -1:
		if K >> shift & 1:
			dp[d + 1][EQUAL] = max(dp[d + 1][EQUAL], dp[d][EQUAL] + cost1)
		else:
			dp[d + 1][EQUAL] = max(dp[d + 1][EQUAL], dp[d][EQUAL] + cost0)
ans = max(dp[MAX_DIGIT][EQUAL], dp[MAX_DIGIT][SMALL])
print(ans)


(n, k) = map(int, input().split())
A = tuple(map(int, input().split()))
ones = [0] * k.bit_length()
for a in A:
	for i in range(k.bit_length()):
		ones[i] += a >> i & 1
x = 0
for (idx, one) in enumerate(ones[::-1]):
	if n - one > one:
		xx = 2 ** (len(ones) - 1 - idx)
		if x + xx > k:
			continue
		else:
			x += xx
ans = 0
for a in A:
	ans += a ^ x
print(ans)


(n, k) = map(int, input().split())
A = list(map(int, input().split()))
cnt = [0] * 50
L = format(k, 'b').zfill(50)
S = [[0, 0] for _ in range(50)]
for a in A:
	a_ = format(a, 'b').zfill(50)
	for i in range(50):
		if a_[i] == '1':
			S[i][0] += 1
		else:
			S[i][1] += 1
for i in range(len(S)):
	S[i][0] *= pow(2, 50 - i - 1)
	S[i][1] *= pow(2, 50 - i - 1)
dp = [[-1, -1] for _ in range(55)]
dp[0][0] = 0
for i in range(50):
	if L[i] == '1':
		if dp[i][1] != -1:
			dp[i + 1][1] = dp[i][1] + max(S[i][0], S[i][1])
		if dp[i][0] != -1:
			dp[i + 1][1] = max(dp[i + 1][1], dp[i][0] + S[i][0])
			dp[i + 1][0] = dp[i][0] + S[i][1]
	else:
		if dp[i][1] != -1:
			dp[i + 1][1] = dp[i][1] + max(S[i][1], S[i][0])
		if dp[i][0] != -1:
			dp[i + 1][0] = dp[i][0] + S[i][0]
print(max(dp[50]))


import sys
input = sys.stdin.readline

def main():
	(N, K) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	K_d = [0] * (len(bin(10 ** 12)) - 2)
	L = len(K_d)
	for (i, k) in enumerate(bin(K)[2:][::-1], 1):
		k = int(k)
		K_d[L - i] = k
	cnt_d = [[0, 0] for _ in range(L)]
	for a in A:
		for i in range(L - 1, -1, -1):
			cnt_d[i][a & 1] += 1
			a >>= 1
	ans = 0
	lower_K = False
	for (i, (d, (n_zero, n_one))) in enumerate(zip(K_d, cnt_d)):
		if n_zero <= n_one:
			n = 0
		else:
			n = 1
		if not lower_K:
			if d == 1 and n == 0:
				lower_K = True
			elif d == 0 and n == 1:
				n = 0
		num = cnt_d[i][n ^ 1]
		ans += num * 2 ** (L - 1 - i)
	print(ans)

def __starting_point():
	main()
__starting_point()


(N, K) = map(int, input().split())
A = list(map(int, input().split()))

def f(x, n):
	i = 0
	while x != 0:
		cnt_bit[i] += x % abs(n)
		if n < 0:
			x = -(-x // n)
		else:
			x //= n
		i += 1

def base(x, n):
	ret = []
	while x != 0:
		ret.append(x % abs(n))
		if n < 0:
			x = -(-x // n)
		else:
			x //= n
	return ret
cnt_bit = [0] * 50
Flag = True
for a in A:
	f(a, 2)
B = base(K, 2)
M = len(B)
for i in range(M):
	(c1, c0) = (cnt_bit[M - 1 - i], N - cnt_bit[M - 1 - i])
	if Flag:
		if c1 >= c0:
			if B[M - 1 - i] == 1:
				Flag = False
			B[M - 1 - i] = 0
	elif c1 >= c0:
		B[M - 1 - i] = 0
	else:
		B[M - 1 - i] = 1
if K == 0:
	k = 0
else:
	k = int(''.join(map(str, reversed(B))), 2)
ans = 0
for a in A:
	ans += a ^ k
print(ans)


(n, k) = list(map(int, input().split()))
A = list(map(int, input().split()))
b = 1
c = 0
while b <= k:
	b *= 2
	c += 1
B = [0] * c
for a in A:
	for i in range(c):
		if a >> i & 1:
			B[i] += 1
x = 0
for b in range(c, -1, -1):
	if x + (1 << b) > k:
		continue
	if B[b] < (n + 1) // 2:
		x += 1 << b
ans = 0
for a in A:
	ans += x ^ a
print(ans)


(n, k) = list(map(int, input().split()))
A = sorted(map(int, input().split()), reverse=True)
cnt = [0 for _ in range(100)]
m = A[0]
for i in range(100):
	ten = 2 ** i
	if ten > m:
		break
	for a in A:
		if a < ten:
			break
		if a >> i & 1:
			cnt[i] += 1
for m in range(100):
	if 2 ** m > k + 1:
		j = m - 1
		break
initial = 0
if i > j:
	for (m, c) in enumerate(cnt[j + 1:i + 1], j + 1):
		initial += 2 ** m * c
best = [0]
for (m, c) in enumerate(cnt[:j + 1]):
	best.append(best[-1] + max(c, n - c) * 2 ** m)
K = list()
equal = [initial]
for (m, c) in enumerate(cnt[j::-1]):
	y = k + 1 >> j - m & 1
	K.append(y)
	if y == 1:
		equal.append(equal[-1] + (n - c) * 2 ** (j - m))
	else:
		equal.append(equal[-1] + c * 2 ** (j - m))
ans = 0
K.reverse()
for i in range(j + 1):
	if K[i]:
		ans = max(ans, best[i] + 2 ** i * cnt[i] + equal[-(i + 2)])
print(ans)


def main():
	(N, K) = list(map(int, input().split()))
	(*A,) = list(map(int, input().split()))

	def bit60(x):
		(*bit,) = list(map(int, bin(x)[2:]))
		zfill = 60 - len(bit)
		ret = [0] * zfill
		ret += bit
		return ret
	ctr = [0] * 60
	for bit in map(bit60, A):
		for j in range(60):
			ctr[j] += bit[j]
	ans = 0
	d = 1 << 60 - 1
	x = 0
	for j in range(60):
		if ctr[j] < N - ctr[j] and x + d <= K:
			x += d
			ans += (N - ctr[j]) * d
		else:
			ans += ctr[j] * d
		d >>= 1
	print(ans)

def __starting_point():
	main()
__starting_point()


import sys
readline = sys.stdin.readline
(N, K) = map(int, readline().split())
A = list((int(x) for x in readline().split()))
bits = [0] * 50
for i in range(N):
	for j in range(50):
		if A[i] >> j & 1:
			bits[j] += 1
dp = [[0] * 2 for _ in range(50)]
for i in range(49)[::-1]:
	check_bit = 1 << i
	if K & check_bit:
		dp[i][1] = dp[i + 1][0] + bits[i] * check_bit
		if dp[i + 1][1] != 0:
			dp[i][1] = max(dp[i][1], dp[i + 1][1] + max(N - bits[i], bits[i]) * check_bit)
		dp[i][0] = dp[i + 1][0] + (N - bits[i]) * check_bit
	else:
		if dp[i + 1][1] != 0:
			dp[i][1] = dp[i + 1][1] + max(N - bits[i], bits[i]) * check_bit
		dp[i][0] = dp[i + 1][0] + bits[i] * check_bit
print(max(dp[0][0], dp[0][1]))


import sys
input = sys.stdin.readline
(N, K) = map(int, input().split())
A = list(map(int, input().split()))
B = [0] * 40
for a in A:
	for i in range(40):
		if a & 1 << i:
			B[i] += 1
X = [0] * 40
for i in range(40):
	if B[i] <= N / 2:
		X[i] = 1
bitK = list(map(int, list(bin(K)[2:].zfill(40)[::-1])))
ans = 0
for i in range(40):
	if bitK[i] == 1:
		ans += N - B[i] << i
	else:
		ans += B[i] << i
for k in range(40):
	if bitK[k] == 0:
		continue
	total = 0
	leqK = False
	for i in range(39, -1, -1):
		if leqK:
			if X[i] == 1:
				total += N - B[i] << i
			else:
				total += B[i] << i
		elif i == k:
			total += B[i] << i
			leqK = True
		elif bitK[i] == 1:
			total += N - B[i] << i
		else:
			total += B[i] << i
	ans = max(total, ans)
print(ans)


import sys
import math
from collections import defaultdict
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7

def input():
	return sys.stdin.readline().strip()

def main():
	(N, K) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	d = defaultdict(int)
	for a in A:
		if a == 0:
			continue
		digit = int(math.log2(a))
		for i in range(digit + 1):
			if 1 << i & a:
				d[i] += 1
	x = 0
	if K != 0:
		digit = int(math.log2(K))
		for i in range(digit + 1)[::-1]:
			if N / 2 <= d[i]:
				continue
			elif K < x + 2 ** i:
				continue
			else:
				x += 2 ** i
	ans = 0
	for a in A:
		ans += x ^ a
	print(ans)

def __starting_point():
	main()
__starting_point()


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(40, -1, -1):
	j = 2 ** i
	count = len([1 for temp in a if temp & j > 0])
	if count < n - count and j <= k:
		k -= j
		count = n - count
	ans += j * count
print(ans)


def Z():
	return int(input())

def ZZ():
	return [int(_) for _ in input().split()]

def main():
	(N, K) = ZZ()
	A = ZZ()
	if K == 0:
		print(sum(A))
		return
	bK = format(K, 'b')
	msb = len(bK)
	plus = [0] * msb
	for a in A:
		for (i, b) in enumerate(format(a, '0{}b'.format(msb))[::-1][:msb]):
			plus[i] += int(b)
	e = 1
	for i in range(msb):
		plus[i] = e * (N - 2 * plus[i])
		e *= 2
	dp = [[0] * 2 for _ in range(msb)]
	dp[0][0] = plus[i]
	plus = plus[::-1]
	for i in range(msb - 1):
		dp[i + 1][0] = max(dp[i + 1][0], dp[i][0] + plus[i + 1] * int(bK[i + 1]))
		if int(bK[i + 1]) == 1:
			dp[i + 1][1] = max(dp[i + 1][1], dp[i][0])
		dp[i + 1][1] = max(dp[i + 1][1], dp[i][1], dp[i][1] + plus[i + 1])
	print(max(dp[msb - 1]) + sum(A))
	return

def __starting_point():
	main()
__starting_point()


(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
UP = max(A)
UL = UP.bit_length()
KL = K.bit_length()
MAX = max(UL, KL)
INF = float('inf')
dp = [[-INF] * 2 for _ in range(MAX + 1)]
dp[0][0] = 0
ZF = True
L = [[0] * 2 for _ in range(MAX)]
for i in range(N):
	for j in range(MAX):
		if A[i] >> j & 1 == 1:
			L[MAX - 1 - j][1] += 1
		else:
			L[MAX - 1 - j][0] += 1
for i in range(MAX):
	fac = pow(2, MAX - 1 - i)
	if K >> MAX - 1 - i & 1 == 1:
		ZF = False
	if ZF:
		dp[i + 1][0] = dp[i][0] + fac * L[i][1]
		dp[i + 1][1] = 0
	else:
		if K >> MAX - 1 - i & 1 == 1:
			kbit = 1
		else:
			kbit = 0
		if kbit == 1:
			dp[i + 1][1] = max(dp[i][1] + fac * max(L[i]), dp[i][0] + fac * L[i][1])
			dp[i + 1][0] = dp[i][0] + fac * L[i][0]
		else:
			dp[i + 1][1] = dp[i][1] + fac * max(L[i])
			dp[i + 1][0] = dp[i][0] + fac * L[i][1]
ans = max(dp[MAX])
print(ans)


(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
less = -float('inf')
eq = 0
for d in range(60)[::-1]:
	mask = 1 << d
	one = len([0 for a in A if a & mask != 0])
	zero = N - one
	l = less + mask * max(one, zero)
	if K & mask != 0:
		l = max(l, eq + mask * one)
	less = l
	eq += mask * (one if K & mask == 0 else zero)
print(max(less, eq))


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
t = 2 ** 40
ans = 0
while t:
	c = sum([(A[n] & t) // t for n in range(N)])
	if K < t or c * 2 >= N:
		ans += t * c
	else:
		ans += t * (N - c)
		K -= t
	t //= 2
print(ans)


(N, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
dp = [[0, 0] for _ in range(42)]
for i in range(1, 42):
	if dp[i - 1][1] == 1:
		dp[i][1] = 1
	cnt = 0
	for j in range(N):
		if A[j] >> 41 - i & 1 == 1:
			cnt += 1
	if cnt >= N - cnt:
		dp[i][0] = dp[i - 1][0] + 2 ** (41 - i) * cnt
		if K >> 41 - i & 1 == 1:
			dp[i][1] = 1
	elif dp[i][1] == 1:
		dp[i][0] = dp[i - 1][0] + 2 ** (41 - i) * (N - cnt)
	elif K >> 41 - i & 1 == 1:
		dp[i][0] = dp[i - 1][0] + 2 ** (41 - i) * (N - cnt)
	else:
		dp[i][0] = dp[i - 1][0] + 2 ** (41 - i) * cnt
print(dp[41][0])


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
d = {j: 0 for j in range(41)}
for ai in a:
	for j in range(41):
		if ai >> j & 1:
			d[j] += 1
ans = 0
l = len(bin(k)) - 3
import sys
sys.setrecursionlimit(10 ** 7)

def dp(m, t):
	if m < 0:
		return 0
	ret = 0
	if t == 1:
		for i in range(m + 1):
			tmp = max(0, n - 2 * d[i])
			ret += pow(2, i) * tmp
	elif k >> m & 1:
		tmp1 = dp(m - 1, 0) + (n - 2 * d[m]) * pow(2, m)
		tmp0 = dp(m - 1, 1)
		ret += max(tmp1, tmp0)
	else:
		ret += dp(m - 1, 0)
	return ret
print(sum(a) + dp(l, 0))


def main():
	(N, K) = list(map(int, input().split()))
	(*A,) = list(map(int, input().split()))
	ctr = [0] * 60
	for x in A:
		(*bit,) = list(map(int, bin(x)[2:]))
		bit.reverse()
		for (j, b) in enumerate(bit):
			i = 60 - 1 - j
			ctr[i] += b
	ans = 0
	d = 1 << 60 - 1
	x = 0
	for (j, cnt) in enumerate(ctr):
		if cnt < N - cnt and x + d <= K:
			x += d
			ans += (N - cnt) * d
		else:
			ans += cnt * d
		d >>= 1
	print(ans)

def __starting_point():
	main()
__starting_point()


import numpy as np
st = lambda n: bin(n)[2:]
(N, K) = map(int, input().split())
a = np.array([int(i) for i in input().split()])
MAX = 10 ** 12
mm = MAX.bit_length()
chk = [(a >> K & 1).sum() for K in range(mm)]
ll = []
q = 0
for (num, cc) in enumerate(chk):
	if cc < N / 2:
		q += 2 ** num
	ll.append((num, 2 ** num * (N - cc), 2 ** num * cc))
max_V = 0
KK = (K + 1).bit_length()
sk = st(K + 1).zfill(mm)
sq = st(q).zfill(mm)
for i in range(KK):
	if K + 1 >> i & 1 == 1:
		op = q
		if q >> i & 1 == 1:
			op -= 2 ** i
		I = KK - i
		for j in range(mm - KK + I):
			if sq[j] == '1' and sk[j] == '0':
				op -= 2 ** (mm - 1 - j)
		V = 0
		for (nm, v_1, v_0) in ll:
			if op >> nm & 1 == 1:
				V += v_1
			else:
				V += v_0
		max_V = max(max_V, V)
print(max_V)


import sys

def input():
	return sys.stdin.readline().strip()

def main():
	(N, K) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	B = K
	k = 1
	while K > 1:
		K //= 2
		k += 1
	if K == 0:
		print(sum(A))
		return
	cnt = [0 for _ in range(k)]
	for a in A:
		for i in range(k):
			if a >> i & 1:
				cnt[i] += 1
	X = 0
	if N % 2 == 0:
		n = N // 2 - 1
	else:
		n = N // 2
	for i in range(k):
		if cnt[k - i - 1] <= n:
			if X + 2 ** (k - i - 1) <= B:
				X += 2 ** (k - i - 1)
	answer = 0
	for a in A:
		answer += X ^ a
	print(answer)

def __starting_point():
	main()
__starting_point()


(n, k) = list(map(int, input().split()))
nums = list(map(int, input().split()))
kp1_2 = format(k + 1, 'b')
digit_len = len(kp1_2)
sum_nums = sum(nums)
bit_count_dict = {}
for digit in range(digit_len):
	bit_count = 0
	for num in nums:
		if 2 ** digit & num:
			bit_count += 1
	bit_count_dict[digit] = bit_count
ans = 0
for digit in range(digit_len):
	temp = kp1_2
	ans_candi = sum_nums
	if kp1_2[digit_len - 1 - digit] == '1':
		for d_upper in range(digit + 1, digit_len):
			if kp1_2[digit_len - 1 - d_upper] == '1':
				ans_candi += (n - 2 * bit_count_dict[d_upper]) * 2 ** d_upper
		for d_lower in range(0, digit):
			if n - 2 * bit_count_dict[d_lower] > 0:
				ans_candi += (n - 2 * bit_count_dict[d_lower]) * 2 ** d_lower
		ans = max(ans, ans_candi)
print(ans)