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):
import sys
fin = sys.stdin.readline


N, K = [int(elem) for elem in fin().split()]
A_list = [int(elem) for elem in fin().split()]
assert len(A_list) == N

n_digits_K = 0
cur_K = K
while cur_K > 0:
    n_digits_K += 1
    cur_K >>= 1



count_1_by_digit = [0] * n_digits_K
for A in A_list:
    i = 0
    while A > 0 and i < len(count_1_by_digit):
        count_1_by_digit[i] += A & 1
        i += 1
        A >>= 1


X = 0
for i in reversed(range(len(count_1_by_digit))):
    count = count_1_by_digit[i]
    if count <= (N - 1) // 2:  
        X += (1 << i)
        if X > K:
            X -= (1 << i)


f = 0
for A in A_list:
    f += A ^ X
print(f)

Solution (Python):
import math

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

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

for a in A:
    for i in range(41):
        bit[i]+= 1 if (a & (0b1<<i))>0 else 0

ans = 0
x = 0
bit.reverse()

for i,b in zip(range(40, -1, -1), bit):


    if b<(N-b) and x+2**i <= K:
        ans += (N-b)*2**i
        x += 2**i                        
    else:
        ans += b*2**i

print(ans)

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
L=[0 for i in range(40)]
for i in range(N):
    A[i]+=2**40
    A[i]=bin(A[i])
    A[i]=A[i][3:]
    A[i]=list(A[i])

for i in range(len(A)):
    for j in range(len(A[0])):
        L[j]+=int(A[i][j])


ans=0
for i in range(40):
    if K>=2**(39-i):
        if L[i]<=(N/2):
            ans+=(2**(39-i))*(N-L[i])
            K-=2**(39-i)
        else:
            ans+=(2**(39-i))*(L[i])
    else:
        ans+=(2**(39-i))*(L[i])
print(ans)

Solution (Python):
from math import log

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

x = 0
if K != 0:
    for i in range(int(log(K, 2)), -1, -1):
        cnt_zero = 0
        cnt_one = 0
        for a in A:
            digit = (a >> i) & 1
            if digit == 0:
                cnt_zero += 1
            else:
                cnt_one += 1

        x += 2**i if cnt_zero > cnt_one else 0
        if x > K:
            x -= 2**i

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()))

keta = 0
kc = k
while kc != 0:
    kc = kc//2
    keta += 1

i = keta-1
num = 0
res = 0

while i != -1:
    num *= 2
    count0 = 0
    count1 = 0
    for j in a:
        if j & pow(2, i) == 0:
            count0 += 1
        else:
            count1 += 1
    if count0 >= count1 and k >> i >= num+1:
        num += 1
    i -= 1
    
for i in a:
    res += num ^ i
    
print(res)

Solution (Python):

from functools import reduce
from operator import xor,add
from collections import Counter

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

mlist = [0]
powlist = []
bitcount = Counter()
bitunseencount = {}
s = 0
x = 2**s

while x <= M:
    powlist.append(x)
    s += 1
    x = 2**s

for a in A:
    for i,p in enumerate(powlist):
        bitcount[i] += (a&(1<<i))>>i

for i, p in enumerate(powlist):
    bitunseencount[i] = N - bitcount[i]

importance = []
for i,p in enumerate(powlist):
    importance.append( (1<<i) * bitcount[i])

unseenimportance = []
for i,p in enumerate(powlist):
    unseenimportance.append( (1<<i) * bitunseencount[i])


MASK = 0
for i,p in enumerate(powlist):
    if unseenimportance[i] > importance[i]:
        if M >= MASK|(1<<i):
            MASK |= 1<<i

ans = reduce(add, map(lambda x: x^MASK, A))

MASK = 0
for i,p in list(enumerate(powlist))[::-1]:
    
    if unseenimportance[i] > importance[i]:
        if M >= MASK|(1<<i):
            MASK |= 1<<i

ans = max(ans, reduce(add, map(lambda x: x^MASK, A)))
print(ans)
exit(0)

Solution (Python):
def read():
    return int(input())


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


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


N, K = readmap()
A = readlist()

cnt = [0] * 50  

