Problem p02798 (python)

Fixed submission:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

def f(n):
    count=0
    while n:
        count+=n%2
        n//=2
    return count

n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
for bit in range(2**n):
    # if f(bit)%2!=0:
    #     continue
    arr=[]
    for i in range(n):
        if bit >> i & 1:
            arr.append((B[i],i))
        else:
            arr.append((A[i],i))
    i=0;flug=1;tmp=0
    while flug:
        flug=0
        for j in range(n-1,i,-1):
            if arr[j-1][0]>arr[j][0]:
                arr[j-1],arr[j]=arr[j],arr[j-1]
                tmp+=1; flug=1
        i+=1
    stack=[]
    for i in range(n):
        if (abs(arr[i][1]-i)) % 2 != bit >> arr[i][1] & 1:
            stack.append(arr[i][0]*1000+i)
    # if bit == 3:
        # print(tmp,stack,arr,bin(bit))
        # print((abs(arr[1][1]-1)) % 2 , bit >> arr[1][1])
    if len(stack) & 1:
        tmp=1000; stack.pop()
    while stack:
        p=stack.pop(); q=stack.pop()
        if (p-q) & 1:
            tmp+=p-q
            # if bit==3:
                # print(p,q,tmp,mans)
                # print(arr)
        else:
            tmp+=1000
    mans=min(mans,tmp)

print(-1 if mans > 999 else mans)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1


def babble_sort(arr):
    """ バブルソート """
    length = len(arr)
    count=0
    data=[i for i in range(length)]
    swap=[0]*length

    for i in range(length):
        # 3. 走査範囲を前からひとつ狭める
        for j in reversed(range(i+1, length)):
            # 1. 後ろから順に隣り合う要素を比較する。
            if arr[j-1] > arr[j]:
                # 2. 左が右の要素に比べ大きい場合交換する。
                arr[j-1], arr[j] = arr[j], arr[j-1]
                data[j-1], data[j] = data[j], data[j-1]
                swap[data[j]]+=1
                swap[data[j-1]]+=1
                count+=1
    return count,swap

def f(n):
    count=0
    while n:
        count+=n%2
        n//=2
    return count

n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
for bit in range(2**n):
    # if bit==3:
    #     print(f(bit))
    if f(bit)%2!=0:
        continue
    tar=[0]*n
    data=[0]*n
    for i in range(n):
        if (bit>>i & 1):
            tar[i]=B[i]; data[i]=1
        else:
            tar[i]=A[i]
    tmp,swap=babble_sort(tar[:])
    S=[[]]*51
    # if bit==3:
    #     print(data,swap,tar,bit,sep="\n")
    for i in range(n):
        if swap[i]%2!=data[i]:
            S[tar[i]].append(1)
        else:
            S[tar[i]].append(0)
    for k in range(51):
        for s in range(len(S[k])):
            if S[k][s]==1:
                for t in range(s+1,len(S),2):
                    if S[k][t]==1:
                        S[k][s]=S[k][t]=0
                        tmp+=t-s
                        break
                else:
                    break

                continue
        else:
            continue
        break
    else:
        mans=min(mans,tmp)
    continue
print(-1 if mans==inf else mans)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1


def babble_sort(arr):
    """ バブルソート """
    length = len(arr)
    count=0
    data=[i for i in range(length)]
    swap=[0]*length

    for i in range(length):
        # 3. 走査範囲を前からひとつ狭める
        for j in reversed(range(i+1, length)):
            # 1. 後ろから順に隣り合う要素を比較する。
            if arr[j-1] > arr[j]:
                # 2. 左が右の要素に比べ大きい場合交換する。
                arr[j-1], arr[j] = arr[j], arr[j-1]
                data[j-1], data[j] = data[j], data[j-1]
                swap[data[j]]+=1
                swap[data[j-1]]+=1
                count+=1
    return count,swap

def f(n):
    count=0
    while n:
        count+=n%2
        n//=2
    return count

n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
for bit in range(2**n):
    # if bit==3:
    #     print(f(bit))
    if f(bit)%2!=0:
        continue
    tar=[0]*n
    data=[0]*n
    for i in range(n):
        if (bit>>i & 1):
            tar[i]=B[i]; data[i]=1
        else:
            tar[i]=A[i]
    tmp,swap=babble_sort(tar[:])
    S=[[]]*51
    # if bit==3:
    #     print(data,swap,tar,bit,sep="\n")
    for i in range(n):
        if swap[i]%2!=data[i]:
            S[tar[i]].append(1)
        else:
            S[tar[i]].append(0)
    for k in range(52):
        for s in range(len(S[k])):
            if S[k][s]==1:
                for t in range(s+1,len(S),2):
                    if S[k][t]==1:
                        S[k][s]=S[k][t]=0
                        tmp+=t-s
                        break
                else:
                    break

                continue
        else:
            continue
        break
    else:
        mans=min(mans,tmp)
    continue
print(-1 if mans==inf else mans)