Buggy python submission (verdict: Runtime Error):
from collections import Counter

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

AC = Counter(A)
BC = Counter(B)
if N < max((AC + BC).values()):
    print('No')
    exit()

a = 0
b = 0
while a < N:
    if A[a] == B[a]:
        while A[a] == B[b] or A[b] == B[a]:
            b = (b + 1)
            if b >= N:
                raise Exception
        B[a], B[b] = B[b], B[a]
    a += 1

print('Yes')
print(*B)


Fixed submission:
from collections import Counter
from heapq import heappush, heappop


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

AC = Counter(A)
BC = Counter(B)
S = AC + BC

q = []
for k, v in S.items():
    heappush(q, (-v, k))

ans = []
while q:
    v, k = heappop(q)
    v = -v
    if v != S[k]:
        if S[k] != 0:
            heappush(q, (-S[k], k))
    else:
        if v != 1:
            heappush(q, (-(v - 1), k))
        if v > N:
            print('No')
            exit()
        else:
            if AC[k] > 0:
                a = k
                for l in BC.keys():
                    if l != k:
                        b = l
                        break
            else:
                b = k
                for l in AC.keys():
                    if l != k:
                        a = l
                        break
            ans.append((a, b))
            S[a] -= 1
            S[b] -= 1
            AC[a] -= 1
            if not AC[a]:
                AC.pop(a)
            BC[b] -= 1
            if not BC[b]:
                BC.pop(b)

print('Yes')
ans.sort()
X = [b for a, b in ans]
print(*X)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
from operator import itemgetter


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

AC = Counter(A)
BC = Counter(B)

judge = True
for a, na in AC.items():
    nb = BC[a]
    judge &= (na <= (N - nb))

if judge:
    ans = [None] * N
    print('Yes')
    cur = 0
    for b, nb in reversed(sorted(BC.items(), key=itemgetter(1))):
        cnt = nb
        while cnt:
            if A[cur] != b and ans[cur] is None:
                ans[cur] = b
                cnt -= 1
            cur = (cur + 1) % N
    print(*ans)
else:
    print('No')


Fixed submission:
from collections import Counter
from heapq import heappush, heappop


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

AC = Counter(A)
BC = Counter(B)
S = AC + BC

q = []
for k, v in S.items():
    heappush(q, (-v, k))

ans = []
while q:
    v, k = heappop(q)
    v = -v
    if v != S[k]:
        if S[k] != 0:
            heappush(q, (-S[k], k))
    else:
        if v != 1:
            heappush(q, (-(v - 1), k))
        if v > N:
            print('No')
            exit()
        else:
            if AC[k] > 0:
                a = k
                for l in BC.keys():
                    if l != k:
                        b = l
                        break
            else:
                b = k
                for l in AC.keys():
                    if l != k:
                        a = l
                        break
            ans.append((a, b))
            S[a] -= 1
            S[b] -= 1
            AC[a] -= 1
            if not AC[a]:
                AC.pop(a)
            BC[b] -= 1
            if not BC[b]:
                BC.pop(b)

print('Yes')
ans.sort()
X = [b for a, b in ans]
print(*X)