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):
from sys import exit, stderr, stdin
input = stdin.readline


def debug(var, name="hoge"):
    print(str(type(var)) + name +":" + " = " + repr(var), file=stderr)
    return


def main():
    N, K = list(map(int,input().split()))
    K = bin(K)[2:].zfill(51)
    a = list(map(int,input().split()))
    for i in range(len(a)):
        a[i] = bin(a[i])[2:].zfill(51)
    
    memo = [0 for __ in range(51)]
    LT = False
    for i in range(51):
        val = 0
        br = 0
        
        for b in range(2 if LT or K[i] == '1' else 1):
            valtmp = 0
            for n in a:
                valtmp += b ^ int(n[i])
            if val < valtmp :
                br = b
                val = valtmp
        memo[i] = val
        LT = br < int(K[i]) or LT


    p = 1
    ans = 0
    for i in range(50,-1,-1):
        ans += p*memo[i]
        p *= 2
    print(ans)


if __name__ == "__main__":
    main()

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

c = [0] * 40

for i in range(40):
  for a in A:
    c[i] += (a >> i) & 1

X = 0
res = 0
for i in range(39, -1, -1):
  
  
  
  
  
  
  if c[i] < N - c[i] and X + (1 << i) <= K:
    X += (1 << i)
    res += (N - c[i]) * (1 << i)
  else:
    res += (c[i]) * (1 << i)

print(res)

Solution (Python):
n,k = map(int, input().split())
a = list(map(int, input().split()))
bita = [sum(1 for x in a if x&2**i) for i in range(41)]
bita2 = [max(x,n-x) for x in bita]
ans = 0
for i in range(41):
  if k&2**i:
    ans += bita2[i]*2**i
  else:
    ans += bita[i]*2**i
for bit in range(41):
  if not k&2**bit:
    continue
  x = ((k>>bit)<<bit) - 1
  c = 0
  for i in range(41):
    if x&2**i:
      c += bita2[i]*2**i
    else:
      c += bita[i]*2**i
  if c > ans:
    ans = c
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=[int(i) for i in input().split()]

m=40
f=[0]*m
b=False
ans=0
for i in a:
    for j in range(m):
        f[j]+=i>>j&1
for i in range(m)[::-1]:
    if f[i]>n//2:
        ans+=f[i]*2**i
        if k>>i&1:
            b=True
    else:
        if b or k>>i&1:
            ans+=(n-f[i])*2**i
        else:
            ans+=f[i]*2**i
print(ans)

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

MAXBIT = (10 ** 12).bit_length()
F = [0] * MAXBIT  

for a in A:
    for i in range(MAXBIT):
        F[i] += a >> i & 1

small = False
ans = 0
for i in range(MAXBIT-1, -1, -1):
    if F[i] > N//2:  
        ans += F[i] * 2 ** i
        if K >> i & 1:
            small = True
    elif F[i] <= N//2:  
        if small or K >> i & 1:
            ans += (N - F[i]) * 2 ** i
        else:
            ans += F[i] * 2 ** i

print(ans)

Solution (Python):
import numpy as np

n, k = map(int, input().split())
A = np.array(list(map(int, input().split())))

X = 0
for i in range(40, -1, -1):
    count_1 = np.sum((A >> i) & 1)
    count_0 = n - count_1

    if count_0 > count_1:
        X += 2**i
        if X > k:
            X -= 2 ** i
    else:
        continue

ans = np.sum(X ^ A)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline


N, K = map(int,input().split())
A = list(map(int, input().split()))
K=format(K,"040b")
K=list(K)

length = 40
keta = [[0,0] for _ in range(length)]

for i in range(N):
    A[i]=format(A[i], "040b")
    A[i]=list(A[i])


for i in range(length):
    cnt=0
    for j in range(N):
        if A[j][i] == "1": cnt+=1
    keta[i][0] = N-cnt
    keta[i][1] = cnt

max_sum = [0]*length
for i in range(length):
    max_sum[i] = max(keta[i][0],keta[i][1]) * 2**(length-1-i)

dp = [[0,0] for _ in range(length)]
if K[0] == "1":
    dp[0][1] = keta[0][1-int(K[0])]*2**(length-1)
    dp[0][0] = keta[0][1]*2**(length-1)+ sum(max_sum[1:])
else:
    dp[0][1] = keta[0][1]*2**(length-1)

