Problem p03138 (python)

Fixed submission:
#https://atcoder.jp/contests/abc117/submissions/4165407
n,k=map(int,input().split())
a=list(map(int,input().split()))
t,ans=2**40,0
while t:
    c=sum([(a[i]&t)//t for i in range(n)])
    if k<t or c*2>=n:
        ans+=t*c
    else:
        ans+=t*(n-c)
        k -= t
    t //= 2

print(ans)

Buggy submission (verdict: Runtime Error):
def Sum_xxor(ans_tmp,A,tmp,saiteki,ind_max):

    ans_ret = 0
    ans_x = ans_tmp

    for i in range(ind_max):
        if Ones[i] <= N-Ones[i]:
            ans_x += bits[i]*1
        else:
            ans_x += bits[i]*0

    for a in A:
        ans_ret += ans_x ^ a

    return ans_ret

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

Ones   = [0 for _ in range(41)]
bits  = [0 for _ in range(41)]

for i in range(41):
    if i == 0:
        bit = 1
    else:
        bit *= 2
    bits[i] = bit

    Ones_i = 0

    for a in A:
        if a & bit:
            Ones_i += 1

    Ones[i] = Ones_i

t,ans = bit, 0
ind = 41
ans_tmp = 0
ch = 0
while t:
    if ch == 0:
        if not(t & (K+1)):
            ans_tmp += 0
        else:
            ch = 1

            ind_max = ind - 1
            ans = max(ans, Sum_xxor(ans_tmp,A,Ones,saiteki,ind_max))

            ans_tmp += t

    else:
        if t & (K+1):

            ind_max = ind - 1
            ans = max(ans, Sum_xxor(ans_tmp,A,Ones,saiteki,ind_max))

            ans_tmp += t


    t  //= 2
    ind -= 1


print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = list(map(int,input().split()))

ch = 1
ind = 0
bits = [ch]
while ch*2 <= K:
    ind += 1
    ch *= 2
    bits.append(ch)

#print(ch,ind)
#ch_max = 2**(ind+1) -1
#print(ch_max)

tmp = [0 for _ in range(ind+1)]

for a in A:
    for i,bit in enumerate(bits):
        if a & bit:
            tmp[i] = tmp[i] + 1
#print(tmp)

ans_x = 0
for i,t in enumerate(tmp):
    if 2*t < N and K > 0:# and ans_x + bits[i] <= K:
        ans_x += bits[i]
#        print('koko')
    else:
        ans_x += 0
#        print(bits[i])

ans = 0
if ans_x <= K:
    for n in range(N):
        ans += ans_x ^ A[n]
else:
    ans_max = 0
    for i in range(min(100,K)):
        ans_tmp = 0
        for n in range(N):
            ans_tmp += (K-i) ^ A[n]
        ans_max = max(ans_tmp,ans_max)

    ans = ams_max

    # for i in range(ans_x - bits[-1],K+1):
    #     ans_x2 = i
    #     ans_tmp = 0
    #     for n in range(N):
    #         ans_tmp += ans_x2 ^ A[n]
    #     ans_max = max(ans_max, ans_tmp)

    # ans = ans_max

print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = list(map(int,input().split()))

ch = 1
ind = 0
bits = [ch]
while ch*2 <= K:
    ind += 1
    ch *= 2
    bits.append(ch)

#print(ch,ind)
#ch_max = 2**(ind+1) -1
#print(ch_max)

tmp = [0 for _ in range(ind+1)]

for a in A:
    for i,bit in enumerate(bits):
        if a & bit:
            tmp[i] = tmp[i] + 1
#print(tmp)

ans_x = 0
for i,t in enumerate(tmp):
    if 2*t < N and K > 0:# and ans_x + bits[i] <= K:
        ans_x += bits[i]
#        print('koko')
    else:
        ans_x += 0
#        print(bits[i])

ans = 0
if ans_x <= K:
    for n in range(N):
        ans += ans_x ^ A[n]
else:
    ans_max = 0
    for i in range(100):
        ans_tmp = 0
        for n in range(N):
            ans_tmp += (K-i) ^ A[n]
        ans_max = max(ans_tmp,ans_max)

    ans = ams_max

    # for i in range(ans_x - bits[-1],K+1):
    #     ans_x2 = i
    #     ans_tmp = 0
    #     for n in range(N):
    #         ans_tmp += ans_x2 ^ A[n]
    #     ans_max = max(ans_max, ans_tmp)

    # ans = ans_max

print(ans)