Problem p03138 (python)

Fixed submission:
n, input_k = map(int, input().split())
a = list(map(int, input().split()))

MAX = 41

k = [0 for _ in range(MAX)]
c = [0 for _ in range(MAX)]
b = [0 for _ in range(MAX)]
v = [[0 for _ in range(MAX)] for _ in range(2)]

K = input_k + 1
d = 0
while K > 0:
    if K % 2 == 1:
        k[d] = 1
    d += 1
    K //= 2

for p in a:
    counter = 0
    while p > 0:
        if p % 2 == 1:
            c[counter] += 1
        counter += 1
        p //= 2

temp = 1
for i in range(MAX):
    if c[i] > n - c[i]:
        b[i] = 0
    else:
        b[i] = 1
    v[0][i] = temp * c[i]
    v[1][i] = temp * (n - c[i])
    temp *= 2

if input_k == 0:
    print(sum(v[0]))
    exit()

ans = 0
for i in range(MAX - 1, -1, -1):
    if k[i] == 0:
        continue

    temp_ans = 0
    for j in range(i+1, MAX):
        temp_ans += v[k[j]][j]

    temp_ans += v[0][i]

    for j in range(i):
        temp_ans += v[b[j]][j]

    #print(i, temp_ans)
    ans = max(ans, temp_ans)

print(ans)

Buggy submission (verdict: Runtime Error):
n, input_k = map(int, input().split())
a = list(map(int, input().split()))

MAX = 40

k = [0 for _ in range(MAX)]
c = [0 for _ in range(MAX)]
b = [0 for _ in range(MAX)]
v = [[0 for _ in range(MAX)] for _ in range(2)]

K = input_k
d = 0
while K > 0:
    if K % 2 == 1:
        k[d] = 1
    d += 1
    K //= 2

for p in a:
    counter = 0
    while p > 0:
        if p % 2 == 1:
            c[counter] += 1
        counter += 1
        p //= 2

temp = 1
for i in range(MAX):
    if c[i] > n - c[i]:
        b[i] = 0
    else:
        b[i] = 1
    v[0][i] = temp * c[i]
    v[1][i] = temp * (n - c[i])
    temp *= 2

if input_k == 0:
    print(sum(v[0]))
    exit()

ans = 0
for i in range(d, -1, -1):
    if k[i] == 0:
        continue

    temp_ans = 0
    for j in range(i+1, MAX):
        temp_ans += v[k[j]][j]

    temp_ans += v[0][i]

    for j in range(i):
        temp_ans += v[b[j]][j]

    ans = max(ans, temp_ans)

print(ans)

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

MAX = 40

k = [0 for _ in range(MAX)]
c = [0 for _ in range(MAX)]
b = [0 for _ in range(MAX)]
v = [[0 for _ in range(MAX)] for _ in range(2)]

d = 0
while K > 0:
    if K % 2 == 1:
        k[d] = 1
    d += 1
    K //= 2

for p in a:
    counter = 0
    while p > 0:
        if p % 2 == 1:
            c[counter] += 1
        counter += 1
        p //= 2

temp = 1
for i in range(MAX):
    if c[i] > n - c[i]:
        b[i] = 0
    else:
        b[i] = 1
    v[0][i] = temp * c[i]
    v[1][i] = temp * (n - c[i])
    temp *= 2

ans = 0
for i in range(d, -1, -1):
    if k[i] == 0:
        continue

    temp_ans = 0
    for j in range(i+1, MAX):
        temp_ans += v[k[j]][j]

    temp_ans += v[0][i]

    for j in range(i):
        temp_ans += v[b[j]][j]

    ans = max(ans, temp_ans)

print(ans)

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

MAX = 40

k = [0 for _ in range(MAX)]
c = [0 for _ in range(MAX)]
b = [0 for _ in range(MAX)]
v = [[0 for _ in range(MAX)] for _ in range(2)]

d = 0
while K > 0:
    if K % 2 == 1:
        k[d] = 1
    d += 1
    K //= 2

for p in a:
    counter = 0
    while p > 0:
        if p % 2 == 1:
            c[counter] += 1
        counter += 1
        p //= 2

temp = 1
for i in range(MAX):
    if c[i] > n - c[i]:
        b[i] = 0
        v[0][i] = temp * c[i]
        v[1][i] = temp * (n - c[i])
    else:
        b[i] = 1
        v[0][i] = temp * c[i]
        v[1][i] = temp * (n - c[i])

    temp *= 2

#print(c)
#print(k)
#print(b)
#print(v[0])
#print(v[1])

ans = 0
for i in range(d, -1, -1):
    if k[i] == 0:
        continue

    temp_ans = 0
    for j in range(i+1, MAX):
        temp_ans += v[k[j]][j]

    temp_ans += v[0][i]

    for j in range(i):
        temp_ans += v[b[j]][j]

    #print(i, temp_ans)

    ans = max(ans, temp_ans)

print(ans)