Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

#def f(x,a):
#  return np.array([ c^x for c in a ],dtype=np.int64).sum()

def g(y,s):
  return np.array([s[m,y[m]] for m in range(M)]).sum()

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

B = np.empty((M,N),dtype=np.int)
Y = np.empty(M,dtype=np.int)

for n in range(N):
  for m in range(M):
    B[m,n] = (A[n] >> m) & 1

S = np.empty((M,2),dtype=np.int64)
for m in range(M):
  s = B[m].sum()
  S[m] = [s<<m,(N-s)<<m]
  if 2*s < N:
    Y[m] = 1
  else:
    Y[m] = 0

Kb = np.array([ ((K+1) >> m) & 1 for m in range(M)],dtype=np.int)

ans = 0
for m in range(M):
  if Kb[m] == 0:
    continue
  else:
    Z = np.concatenate((Y[:m],np.array([0],dtype=np.int),Kb[m+1:]))
    ans = max(ans,g(Z,S))

print(ans)

Fixed submission:
inpl = lambda: list(map(int,input().split()))
N, K = inpl()
A = inpl()

L = K+1
M = len(bin(L))-2
MM = max(M, len(bin(max(A)))-2)
best = []
ones_num = []
zeros_num = []
for m in range(MM):
    ones = sum([ (A[i]>>m)&1 for i in range(N) ])
    zeros = N - ones
    ones_num.append(ones)
    zeros_num.append(zeros)
    if ones >= zeros:
        best.append(0)
    else:
        best.append(1)
cur = sum([ (N-ones_num[m] if (best[m] and m < M-1) else ones_num[m])<<m for m in range(MM)])
ans = cur
m = M - 1
while m > 0:
    cur += (zeros_num[m]-ones_num[m])<<m
    m -= 1
    while (L >> m)&1 == 0:
        if best[m] == 1:
            cur += (ones_num[m]-zeros_num[m])<<m
        m -= 1
        if m < 0:
            break
    else:
        if best[m] == 1:
            cur += (ones_num[m]-zeros_num[m])<<m
        if cur > ans:
            ans = cur
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

#def f(x,a):
#  return np.array([ c^x for c in a ],dtype=np.int64).sum()

def g(y,s):
  return np.array([s[m,y[m]] for m in range(M)]).sum()

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

B = np.empty((M,N),dtype=np.int)
Y = np.empty(M,dtype=np.int)

for n in range(N):
  for m in range(M):
    B[m,n] = (A[n] >> m) & 1

S = np.empty((M,2),dtype=np.int64)
for m in range(M):
  s = B[m].sum()
  S[m] = [s<<m,(N-s)<<m]
  if 2*s < N:
    Y[m] = 1
  else:
    Y[m] = 0

Kb = np.array([ (K >> m) & 1 for m in range(M)],dtype=np.int)

ans = S[:,0].sum()
for m in range(M):
  if Kb[m] == 0:
    continue
  else:
    Z = np.concatenate((Y[:m],np.array([0],dtype=np.int),Kb[m+1:]))
    ans = max(ans,g(Z,S))

print(ans)

Fixed submission:
inpl = lambda: list(map(int,input().split()))
N, K = inpl()
A = inpl()

L = K+1
M = len(bin(L))-2
MM = max(M, len(bin(max(A)))-2)
best = []
ones_num = []
zeros_num = []
for m in range(MM):
    ones = sum([ (A[i]>>m)&1 for i in range(N) ])
    zeros = N - ones
    ones_num.append(ones)
    zeros_num.append(zeros)
    if ones >= zeros:
        best.append(0)
    else:
        best.append(1)
cur = sum([ (N-ones_num[m] if (best[m] and m < M-1) else ones_num[m])<<m for m in range(MM)])
ans = cur
m = M - 1
while m > 0:
    cur += (zeros_num[m]-ones_num[m])<<m
    m -= 1
    while (L >> m)&1 == 0:
        if best[m] == 1:
            cur += (ones_num[m]-zeros_num[m])<<m
        m -= 1
        if m < 0:
            break
    else:
        if best[m] == 1:
            cur += (ones_num[m]-zeros_num[m])<<m
        if cur > ans:
            ans = cur
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

