Problem p02798 (python)

Fixed submission:
def main():
    import sys
    from itertools import combinations
    from operator import itemgetter
    input = sys.stdin.readline

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

    even = []
    odd = []
    for i in range(N):
        if i%2:
            odd.append(A[i])
            even.append(B[i])
        else:
            even.append(A[i])
            odd.append(B[i])

    L = list(range(N))
    ans = 10**9
    for v in combinations(L, N//2):
        ok = 1
        tmp = []
        tmp_odd = []
        tmp_even = []
        for i in range(N):
            if i in v:
                tmp.append(odd[i] * 100)
                tmp_odd.append((odd[i], i))
            else:
                tmp.append(even[i] * 100)
                tmp_even.append((even[i], i))

        tmp_odd.sort(key=itemgetter(0))
        tmp_even.sort(key=itemgetter(0))

        tmp_check = []
        p = [0] * N
        for i in range(N):
            if i%2:
                tmp_check.append(tmp_odd[i//2][0])
                p[tmp_odd[i//2][1]] = i
            else:
                tmp_check.append(tmp_even[i // 2][0])
                p[tmp_even[i // 2][1]] = i
        for i in range(N-1):
            if tmp_check[i] > tmp_check[i+1]:
                ok = 0
                # print(v, tmp_check, odd, even)
                break
        if not ok:
            continue

        for i in range(N):
            tmp[i] += p[i]

        ans_new = 0
        for i in range(N):
            for j in range(i+1, N):
                if tmp[i] > tmp[j]:
                    ans_new += 1
        # print(v, tmp, ans_new, odd, even)
        ans = min(ans, ans_new)

    if ans == 10**9:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    from itertools import combinations
    input = sys.stdin.readline

    class Bit:
        def __init__(self, n):
            self.size = n
            self.tree = [0] * (n + 1)

        def sum(self, i):
            s = 0
            while i > 0:
                s += self.tree[i]
                i -= i & -i
            return s

        def add(self, i, x):
            while i <= self.size:
                self.tree[i] += x
                i += i & -i

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

    even = []
    odd = []
    for i in range(N):
        if i%2:
            odd.append(A[i])
            even.append(B[i])
        else:
            even.append(A[i])
            odd.append(B[i])

    L = list(range(N))
    ans = 10**9
    for v in combinations(L, N//2):
        ok = 1
        tmp = []
        tmp_odd = []
        tmp_even = []
        for i in range(N):
            if i in v:
                tmp.append(odd[i] * 100 + (v.index(i) * 2+1))
                tmp_odd.append(odd[i])
            else:
                cnt = 0
                for j in range(N):
                    if j == i:
                        break
                    if j not in v:
                        cnt += 1
                tmp.append(even[i] * 100 + cnt * 2)
                tmp_even.append(even[i])

        tmp_odd.sort()
        tmp_even.sort()
        tmp_check = []
        for i in range(N):
            if i%2:
                tmp_check.append(tmp_odd[i//2])
            else:
                tmp_check.append(tmp_even[i // 2])
        for i in range(N-1):
            if tmp_check[i] > tmp_check[i+1]:
                ok = 0
                # print(v, tmp_check, odd, even)
                break
        if not ok:
            continue

        bit = Bit(5100)
        ans_new = 0
        for i in range(N-1, -1, -1):
            ans_new += bit.sum(tmp[i]-1)
            bit.add(tmp[i], 1)
        # print(v, tmp, ans_new, odd, even)
        ans = min(ans, ans_new)

    if ans == 10**9:
        assert False
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    main()