for i in range(1,length):
    if K[i] == "1":
        dp[i][0] = dp[i-1][1] + keta[i][1]*2**(length-1-i)+ sum(max_sum[i+1:]) 
        dp[i][1] = dp[i-1][1] + keta[i][0]*2**(length-1-i)
    if K[i] == "0":
        dp[i][1] = dp[i-1][1] + keta[i][1]*2**(length-1-i)
    
ans = 0
for i in range(length):
    ans=max(ans,dp[i][0])
print(max(ans,dp[length-1][1]))

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    
    N,K = map(int, input().split())
    A = list(map(int, input().split()))
    A = sorted(A)
    B = []
    L = bin(K)
    flag = 1
    for _ in range(max(len(L)-2,len(bin(A[-1]))-2)):
        zero = 0
        one = 0
        flag = 0
        for i in range(N):
            if A[i] > 0:
                flag = 1
            if A[i]%2 == 0:
                zero += 1
            else:
                one += 1
            A[i] = A[i]//2
        B.append([zero,one])
    B = B[::-1]
    k = pow(2,len(B)-1)
    ans = 0
    tmp = K
    for i in range(len(B)):
        zero,one = B[i]
        if zero > one and tmp >= k:
            ans += zero*k
            tmp -= k
            k //= 2
        else:
            ans += one*k
            k //= 2
    print(ans)
    
main()

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

def func(A,X):
    out = 0
    for i in A:
        if i^X > i:
            out += 1
        else:
            out -= 1
    return (out > 0)
X = [0]
i = 1
while i <= K:
    if  func(A,i):
        X.append(i)
    i*=2

hoge = 0
X.reverse()
for i in X:
    if hoge + i <= K:
        hoge += i

ans = 0
for i in A:
    ans += i^hoge

print(ans)

Solution (Python):
import collections
def main():
    N, K = map(int, input().split())
    A = [int(a) for a in input().split()]

    ones = collections.defaultdict(int)
    max_i = 0
    for a in A:
        i = 0
        while a:
            ones[i] += a % 2
            max_i = max(max_i, i)
            a = a // 2
            i += 1

    if K > max(A):
        i = 0
        M = K
        while M:
            if i > max_i:
                ones[i] = 0
            M = M // 2
            i += 1

    gain = collections.defaultdict(dict)
    for i in ones:
        gain[i][0] = ones[i]*pow(2, i) 
        gain[i][1] = (N-ones[i])*pow(2, i) 

    
    b = format(K+1, "b")
    
    best = 0
    for i in range(len(b)):
        if b[i] == "1":
            s = b[:i] + "0"
            for j in range(i+1,len(b)):
                k = len(b)-1-j
                
                if gain[k][0] >= gain[k][1]:
                    s += "0"
                else:
                    s += "1"
                
            f = 0
            v = int(s, 2)
            for a in A:
                f += a^v
            
            best = max(best, f)

    return best


if __name__ == '__main__':
    print(main())