#def f(x,a):
#  return np.array([ c^x for c in a ],dtype=np.int64).sum()

def g(y,s):
  return np.array([s[m,y[m]] for m in range(M)]).sum()

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

B = np.empty((M,N),dtype=np.int)
Y = np.empty(M,dtype=np.int)

for n in range(N):
  for m in range(M):
    B[m,n] = (A[n] >> m) & 1

S = np.empty((M,2),dtype=np.int64)
for m in range(M):
  s = B[m].sum()
  S[m] = [s<<m,(N-s)<<m]
  if 2*s < N:
    Y[m] = 1
  else:
    Y[m] = 0

Kb = np.array([ (K >> m) & 1 for m in range(M)],dtype=np.int64)

ans = S[:,0].sum()
for m in range(M):
  if Kb[m] == 0:
    continue
  else:
    Z = np.concatenate((Y[:m],np.array([0],dtype=np.int64),Kb[m+1:]))
    ans = max(ans,g(Z,S))

print(ans)

Fixed submission:
inpl = lambda: list(map(int,input().split()))
N, K = inpl()
A = inpl()

L = K+1
M = len(bin(L))-2
MM = max(M, len(bin(max(A)))-2)
best = []
ones_num = []
zeros_num = []
for m in range(MM):
    ones = sum([ (A[i]>>m)&1 for i in range(N) ])
    zeros = N - ones
    ones_num.append(ones)
    zeros_num.append(zeros)
    if ones >= zeros:
        best.append(0)
    else:
        best.append(1)
cur = sum([ (N-ones_num[m] if (best[m] and m < M-1) else ones_num[m])<<m for m in range(MM)])
ans = cur
m = M - 1
while m > 0:
    cur += (zeros_num[m]-ones_num[m])<<m
    m -= 1
    while (L >> m)&1 == 0:
        if best[m] == 1:
            cur += (ones_num[m]-zeros_num[m])<<m
        m -= 1
        if m < 0:
            break
    else:
        if best[m] == 1:
            cur += (ones_num[m]-zeros_num[m])<<m
        if cur > ans:
            ans = cur
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

def f(x,a):
  return np.array([ c^x for c in a ],dtype=np.int64).sum()

def g(y,s):
  return np.array([s[m,y[m]] for m in range(M)]).sum()

def bitlength(x):
  i=0
  while x > 0:
    x //= 2
    i += 1
  return i

def bitlengthceil(x):
  l = bitlength(x)
  if x == 2**(l-1):
    return l
  else:
    return (l+1)

def bit2num(y):
  return np.array([ y[m]<<m for m in range(M)]).sum()

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

B = np.empty((M,N),dtype=np.int64)
Y = np.empty(M,dtype=np.int64)

for n in range(N):
  for m in range(M):
    B[m,n] = (A[n] >> m) % 2

S = np.empty((M,2),dtype=np.int64)
for m in range(M):
  s = B[m].sum()
  S[m] = [s<<m,(N-s)<<m]
  if 2*s < N:
    Y[m] = 1
  else:
    Y[m] = 0

Kb = np.array([ (K >> i) % 2 for i in range(M)],dtype=np.int64)

ans = S[:,0].sum()
for m in range(M):
  if Kb[m] == 0:
    continue
  else:
    Z = np.concatenate((Y[:m],np.array([0],dtype=np.int64),Kb[m+1:]))
    ans = max(ans,g(Z,S))

print(ans)

Fixed submission:
inpl = lambda: list(map(int,input().split()))
N, K = inpl()
A = inpl()

L = K+1
M = len(bin(L))-2
MM = max(M, len(bin(max(A)))-2)
best = []
ones_num = []
zeros_num = []
for m in range(MM):
    ones = sum([ (A[i]>>m)&1 for i in range(N) ])
    zeros = N - ones
    ones_num.append(ones)
    zeros_num.append(zeros)
    if ones >= zeros:
        best.append(0)
    else:
        best.append(1)
cur = sum([ (N-ones_num[m] if (best[m] and m < M-1) else ones_num[m])<<m for m in range(MM)])
ans = cur
m = M - 1
while m > 0:
    cur += (zeros_num[m]-ones_num[m])<<m
    m -= 1
    while (L >> m)&1 == 0:
        if best[m] == 1:
            cur += (ones_num[m]-zeros_num[m])<<m
        m -= 1
        if m < 0:
            break
    else:
        if best[m] == 1:
            cur += (ones_num[m]-zeros_num[m])<<m
        if cur > ans:
            ans = cur
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

