Problem:
 Score : 400 points    Problem Statement 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 .)

Solution (Python):

N,K=map(int,raw_input().split())
A=map(int,raw_input().split())


dp = [[-1, -1] for _ in range(51)]

dp[50][1]=0
for i in range(50):
    k = 49 - i
    mask = 1 << k
    num = 0
    for a in A:
        if (a&mask)!=0: num+=1
    

    if dp[k+1][0]>=0:
        
        dp[k][0] = max(dp[k+1][0] + mask*num, dp[k+1][0] + mask*(N-num))
    
    if dp[k+1][1]>=0:
        
        if K&mask!=0:
            
            dp[k][1] = dp[k+1][1] + mask*(N-num)

            
            dp[k][0] = max(dp[k][0], dp[k+1][1] + mask * num) 
        else:
            

            
            dp[k][1] = dp[k+1][1]+mask*num
print(max(dp[0]))

Solution (Python):
import math
def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]
def zeros(n): return [0]*n
def zeros2(n, m): return [zeros(m) for i in range(n)] 

class Debug():
    def __init__(self):
        self.debug = True
 
    def off(self):
        self.debug = False
 
    def dmp(self, x, cmt=''):
        if self.debug:
            if cmt != '':
                w = cmt + ': ' + str(x)
            else:
                w = str(x)
            print(w)
        return x

def prob():  
    d = Debug()
    d.off()
    N, K = getIntList()
    d.dmp((N, K), 'N, K')
    A = getIntList()
    d.dmp((A), 'A')
    if K == 0:
        x = 0
    else:
        lg = int(math.log2(K+0.1))
        mask = 1 << lg
        x = 0
        d.dmp(x,'x')
        
        lessK = False  
        while mask > 0:
            cz, co = 0, 0
            for i in range(N):
                if A[i] & mask == 0:
                    cz += 1
                else:
                    co += 1
            if cz > co:
                if K & mask or lessK:
                    x += mask
            if (x & mask == 0) and mask & K:
                lessK = True
            mask >>= 1
            d.dmp((mask,x),'mask,x')
    d.dmp((x),'x')
    ans = 0
    for i in range(N):
        d.dmp((x ^ A[i]),'x ^ A[i]')
        ans += x ^ A[i]
    return ans


ans = prob()
print(ans)

Solution (Python):
import math
def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]

def zeros(n): return [0]*n

def zeros2(n, m): return [zeros(m) for i in range(n)] 

class Debug():
    def __init__(self):
        self.debug = True
 
    def off(self):
        self.debug = False
 
    def dmp(self, x, cmt=''):
        if self.debug:
            if cmt != '':
                w = cmt + ': ' + str(x)
            else:
                w = str(x)
            print(w)
        return x

def getMask(n):
    mask = 2**60
    while n & mask == 0:
        mask >>= 1
    return mask

def prob():  
    d = Debug()
    d.off()
    N, K = getIntList()
    d.dmp((N, K), 'N, K')
    A = getIntList()
    d.dmp((A), 'A')
    if K == 0:
        x = 0
    else:
        
        
        mask = getMask(K)
        x = 0
        d.dmp(x,'x')
        
        lessK = False  
        while mask > 0:
            cz, co = 0, 0
            for i in range(N):
                if A[i] & mask == 0:
                    cz += 1
                else:
                    co += 1
            if cz > co:
                if K & mask or lessK:
                    x += mask
            if (x & mask == 0) and mask & K:
                lessK = True
            mask >>= 1
            d.dmp((mask,x),'mask,x')
    d.dmp((x),'x')
    ans = 0
    for i in range(N):
        d.dmp((x ^ A[i]),'x ^ A[i]')
        ans += x ^ A[i]
    return ans


ans = prob()
print(ans)

Solution (Python):
N,K=map(int,input().split())
a=list(map(int,input().split()))
l=max( len(bin(K)) ,len(bin(max(a))) )-2