for a in A:
    i = 0
    while a:
        if a % 2:
            cnt[i] += 1
        i += 1
        a = a // 2

is_zero = [False] * 50

for i in range(50):
    if 2 * cnt[i] >= N:
        is_zero[i] = True

candidate = [0]

for i in range(49):
    if 2**i <= K:
        c = 0
        for j in reversed(range(i+1)):
            if (not is_zero[j]) and c + 2 ** j <= K:
                c += 2 ** j
        candidate.append(c)

can = [sum([c ^ a for a in A]) for c in candidate]

print(max(can))

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

n = 41

B = [0 for _ in range(n)]
result = [0 for _ in range(n)]

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



result = 0
k_rest = K

for i in list(range(n))[::-1]:
    if B[i] * 2 < N and (k_rest >= (1 << i)):
        k_rest = k_rest - (1 << i)
        result += (1 << i) * (N - B[i])
    else:
        result += (1 << i) * B[i]

print(result)

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

Knum =[0]*40
K2 = 0 

while K != 0:
    Knum[K2] = K%2
    K = K//2
    K2 += 1
K2 -= 1
if K2 == -1:
    K2 = 0

num =[0]*40
for i in input().split():
    A =int(i)
    m = 0
    while A!=0:
        if A%2==1:
            num[m] += 1
        A = A//2
        m += 1

fmax =[0]*40
for i,j in enumerate(num):
    fmax[i] = fmax[i-1] +(2**i)*max(j,N-j)

ans =[]
karians = 0
for i in range(40):
    j =39 - i
    if Knum[j] == 0:
        karians += (2**j)*num[j]
    elif j !=0:
        ans.append(karians+(2**j)*num[j]+fmax[j-1])
        karians += (2**j)*(N-num[j])
    else:
        ans.append(karians+(2**j)*num[j])
        karians += (2**j)*(N-num[j])
ans.append(karians)
print(max(ans))

Solution (Python):
n, k = [int(i) for i in input().strip().split(" ")]
As = [int(i) for i in input().strip().split(" ")]
M = len(bin(max(As+ [k]))[2:])
As = ['0' * (M - len(bin(int(i))[2:])) + bin(int(i))[2:] for i in As]
As = [[int(i) for i in a] for a in As]
As = list(zip(*As))
k = '0' * (M - len(bin(int(k))[2:])) + bin(int(k))[2:]
ok = False
result = 0
for i, a in enumerate(As):
    x = sum(a)
    if x > n / 2:
        y = "0"
    elif x - n/2 == 0.0:
        y = "0"
    else:
        y = "1"
    if ok:
        if y == "1":
            result += (n - x) * pow(2, (M - i - 1))
        else:
            result += x * pow(2, (M - i - 1))
        continue
    if k[i] == "0" and y == "1":
        y = "0"
    if y == "1":
        result += (n - x) * pow(2, (M - i - 1))
    else:
        result += x * pow(2, (M - i - 1))
    if k[i] == "1" and y == "0":
        ok = True
print(result)

Solution (Python):
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, K] = [long(input_raw[0]), long(input_raw[1])]
    input_raw = read_func().strip().split()
    A = []
    for i in range(N):
        A.append(long(input_raw[i]))

    max_x = 0L
    for i in range(41):
        mask = 0x01 << i
        one_count = 0
        for j in range(len(A)):
            one_count += 1 if (A[j] &  mask ) > 0 else 0
        if one_count <= N/2:
            max_x += (0x01 << i)

    x = 0L
    flag = False
    for i in range(40, -1, -1):
        mask = 0x01 << i
        if K & mask != 0 & flag == False:
            if (max_x & mask) == 0:
                flag = True
            else:
                x += (max_x & mask)
        elif flag == True:
            x += (max_x & mask)

    fx = 0L
    for i in range(len(A)):
        fx += (x ^ A[i])

    print fx






if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split(" "))
A = list(map(int, input().split(" ")))
Abin = map(bin, A)
ans_length = max(len(bin(K)), len(bin(max(A)))) - 2
one_counter = [0 for i in range(ans_length)]
for abin in Abin:
    rabin = abin[::-1]
    for i, c in enumerate(rabin):
        if ans_length <= i:
            break
        if c == "1":
            one_counter[i] += 1
        elif c == "b":
            break
