Problem p02798 (python)

Fixed submission:
def main():
    from itertools import combinations
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    cc = combinations(range(n), (n-1)//2+1)
    m = 10**9
    if n == 1:
        print(0)
        return
    for c in cc:
        memo1 = []
        memo2 = []
        for i in c:
            if i % 2 == 0:
                memo1.append((a[i], i))
            else:
                memo1.append((b[i], i))
        for i in set(range(n))-set(c):
            if i % 2 == 1:
                memo2.append((a[i], i))
            else:
                memo2.append((b[i], i))
        memo1.sort()
        memo2.sort()
        memo = []
        for i in range(n//2):
            memo.append(memo1[i])
            memo.append(memo2[i])
        if n % 2 == 1:
            memo.append(memo1[i+1])
        flag = 0
        for i in range(n-1):
            if memo[i][0] > memo[i+1][0]:
                flag = 1
                break
        if flag == 1:
            continue
        memo = [i for j, i in memo]
        cnt = 0
        for i in range(n-1):
            for j in range(i+1, n):
                if memo[i] > memo[j]:
                    cnt += 1
        m = min(cnt, m)
    if m == 10**9:
        print(-1)
    else:
        print(m)


main()

Buggy submission (verdict: Runtime Error):
def main():
    from itertools import combinations
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    cc = combinations(range(n), (n-1)//2+1)
    m = 10**9
    for c in cc:
        memo1 = []
        memo2 = []
        for i in c:
            if i % 2 == 0:
                memo1.append((a[i], i))
            else:
                memo1.append((b[i], i))
        for i in set(range(n))-set(c):
            if i % 2 == 1:
                memo2.append((a[i], i))
            else:
                memo2.append((b[i], i))
        memo1.sort()
        memo2.sort()
        memo = []
        for i in range(n//2):
            memo.append(memo1[i])
            memo.append(memo2[i])
        if n % 2 == 1:
            memo.append(memo1[i+1])
        flag = 0
        for i in range(n-1):
            if memo[i][0] > memo[i+1][0]:
                flag = 1
                break
        if flag == 1:
            continue
        memo = [i for j, i in memo]
        cnt = 0
        for i in range(n-1):
            for j in range(i+1, n):
                if memo[i] > memo[j]:
                    cnt += 1
        m = min(cnt, m)
    if m == 10**9:
        print(-1)
    else:
        print(m)


main()

Buggy submission (verdict: Runtime Error):
def main():
    from itertools import combinations
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    cc = combinations(range(n), (n-1)//2+1)
    m = -1
    for c in cc:
        memo1 = []
        memo2 = []
        for i in c:
            if i % 2 == 0:
                memo1.append((a[i], i))
            else:
                memo1.append((b[i], i))
        for i in set(range(n))-set(c):
            if i % 2 == 1:
                memo2.append((a[i], i))
            else:
                memo2.append((b[i], i))
        memo1.sort()
        memo2.sort()
        memo = []
        for i in range(n//2):
            memo.append(memo1[i])
            memo.append(memo2[i])
        if n % 2 == 1:
            memo.append(memo1[i+1])
        flag = 0
        for i in range(n-1):
            if memo[i][0] > memo[i+1][0]:
                flag = 1
                break
        if flag == 1:
            continue
        memo = [i for j, i in memo]
        cnt = 0
        for i in range(n-1):
            for j in range(i+1, n):
                if a[i] > a[j]:
                    cnt += 1
        m = max(cnt, m)
    print(m)


main()