Solution (Python):
import sys
sys.setrecursionlimit(10**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 LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main(): 
    N, K = LI()
    A_li = LI()
    最上位桁 = len(bin(K))-3
    x = ''

    one_to_zero = False
    
    for i in range(最上位桁, -1, -1):
        
        counter = {0: 0, 1: 0}
        for a in A_li:
            if a & (1<<i):
                counter[1] += 1
            else:
                counter[0] += 1
        if counter[0] > counter[1]:
            
            
            if K & (1<<i):
                x += '1'
            
            elif one_to_zero:
                x += '1'
            
            else:
                x += '0'
        else:
            
            
            x += '0'
            if K & (1<<i):
                one_to_zero = True

    x = int(x, 2)
    if K == 0: x = 0

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

main()

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

np = [0]*40

for i in range(40):
  for j in A:
    if j&(1<<i):
      np[i] += 1

v = 0
for i in range(39,-1,-1):
  if v + (1<<i) > K:
    continue
  elif np[i] < N - np[i]:
    v += (1<<i)
    
ans = 0
for i in A:
  ans += (v^i)

print(ans)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    def f(n):
        a = str(n)
        l = len(a)
        dp = [[[0]*2 for i in range(2)] for j in range(l+1)]
        dp[0][0][0] = 1
        for i in range(l):
            ni = i+1
            for j in range(2):
                x = 9 if j else int(a[i])
                for k in range(2):
                    for d in range(x+1):
                        nj = j|(d < int(a[i]))
                        nk = k|(d in (4,9))
                        dp[ni][nj][nk] += dp[i][j][k]
        return dp[l][0][1]+dp[l][1][1]

    a,b = LI()
    print(f(b)-f(a-1))
    return


def B():
    D = I()
    n = list(map(int, input()))
    l = len(n)
    dp = [[[0]*2 for i in range(D)] for j in range(l+1)]
    dp[0][0][0] = 1
    for i in range(l):
        ni = i+1
        for j in range(D):
            for k in range(2):
                x = 9 if k else int(n[i])
                for d in range(x+1):
                    nj = (j+d)%D
                    nk = k|(d < int(n[i]))
                    dp[ni][nj][nk] += dp[i][j][k]
                    dp[ni][nj][nk] %= mod
    print((dp[l][0][0]+dp[l][0][1]-1)%mod)
    return


def C():
    n = list(map(int, input()))
    l = len(n)
    dp = [[[0]*2 for j in range(l+1)] for i in range(l+1)]
    dp[0][0][0] = 1
    for i in range(l):
        ni = i+1
        for j in range(l):
            for k in range(2):
                x = 9 if k else n[i]
                for d in range(x+1):
                    nj = j+(d == 1)
                    nk = k|(d < n[i])
                    dp[ni][nj][nk] += dp[i][j][k]
    ans = 0
    for i in range(l+1):
        ans += i*sum(dp[l][i])
    print(ans)
    return


def D():
    n = list(map(int, input()))
    l = len(n)
    D = I()
    dp = [[[0]*2 for i in range(D)] for i in range(l+1)]
    dp[0][0][0] = 1
    for i in range(l):
        for j in range(D):
            for k in range(2):
                x = 9 if k else n[i]
                for d in range(x+1):
                    ni = i+1
                    nj = (j+d)%D
                    nk = k|(d<n[i])
                    dp[ni][nj][nk] += dp[i][j][k]
                    dp[ni][nj][nk] %= mod
    print((sum(dp[l][0])-1)%mod)
    return


def E():
    n,k = LI()
    a = LI()
    m = max(k,max(a)).bit_length()
    f = [0]*m
    for i in a:
        for j in range(m):
            if i&(1<<j):
                f[j] += 1
    dp = [[-float("inf")]*2 for i in range(m+1)]
    dp[0][0] = 0
    for i in range(m):
        ni = i+1
        ki = (k>>(m-i-1))&1
        for j in range(2):
            x = 1 if j else ki
            for d in range(x+1):
                nj = j|(d<ki)
                dp[ni][nj] = max(dp[ni][nj], dp[i][j]+(d*(n-f[m-i-1])+(1-d)*f[m-i-1])*(1<<(m-i-1)))
    print(max(dp[m]))
    return


def F():
    def bit_count(n):
        return bin(n).count("1")
    a,K = LI()
    a = list(map(int, str(a)))
    l = len(a)
    m = 1024
    dp = [[[float("inf")]*2 for j in range(m)] for i in range(l+1)]
    dp[0][0][0] = 0
    for i in range(l):
        ni = i+1
        for j in range(m):
            if bit_count(j) > K:continue
            for k in range(2):
                x = 9 if k else a[i]
                for d in range(x+1):
                    nj = j|(1<<d)
                    nk = k|(d<a[i])
                    c = dp[i][j][k]*10+a[i]-d
                    if c < dp[ni][nj][nk]:
                        dp[ni][nj][nk] = c
    ans = float("inf")
    for i in range(m):
        if bit_count(i) <= K:
            mi = min(dp[l][i])
            if mi < ans:
                ans = mi
    dp = [[[float("inf")]*2 for j in range(m)] for i in range(l+1)]
    dp[0][0][0] = 0
    for i in range(l):
        ni = i+1
        for j in range(m):
            if bit_count(j) > K:continue
            for k in range(2):
                x = 0 if k else a[i]
                for d in range(x,10):
                    nj = j|(1<<d)
                    nk = k|(d>a[i])
                    c = dp[i][j][k]*10+d-a[i]
                    if c < dp[ni][nj][nk]:
                        dp[ni][nj][nk] = c
    for i in range(m):
        if bit_count(i) <= K:
            mi = min(dp[l][i])
            if mi < ans:
                ans = mi
    print(ans)
    return


def G():

    return


def H():

    return


def I_():

    return


def J():

    return


def K():

    return


if __name__ == "__main__":
    E()

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    def f(n):
        a = str(n)
        l = len(a)
        dp = [[[0]*2 for i in range(2)] for j in range(l+1)]
        dp[0][0][0] = 1
        for i in range(l):
            ni = i+1
            for j in range(2):
                x = 9 if j else int(a[i])
                for k in range(2):
                    for d in range(x+1):
                        nj = j|(d < int(a[i]))
                        nk = k|(d in (4,9))
                        dp[ni][nj][nk] += dp[i][j][k]
        return dp[l][0][1]+dp[l][1][1]

    a,b = LI()
    print(f(b)-f(a-1))
    return


def B():
    D = I()
    n = list(map(int, input()))
    l = len(n)
    dp = [[[0]*2 for i in range(D)] for j in range(l+1)]
    dp[0][0][0] = 1
    for i in range(l):
        ni = i+1
        for j in range(D):
            for k in range(2):
                x = 9 if k else int(n[i])
                for d in range(x+1):
                    nj = (j+d)%D
                    nk = k|(d < int(n[i]))
                    dp[ni][nj][nk] += dp[i][j][k]
                    dp[ni][nj][nk] %= mod
    print((dp[l][0][0]+dp[l][0][1]-1)%mod)
    return


def C():
    n = list(map(int, input()))
    l = len(n)
    dp = [[[0]*2 for j in range(l+1)] for i in range(l+1)]
    dp[0][0][0] = 1
    for i in range(l):
        ni = i+1
        for j in range(l):
            for k in range(2):
                x = 9 if k else n[i]
                for d in range(x+1):
                    nj = j+(d == 1)
                    nk = k|(d < n[i])
                    dp[ni][nj][nk] += dp[i][j][k]
    ans = 0
    for i in range(l+1):
        ans += i*sum(dp[l][i])
    print(ans)
    return


def D():
    n = list(map(int, input()))
    l = len(n)
    D = I()
    dp = [[[0]*2 for i in range(D)] for i in range(l+1)]
    dp[0][0][0] = 1
    for i in range(l):
        for j in range(D):
            for k in range(2):
                x = 9 if k else n[i]
                for d in range(x+1):
                    ni = i+1
                    nj = (j+d)%D
                    nk = k|(d<n[i])
                    dp[ni][nj][nk] += dp[i][j][k]
                    dp[ni][nj][nk] %= mod
    print((sum(dp[l][0])-1)%mod)
    return


def E():
    n,k = LI()
    a = LI()
    m = max(k,max(a)).bit_length()
    f = [0]*m
    for i in a:
        for j in range(m):
            if i&(1<<j):
                f[j] += 1
    dp = [[-float("inf")]*2 for i in range(m+1)]
    dp[0][0] = 0
    for i in range(m):
        ni = i+1
        ki = (k>>(m-i-1))&1
        for j in range(2):
            x = 1 if j else ki
            for d in range(x+1):
                nj = j|(d<ki)
                dp[ni][nj] = max(dp[ni][nj], dp[i][j]+(d*(n-f[m-i-1])+(1-d)*f[m-i-1])*(1<<(m-i-1)))
    print(max(dp[m]))
    return


def F():

    return


def G():

    return


def H():

    return


def I_():

    return


def J():

    return


def K():

    return


if __name__ == "__main__":
    E()

Solution (Python):
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:  
    for i in range(40):
        if a&(1<<i):
            tmp[i]+=1

a = 0  
for i in range(39,-1,-1):
	if a + (1<<i) <= K and tmp[i] < (N+1)//2:
		a += (1<<i)



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

Solution (Python):
n, k, *a = map(int, open(0).read().split())
b = [0]*49
for i in a:
    c = bin(i)[::-1][:-2]
    for j in range(len(c)):
        b[j] += int(c[j])
d = [int(i < n-i) for i in b]
*e, = bin(k)[2:]
*e, = map(int, e)
d = d[:len(e)][::-1]
for i in range(len(e)):
    d[i] = d[i]&e[i]
    if d[i] < e[i]: break
*d, = map(str, d)
f = int(''.join(d), 2)
print(sum(f^i for i in a))

Solution (Python):
n, k, *a = map(int, open(0).read().split())
b = [0]*49
for i in a:
    c = bin(i)[::-1][:-2]
    for j in range(len(c)):
        b[j] += int(c[j])
d = [int(i < n-i) for i in b]
*e, = bin(k)[2:]
*e, = map(int, e)
d = d[:len(e)][::-1]
for i in range(len(e)):
    d[i] = d[i]&e[i]
    if d[i] < e[i]: break
*d, = map(str, d)
f = int(''.join(d), 2)
print(sum(f^i for i in a))

Solution (Python):
N,K = map(int,input().split())
vote = [0 for i in range(41)]
for i,x in enumerate(input().split()):
    for j,y in enumerate(str(bin(int(x)))[2:][::-1]):
        vote[40-j] += int(y)

binK = [int(x) for x in str(bin(K))[2:]]
largest_list = [0 if x > N/2 else 1 for x in vote][-len(binK):]

for i,x in enumerate(largest_list):
    if binK[i] == 1 and x == 0:
        break
    if binK[i] == 0 and x == 1:
        largest_list[i] = 0

maxf=0

for i,x in enumerate(largest_list[::-1]):
    maxf += (2**i) * (N-vote[40-i]) if x==1 else (2**i) * vote[40-i]
for i in range(len(largest_list),41):
    maxf += (2**i) * vote[40-i]

print(int(maxf))

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

blist = [0] * 50
for i in range(N):
  bn = bin(A[i])
  for j in range(len(bn)):
    if bn[len(bn) - 1 - j] == '1':
      blist[j] += 1
      
K = bin(K)[2::]
digit = len(K)
dp = [[0, 0] for i in range(digit)]
t = 0
for i in range(digit, 45):
  t += blist[i] * (2 ** i)
if K != '0':
  dp[digit - 1][0] = t + blist[digit - 1] * 2 ** (digit - 1)
  dp[digit - 1][1] = t + (N - blist[digit - 1]) * 2 ** (digit - 1)
else:
  dp[digit - 1] = [t, t]
for i in reversed(range(0, digit - 1)):
  if K[len(K) - i - 1] == '1':
    dp[i][1] = dp[i + 1][1] + (N - blist[i]) * (2 ** i)
    dp[i][0] = max([dp[i + 1][1] + blist[i] * (2 ** i),
                    dp[i + 1][0] + max([blist[i], N - blist[i]]) * (2 ** i)])
  else:
    dp[i][1] = dp[i + 1][1] + blist[i] * (2 ** i)
    dp[i][0] = dp[i + 1][0] + max([blist[i], N - blist[i]]) * (2 ** i)
    
print(max(dp[0]))

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

b=[0]*40
for a in A:
    for k in range(40):
        if a&(1<<k):
            b[k]+=1

x=0
for k in range(39,-1,-1):
    if x+(1<<k)>K:
        continue
    if b[k]<(N+1)//2:
        x+=(1<<k)
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()))

