Problem p02798 (python)

Fixed submission:
# coding: utf-8
import sys
from itertools import permutations,combinations
sys.setrecursionlimit(10 ** 7)

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 全探索
INF = 10 ** 5
answer = INF
N = ir()
if N == 1:
    print(0); exit()
A = lr()
B = lr()
for pattern in combinations(range(N), (N+1)//2):
    pattern = set(pattern)
    red = []
    blue = []
    for i in range(N):
        if i in pattern:
            red.append((B[i], i) if i&1 else (A[i], i))
        else:
            blue.append((A[i], i) if i&1 else (B[i], i))
    red.sort()
    blue.sort()
    result = []
    
    for i in range(N//2):
        if red[i][0] > blue[i][0]:
            break
        if i + 1 < len(red) and red[i+1][0] < blue[i][0]:
            break
        result.append(red[i][1])
        result.append(blue[i][1])
    else:
        if N&1 and blue[-1][0] > red[-1][0]:
            continue
        if N&1:
            result.append(red[-1][1])
        temp = 0
        used = set()
        for i in range(N):
            for x in used:
                if result[i] < x:
                    temp += 1
            used.add(result[i])            
        if temp < answer:
            answer = temp

print(answer if answer != INF else -1)
# 12

Buggy submission (verdict: Runtime Error):
# coding: utf-8
import sys
from itertools import permutations,combinations
sys.setrecursionlimit(10 ** 7)

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 全探索
INF = 10 ** 5
answer = INF
N = ir()
A = lr()
B = lr()
for pattern in combinations(range(N), (N+1)//2):
    pattern = set(pattern)
    red = []
    blue = []
    for i in range(N):
        if i in pattern:
            red.append((B[i], i) if i&1 else (A[i], i))
        else:
            blue.append((A[i], i) if i&1 else (B[i], i))
    red.sort()
    blue.sort()
    temp = 0
    for i in range(N//2):
        if red[i][0] > blue[i][0]:
            break
        if i + 1 < len(red) and red[i+1] < blue[i]:
            break
        temp += abs(red[i][1] - 2 * i)
        temp += abs(blue[i][1] - (2 * i + 1))
    else:
        if N&1 and blue[-1][0] > red[-1][0]:
            continue
        temp //= 2
        if temp < answer:
            answer = temp

print(answer if answer != INF else -1)
# 12

Buggy submission (verdict: Runtime Error):
# coding: utf-8
import sys
from itertools import permutations,combinations

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 全探索
INF = 10 ** 5
answer = INF
N = ir()
A = lr()
B = lr()
for pattern in combinations(range(N), (N+1)//2):
    pattern = set(pattern)
    red = []
    blue = []
    for i in range(N):
        if i in pattern:
            red.append((B[i], i) if i&1 else (A[i], i))
        else:
            blue.append((A[i], i) if i&1 else (B[i], i))
    red.sort()
    blue.sort()
    temp = 0
    for i in range(N//2):
        if red[i][0] > blue[i][0]:
            break
        if i + 1 < len(red) and red[i+1] < blue[i]:
            break
        temp += abs(red[i][1] - 2 * i)
        temp += abs(blue[i][1] - (2 * i + 1))
    else:
        if N&1 and blue[-1][0] > red[-1][0]:
            continue
        temp //= 2
        if temp < answer:
            answer = temp

print(answer if answer != INF else -1)
# 12