K=bin(K)[2:].zfill(l)[::-1]
a=[bin(a[i])[2:].zfill(l)[::-1] for i in range(N)]
val=[[0,0] for i in range(l)]

for i in range(N):
    for j in range(l):
        if a[i][j]=="0": val[j][0]+=1
        else: val[j][1]+=1
        



tmp=0
for j in range(l):
    tmp+=(N-val[j][int(K[j])])*2**j

ans=tmp

for i in range(l):
    tmp=0
    if K[i]=="1":
        for j in range(i):
            tmp+=max(val[j])*2**j
        
        tmp+=val[i][1]*2**i
        
        for j in range(i+1,l):
            tmp+=(N-val[j][int(K[j])])*2**j
            
    ans=max(ans,tmp)
    
print(ans)

Solution (Python):
from collections import defaultdict
N,K = map(int,input().split())
K = bin(K)[2:]
A = list(map(lambda x:bin(int(x))[2:],input().split()))
count = defaultdict(lambda:0)
for i in range(N):
    for j in range(len(A[i])):
        if A[i][-j-1] == '1':
            count[j] += 1
def rec(x):
    if x=='':
        return 0
    if '0' not in x:
        res = 0
        for i in range(len(x)):
            res += max(count[i],N-count[i])*2**i
        return res
    if x[0] == '1':
        return max(rec('1'*(len(x)-1))+count[len(x)-1]*2**(len(x)-1),rec(x[1:])+(N-count[len(x)-1])*2**(len(x)-1))
    else:
        return rec(x[1:])+count[len(x)-1]*2**(len(x)-1)
ans = rec(K)
for i in range(len(K),41):
    ans += count[i]*2**i
print(ans)

Solution (Python):
from collections import defaultdict
N,K = map(int,input().split())
A = list(map(lambda x:bin(int(x))[2:],input().split()))
l = 0
for i in range(N):
    l = max(l,len(A[i]))
count = defaultdict(lambda:0)
for i in range(N):
    for j in range(len(A[i])):
        if A[i][-j-1] == '1':
            count[j] += 1
l = max(count.keys())

def rec(x):
    if x=='':
        return 0
    if '0' not in x:
        res = 0
        for i in range(len(x)):
            res += max(count[i],N-count[i])*2**i
        return res
    if x[0] == '1':
        return max(rec('1'*(len(x)-1))+count[len(x)-1]*2**(len(x)-1),rec(x[1:])+(N-count[len(x)-1])*2**(len(x)-1))
    else:
        return rec(x[1:])+count[len(x)-1]*2**(len(x)-1)
ans = rec(bin(K)[2:])
for i in range(len(bin(K)[2:]),l+1):
    ans += count[i]*2**i
print(ans)

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

x = 0
b = 1<<50
for _ in range(50):
    b = b>>1
    if x+b>K:
        continue

    cnt = 0
    for a in A:
        if a & b:
            cnt += 1
    if cnt < N/2:
        x += b

ans = 0
for a in A:
    ans += x^a

print(ans)

Solution (Python):
n,k = 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
    
    
    ctr = 0
    for j in range(n):
        if (a[j]>>(41-i))&1 == 1:
            ctr += 1

    if ctr >= n-ctr:
        
        dp[i][0] = dp[i-1][0] + ctr*(2**(41-i))
        if (k >> (41-i))&1 == 1:
            dp[i][1] = 1
    else:
        
        if dp[i][1] == 1:
            
            dp[i][0] = dp[i-1][0]+(n-ctr)*(2**(41-i))
        else:
            if (k >> (41-i))&1 == 1:
                
                dp[i][0] = dp[i-1][0]+(n-ctr)*(2**(41-i))
            else:
                
                dp[i][0] = dp[i-1][0] + ctr*(2**(41-i))

print(dp[41][0])

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt = [0] * 50