b=[0]*40
for a in A:
    for k in range(40):
        if a&(1<<k):
            b[k]+=1

x=0
for k in range(39,-1,-1):
    if x+(1<<k)>K:
        continue
    if b[k]<(N+1)//2:
        x+=(1<<k)
ans=0
for a in A:
    ans+=x^a

print(ans)

Solution (Python):

import bisect
import collections
import itertools


def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]

n,k=getints()
vs=getints()

cache=array2d(65, 2, -1)

def solve(pos, is_small):
    if pos < 0: return 0
    res = cache[pos][is_small]
    if res >= 0: return res
    res1, res2 = 0, 0
    val = 1 << pos
    for v in vs:
        m = v >> pos & 1
        res1 += m * val 
        res2 += (1 - m) * val 

    if is_small:
        res = max(res1, res2) + solve(pos - 1, True)
    else:
        kbit = k >> pos & 1
        if kbit == 0:
            res1 += solve(pos - 1, False)
            res2 = 0
        else:
            res1 += solve(pos - 1, True)
            res2 += solve(pos - 1, False)
        res = max(res1, res2)

    cache[pos][is_small] = res
    return res

print(solve(64, False))

Solution (Python):
import sys


INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def parse(args, *params):
    return tuple(p(v) for p, v in zip(params, next(args).split()))
