Problem p03138 (python)

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

def f(x):
    ret = 0
    for i in range(N):
        ret += A[i] ^ x
    return ret

###2**(num-1)<=K<2**numなるnumを求める
P = bin(K)
num = len(list(map(int,P[2:])))

best = 0

for i in range(num):
    count = 0###2**iの位が1になるAの個数
    for j in range(N):
        if A[j] > A[j]^(2**i):
            count += 1
    
    if count <= N - count:
        best += 2**i

#print(best)
can = [0]*40

for i in range(40):
    if K & 2**i == 0:
        continue
    for j in range(0,i):
        can[i] += best & 2**j
    for j in range(i+1,40):
        can[i] += K & 2**j

can.append(K)

print(max(list(map(f,can))))

Buggy submission (verdict: Runtime Error):
def xor_plus(l1,l2):#引数はリストであることに注意
    l = [0] * max(len(l1),len(l2))
    for i in range(1,len(l)):
        if i > len(l1):
            l[-i] = l2[-i]
        elif i > len(l2):
            l[-i] = l1[-i]
        elif l1[-i] == l2[-i]:
            l[-i] = 0
        else:
            l[-i] = 1
    return l#返り値はリストであることに注意


n, k = map(int,input().split())
a = list(map(int,input().split()))

a_bin = [0] * n

for i in range(0,n):
    a_bin[i] = list(map(int,list(bin(a[i]))[2:]))
    
print("a_bin")
print(a_bin)

k_bin = list(map(int, list(bin(k+1))[2:]))
print("k_bin")
print(k_bin)


print("x_bin")
print(x_bin)

m = [0] * len(k_bin)

for i in range(0,len(k_bin)):
    x_bin = [0] * (len(k_bin))
    if k_bin[i] == 0:
        continue
    for j in range(0,i):
        x_bin[j] = k_bin[j]
    
    print(x_bin)
    
    
    for j in range(1,len(k_bin)-i):
        count = 0
        for l in range(0,n):
            if j <= len(a_bin[l]):
                count += a_bin[l][-j]
        print([-j,count])
        if count <= n - count:
            x_bin[-j] = 1
    
    val = 0
    print(x_bin)
    
    for j in range(0,n):
        lis = list(map(str, xor_plus(x_bin,a_bin[j])))
        print(lis)
        val = val + int(''.join(lis),2)
        print(val)
    m[i] = val

print(max(m))

Buggy submission (verdict: Runtime Error):
import math

def binary(n):
    l = [0] * 40#n<10^12<2^40
    for i in range(39,-1,-1):
        if n >= 2 ** i:
            l[i] = 1
            n = n - 2 ** i
        else:
            l[i] = 0
    
    return l#返り値はリストであることに注意

def xor_plus(l1,l2):#引数はリストであることに注意
    l = [0] * 40
    for i in range(0,40):
        if l1[i] == l2[i]:
            l[i] = 0
        else:
            l[i] = 1
    return l#返り値はリストであることに注意

def backto10(l):
    s = 0
    for i in range(0,len(l)):
        s = s + l[i] * (2 ** i)
    return s
        

n, k = map(int,input().split())
a = list(map(int,input().split()))

b = [0] * n

for i in range(0,n):
    b[i] = binary(a[i])
    
l = int(math.log2(k))
ansx = 2 ** l

s = 0

for i in range(0,n):
    s = s + backto10(xor_plus(binary(ansx),b[i]))

print(s)

Buggy submission (verdict: Runtime Error):
import math

def binary(n):
    l = [0] * 40#n<10^12<2^40
    for i in range(39,-1,-1):
        if n >= 2 ** i:
            l[i] = 1
            n = n - 2 ** i
        else:
            l[i] = 0
    
    return l#返り値はリストであることに注意

def xor_plus(l1,l2):#引数はリストであることに注意
    l = [0] * 40
    for i in range(0,40):
        if l1[i] == l2[i]:
            l[i] = 0
        else:
            l[i] = 1
    return l#返り値はリストであることに注意

def backto10(l):
    s = 0
    for i in range(0,len(l)):
        s = s + l[i] * (2 ** i)
    return s
        

n, k = map(int,input().split())
a = list(map(int,input().split()))

b = [0] * n

for i in range(0,n):
    b[i] = binary(a[i])
    
l = int(math.log2(k))
ansx = 2 ** l

s = 0

for i in range(0,n):
    s = s + backto10(xor_plus(binary(ansx),b[i]))

print(s)

Buggy submission (verdict: Runtime Error):
import math

def binary(n):
    l = [0] * 40#n<10^12<2^40
    for i in range(39,-1,-1):
        if n >= 2 ** i:
            l[i] = 1
            n = n - 2 ** i
        else:
            l[i] = 0
    
    return l#返り値はリストであることに注意

def xor_plus(l1,l2):#引数はリストであることに注意
    l = [0] * 40
    for i in range(0,40):
        if l1[i] == l2[i]:
            l[i] = 0
        else:
            l[i] = 1
    return l#返り値はリストであることに注意

n, k = map(int,input().split())
a = list(map(int,input().split()))

b = [0] * n

for i in range(0,n):
    b[i] = binary(a[i])
    
l = int(math.log2(k))
ansx = 2 ** l

sum = 0

for i in range(0,n):
    sum = sum + xor_plus(binary(ansx),b[i])

return(sum)