Buggy python submission (verdict: Runtime Error):
from math import log2

N, K = map(int, input().split())
a = list(map(int, input().split()))
sa = sum(a)

if sa == 0:
    print(K * N)
    exit()

b = [0 for _ in range(int(log2(sa)) + 1)]
# b[2進数の位] = そのbitが立っているAiの数

ls = int(log2(sa))
# print(ls)

for aa in a:
    for i in range(ls + 1):
        # print(aa, aa >> i, i)
        if (aa >> i) & 1 == 1:
            # print('if')
            b[i] += 1
# print(b)

x = []
if K > 0:
    for i in range(int(log2(K)) + 1):
        t = N - 2 * b[i]
        if t > 0:
            x.append((1 << i, t * (1 << i)))
            # (立てるフラグ値、答えの増加量)

    x = sorted(x, key=lambda xx: xx[1], reverse=True)
# print(x)

sx = 0
st = 0
for xx in x:
    if sx + xx[0] <= K:
        sx += xx[0]
        st += xx[1]

ans = sa + st
print(ans)

Fixed submission:
from math import log2

N, K = map(int, input().split())
a = list(map(int, input().split()))
sa = sum(a)

if sa == 0:
    print(K * N)
    exit()

b = [0 for _ in range(int(log2(max(sa, K))) + 1)]
# b[2進数の位] = そのbitが立っているAiの数

ls = int(log2(sa))
# print(ls)

for aa in a:
    for i in range(ls + 1):
        # print(aa, aa >> i, i)
        if (aa >> i) & 1 == 1:
            # print('if')
            b[i] += 1
# print(b)

x = []
if K > 0:
    for i in range(int(log2(K)) + 1):
        t = N - 2 * b[i]
        if t > 0:
            x.append((1 << i, t * (1 << i)))
            # (立てるフラグ値、答えの増加量)

    x = sorted(x, key=lambda xx: xx[1], reverse=True)
# print(x)

sx = 0
st = 0
for xx in x:
    if sx + xx[0] <= K:
        sx += xx[0]
        st += xx[1]

ans = sa + st
print(ans)

Buggy python submission (verdict: Runtime Error):
from math import log2

N, K = map(int, input().split())
a = list(map(int, input().split()))
sa = sum(a)

b = [0 for _ in range(int(log2(sa)) + 1)]
# b[2進数の位] = そのbitが立っているAiの数

ls = int(log2(sa))
# print(ls)

for aa in a:
    for i in range(ls + 1):
        # print(aa, aa >> i, i)
        if (aa >> i) & 1 == 1:
            # print('if')
            b[i] += 1
# print(b)

x = []
if K > 0:
    for i in range(int(log2(K)) + 1):
        t = N - 2 * b[i]
        if t > 0:
            x.append((1 << i, t * (1 << i)))
            # (立てるフラグ値、答えの増加量)

    x = sorted(x, key=lambda xx: xx[1], reverse=True)
# print(x)

sx = 0
st = 0
for xx in x:
    if sx + xx[0] <= K:
        sx += xx[0]
        st += xx[1]

ans = sa + st
print(ans)

Fixed submission:
from math import log2

N, K = map(int, input().split())
a = list(map(int, input().split()))
sa = sum(a)

if sa == 0:
    print(K * N)
    exit()

b = [0 for _ in range(int(log2(max(sa, K))) + 1)]
# b[2進数の位] = そのbitが立っているAiの数

ls = int(log2(sa))
# print(ls)

for aa in a:
    for i in range(ls + 1):
        # print(aa, aa >> i, i)
        if (aa >> i) & 1 == 1:
            # print('if')
            b[i] += 1
# print(b)

x = []
if K > 0:
    for i in range(int(log2(K)) + 1):
        t = N - 2 * b[i]
        if t > 0:
            x.append((1 << i, t * (1 << i)))
            # (立てるフラグ値、答えの増加量)

    x = sorted(x, key=lambda xx: xx[1], reverse=True)
# print(x)

sx = 0
st = 0
for xx in x:
    if sx + xx[0] <= K:
        sx += xx[0]
        st += xx[1]

ans = sa + st
print(ans)