now = 0
border = N/2
ans = 0

for i, n in enumerate(one_counter[::-1]):

    temp = now
    point = 2**(ans_length - i - 1)
    if n >= border:
        ans += n*point
    else:
        temp += point
        if temp <= K:
            now = temp
            ans += (N-n)*point
        else:
            ans += n*point

print(ans)

Solution (Python):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
if k == 0:
    print(sum(a))
    exit()
max_ = len(bin(10**12))-2

bins = [[0,0] for i in range(max_)]
for i in a:
    s = bin(i)[2:][::-1]
    for ind in range(max_):
        if ind >= len(s):
            bins[ind][0] += 1
        else:
            if s[ind] == "1":
                bins[ind][1] += 1
            else:
                bins[ind][0] += 1
keta = len(bin(k))-2
ans = 0
def DFS(now_point, now_keta, can_all=False):
    global ans
    if now_keta < 1:
        ans = max(ans, now_point)
        return
    if can_all:
        for i in range(now_keta):
            now_point += max(bins[i])*2**i
        ans = max(ans, now_point)
        return
    now = bin(k)[2:][::-1][now_keta-1]
    if now == "0":
        new_point = now_point + bins[now_keta-1][1]*2**(now_keta-1)
        DFS(new_point, now_keta-1, can_all=False)
    else:
        new_point_0 = now_point + bins[now_keta-1][1]*2**(now_keta-1)
        DFS(new_point_0, now_keta-1, can_all=True)
        new_point_1 = now_point + bins[now_keta-1][0]*2**(now_keta-1)
        DFS(new_point_1, now_keta-1, can_all=False)
DFS(0, keta, False)
for i in range(keta, max_):
    ans += bins[i][1]*(2**i)
print(ans)

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

t = 0
while (k >> t) != 0:
    t += 1

cnts = [0] * t
for i in range(n):
    for j in range(t):
        if (a[i] & (1 << j)) != 0:
            cnts[j] += 1

x = 0
for i in reversed(range(t)):
    p = cnts[i] 
    if p < n - p and (x | (1 << i)) <= k:
        x = x | (1 << i)

print(sum(x ^ p for p in a))

Solution (Python):
N,K = 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]))

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

k = K
d = 0
while k > 0:
    d += 1
    k = k >> 1

bset = 0x0
for i in range(d-1, -1, -1):
    b = 0x1 << i
    bcnt0 = 0
    for j in range(len(A)):
        if A[j] & b == 0:
            bcnt0 += 1
    
    if bcnt0 > len(A)//2 and bset | b <= K:
        bset |= b

    
    

X = bset
ans = 0
for i in range(len(A)):
    ans += A[i] ^ X

print(ans)

Solution (Python):
from collections import deque
import math

if __name__ == '__main__':
    n, k = map(int, input().split())
    a = [int(s) for s in input().split()]
    st = deque()

    if k == 0:
        print(sum(a))
        quit(0)

    log2k = int(math.log2(k))

    i = log2k
    while i >= 0 or any(x > 0 for x in a):
        lbits = [x & 1 for x in a]
        a = [x >> 1 for x in a]
        zeros = lbits.count(0)
        st.appendleft(zeros)
        i -= 1

    ans = 0
    mask = 1 << max(log2k, len(st) - 1)
    for zeros in st:
        ans <<= 1
        if zeros > n - zeros and k >= mask:
            ans += zeros
            k -= mask
        else:
            ans += n - zeros
        mask >>= 1
    print(ans)

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


def int2bin(n):
    return '{:040b}'.format(n)
B = list(map(int2bin, A))

Kbin = int2bin(K)
res = 0
flag = False
for i in range(40):
    cnt1 = 0
    for elem in B:
        if elem[i] == '1':
            cnt1 += 1
    if flag:
        if cnt1 <= N//2:
            res += 2**(39-i)
        continue
    if cnt1 <= N//2 and Kbin[i] == '1':
        res += 2**(39-i)
    else:
        if Kbin[i] == '1':
            flag = True

ans = 0
for elem in A:
    ans += res ^ elem
