Problem p03138 (python)

Fixed submission:
def XOR(A, B):
    BA = int(A)
    BB = int(B)
    result = BA + BB
    result = str(result)
    result = result.replace('2', '0')
    result = int(result, 2)
    return result


L1 = input()
L2 = input()
L1 = L1.split()
L2 = L2.split()
for i, val in enumerate(L1):
    L1[i] = int(val)
K = L1[1]
K = bin(K)
K = K[2:]
lenK = len(K)
L3 = []
for i, val in enumerate(L2):
    L2[i] = int(val)
    L2[i] = bin(L2[i])
    lenL2i = len(L2[i])-2
    L2[i] = L2[i][2:]
    if lenL2i < lenK:
        dif = lenK - lenL2i
        for a in range(dif):
            L2[i] = '0' + L2[i]
        L3.append(L2[i])
    elif lenL2i == lenK:
        L3.append(L2[i])
    else:
        dif = lenL2i - lenK
        L3.append(L2[i][dif:])
final = ''
judgeFlag = 0
i = 0
finalJudge = 0
# print('K'+K)
if K == '0':
    final = '0'
else:
    while i < lenK:
        # print('roop'+str(i))
        # print(final)
        # print('judgeFlag' + str(judgeFlag))
        # print('finalJudge' + str(finalJudge))
        if (judgeFlag == 1) & (finalJudge == 0):
            if K[i] == '0':
                final += '0'
                i += 1
            else:
                # print(i)
                count_0 = 0
                count_1 = 0
                for each in L3:
                    if each[i] == '0':
                        count_1 += 1
                    else:
                        count_0 += 1
                if count_1 >= count_0:
                    final += '1'
                    judgeFlag = 1
                else:
                    final += '0'
                    finalJudge = 1
                i += 1
        else:
            # print(i)
            count_0 = 0
            count_1 = 0
            for each in L3:
                if each[i] == '0':
                    count_1 += 1
                else:
                    count_0 += 1
            if count_1 >= count_0:
                final += '1'
                judgeFlag = 1
            else:
                final += '0'
                finalJudge = 1
            i += 1
sumInt = 0
for each in L2:
    sumInt += XOR(final, each)
# print(final)
print(sumInt)

Buggy submission (verdict: Runtime Error):
def XOR(A, B):
    BA = int(A)
    BB = int(B)
    result = BA + BB
    result = str(result)
    result = result.replace('2', '0')
    result = int(result, 2)
    return result


L1 = input()
L2 = input()
L1 = L1.split()
L2 = L2.split()
for i, val in enumerate(L1):
    L1[i] = int(val)
K = L1[1]
K = bin(K)
K = K[2:]
N = L1[0]
# print(K)
flag = 1
count = 1
lenK = len(K)
while flag == 1:
    if K[count] == 1:
        flag = 0
    elif (count+1) == lenK:
        flag = 0
        count += 1
    else:
        count += 1
if count == lenK:
    specialFlag = 1
    count = 1
    final = '0'
else:
    final = ''


# print(count)

for i, val in enumerate(L2):
    L2[i] = int(val)
    L2[i] = bin(L2[i])
    lenL2i = len(L2[i])-2
    L2[i] = L2[i][2:]
    if lenL2i < lenK:
        dif = lenK - lenL2i
        for a in range(dif):
            L2[i] = '0' + L2[i]

for i in range(count, lenK, 1):
    count_0 = 0
    count_1 = 0
    for each in L2:
            if each[i] == '1':
                count_0 += 1
            if each[i] == '0':
                count_1 += 1
    # print(count_0)
    # print(count_1)
    if count_1 >= count_0:
        final += '1'
    else:
        final += '0'
# print(final)
sumInt = 0
for each in L2:
    sumInt += XOR(final, each)
if specialFlag == 1:
    specialInt = 0
    for each in L2:
        specialInt += XOR(K, each)
    # print(specialInt)
    # print(sumInt)
    sumInt = max(sumInt, specialInt)
# print(final)
print(sumInt)