Problem p03138 (python)

Fixed submission:
import math
def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]
# def getIntList(): return np.array(input().split(), dtype=np.longlong)
def zeros(n): return [0]*n
# def zeros(n): return np.zeros(n, dtype=np.longlong)
def zeros2(n, m): return [zeros(m) for i in range(n)] # obsoleted zeros((n, m))で代替

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():  # WA 2
    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
        mask = getMask(K)
        x = 0
        d.dmp(x,'x')
        #mask >>= 1
        lessK = False  # x<Kか否か
        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)

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

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

ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
MOD = 10**9 + 7
INF = 10**18

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))
        mask = 1 << lg
        x = mask
        d.dmp(x,'x')
        mask >>= 1
        lessK = False
        while mask > 0:
            cz, co = 0, 0
            if K & mask == 0 and not lessK:
                pass
            else:
                for i in range(N):
                    if A[i] & mask == 0:
                        cz += 1
                    else:
                        co += 1
                if cz > co:
                    ans += mask
                    lessK = True
            mask >>= 1
            d.dmp((mask,x),'mask,x')
    d.dmp((x),'x')
    ans = 0
    for i in range(N):
        ans += x ^ A[i]
    return ans


ans = prob()
print(ans)