for i in a:
    for j in range(50):
        if i & (1 << j) == 0:
            cnt[j] += 1 << j
        else:
            cnt[j] -= 1 << j

gg = []
for i in range(50):
    gg.append([cnt[i], i])
gg.sort(reverse = True)

ans = 0
for i in gg:
    if i[0] < 0:
        break
    if (1 << i[1]) <= k:
        k -= 1 << i[1]
        ans += i[0]

print(ans + sum(a))

Solution (Python):


import sys
INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n, k = LI()
    A = LI()

    length = 40
    cnt = [0] * length
    for i in range(n):
        for j in range(length):
            if A[i] >> j & 1:
                cnt[j] += 1

    res = sum([k ^ a for a in A])
    for d in range(length):
        if k >> d & 1 == 0:
            continue

        t = 0
        for e in range(length):
            mask = 1 << e

            if e > d:
                if k & mask:
                    t += mask * (n - cnt[e])
                else:
                    t += mask * cnt[e]
            elif e == d:
                
                t += mask * cnt[e]
            else:
                t += mask * max(cnt[e], n - cnt[e])

        res = max(res, t)

    return res


print(main())

Solution (Python):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

b = 1 << 50
x = 0

for i in range(50):
    b = b >> 1

    if x + b > k:
        continue

    cnt = 0

    for j in a:
        if j & b:
            cnt += 1

    if cnt < len(a) / 2:
        x += b

ans = 0
for i in a:
    ans += i ^ x

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)
n,k=map(int,input().split())
A=list(map(int,input().split()))
l=40

def bit_cnt(X,d):
    c=0
    d=l-d
    for x in X:
        if x&(1<<d):
            c+=1
    return c

DP=[[0]*2 for _ in range(l+1)]
for i in range(1,l+1):
    if DP[i-1][1]:
        DP[i][1]=1
    cnt=bit_cnt(A,i)
    if cnt>=n-cnt:
        DP[i][0]=DP[i-1][0]+cnt*(1<<(l-i))
        if (k>>(l-i))&1:
            DP[i][1]=1
    else:
        if DP[i][1]:
            DP[i][0]=DP[i-1][0]+(n-cnt)*(1<<(l-i))
        else:
            if (k>>(l-i))&1:
                DP[i][0]=DP[i-1][0]+(n-cnt)*(1<<(l-i))
            else:
                DP[i][0]=DP[i-1][0]+cnt*(1<<(l-i))
print(DP[-1][0])

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)
n,k=map(int,input().split())
A=list(map(int,input().split()))
l=40

def bit_cnt(X,d):
    c=0
    d=l-d
    for x in X:
        if x&(1<<d):
            c+=1
    return c

DP=[[0]*2 for _ in range(l+1)]
for i in range(1,l+1):
    if DP[i-1][1]:
        DP[i][1]=1
    cnt=bit_cnt(A,i)
    if cnt>=n-cnt:
        DP[i][0]=DP[i-1][0]+cnt*(1<<(l-i))
        if (k>>(l-i))&1:
            DP[i][1]=1
    else:
        if DP[i][1]:
            DP[i][0]=DP[i-1][0]+(n-cnt)*(1<<(l-i))
        else:
            if (k>>(l-i))&1:
                DP[i][0]=DP[i-1][0]+(n-cnt)*(1<<(l-i))
            else:
                DP[i][0]=DP[i-1][0]+cnt*(1<<(l-i))
print(DP[-1][0])

Solution (Python):

import sys

def _solve(counts, k, n, used=[]):
    ret = 0
    digits = 0
    while k >= 2 ** digits:
        digits += 1
    tmp = 0
    for idx, c in enumerate(counts):
        if not idx in used:
            if n - 2 * c > 0 and tmp + 2 ** idx <= k:
                ret += (2 ** idx) * (n - c)
                tmp += 2 ** idx
            else:
                ret += (2 ** idx) * c
    if digits > 0:
        last = counts[digits - 1]
        if n - 2 * last >= 0:
            most_bit = 2 ** (digits - 1)
            k_ = k - most_bit
            ret = max(ret, _solve(counts, k_, n, used + [digits - 1]) + most_bit * (n - last))
    return ret


