Problem p03138 (python)

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

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