def debug(*v):
    if __debug__:
        print(*v, file=sys.stderr)


def main(args):
    N, K = parse(args, int, int)
    A = tuple(map(int, next(args).split()))

    MAX_DIG = 45
    bind, mask = True, 0
    for dig in reversed(range(MAX_DIG)):
        count = [0, 0]
        for a in A:
            count[(a & 1 << dig) != 0] += 1
        if bind:
            if count[1] < count[0]:
                if K & (1 << dig):
                    mask += 1 << dig
            else:
                if K & (1 << dig):
                    bind = False
        else:
            if count[1] < count[0]:
                mask += 1 << dig
    debug(mask)
    res = 0
    for a in A:
        res += a ^ mask
    print(res)

if __name__ == '__main__':
    main(iter(sys.stdin.read().split("\n")))

Solution (Python):
import sys


INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def parse(args, *params):
    return tuple(p(v) for p, v in zip(params, next(args).split()))
def debug(*v):
    if __debug__:
        print(*v, file=sys.stderr)


def main(args):
    N, K = parse(args, int, int)
    A = tuple(map(int, next(args).split()))

    MAX_DIG = 60
    bind, mask = True, 0
    for dig in reversed(range(MAX_DIG)):
        count = [0, 0]
        for a in A:
            count[(a & 1 << dig) != 0] += 1
        if bind:
            if count[1] < count[0]:
                if K & (1 << dig):
                    mask += 1 << dig
            else:
                if K & (1 << dig):
                    bind = False
        else:
            if count[1] < count[0]:
                mask += 1 << dig
    debug(mask)
    res = 0
    for a in A:
        res += a ^ mask
    print(res)

