Buggy python submission (verdict: Time Limit Exceeded):
import sys
readline = sys.stdin.readline

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

from collections import Counter

cA = Counter(A)
cB = Counter(B)

# 同じ文字が両方に存在していて、その個数の和がNを越えていたらNG
for key,value in cA.items():
  if key in cB:
    if value + cB[key] > N:
      print("No")
      exit(0)
print("Yes")

from collections import deque
q = deque(B)

ans = []

a_ind = 0
while a_ind < N:
  while True:
    b = q.popleft()
    if b != A[a_ind]:
      ans.append(b)
      break
    else:
      q.append(b)
  a_ind += 1
  
print(*ans)

Fixed submission:
import sys
readline = sys.stdin.readline

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

from collections import Counter

cA = Counter(A)
cB = Counter(B)

# 両チームに入っている文字の合計数が多い順に処理していく
# heapqで、(両方に入っている個数, Bにいる個数, その数)
# 使うごとに、1つ目、2つ目の要素を-1してもう一度heappush

import heapq as hq
q = []
hq.heapify(q)
for key,value in cB.items():
  if key in cA:
    hq.heappush(q, (-(value + cA[key]), value, key))
    if value + cA[key] > N:
      print("No")
      exit(0)
  else:
    hq.heappush(q, (-value, value, key))
print("Yes")
a_ind = 0
ans = []

while a_ind < N:
  keep = []
  while True:
    allcnt, bcnt, key = hq.heappop(q)
    allcnt *= (-1)
    if A[a_ind] == key:
      keep.append((allcnt, bcnt, key))
    else:
      ans.append(key)
      if bcnt - 1 > 0:
        hq.heappush(q, (-(allcnt - 1), bcnt - 1, key))
      break
  for k in keep:
    hq.heappush(q, k)
  a_ind += 1
    
print(*ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
readline = sys.stdin.readline

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

from collections import Counter

cA = Counter(A)
cB = Counter(B)

# 同じ文字が両方に存在していて、その個数の和がNを越えていたらNG
for key,value in cA.items():
  if key in cB:
    if value + cB[key] > N:
      print("No")
      exit(0)
print("Yes")

from collections import deque
q = deque(B)

ans = []

a_ind = 0
while a_ind < N:
  while True:
    b = q.popleft()
    if b != A[a_ind]:
      ans.append(b)
      break
    else:
      q.append(b)
  a_ind += 1
  
print(*ans)

Fixed submission:
import sys
readline = sys.stdin.readline

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

from collections import Counter

cA = Counter(A)
cB = Counter(B)

# 両チームに入っている文字の合計数が多い順に処理していく
# heapqで、(両方に入っている個数, Bにいる個数, その数)
# 使うごとに、1つ目、2つ目の要素を-1してもう一度heappush

import heapq as hq
q = []
hq.heapify(q)
for key,value in cB.items():
  if key in cA:
    hq.heappush(q, (-(value + cA[key]), value, key))
    if value + cA[key] > N:
      print("No")
      exit(0)
  else:
    hq.heappush(q, (-value, value, key))
print("Yes")
a_ind = 0
ans = []

while a_ind < N:
  keep = []
  while True:
    allcnt, bcnt, key = hq.heappop(q)
    allcnt *= (-1)
    if A[a_ind] == key:
      keep.append((allcnt, bcnt, key))
    else:
      ans.append(key)
      if bcnt - 1 > 0:
        hq.heappush(q, (-(allcnt - 1), bcnt - 1, key))
      break
  for k in keep:
    hq.heappush(q, k)
  a_ind += 1
    
print(*ans)