def f(x,a):
  return np.array([ c^x for c in a],dtype=np.int64).sum()

def bitlength(x):
  i=0
  while x > 0:
    x //= 2
    i += 1
  return i

def bitlengthceil(x):
  l = bitlength(x)
  if x == 2**(l-1):
    return l
  else:
    return (l+1)

def bit2num(y):
  return np.array([ y[m]<<m for m in range(M)]).sum()

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

B = np.empty((M,N),dtype=np.int64)
Y = np.empty(M,dtype=np.int64)
D = np.empty(M,dtype=np.int64)

for n in range(N):
  for m in range(M):
    B[m,n] = (A[n] >> m) % 2

for m in range(M):
  s = B[m].sum()
  if 2*s < N:
    Y[m] = 1
    D[m] = (N-2*s)<<m
  else:
    Y[m] = 0
    D[m] = 0

Kb = np.array([ (K >> i) % 2 for i in range(M)],dtype=np.int64)

ans = f(0,A)
for m in range(M):
  if Kb[m] == 0:
    continue
  else:
    X = bit2num(np.concatenate((Y[:m],np.array([0],dtype=np.int64),Kb[m+1:])))
    ans = max(ans,f(X,A))

print(ans)

Fixed submission:
inpl = lambda: list(map(int,input().split()))
N, K = inpl()
A = inpl()

L = K+1
M = len(bin(L))-2
MM = max(M, len(bin(max(A)))-2)
best = []
ones_num = []
zeros_num = []
for m in range(MM):
    ones = sum([ (A[i]>>m)&1 for i in range(N) ])
    zeros = N - ones
    ones_num.append(ones)
    zeros_num.append(zeros)
    if ones >= zeros:
        best.append(0)
    else:
        best.append(1)
cur = sum([ (N-ones_num[m] if (best[m] and m < M-1) else ones_num[m])<<m for m in range(MM)])
ans = cur
m = M - 1
while m > 0:
    cur += (zeros_num[m]-ones_num[m])<<m
    m -= 1
    while (L >> m)&1 == 0:
        if best[m] == 1:
            cur += (ones_num[m]-zeros_num[m])<<m
        m -= 1
        if m < 0:
            break
    else:
        if best[m] == 1:
            cur += (ones_num[m]-zeros_num[m])<<m
        if cur > ans:
            ans = cur
print(ans)

Buggy python submission (verdict: Runtime Error):
import numpy as np
#import math

def f(x,a):
  return np.array([ c^x for c in a],dtype=np.int64).sum()

def bitlength(x):
  i=0
  while x > 0:
    x //= 2
    i += 1
  return i

def bitlengthceil(x):
  l = bitlength(x)
  if x == 2**(l-1):
    return l
  else:
    return (l+1)

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

B = np.empty((M,N),dtype=np.int64)
Y = np.empty(M,dtype=np.int64)
D = np.empty(M,dtype=np.int64)

for n in range(N):
  for m in range(M):
    B[m,n] = (A[n] >> m) % 2

for m in range(M):
  s = B[m].sum()
  if 2*s < N:
    Y[m] = 1
    D[m] = (N-2*s)<<m
  else:
    Y[m] = 0
    D[m] = 0

#print(Y[0])

#k = math.floor(math.log2(K))
k = bitlength(K)
X = np.array([Y[m]<<m for m in range(k+1)]).sum()
#print(X)
if X <= K:
  print(f(X,A))
else:
  d = X - K
#  print("d=",d)
#  p = math.ceil(math.log2(d))
  p = bitlengthceil(d)-1
#  print("p=",p)
#  w = np.array([Y[m]<<m for m in range(p)]).sum()
  ans=0
  for m in range(p,k+1):
    if Y[m] == 1:
      ans = max(ans,f(X^(Y[m]<<m),A))

  for m in range(p-1,-1,-1):
    if Y[m] == 0:
      continue
    elif Y[m]<<(m+1) < d:
      break
    elif Y[m]<<m < d:
      X = X^(Y[m]<<m)
      continue
    else:
