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

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

if K == 0:
	print(sum(A))

else:
	d = math.floor(math.log2(K)) + 1
	t = [0]*d

	for a in A:
		for i in range(d):
			if a == 0:
				break
			else:
				t[i] += a & 1
				a = a >> 1

	t = list(map(lambda x:N-2*x, t))


	w = []
	v = []

	for i in range(len(t)):
		if t[i] > 0:
			w.append(int(math.pow(2, i)))
			v.append(t[i])

	if w == []:
		print(sum(A))
		sys.exit()

	n = len(w)
	m = [[0]*(K+1) for i in range(n+1)]

	for i in range(1, n+1):
		for j in range(K+1):
			if w[i-1] <= j:
				remaining = j - w[i-1]
				m[i][j] = max(m[i-1][j] ,m[i-1][remaining] + w[i-1]*v[i-1])
			else:
				m[i][j] = m[i-1][j]

	print(sum(A)+m[n][K])

Fixed submission:
import operator

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

k = [((K+1)&(1<<i))>>i for i in range(40)]

m = [0]*40
for i in range(40):
	c = 0
	for a in A:
		c += (a&(1<<i))>>i
	
	if N < 2*c:
		m[i] = 0
	else:
		m[i] = 1

f = 0
b = [1<<i for i in range(40)]
for i in range(40):
	if k[i]==1:
		x = sum(map(operator.mul, m[:i]+[0]+k[i+1:], b))
		f = max(f, sum([x^a for a in A]))

print(f)

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

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

if K == 0:
	print(sum(A))

else:
	d = math.floor(math.log2(K)) + 1
	t = [0]*d

	for a in A:
		for i in range(d):
			if a == 0:
				break
			else:
				t[i] += a & 1
				a = a >> 1

	t = list(map(lambda x:N-2*x, t))


	w = []
	v = []

	for i in range(len(t)):
		if t[i] > 0:
			w.append(int(math.pow(2, i)))
			v.append(t[i])

	n = len(w)
	m = [[0]*(K+1) for i in range(n+1)]

	for i in range(1, n+1):
		for j in range(K+1):
			if w[i-1] <= j:
				remaining = j - w[i-1]
				m[i][j] = max(m[i-1][j] ,m[i-1][remaining] + w[i-1]*v[i-1])
			else:
				m[i][j] = m[i-1][j]

	print(sum(A)+m[n][K])

Fixed submission:
import operator

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

k = [((K+1)&(1<<i))>>i for i in range(40)]

m = [0]*40
for i in range(40):
	c = 0
	for a in A:
		c += (a&(1<<i))>>i
	
	if N < 2*c:
		m[i] = 0
	else:
		m[i] = 1

f = 0
b = [1<<i for i in range(40)]
for i in range(40):
	if k[i]==1:
		x = sum(map(operator.mul, m[:i]+[0]+k[i+1:], b))
		f = max(f, sum([x^a for a in A]))

print(f)

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

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

if K == 0:
	print(sum(A))

else:
	d = math.floor(math.log2(K)) + 1
	t = [0]*d

	for a in A:
		for i in range(d):
			if a == 0:
				break
			else:
				t[i] += a & 1
				a = a >> 1

	t = list(map(lambda x:N-2*x, t))


	w = []
	v = []

	for i in range(len(t)):
		if t[i] > 0:
			w.append(int(math.pow(2, i)))
			v.append(t[i])

	n = len(w)
	m = [[0]*(K+1) for i in range(n+1)]

	for i in range(1, n+1):
		for j in range(K+1):
			if w[i-1] <= j:
				remaining = j - w[i-1]
				m[i][j] = max(m[i-1][j] ,m[i-1][remaining] + w[i-1]*v[i-1])
			else:
				m[i][j] = m[i-1][j]

print(sum(A)+m[n][K])

Fixed submission:
import operator

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

k = [((K+1)&(1<<i))>>i for i in range(40)]

m = [0]*40
for i in range(40):
	c = 0
	for a in A:
		c += (a&(1<<i))>>i
	
	if N < 2*c:
		m[i] = 0
	else:
		m[i] = 1

f = 0
b = [1<<i for i in range(40)]
for i in range(40):
	if k[i]==1:
		x = sum(map(operator.mul, m[:i]+[0]+k[i+1:], b))
		f = max(f, sum([x^a for a in A]))

print(f)

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

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

d = math.floor(math.log2(K)) + 1
m = [0]*d

for a in A:
	for i in range(d):
		if a == 0:
			break
		else:
			m[i] += a & 1
			a = a >> 1

m = list(map(lambda x:N-2*x, m))

a = sum(A)
for i in range(len(m)):
	if m[i] > 0:
		a += int(math.pow(2, i)*m[i])


print(a)


Fixed submission:
import operator

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

k = [((K+1)&(1<<i))>>i for i in range(40)]

m = [0]*40
for i in range(40):
	c = 0
	for a in A:
		c += (a&(1<<i))>>i
	
	if N < 2*c:
		m[i] = 0
	else:
		m[i] = 1

f = 0
b = [1<<i for i in range(40)]
for i in range(40):
	if k[i]==1:
		x = sum(map(operator.mul, m[:i]+[0]+k[i+1:], b))
		f = max(f, sum([x^a for a in A]))

print(f)