if __name__ == '__main__':
    main(iter(sys.stdin.read().split("\n")))

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

sum_bin = [0] * 41

for a in As:
  a_binary = format(a, 'b')
  for j in range(len(a_binary)):
    tmp = a >> j
    
    if tmp & 1:
      sum_bin[j] += 1



def f(x):
  c = 0
  for i in range(40, -1, -1):
    
    if x >> i & 1:
      c += (N-sum_bin[i])*2**i
    else:
      c += sum_bin[i]*2**i
  return c

g_X = 0
for i in range(40, -1, -1):
  if sum_bin[i] <= N - sum_bin[i]:
    g_X += 2 ** i 
    
ans = f(0)
l = len(format(K, 'b'))

for i in range(l):
  
  if K >> i & 1:
    c = ''
    if K >> i+1 != 0:
      c = format(K >> i+1, 'b')
    c += '0'+format(g_X, 'b')[41-i:]
    
    
    ans = max(ans, f(int(c, 2)))
ans = max(ans, f(K))
print(ans)

Solution (Python):
N, K = map(int, input().split())
A = [int(i) for i in input().split()]

sum_bin = [0 for _ in range(41)]  

for a in A:
    for j in range(len(format(a, 'b'))):
        if a >> j & 1:
            sum_bin[j] += 1



def f(x):
    c = 0
    for i in range(40, -1, -1):
        if x >> i & 1:
            c += (N-sum_bin[i])*2**i
        else:
            c += sum_bin[i]*2**i
    return c



g_X = 0
for i in range(40, -1, -1):
    if sum_bin[i] <= N-sum_bin[i]:
        g_X += 2**i



ans = f(0)

l = len(bin(K))-2

for i in range(l):  
    if K >> i & 1:
        c = ''
        if K >> i+1 != 0:
            c = format(K >> i+1, 'b')
        c += '0'+bin(g_X)[43-i:]
        
        ans = max(ans, f(int(c, 2)))
ans = max(ans, f(K))
print(ans)

Solution (Python):
N, K = map(int, input().split())
A = [int(i) for i in input().split()]

sum_bin = [0 for _ in range(41)]  

for a in A:
    for j in range(len(format(a, 'b'))):
        if a >> j & 1:
            sum_bin[j] += 1



def f(x):
    c = 0
    for i in range(40, -1, -1):
        if x >> i & 1:
            c += (N-sum_bin[i])*2**i
        else:
            c += sum_bin[i]*2**i
    return c



g_X = 0
for i in range(40, -1, -1):
    if sum_bin[i] < N-sum_bin[i]:
        g_X += 2**i



ans = f(0)

l = len(bin(K))-2

for i in range(l):  
    if K >> i & 1:
        c = ''
        if K >> i+1 != 0:
            c = format(K >> i+1, 'b')
        c += '0'+bin(g_X)[43-i:]
        
        ans = max(ans, f(int(c, 2)))
ans = max(ans, f(K))
print(ans)

Solution (Python):

import sys, math, fractions, itertools