#      print(X,m)
      ans = max(ans,f(X^(Y[m]<<m),A))
  print(ans)


Fixed submission:
inpl = lambda: list(map(int,input().split()))
N, K = inpl()
A = inpl()

L = K+1
M = len(bin(L))-2
MM = max(M, len(bin(max(A)))-2)
best = []
ones_num = []
zeros_num = []
for m in range(MM):
    ones = sum([ (A[i]>>m)&1 for i in range(N) ])
    zeros = N - ones
    ones_num.append(ones)
    zeros_num.append(zeros)
    if ones >= zeros:
        best.append(0)
    else:
        best.append(1)
cur = sum([ (N-ones_num[m] if (best[m] and m < M-1) else ones_num[m])<<m for m in range(MM)])
ans = cur
m = M - 1
while m > 0:
    cur += (zeros_num[m]-ones_num[m])<<m
    m -= 1
    while (L >> m)&1 == 0:
        if best[m] == 1:
            cur += (ones_num[m]-zeros_num[m])<<m
        m -= 1
        if m < 0:
            break
    else:
        if best[m] == 1:
            cur += (ones_num[m]-zeros_num[m])<<m
        if cur > ans:
            ans = cur
print(ans)

Buggy python submission (verdict: Runtime Error):
import numpy as np
import math

def f(x,a):
  return np.array([ c^x for c in a],dtype=np.int64).sum()

def bitlength(x):
  i=0
  while x > 0:
    x //= 2
    i += 1
  return i

def bitlengthceil(x):
  l = bitlength(x)
  if x == 2**l:
    return l
  else:
    return (l+1)

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

B = np.empty((M,N),dtype=np.int64)
Y = np.empty(M,dtype=np.int64)
D = np.empty(M,dtype=np.int64)

for n in range(N):
  for m in range(M):
    B[m,n] = (A[n] >> m) % 2

for m in range(M):
  s = B[m].sum()
  if 2*s < N:
    Y[m] = 1
    D[m] = (N-2*s)<<m
  else:
    Y[m] = 0
    D[m] = 0

#print(Y[0])

#k = math.floor(math.log2(K))
k = bitlength(K)
X = np.array([Y[m]<<m for m in range(k+1)]).sum()
#print(X)
if X <= K:
  print(f(X,A))
else:
  d = X - K
#  print("d=",d)
#  p = math.ceil(math.log2(d))
  p = bitlengthceil(d)-1
#  print("p=",p)
#  w = np.array([Y[m]<<m for m in range(p)]).sum()
  ans=0
  for m in range(p,k+1):
    if Y[m] == 1:
      ans = max(ans,f(X^(Y[m]<<m),A))

  for m in range(p-1,-1,-1):
    if Y[m] == 0:
      continue
    elif Y[m]<<(m+1) < d:
      break
    elif Y[m]<<m < d:
      X = X^(Y[m]<<m)
      continue
    else:
#      print(X,m)
      ans = max(ans,f(X^(Y[m]<<m),A))
  print(ans)

Fixed submission:
inpl = lambda: list(map(int,input().split()))
N, K = inpl()
A = inpl()

L = K+1
M = len(bin(L))-2
MM = max(M, len(bin(max(A)))-2)
best = []
ones_num = []
zeros_num = []
for m in range(MM):
    ones = sum([ (A[i]>>m)&1 for i in range(N) ])
    zeros = N - ones
    ones_num.append(ones)
    zeros_num.append(zeros)
    if ones >= zeros:
        best.append(0)
    else:
        best.append(1)
cur = sum([ (N-ones_num[m] if (best[m] and m < M-1) else ones_num[m])<<m for m in range(MM)])
ans = cur
m = M - 1
while m > 0:
    cur += (zeros_num[m]-ones_num[m])<<m
    m -= 1
    while (L >> m)&1 == 0:
        if best[m] == 1:
            cur += (ones_num[m]-zeros_num[m])<<m
        m -= 1
        if m < 0:
            break
    else:
        if best[m] == 1:
            cur += (ones_num[m]-zeros_num[m])<<m
        if cur > ans:
            ans = cur
print(ans)