def solve(N: int, K: int, A: "List[int]"):
    counts = [0] * 45
    for num in A:
        n = num
        idx = 0
        while n > 0:
            if n % 2 == 1:
                counts[idx] += 1
            idx += 1
            n //= 2
    ret = _solve(counts, K, N)
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    A = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, K, A)

if __name__ == '__main__':
    main()

Solution (Python):

import sys

def _solve(counts, k, n, used=[]):
    ret = 0
    digits = 0
    while k >= 2 ** digits:
        digits += 1
    tmp = 0
    for idx, c in enumerate(counts):
        if not idx in used:
            if n - 2 * c > 0 and tmp + 2 ** idx <= k:
                ret += (2 ** idx) * (n - c)
                tmp += 2 ** idx
            else:
                ret += (2 ** idx) * c
    
    
    if digits > 0:
        last = counts[digits - 1]
        if n - 2 * last >= 0:
            most_bit = 2 ** (digits - 1)
            k_ = k - most_bit
            
            ret = max(ret, _solve(counts, k_, n, used + [digits - 1]) + most_bit * (n - last))
        
        
        
    
    return ret


def solve(N: int, K: int, A: "List[int]"):
    counts = [0] * 45
    for num in A:
        n = num
        idx = 0
        while n > 0:
            if n % 2 == 1:
                counts[idx] += 1
            idx += 1
            n //= 2
    ret = _solve(counts, K, N)
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    A = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, K, A)

if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
As = list(map(int, input().split()))

maxDgt = 40

fss = [[0]*(2) for _ in range(maxDgt)]
for A in As:
    for i in range(maxDgt):
        fss[maxDgt-1-i][A&(1<<i) == 0] += 1<<i

K += 1

dp = [[-float('inf')]*(2) for _ in range(maxDgt+1)]
dp[0][0] = 0
for dgt, Kd in enumerate(map(int, bin(K)[2:].zfill(maxDgt))):
    for isLeK in range(2):
        xLim = 1 if isLeK else Kd
        for xd in range(xLim+1):
            isLeK2 = isLeK or xd < xLim
            dp[dgt+1][isLeK2] = max(dp[dgt+1][isLeK2], dp[dgt][isLeK] + fss[dgt][xd])

print(dp[-1][1])

Solution (Python):
def f(X):
    return sum([A^X for A in As])

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

maxDgt = 40
Bs = [list(map(int, bin(A)[2:].zfill(maxDgt)))[::-1] for A in As]

XBest = 0
d = 1
for Cs in zip(*Bs):
    if sum(Cs) < N/2:
        XBest += d
    d *= 2

ans = f(0)
for i in range(maxDgt):
    if (K+1) & (1<<i) == 0: continue
    X = ((K+1)>>(i+1)<<(i+1)) + (XBest&((1<<i)-1))
    ans = max(ans, f(X))

print(ans)

Solution (Python):
N, K = map(int, input().split())
A=list(map(int, input().split()))
L=[0 for _ in range(40)]
ans=0

for a in A:
    i=0
    while a:
        if a&1==1:
            L[i]+=1
        a=a>>1
        i+=1
opt=0
mask=1

for l in L:
    if l<N-l:
        opt = opt | mask
    mask=mask<<1

ans=sum(a^K for a in A)
for i in range(0,40):
    x=0
    for j in range(0,40):
        if j<i:
            x+=(opt>>j&1)*(2**j)
        elif j>i:
            x+=(K>>j&1)*(2**j)

    if x<=K:
        tmp=sum(a^x for a in A)
        ans=max(tmp,ans)

print(ans)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))