def solve(n: int, k: int, a: "List[int]"):
    num = 2 ** (max(int(math.log2(max(a))) + 1, int(math.log2(k + 0.0001)) + 1))
    num2 = int(math.log2(num))
    for i in range(n):
        a[i] = bin(a[i] + num)[3:]
    binary = [[i,0,0] for i in range(num2)]
    for i in range(n):
        for j in range(num2):
            if a[i][j] == "0":
                binary[num2 - j - 1][1] += 1
            else:
                binary[num2 - j - 1][2] += 1
    binary.sort(reverse = True)
    ans = 0
    check = 0
    for i in range(len(binary)):
        if binary[i][1] > binary[i][2]:
            check += 2 ** binary[i][0]
            if check <= k:
                ans += binary[i][1] * (2 ** binary[i][0])
            else:
                check -= 2 ** binary[i][0]
                ans += binary[i][2] * (2 ** binary[i][0])
        else:
            ans += binary[i][2] * (2 ** binary[i][0])
    print(ans)
    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 math

n, k = map(int, input().split())
a = list(map(int, input().split()))
num = 2 ** (max(int(math.log2(max(a))) + 1, int(math.log2(k + 0.0001)) + 1))
num2 = int(math.log2(num))
for i in range(n):
    a[i] = bin(a[i] + num)[3:]
binary = [[i,0,0] for i in range(num2)]
for i in range(n):
    for j in range(num2):
        if a[i][j] == "0":
            binary[num2 - j - 1][1] += 1
        else:
            binary[num2 - j - 1][2] += 1
binary.sort(reverse = True)
ans = 0
check = 0
for i in range(len(binary)):
    if binary[i][1] > binary[i][2]:
        check += 2 ** binary[i][0]
        if check <= k:
            ans += binary[i][1] * (2 ** binary[i][0])
        else:
            check -= 2 ** binary[i][0]
            ans += binary[i][2] * (2 ** binary[i][0])
    else:
        ans += binary[i][2] * (2 ** binary[i][0])
print(ans)

Solution (Python):
n,k = map(int,input().split())
a = [int(_) for _ in input().split()]

dp = [[-1,-1] for i in range(51)]
dp[50][0] = 0

for i in range(50)[::-1]:
    mask = 1<<i
    num = 0
    for j in range(n):
        num += (a[j]>>i)&1
    cost0 = num * mask
    cost1 = (n-num)*mask
    if not (k>>i)&1:
        dp[i][0] = dp[i+1][0] + cost0
        if dp[i+1][1]!=-1: dp[i][1] = dp[i+1][1] + max(cost0,cost1)
    if (k>>i)&1:
        dp[i][0] = dp[i+1][0] + cost1
        if dp[i+1][1] != -1:
            dp[i][1] = max(dp[i+1][0] + cost0,dp[i+1][1]+max(cost0,cost1))
        else:
            dp[i][1] = cost0
print(max(dp[0][0],dp[0][1]))

Solution (Python):
import numpy as np
from functools import lru_cache

N, K = map(int, input().split())
A = np.array(input().split(), dtype = np.int64)

bit_cnt = [((A >> k) & 1).sum() for k in range(40)]

@lru_cache(maxsize = 10 ** 6)

def F(K, k = 39):
    
    if k == -1: 
        return 0
    bit = 1 << k
    
    x = 0
    if K >= bit:
        x = ((N - bit_cnt[k]) << k) + F(K - bit, k - 1)
    
    y = (bit_cnt[k] << k) + F(min(K, bit - 1), k - 1)
    return max(x, y)

ans = F(K)
print (ans)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
ones = [0] * 40
for i in A:
    for j in range(40):
        if (i >> j) & 1:
            ones[j] += 1
x = 0
for i in range(39, -1, -1):
    if x + (1 << i) > K:
        continue
    elif ones[i] < N - ones[i]:
        x += (1 << i)

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

print (ans)

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

c = [0] * 10000
for aa in a:
    ba = format(aa, 'b')
    ba = ba[::-1]
    for i in range(len(ba)):
        if ba[i] == '1':
            c[i] += 1
d = ''
for cc in c:
    if cc <= (n-1) //2:
        d += '1'
    else:
        d += '0'


kb = format(k, 'b')
can = False
kk = ''
d = d[:len(kb)]
d = d[::-1]
for i in range(len(kb)):
    tmp = kb[i]
    if tmp == '1' and d[i] == '0':
        tmp = '0'
        can = True
    elif tmp == '0' and d[i] == '1' and can:
        tmp = '1'
    kk += tmp
x = int(kk, 2)
ans = 0
for aa in a:
    ans += (x ^ aa)
print(ans)