Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

used = [False] * N
def solve(index, cur_elem, cost, cur_min):
    #print(" " * index, cur_elem, cost, cur_min)
    for i in range(N):
        if used[i]:
            continue

        if (index - i) % 2 == 0:
            if A[i] >= cur_elem:
                new_cost = cost
                for j in range(i, N):
                    if used[j]:
                        new_cost += 1
                if new_cost < cur_min:
                    if index + 1 == N and new_cost < cur_min:
                        cur_min = new_cost
                    else:
                        used[i] = True
                        cur_min = min(cur_min, solve(index + 1, A[i], new_cost, cur_min))
                        used[i] = False
        else:
            if B[i] >= cur_elem:
                cost_delta = 0
                for j in range(i, N):
                    if used[j]:
                        cost_delta += 1
                new_cost = cost + cost_delta
                if new_cost < cur_min:
                    if index + 1 == N and new_cost < cur_min:
                        cur_min = new_cost
                    else:
                        used[i] = True
                        cur_min = min(cur_min, solve(index + 1, B[i], new_cost, cur_min))
                        used[i] = False
    return cur_min

ans = solve(0, -1, 0, 100000)
if ans == 100000:
    print(-1)
else:
    print(ans)



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

dp = [[100100] * (1 << N) for _ in range(N)]
for i in range(N):
    dp[i][1 << i] = 0
for mask in range(1, 1 << N):
    mask_pop = format(mask, 'b').count('1')
    for j in range(N):
        if (1 << j) & mask:
            if (j - mask_pop) % 2 == 1:
                cur_elem = A[j]
            else: 
                cur_elem = B[j]
            prev_mask = mask & ~(1 << j)
            cost_delta = format(prev_mask & (-1 << j), 'b').count('1')
            for t in range(N):
                if (1 << t) & prev_mask:
                    prev_elem = A[t] if (t - mask_pop - 1) % 2 == 1 else B[t]
                    if prev_elem <= cur_elem:
                        if dp[j][mask] > dp[t][prev_mask] + cost_delta:
                            dp[j][mask] = dp[t][prev_mask] + cost_delta
            
# print(*dp, sep = '\n')

ans = min((dp[i][-1] for i in range(N)))
if ans == 100100:
    print(-1)
else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

used = [False] * N
def solve(index, cur_elem, cost, cur_min):
    #print(" " * index, cur_elem, cost, cur_min)
    for i in range(N):
        if used[i]:
            continue

        if (index - i) % 2 == 0:
            if A[i] >= cur_elem:
                new_cost = cost
                for j in range(i, N):
                    if used[j]:
                        new_cost += 1
                if new_cost < cur_min:
                    if index + 1 == N and new_cost < cur_min:
                        cur_min = new_cost
                    else:
                        used[i] = True
                        cur_min = min(cur_min, solve(index + 1, A[i], new_cost, cur_min))
                        used[i] = False
        else:
            if B[i] >= cur_elem:
                cost_delta = 0
                for j in range(i, N):
                    if used[j]:
                        cost_delta += 1
                new_cost = cost + cost_delta
                if new_cost < cur_min:
                    if index + 1 == N and new_cost < cur_min:
                        cur_min = new_cost
                    else:
                        used[i] = True
                        cur_min = min(cur_min, solve(index + 1, B[i], new_cost, cur_min))
                        used[i] = False
    return cur_min

ans = solve(0, -1, 0, 100000)
if ans == 100000:
    print(-1)
else:
    print(ans)


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

dp = [[100100] * (1 << N) for _ in range(N)]
for i in range(N):
    dp[i][1 << i] = 0
for mask in range(1, 1 << N):
    mask_pop = format(mask, 'b').count('1')
    for j in range(N):
        if (1 << j) & mask:
            if (j - mask_pop) % 2 == 1:
                cur_elem = A[j]
            else: 
                cur_elem = B[j]
            prev_mask = mask & ~(1 << j)
            cost_delta = format(prev_mask & (-1 << j), 'b').count('1')
            for t in range(N):
                if (1 << t) & prev_mask:
                    prev_elem = A[t] if (t - mask_pop - 1) % 2 == 1 else B[t]
                    if prev_elem <= cur_elem:
                        if dp[j][mask] > dp[t][prev_mask] + cost_delta:
                            dp[j][mask] = dp[t][prev_mask] + cost_delta
            
# print(*dp, sep = '\n')

ans = min((dp[i][-1] for i in range(N)))
if ans == 100100:
    print(-1)
else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

def solve(not_used, index, cur_elem, cost, cur_min):
    #print("\t" * index, cur_elem, cost)
    if index == N:
        return cost
    for i in not_used:
        if (index - i) % 2 == 0:
            if A[i] >= cur_elem:
                cost_delta = 0
                for j in range(i, N):
                    if j not in not_used:
                        cost_delta += 1
                new_cost = cost + cost_delta
                if new_cost < cur_min:
                    cur_min = min(cur_min, solve(not_used - {i}, index + 1, A[i], new_cost, cur_min))
        else:
            if B[i] >= cur_elem:
                cost_delta = 0
                for j in range(i, N):
                    if j not in not_used:
                        cost_delta += 1
                new_cost = cost + cost_delta
                if new_cost < cur_min:
                    cur_min = min(cur_min, solve(not_used - {i}, index + 1, B[i], new_cost, cur_min))
    return cur_min

ans = solve({i for i in range(N)}, 0, -1, 0, 100000)
if ans == 100000:
    print(-1)
else:
    print(ans)


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

dp = [[100100] * (1 << N) for _ in range(N)]
for i in range(N):
    dp[i][1 << i] = 0
for mask in range(1, 1 << N):
    mask_pop = format(mask, 'b').count('1')
    for j in range(N):
        if (1 << j) & mask:
            if (j - mask_pop) % 2 == 1:
                cur_elem = A[j]
            else: 
                cur_elem = B[j]
            prev_mask = mask & ~(1 << j)
            cost_delta = format(prev_mask & (-1 << j), 'b').count('1')
            for t in range(N):
                if (1 << t) & prev_mask:
                    prev_elem = A[t] if (t - mask_pop - 1) % 2 == 1 else B[t]
                    if prev_elem <= cur_elem:
                        if dp[j][mask] > dp[t][prev_mask] + cost_delta:
                            dp[j][mask] = dp[t][prev_mask] + cost_delta
            
# print(*dp, sep = '\n')

ans = min((dp[i][-1] for i in range(N)))
if ans == 100100:
    print(-1)
else:
    print(ans)