Problem p03138 (python)

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

def f(X):
    return sum([X ^ a for a in A])

bin_K = '{:040b}'.format(K)
bin_A = ['{:040b}'.format(a) for a in A]
counter = [{'0': 0, '1': 0} for i in range(40)]
for ba in bin_A:
    for i in range(40):
        counter[i][ba[i]] += 1

cand = []
for i in range(40):
    if counter[i]['0'] < counter[i]['1']:
        cand.append('0')
    else:
        cand.append('1')

i = 0
for i in range(40):
    if bin_K[i] == '1':
        break

bin_K = bin_K[i:]
cand = cand[i:]
counter = counter[i:]

candidate = [f(0), f(K)]
for i in range(len(cand)):
    if bin_K[i] == '1':
        tmp = cand[i:]
        tmp[0] = '0'
        candidate.append(f(int(bin_K[:i]+''.join(tmp), 2)))

print(max(candidate))

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

def f(X):
    return sum([X ^ a for a in A])

bin_K = '{:040b}'.format(K)
bin_A = ['{:040b}'.format(a) for a in A]
counter = [{'0': 0, '1': 0} for i in range(40)]
for ba in bin_A:
    for i in range(40):
        counter[i][ba[i]] += 1

cand = []
for i in range(40):
    if counter[i]['0'] <= counter[i]['1']:
        cand.append('0')
    else:
        cand.append('1')

i = 0
for i in range(40):
    if bin_K[i] == '1':
        break

bin_K = bin_K[i:]
cand = cand[i:]
counter = counter[i:]

candidate = []
for i in range(len(cand)):
    if bin_K[i] == '1':
        tmp = cand[i:]
        tmp[0] = '0'
        candidate.append(f(int(bin_K[:i]+''.join(tmp), 2)))

print(max(candidate))

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

def f(X):
    return sum([X ^ a for a in A])

bin_K = '{:040b}'.format(K)
bin_A = ['{:040b}'.format(a) for a in A]
counter = [{'0': 0, '1': 0} for i in range(40)]
for ba in bin_A:
    for i in range(40):
        counter[i][ba[i]] += 1

cand = []
for i in range(40):
    if counter[i]['0'] <= counter[i]['1']:
        cand.append('0')
    else:
        cand.append('1')

i = 0
for i in range(40):
    if bin_K[i] == '1':
        break

bin_K = bin_K[i:]
cand = cand[i:]
counter = counter[i:]

candidate = []
for i in range(len(num)):
    if bin_K[i] == '1':
        tmp = cand[i:]
        tmp[0] = '0'
        print(bin_K[:i]+''.join(tmp))
        candidate.append(f(int(bin_K[:i]+''.join(tmp), 2)))

print(max(candidate))

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

def f(X):
    return sum([X ^ a for a in A])

bin_K = '{:040b}'.format(K)
bin_A = ['{:040b}'.format(a) for a in A]
counter = [{'0': 0, '1': 0} for i in range(40)]
for ba in bin_A:
    for i in range(40):
        counter[i][ba[i]] += 1

cand = []
for i in range(40):
    if counter[i]['0'] <= counter[i]['1']:
        cand.append('0')
    else:
        cand.append('1')

i = 0
for i in range(40):
    if bin_K[i] == '1':
        break

bin_K = bin_K[i:]
cand = cand[i:]
counter = counter[i:]

candidate = []
num = ''.join(cand)
for i in range(len(num)):
    if bin_K[i] == '1':
        candidate.append(f(int(bin_K[:i]+'0'+num[i+1:], 2)))

print(max(candidate))

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

def f(X):
    return sum([X ^ a for a in A])

bin_K = '{:040b}'.format(K)
bin_A = ['{:040b}'.format(a) for a in A]
counter = [{'0': 0, '1': 0} for i in range(40)]
for ba in bin_A:
    for i in range(40):
        counter[i][ba[i]] += 1

cand = []
for i in range(40):
    if counter[i]['0'] <= counter[i]['1']:
        cand.append('0')
    else:
        cand.append('1')

i = 0
while bin_K[i] == '0':
    cand[i] = '0'
    i += 1

if int(''.join(cand), 2) >= K:
    print(f(K))
else:
    print(f(int(''.join(cand), 2)))