Problem p02557 (python)

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cntA = [0] * (N + 1)
cntB = [0] * (N + 1)
for i in range(N):
    cntA[A[i]] += 1
    cntB[B[i]] += 1
nums = [(cntA[i], cntB[i], i) for i in range(1, N+1)]
nums.sort(key=lambda p: p[0] + p[1], reverse=True)
a = deque()
b = deque()
c = deque(list(range(N)))
ansa = [0] * N
ansb = [0] * N
for x, y, n in nums:
    if x == y == 0:
        break
    if x + y > len(a) + len(b) + len(c):
        print('No')
        exit()
    if x + y <= len(c):
        for i in range(x):
            ansa[c[0]] = n
            b.append(c[0])
            c.popleft()
        for i in range(y):
            ansb[c[0]] = n
            a.append(c[0])
            c.popleft()
    else:
        ra = x - len(a)
        rb = y - len(b)
        if ra > 0:
            for i in range(ra):
                ansa[c[0]] = n
                b.append(c[0])
                c.popleft()
            x -= ra
        if rb > 0:
            for i in range(rb):
                ansb[c[0]] = n
                a.append(c[0])
                c.popleft()
            y -= rb
        while len(c):
            if x:
                ansa[c[0]] = n
                b.append(c[0])
                x -= 1
            else:
                ansb[c[0]] = n
                a.append(c[0])
                y -= 1
            c.popleft()
        for i in range(x):
            ansa[a[0]] = n
            a.popleft()
        for i in range(y):
            ansb[b[0]] = n
            b.popleft()
ans = []
for i in range(N):
    ans.append((ansa[i], ansb[i]))
ans.sort()
print('Yes')
print(*[p[1] for p in ans])

Buggy submission (verdict: Runtime Error):
from collections import deque
import sys
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cntA = [0] * (N + 1)
cntB = [0] * (N + 1)
for i in range(N):
    cntA[A[i]] += 1
    cntB[B[i]] += 1
nums = [(cntA[i], cntB[i], i) for i in range(1, N+1)]
nums.sort(key=lambda p: p[0] + p[1], reverse=True)
a = deque()
b = deque()
c = deque(list(range(N)))
ansa = [0] * N
ansb = [0] * N
for x, y, n in nums:
    if x == y == 0:
        break
    if x + y > len(a) + len(b) + len(c):
        print('No')
        exit()
    if x + y <= len(c):
        for i in range(x):
            ansa[c[0]] = n
            b.append(c[0])
            c.popleft()
        for i in range(y):
            ansb[c[0]] = n
            a.append(c[0])
            c.popleft()
    else:
        m = x - len(a)
        if m > 0:
            for i in range(m):
                ansa[c[0]] = n
                b.append(c[0])
                c.popleft()
            x -= m
        m = len(c)
        for i in range(m):
            ansb[c[0]] = n
            a.append(c[0])
            c.popleft()
        y -= m
        for i in range(x):
            ansa[a[0]] = n
            a.popleft()
        for i in range(y):
            ansb[b[0]] = n
            b.popleft()
ans = []
for i in range(N):
    ans.append((ansa[i], ansb[i]))
ans.sort()
print('Yes')
print(*[p[1] for p in ans])