print(ans)

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys


def func(As, x):
    ans = 0
    for a in As:
        ans += a ^ x
    return ans


def add_bit(bits, idx):
    ret = bits[:]
    ret[idx] = 1
    return ret


def arr_to_num(arr):
    num_str = ''.join([str(x) for x in arr])
    return int(num_str, 2)
    

def solve(N, K, As):
    if K == 0:
        return func(As, 0)

    best_x = 0
    IDX_MAX = 42
    idx = IDX_MAX
    bits = [0] * (idx + 1)
    while idx >= 0:
        if arr_to_num(add_bit(bits, IDX_MAX - idx)) > K:
            pass
        else:
            one = 0
            zero = 0
            for a in As:
                if a & (1 << idx): one += 1
                else: zero += 1
            if one >= zero:
                bits[IDX_MAX - idx] = 0
            else:
                bits[IDX_MAX - idx] = 1
        idx -= 1

    best_x = arr_to_num(bits)
    return func(As, best_x)


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

Solution (Python):

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

DP = [0]*41

for i in range(N):
    for j in range(41):
        DP[j] += (A[i] >> j)&1

NEED = [DP[i] < N//2 for i in range(41)]

default = [(N - DP[i]) * (2**i) if (K>>i)&1 == 1 else DP[i] * (2**i) for i in range(41)]
free = [max(DP[i] * (2**i), (N-DP[i])*(2**i)) for i in range(41)]

ans = sum([K^A[i] for i in range(N)])
for i in range(41):
    if (K >> i)&1 == 1:
        ans = max(ans,
                  sum(free[:i]) + DP[i]*(2**i) + sum(default[i+1:]))
print(ans)

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

V = [0]*41
for a in A:
    for i in range(40,-1,-1):
        if a%2 == 0:
            pass
        else:
            V[i] += 1
        a //= 2
        if a == 0:
            break
W = [0]*41
for i in range(40,-1,-1):
    if K%2 == 0:
        pass
    else:
        W[i] += 1
    K //= 2
    if K == 0:
        break
ans = 0
check = 0
for i in range(41):
    if W[i] == 1:
        if V[i] > N - V[i]:
            ans += (2**(40-i))*V[i]
            check = 1
        else:
            ans += (2**(40-i))*(N-V[i])
    else:
        if check == 0:
            ans += (2**(40-i))*V[i]
        else:
            if V[i] > N - V[i]:
                ans += (2**(40-i))*V[i]
            else:
                ans += (2**(40-i))*(N-V[i])
print( ans)

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


def slv2(N, K, A):
    ans = (-1, -1)
    for k in range(K+1):
        tmp = 0
        for a in A:
            tmp += k ^ a
        if ans[0] < tmp:
            ans = (tmp, k)
    
    print(ans)
    return ans[0]


@mt
def slv(N, K, A):
    max_k_A = max(max(A), K)
    
    mi = int(math.ceil(math.log2(max_k_A)))
    

    ans = 0
    ak = 0
    for i in range(mi-1, -1, -1):
        s0 = 0
        s1 = 0
        i1 = 1 << i
        for a in A:
            if ak + i1<= K:
                s1 += (a & i1) ^ i1
            s0 += (a & i1) ^ (0)
        ans += max(s0, s1)
        if s1 > s0:
            ak |= i1
    
    
    return ans


def main():
    N, K = read_int_n()
    A = read_int_n() 
    print(slv(N, K, A))

    
    
    
    
    
    
    


if __name__ == '__main__':
    main()

Solution (Python):

n,k=map(int,input().split())
a=list(map(int,input().split()))
v=[0 for i in range(42)]
for i in range(n):
    t=a[i]
    for j in range(42):
        v[j]+=t%2
        t=t>>1

x=0
tmp=2**42
for i in range(42):
    tmp//=2
    if v[41-i]<(n-v[41-i]):
        if x+tmp>k:
            continue
        else:
            x+=tmp
ans=0
for i in range(n):
    ans+=x^a[i]
print(ans)

Solution (Python):
N, K = (int(i) for i in input().split())
A = [int(i) for i in input().split()]
A = [format(a,'b').zfill(40) for a in A]
C = [0]*40
for i in range(40):
    cc = 0
    for a in A:
        keta = 39 - i
        if a[keta] == "1":
            cc += 1
    C[keta] = cc
ans = 0
for i in range(40):
    b = 39 - i
    if C[i] < N - C[i]:
        if K < 2**b:
            ans += (2**b * C[i])
        else:
            ans += (2**b * (N-C[i]))
            K -= 2**b
    else:
        ans += (2**b * C[i])
print(ans)

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

lk = k.bit_length()

nc = 0
for i in range(lk):
    c = 0
    for ai in a:
        if (ai >> i) & 1:
            c += 1
    if n - c > c:
        nc |= (1 << i)

for i in reversed(range(lk)):
    ki = (k >> i) & 1
    nci = (nc >> i) & 1
    if ki == nci:
        pass
    elif ki:
        break
    else:
        nc &= ~(1 << i)

print(sum(ai ^ nc for ai in a))

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

A.sort()
p = [1]*41

for i in range(40):
	p[i+1] = p[i]*2

small = False
ans = 0
x = 0
for i in range(41):
	t = p[40-i]
	if x+t>K:
		cnt = 0
		for j in range(N):
			if A[j]&t == t:
				cnt += 1
		ans += cnt*t
	else:
		cnt = 0
		for j in range(N):
			if A[j]&t == t:
				cnt += 1
		if cnt <= N/2:
			x += t
		ans += max(cnt,N-cnt)*t
print(ans)

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

b = [format(t, "045b") for t in a]


c = []
for i in range(45):
  q = 0
  for t in b:
    q += int(t[i])
  c += [q]


d = [int(t) for t in list(format(k, "045b"))]

m = []
for i in range(45):
  if d[i] == 0:
    continue
  s = 0
  for j in range(i):
    s += (2**(44-j))*(d[j]*(n-c[j])+(1-d[j])*c[j])
  s += (2**(44-i))*c[i]
  for j in range(i+1,45):
    s += (2**(44-j))*max(c[j],n-c[j])
  m += [s]

s = 0
for j in range(45):
  s += (2**(44-j))*(d[j]*(n-c[j])+(1-d[j])*c[j])
m += [s]
print(max(m))

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
xors = lambda x, y: sum([(a & x) ^ y for a in A])
memo = {}
def dp(flag, k):
  if k < 0:
    return 0
  if (flag, k) in memo:
    return memo[(flag, k)]
  x = 2 ** k
  ret = 0
  if flag:
    if K & x:
      
      ret = xors(x, x) + dp(True, k-1)
      
      tmp = xors(x, 0) + dp(False, k-1)
      ret = ret if ret > tmp else tmp
    else:
      
      ret = xors(x, 0) + dp(True, k-1)
  else:
    ret = xors(x, x) + dp(False, k-1)
    tmp = xors(x, 0) + dp(False, k-1)
    ret = ret if ret > tmp else tmp
  memo[(flag, k)] = ret
  return ret

print(dp(True, 40))

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
    A[i] = format(A[i], "041b")
one_count = [0 for i in range(41)]
for i in range(N):
    for j in range(41):
        if A[i][j] == "1":
            one_count[j] += 1
halfsize = (len(A)+1)//2
Xint = 0
X = ""
for i in range(41):
    if one_count[i] < halfsize and Xint + 2 ** (40-i) <= K:
        Xint += 2 ** (40-i)
        X += "1"
    else:
        X += "0"

ans = 0
for i in range(41):
    if int(X[i]) == 1:
        ans += 2**(40-i) * (N - one_count[i])
    else:
        ans += 2**(40-i) * one_count[i]

print(ans)

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

for i in range(50):
    if K<(1<<i):
        break

keta=i

NOW=1
ANS=0

for i in range(keta-1,-1,-1):
    
    if NOW==1 and K & (1<<i)!=0:
        
        A0=0
        A1=0
        for a in A:
            if a & (1<<i) ==0:
                A1+=(1<<i)
            else:
                A0+=(1<<i)

        if A1>A0:
            ANS+=(1<<i)
        else:
            ANS+=0
            NOW=0

    elif NOW==1 and K&(1<<i)==0:
        A0=0
        for a in A:
            if a & (1<<i) !=0:
                A0+=(1<<i)

        ANS+=0

    elif NOW==0:
        A0=0
        A1=0
        for a in A:
            if a & (1<<i) ==0:
                A1+=(1<<i)
            else:
                A0+=(1<<i)

        if A1>A0:
            ANS+=(1<<i)
        else:
            ANS+=0


SUM=0
for a in A:
    SUM+=ANS^a

print(SUM)

Solution (Python):
n, k = map(int, input().split())
aList = list(map(int, input().split()))
bits0 = [0] * 42
bits1 = [0] * 42
def getBit(a):
    mask = 1
    for i in range(42):
        if mask > a and mask > k:
            break
        if a & mask:
            bits1[i] += 1
        else:
            bits0[i] += 1
        mask <<= 1

for a in aList:
    getBit(a)

ans = 0
for i in range(41, -1, -1):
    cnt1 = bits1[i]
    cnt0 = bits0[i]
    if k < 2 ** i:
        ans += 2 ** i * cnt1
        continue
    if cnt0 <= cnt1:
        ans += 2 ** i * cnt1
    else:
        ans += 2 ** i * cnt0
        k -= 2 ** i
print(ans)

Solution (Python):
from sys import stdin 
n,k = map(int,stdin.readline().split())
a = map(int,stdin.readline().split())
i = 50
cur = 0 
ans = 0 
while i>=0:
    one = 0 
    
    po = 1 << i 
    for j in a:
        if j & po :
            one+= 1 
    zero = n - one 
    if cur + po > k:
        ans += po * one  
    else:
        ans += max(one,zero)*po 
        if zero > one:
            cur += po 
    
    i-=1 
print ans

Solution (Python):
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
M=41
B=[[0 for x in range(M-len(bin(A[i])[2:]))]+list([int(x) for x in bin(A[i])[2:]]) for i in range(N)]
C=[[0,0] for i in range(M)]
for i in range(N):
    for j in range(M):
        C[j][B[i][j]]+=1
D=[-1 for i in range(M)]
for j in range(M):
    if C[j][0]<=C[j][1]:
        D[j]=0
    else:
        D[j]=1
E=[2**(M-i-1) for i in range(M)]
tmp=0
for i in range(M):
    if tmp+E[i]*D[i]<=K:
        tmp+=E[i]*D[i]
ans=0
for i in range(N):
    ans+=tmp^A[i]
print(ans)

Solution (Python):
n,k = list(map(int, input().split()))
a=list(map(int, input().split()))
x=0
ans = 0
for i in range(45,-1,-1):
  c=0
  for p in a:
    if (p >> i) & 1:
      c += 1
  if n - c > c and x + (1<<i) <= k:
    ans += (n-c) * (1<<i)
    x += 1<<i
  else:
    ans += c * (1<<i)
print(ans)

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(42):
        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
sys.setrecursionlimit(200000)
input = sys.stdin.readline
n,k = map(int,input().split())
a = list(map(int,input().split()))
bit = [0]*41
for i in range(n):
        for j in range(41):
                if a[i] & 1<<j:
                        bit[j] += 1
ans = 0
ima = 0
for i in range(40,-1,-1):
        if ima + (1<<i) > k:
                ans +=  bit[i]*(1<<i)
                continue
        if bit[i] < n/2:
                ans += (n-bit[i])*(1<<i)
                ima += 1<<i
        else:
                ans +=  bit[i]*(1<<i)
print(ans)

Solution (Python):
N, K = map(int, input().split())
*A, = 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)

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n, k = na()
a = na()

ans = 0
has = []
for i in range(45):
    s = 0
    for v in a:
        s += v>>i&1
    has.append(s)

und = []
for i in range(45):
    und.append(max(n-has[i], has[i])<<i)

for i in range(44):
    und[i+1] += und[i]

k += 1
top = 0
ans = 0
for i in range(44,-1,-1):
    if k>>i&1:
        
        ans = max(ans, top + (has[i]<<i) + (und[i-1] if i-1 >= 0 else 0))
    top += n-has[i]<<i if k>>i&1 else has[i]<<i

print(ans)