bc = [0]*40
for A in a:
    for i in range(40):
        bc[i]+=(A&(1<<i))>>i

X=0
f=False
for i in range(39, -1, -1):
	bit = 1<<i
	if(f or (k&bit)!=0):
		if(bc[i]<=(n//2)):
			X+=2**i
		else:
			f=True


sum=0
for A in a:
    sum+=A^X
print(sum)

Solution (Python):
def func(k, a):
    bit = [0 for _ in range(50)]
    for i in range(len(a)):
        binnedA = bin(a[i])[2:]
        _a = [int(binnedA[_i]) for _i in range(len(binnedA))]
        _a.reverse()
        for j in range(len(_a)):
            bit[j] += _a[j]
        
    best = [0 if bit[i] > len(a) - bit[i] else 1 for i in range(len(bit))]
    
    upperBin = bin(k)[2:]
    upper = [int(upperBin[_i]) for _i in range(len(upperBin))]
    upper.reverse()
    for i in range(len(bit)-len(upper)):
        upper.append(0)

    limited = True
    upper.reverse()
    best.reverse()
    
    
    for i in range(len(upper)):
        if limited:
            if upper[i] == 1:
                if best[i] == 0:
                    best[i] = 0
                    limited = False
                    
                else:
                    best[i] = 1
            else:
                best[i] = 0
    _sum = 0
    best.reverse()
    
    
    for i in range(len(best)):
        if best[i] == 0:
            _sum += bit[i] * (2 ** i)
        else:
            _sum += (len(a) - bit[i]) * (2 ** i)
    return _sum
"""
print("func(7, [1, 6, 3]):{}".format(func(7, [1, 6, 3])))
print("func(9, [7, 4, 0, 3]):{}".format(func(9, [7, 4, 0, 3])))
print("func(0, [1000000000000]):{}".format(func(0, [1000000000000])))
"""
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
print(func(k, a))

Solution (Python):
n,m = map(int,input().split())
a = list(map(int,input().split()))

fm = 0
for i in range(40,0,-1):
    x = 2**(i-1)
    p = 0
    q = 0
    for j in a:
        b = x & j
        p += x ^ b
        q += b
    if q >= p:
        fm += q
    else:
        if m >= x:
            fm += p
            m -= x
        else:
            fm += q
print(fm)

Solution (Python):
n, k = map(int, input().split())
a=list(map(int, input().split()))
k2=format(k, '041b')
a2=[format(ai, '041b') for ai in a]
memo=[0]*41
for x in range(41):
    cnt0=0
    cnt1=0
    for i in range(n):
        if a2[i][x]=='1':
            cnt0+=1
        else:
            cnt1+=1
    if cnt1>cnt0:
        memo[x]=1
for i in range(41):
    if int(k2[i])>memo[i]:
        break
    if int(k2[i])<memo[i]:
        memo[i]=0
mini=sum(memo[i]*2**(40-i) for i in range(41))
def f(x):
    return sum(x^ai for ai in a)
print(f(mini))

Solution (Python):
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
d=[0]*41
t=[0]*41
p=2**40
x=0
y=1
ans=0
for i,j in enumerate(bin(k)[::-1]):
    if j=="b": break
    if j=="1": d[i]=1

for i in a:
    for j,k in enumerate(bin(i)[::-1]):
        if k=="b": break
        if k=="1": t[j]+=1
for i in range(40,-1,-1):
    if d[i]==1 and t[i]>n//2-(n+1)%2:
        y=0
    elif d[i]==1 and t[i]<=n//2:
        x+=p
    elif y==0 and t[i]<=n//2:
        x+=p
    p//=2
for i in a:
    ans+=x^i
print(ans)

Solution (Python):
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
d=[0]*151
t=[0]*151
p=2**150
x=0
y=1
ans=0
for i,j in enumerate(bin(k)[::-1]):
  if j=="b": break
  if j=="1": d[i]=1

for i in a:
  for j,k in enumerate(bin(i)[::-1]):
    if k=="b": break
    if k=="1": t[j]+=1
for i in range(150,-1,-1):
  if d[i]==1 and t[i]>n//2-(n+1)%2:
    y=0
  elif d[i]==1 and t[i]<=n//2:
    x+=p
  elif y==0 and t[i]<=n//2:
    x+=p
  p//=2
for i in a:
  ans+=x^i
print(ans)

Solution (Python):

from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

def A():
    t,x = LI()
    print(t/x)


def B():
    n = I()
    a = LI()
    m = max(a)
    if m >= sum(a)-m:
        print("No")
    else:
        print("Yes")


def C():
    n,m = LI()
    x = LI()
    x.sort()
    l = [x[i+1]-x[i] for i in range(m-1)]
    l.sort()
    l = l[::-1]
    ans = sum(l[max(n-1,0):])
    print(ans)

def D():
    n,k = LI()
    a = LI()
    k = list(map(int,list(bin(k)[2:])))
    l = len(k)
    for i in range(n):
        a[i] = list(map(int,list(bin(a[i])[2:])))
        l = max(l,len(a[i]))
    for i in range(n):
        for j in range(l-len(a[i])):
            a[i].insert(0,0)
    for j in range(l-len(k)):
        k.insert(0,0)
    s = [[0,0] for i in range(l)]
    pow2 = [1 for i in range(l)]
    for i in range(l-1)[::-1]:
        pow2[i] = pow2[i+1]*2
    for i in a:
        for j in range(l):
            s[j][0] += pow2[j]*i[j]
            s[j][1] += pow2[j]*(not i[j])
    dp = [0 for i in range(l+1)]
    small = False
    for i in range(l):
        if not small:
            if not k[i]:
                dp[i+1] += dp[i]+s[i][0]
            else:
                if s[i][0] >= s[i][1]:
                    dp[i+1] += dp[i]+s[i][0]
                    small = True
                else:
                    dp[i+1] += dp[i]+s[i][1]
        else:
            dp[i+1] += dp[i]+max(s[i])
    print(dp[l])


if __name__ == "__main__":
    D()

Solution (Python):

from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

"""
def A():
    t,x = LI()
    print(t/x)
    return
"""

"""
def B():
    n = I()
    a = LI()
    m = max(a)
    if m >= sum(a)-m:
        print("No")
    else:
        print("Yes")
    return
"""

"""
def C():
    n,m = LI()
    x = LI()
    x.sort()
    l = [x[i+1]-x[i] for i in range(m-1)]
    l.sort()
    l = l[::-1]
    ans = sum(l[max(n-1,0):])
    print(ans)
    return
"""

def D():
    n,k = LI()
    a = LI()
    k = list(map(int,list(bin(k)[2:])))
    l = len(k)
    for i in range(n):
        a[i] = list(map(int,list(bin(a[i])[2:])))
        l = max(l,len(a[i]))
    for i in range(n):
        for j in range(l-len(a[i])):
            a[i].insert(0,0)
    for j in range(l-len(k)):
        k.insert(0,0)
    s = [[0,0] for i in range(l)]
    pow2 = [1 for i in range(l)]
    for i in range(l-1)[::-1]:
        pow2[i] = pow2[i+1]*2
    for i in a:
        for j in range(l):
            s[j][0] += pow2[j]*i[j]
            s[j][1] += pow2[j]*(not i[j])
    dp = [0 for i in range(l+1)]
    small = False
    for i in range(l):
        if not small:
            if not k[i]:
                dp[i+1] += dp[i]+s[i][0]
            else:
                if s[i][0] >= s[i][1]:
                    dp[i+1] += dp[i]+s[i][0]
                    small = True
                else:
                    dp[i+1] += dp[i]+s[i][1]
        else:
            dp[i+1] += dp[i]+max(s[i])
    print(dp[l])
    return

if __name__ == "__main__":
    D()

Solution (Python):

from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

def A():
    t,x = LI()
    print(t/x)
    return


def B():
    n = I()
    a = LI()
    m = max(a)
    if m >= sum(a)-m:
        print("No")
    else:
        print("Yes")
    return


def C():
    n,m = LI()
    x = LI()
    x.sort()
    l = [x[i+1]-x[i] for i in range(m-1)]
    l.sort()
    l = l[::-1]
    ans = sum(l[max(n-1,0):])
    print(ans)
    return


def D():
    n,k = LI()
    a = LI()
    k = list(map(int,list(bin(k)[2:])))
    l = len(k)
    for i in range(n):
        a[i] = list(map(int,list(bin(a[i])[2:])))
        l = max(l,len(a[i]))
    for i in range(n):
        for j in range(l-len(a[i])):
            a[i].insert(0,0)
    for j in range(l-len(k)):
        k.insert(0,0)
    s = [[0,0] for i in range(l)]
    pow2 = [1 for i in range(l)]
    for i in range(l-1)[::-1]:
        pow2[i] = pow2[i+1]*2
    for i in a:
        for j in range(l):
            s[j][0] += pow2[j]*i[j]
            s[j][1] += pow2[j]*(not i[j])
    dp = [0 for i in range(l+1)]
    small = False
    for i in range(l):
        if not small:
            if not k[i]:
                dp[i+1] += dp[i]+s[i][0]
            else:
                if s[i][0] >= s[i][1]:
                    dp[i+1] += dp[i]+s[i][0]
                    small = True
                else:
                    dp[i+1] += dp[i]+s[i][1]
        else:
            dp[i+1] += dp[i]+max(s[i])
    print(dp[l])
    return

if __name__ == "__main__":
    D()

Solution (Python):

from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

def A():
    t,x = LI()
    print(t/x)


def B():
    n = I()
    a = LI()
    m = max(a)
    if m >= sum(a)-m:
        print("No")
    else:
        print("Yes")



def C():
    n,m = LI()
    x = LI()
    x.sort()
    l = [x[i+1]-x[i] for i in range(m-1)]
    l.sort()
    l = l[::-1]
    ans = sum(l[max(n-1,0):])
    print(ans)


def D():
    n,k = LI()
    a = LI()
    k = list(map(int,list(bin(k)[2:])))
    l = len(k)
    for i in range(n):
        a[i] = list(map(int,list(bin(a[i])[2:])))
        l = max(l,len(a[i]))
    for i in range(n):
        for j in range(l-len(a[i])):
            a[i].insert(0,0)
    for j in range(l-len(k)):
        k.insert(0,0)
    s = [[0,0] for i in range(l)]
    pow2 = [1 for i in range(l)]
    for i in range(l-1)[::-1]:
        pow2[i] = pow2[i+1]*2
    for i in a:
        for j in range(l):
            s[j][0] += pow2[j]*i[j]
            s[j][1] += pow2[j]*(not i[j])
    dp = [0 for i in range(l+1)]
    small = False
    for i in range(l):
        if not small:
            if not k[i]:
                dp[i+1] += dp[i]+s[i][0]
            else:
                if s[i][0] >= s[i][1]:
                    dp[i+1] += dp[i]+s[i][0]
                    small = True
                else:
                    dp[i+1] += dp[i]+s[i][1]
        else:
            dp[i+1] += dp[i]+max(s[i])
    print(dp[l])


if __name__ == "__main__":
    D()

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

def f(X):
    return sum([X ^ a for a in A])

if K==0:
    print(f(0))
else:
    l = len(bin(K)[2:])
    a_bit = [N - 2*sum([(a & (1<<i))>>i for a in A]) for i in range(l)]
    k_bit = [a > 0 for a in a_bit]
    k = 0
    for i,b in enumerate(k_bit[::-1],1):
        if b and (k + (1<<(l-i)) <= K):
            k += (1<<(l-i))
    print(f(k))

Solution (Python):
N, K = map(int, input().split())
Ai = list(map(int, input().split()))
 
D = 40
Bi = []
for a in Ai:
    b = bin(a)[2:]
    b = ("0" * (D - len(b))) + b
    Bi.append(b)
 
result = ""
for i in range(D):
    count = 0 
    for i, b in enumerate(Bi):
        if b[0] == '1':
            count += 1
        Bi[i] = b[1:]
    if count >= N / 2:
        result += "0"
    else:
        result += "1"
    
x = 0
for i in range(D):
    if result[0] == '1':
        a = pow(2, D - i - 1)
        if x + a <= K:
            x += a
    result = result[1:]
 
ans = 0
for a in Ai:
    ans += (a ^ x)
print(ans)

Solution (Python):
N, K = map(int, input().split())
Ai = list(map(int, input().split()))

D = 40 


Bi = []
for a in Ai:
    b = bin(a)[2:] 
    b = ("0" * (D - len(b))) + b
    Bi.append(b)

result = ""
for i in range(D):
    count = 0 
    for i, b in enumerate(Bi):
        if b[0] == '1':
            count += 1
        Bi[i] = b[1:]
    if count >= N / 2:
        result += "0"
    else:
        result += "1"
    count = count // 2
x = 0
for i in range(D):
    if result[0] == '1':
        a = pow(2, D - i - 1)
        if x + a <= K:
            x += a
    result = result[1:]

ans = 0
for a in Ai:
    ans += (a ^ x)
print(ans)

Solution (Python):
m, f = 0, 0
n, k = map(int, input().split())
a = list(map(int, input().split()))
for i in range(40, -1, -1):
  _sum = sum(map(lambda x: (x >> i) & 1, a))
  s = 0 if _sum > n//2 else 1 << i
  if not s or s > k - m:
    f += _sum << i
  else:
    f += n-_sum << i
    m += s
print(f)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
m, f = 0, 0
for i in range(40, -1, -1):
  _sum = sum(map(lambda x: (x >> i) & 1, a))
  _min = min(_sum, n-_sum) << i
  _max = max(_sum, n-_sum) << i
  s = 0 if _sum > n//2 else 1 << i
  if s > max(k - m, 0):
    f += _min
  elif s > 0:
    m += s
    f += _max
  else:
    f += _max
print(f)

Solution (Python):
n, k = map(int, input().split())
a = map(int, input().split())
_sum = [0]*41
m, f = 0, 0
for x in a:
  for i in range(41):
    _sum[i] += (x >> i) & 1
for i in range(40, -1, -1):
  s = 0 if _sum[i] > n//2 else 1 << i
  _min = min(_sum[i], n-_sum[i]) << i
  _max = max(_sum[i], n-_sum[i]) << i
  if s:
    m += s
    if m > k:
      m -= s
      f += _min
    else:
      f += _max
  else:
    f += _max
print(f)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
_sum = [0]*41
m = 0
f = 0
for x in a:
  for i in range(41):
    _sum[i] += (x >> i) & 1
for i in range(40, -1, -1):
  flg = 0 if _sum[i] > n//2 else 1
  s = flg << i
  m += s
  if m > k:
    m -= s
    f += min(_sum[i], n-_sum[i]) << i
  else:
    f += max(_sum[i], n-_sum[i]) << i
print(f)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
_sum = [0]*41
ans = 0
f = 0
 
for x in a:
  for i in range(41):
    _sum[i] += (x >> i) & 1
for i in range(41):
  if _sum[i] <= n//2:
    _sum[i] = 1
  else:
    _sum[i] = 0
for i in range(40, -1, -1):
  s = _sum[i] << i
  ans += s
  if ans > k:
    ans -= s
for x in a:
  f += x ^ ans
print(f)