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

def solve(k):
    l = int(math.log2(k + 1))
    if k + 1 == 2 ** l:
        ans = 0
        for i in range(l):
            if v[i] > 0:
                ans += v[i]
        return ans
    else:
        return max(solve(k - 2 ** l) + v[l], solve(2 ** l - 1))

N, K = map(int, input().split())
A = list(map(int, input().split()))
bit = [0] * 40
for i in A:
    for j in range(40):
        bit[j] += (i >> j) & 1
w = [0] * 40
v = [0] * 40
for i in range(40):
    w[i] = 1 << i
    v[i] = (N - bit[i] - bit[i]) * w[i]
print(solve(K)+sum(A))


Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))
bit = [0] * 41
for i in A:
    for j in range(41):
        bit[j] += (i >> j) & 1
x = {}
for i in range(41):
    x[i, 0] = 2 ** i * bit[i]
    x[i, 1] = 2 ** i * (N - bit[i])
ans = sum(A)
for i in range(41):
    if ((K + 1) >> i) & 1 == 0:
        continue
    f = 0
    for j in range(i):
        f += max(x[j, 0], x[j, 1])
    f += x[i, 0]
    for j in range(i + 1, 41):
        f += x[j, ((K + 1) >> j) & 1]
    ans = max(ans, f)
print(ans)

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

def solve(k):
    l = int(math.log2(k + 1))
    if k + 1 == 2 ** l:
        ans = 0
        for i in range(l):
            if v[i] > 0:
                ans += v[i]
        return ans
    else:
        return max(solve(k - 2 ** l) + v[l], solve(2 ** l - 1))

N, K = map(int, input().split())
A = list(map(int, input().split()))
bit = [0] * 40
for i in A:
    for j in range(40):
        bit[j] += (i >> j) & 1
w = [0] * 40
v = [0] * 40
for i in range(40):
    w[i] = 1 << i
    v[i] = (N - bit[i] - bit[i]) * w[i]
print(solve(K)+sum(A))


Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))
bit = [0] * 41
for i in A:
    for j in range(41):
        bit[j] += (i >> j) & 1
x = {}
for i in range(41):
    x[i, 0] = 2 ** i * bit[i]
    x[i, 1] = 2 ** i * (N - bit[i])
ans = sum(A)
for i in range(41):
    if ((K + 1) >> i) & 1 == 0:
        continue
    f = 0
    for j in range(i):
        f += max(x[j, 0], x[j, 1])
    f += x[i, 0]
    for j in range(i + 1, 41):
        f += x[j, ((K + 1) >> j) & 1]
    ans = max(ans, f)
print(ans)

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

def solve(k):
    l = int(math.log2(k + 1))
    if k + 1 == 2 ** l:
        ans = 0
        for i in range(l):
            if v[i] > 0:
                ans += v[i]
        return ans
    else:
        return max(solve(k - 2 ** l) + v[l], solve(2 ** l - 1))

N, K = map(int, input().split())
A = list(map(int, input().split()))
bit = [0] * 40
for i in A:
    for j in range(40):
        bit[j] += (i >> j) & 1
w = [0] * 40
v = [0] * 40
for i in range(40):
    w[i] = 1 << i
    v[i] = (N - bit[i] - bit[i]) * w[i]
print(solve(K))


Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))
bit = [0] * 41
for i in A:
    for j in range(41):
        bit[j] += (i >> j) & 1
x = {}
for i in range(41):
    x[i, 0] = 2 ** i * bit[i]
    x[i, 1] = 2 ** i * (N - bit[i])
ans = sum(A)
for i in range(41):
    if ((K + 1) >> i) & 1 == 0:
        continue
    f = 0
    for j in range(i):
        f += max(x[j, 0], x[j, 1])
    f += x[i, 0]
    for j in range(i + 1, 41):
        f += x[j, ((K + 1) >> j) & 1]
    ans = max(ans, f)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def rec(i, j):
    if (i, j) in dp:
        return dp[i, j]
    if i == 40:
        res = 0
    elif j < w[i] or w[i] < 0:
        res = rec(i + 1, j)
    else:
        res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i])
    dp[i, j] = res
    return res

N, K = map(int, input().split())
A = list(map(int, input().split()))
bit = [0] * 40
for i in A:
    for j in range(40):
        bit[j] += (i >> j) & 1
w = [0] * 40
v = [0] * 40
for i in range(40):
    w[i] = 1 << i
    v[i] = (N - bit[i] - bit[i]) * w[i]
dp = {}
x = rec(0, K)
print(x + sum(A))

Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))
bit = [0] * 41
for i in A:
    for j in range(41):
        bit[j] += (i >> j) & 1
x = {}
for i in range(41):
    x[i, 0] = 2 ** i * bit[i]
    x[i, 1] = 2 ** i * (N - bit[i])
ans = sum(A)
for i in range(41):
    if ((K + 1) >> i) & 1 == 0:
        continue
    f = 0
    for j in range(i):
        f += max(x[j, 0], x[j, 1])
    f += x[i, 0]
    for j in range(i + 1, 41):
        f += x[j, ((K + 1) >> j) & 1]
    ans = max(ans, f)
print(ans)