Problem:
 Score : 600 points    Problem Statement Given are two sequences A and B , both of length N . A and B are each sorted in the ascending order.
Check if it is possible to reorder the terms of B so that for each i ( 1 \leq i \leq N ) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it.      Constraints  1\leq N \leq 2 \times 10^5  1\leq A_i,B_i \leq N  A and B are each sorted in the ascending order.  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  A_1  A_2  \cdots  A_N  B_1  B_2  \cdots  B_N       Output If there exist no reorderings that satisfy the condition, print No .  If there exists a reordering that satisfies the condition, print Yes on the first line.
After that, print a reordering of B on the second line, separating terms with a whitespace.  If there are multiple reorderings that satisfy the condition, you can print any of them.        Sample Input 1 6
1 1 1 2 2 3
1 1 1 2 2 3      Sample Output 1 Yes
2 2 3 1 1 1       Sample Input 2 3
1 1 2
1 1 3      Sample Output 2 No       Sample Input 3 4
1 1 2 3
1 2 3 3      Sample Output 3 Yes
3 3 1 2

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = [0 for _ in range(n+1)]
for ai in a:
  c[ai] += 1
for bi in b:
  c[bi] += 1
if max(c) > n:
  print("No")
  exit()
print("Yes")
b.reverse()
flag = True
si = -1
ti = -1
for i in range(n):
  if flag:
    if a[i] == b[i]:
      si = i
      flag = False
  else:
    if a[i] != b[i]:
      ti = i-1
      break
if si == -1:
  for bi in b:
    print(bi,end=" ")
  exit()
if ti == -1:
  ti = n-1
s = a[si]
ni = [0 for _ in range(ti-si+1)]
xi = 0
for i in range(n):
  if xi >= ti-si+1:
    break
  if a[i] != s and b[i] != s:
    ni[xi] = b[i]
    b[i] = s
    xi += 1
nb = b[0:si] + ni + b[ti+1:]
for nbi in nb:
  print(nbi,end=" ")

Solution (Python):
from collections import Counter
import sys
import random
 
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ca = Counter(A)
cb = Counter(B)
for i in range(N+1):
    if ca[i] + cb[i] > N:
        print('No')
        sys.exit()

C = [0] * (N+1)
D = [0] * (N+1)

for i in range(N):
    C[i+1] += C[i] + ca[i+1]
    D[i+1] += D[i] + cb[i+1]

x = 0

for i in range(N):
    x = max(x, C[i+1] - D[i])

ans = [B[(i-x)%N] for i in range(N)]

print('Yes')
print(*ans)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()][::-1]

ng = []
for i in range(n):
    if a[i] == b[i]:
        ng.append(i)

if len(ng) == 0:
    print('Yes')
    print(*b)
    sys.exit()

min_ng = min(ng)
max_ng = max(ng)

left = 0
right = n - 1

for i in ng:
    if b[i] != b[left] and a[left] != b[i]:
        b[i], b[left] = b[left], b[i]
        left += 1
        continue
    if b[i] != b[right] and a[right] != b[i]:
        b[i], b[right] = b[right], b[i]
        right -= 1

flag = 1

for i in range(n):
    if a[i] == b[i]:
        flag = 0

if flag:
    print('Yes')
    print(*b)
else:
    print('No')

Solution (Python):

from collections import Counter

N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

elem_max_count = max((Counter(As) + Counter(Bs)).values())
if elem_max_count > N:
    print("No")
    exit()




ok = 0
ng = N + 1
while ng - ok > 1:
    mid = (ok + ng) // 2
    isok = True
    for b, a in zip(Bs[-mid:], As[:mid]):
        if b <= a:
            isok = False
            break
    
    if isok:
        ok = mid
    else:
        ng = mid

print("Yes")
print(*(Bs[-ok:] + Bs[:-ok]))

Solution (Python):
N = int(input())
from collections import Counter
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ca = Counter(A)
cb = Counter(B)
mod = 10**6
cnts = [set() for _ in range(N+1)]
for i in range(1,N+1):
  if ca[i]+cb[i]>N:
    print('No')
    exit()
  if ca[i]+cb[i]>0:
    cnts[ca[i]+cb[i]].add(i)

def get_any(counts,r):
  for k in counts.keys():
    if counts[k]>0 and k!=r:
      return k

def update_cnt(cnt_,r):
  cnt_[r] -= 1
  if cnt_[r]==0:
    del cnt_[r]

lis = [[] for _ in range(N+1)]
for i in range(N,0,-1):
  if len(cnts[i])==2:
    p,q = cnts[i]
    for j in range(ca[p]):
      lis[p].append(q)
    for j in range(ca[q]):
      lis[q].append(p)
    break
  elif len(cnts[i])==1:
    p, = cnts[i]
    for j in range(ca[p]):
      q = get_any(cb,p)
      update_cnt(cb,q)
      lis[p].append(q)
    for j in range(cb[p]):
      q = get_any(ca,p)
      update_cnt(ca,q)
      lis[q].append(p)
    break
  else:
    p = get_any(ca,-1)
    q = get_any(cb,p)
    lis[p].append(q)
    cnts[ca[p]+cb[p]].remove(p)
    cnts[ca[q]+cb[q]].remove(q)
    update_cnt(ca,p)
    update_cnt(cb,q)
    cnts[ca[p]+cb[p]].add(p)
    cnts[ca[q]+cb[q]].add(q)
ans = []
for i in range(1,N+1):
  for b in lis[i]:
    ans.append(b)
print('Yes')
print(*ans)

Solution (Python):
import sys
INF = 1 << 60
MOD = 10**9 + 7 
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from time import time
from random import randrange
def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    start = time()

    for i in range(n):
        if A[i] != B[i]:
            continue
        while time() - start < 1.8:
            j = randrange(n)
            if A[i] != B[j] and A[j] != B[i]:
                B[i], B[j] = B[j], B[i]
                break
        else:
            print("No")
            return

    print("Yes")
    print(*B)
resolve()

Solution (Python):
import sys
INF = 1 << 60
MOD = 10**9 + 7 
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from time import time
from random import randrange
def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    start = time()

    B.reverse()
    for i in range(n):
        if A[i] != B[i]:
            continue
        while time() - start < 1.8:
            j = randrange(n)
            if A[i] != B[j] and A[j] != B[i]:
                B[i], B[j] = B[j], B[i]
                break
        else:
            print("No")
            return

    print("Yes")
    print(*B)
resolve()

Solution (Python):
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ca=Counter(a)
cb=Counter(b)
for k in cb:
    if k in ca:
        if cb[k]+ca[k]>n:
            print('No')
            exit()
ans=b.copy()
ans.reverse()
ng=set(())
ok=[]
for i in range(n):
    if a[i]==ans[i]:
        ng.add(i)
    else:
        ok.append(i)
from random import randint
if ng:
    for x in ng:
        y=ok[randint(0,len(ok)-1)]
        while ans[x]==a[y] or ans[y]==a[x]:
            y=ok[randint(0,len(ok)-1)]
        ans[x],ans[y]=ans[y],ans[x]
        ok.append(x)
print('Yes')
print(*ans,sep=' ')

Solution (Python):
import sys
INF = 1 << 60
MOD = 10**9 + 7 
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from collections import Counter
from random import randrange
def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    if Counter(A + B).most_common()[0][1] > n:
        print("No")
        return

    B.reverse()
    for i in range(n):
        if A[i] != B[i]:
            continue
        while 1:
            j = randrange(n)
            if A[i] != B[j] and A[j] != B[i]:
                B[i], B[j] = B[j], B[i]
                break
    print("Yes")
    print(*B)
resolve()

Solution (Python):
import sys
INF = 1 << 60
MOD = 10**9 + 7 
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from collections import Counter
from random import randrange
def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    if Counter(A + B).most_common()[0][1] > n:
        print("No")
        return

    for i in range(n):
        if A[i] != B[i]:
            continue
        while 1:
            j = randrange(0, n)
            if A[i] != B[j] and A[j] != B[i]:
                B[i], B[j] = B[j], B[i]
                break
    print("Yes")
    print(*B)
resolve()

Solution (Python):
import sys
INF = 1 << 60
MOD = 10**9 + 7 
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from collections import Counter
from random import randrange
def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    if Counter(A + B).most_common()[0][1] > n:
        print("No")
        return

    for i in range(n):
        if A[i] != B[i]:
            continue
        while 1:
            j = randrange(0, n)
            if A[i] != B[j] and A[j] != B[i]:
                B[i], B[j] = B[j], B[i]
                break
    print("Yes")
    print(*B)
resolve()

Solution (Python):
import sys
INF = 1 << 60
MOD = 10**9 + 7 
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from collections import Counter
from random import randrange
def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    if Counter(A + B).most_common()[0][1] > n:
        print("No")
        return

    for i in range(n):
        if A[i] != B[i]:
            continue
        while 1:
            j = randrange(0, n)
            if A[i] != B[j] and A[j] != B[i]:
                B[i], B[j] = B[j], B[i]
                break
    print("Yes")
    print(*B)
resolve()

Solution (Python):
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ca=Counter(a)
cb=Counter(b)
for k in cb:
    if k in ca:
        if cb[k]+ca[k]>n:
            print('No')
            exit()
tmpc=0
tmpd=0
c=[0]
d=[0]
for i in range(1,n+1):
    if i in ca:tmpc+=ca[i]
    if i in cb:tmpd+=cb[i]
    c.append(tmpc)
    d.append(tmpd)
x=0
for i in range(n+1):
    x=max(x,c[i]-d[i-1])
ans=b[-x:]+b[:-x]
print('Yes')
print(*ans,sep=' ')
exit()
print(c)
print(d)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
from collections import Counter,deque
x,y=Counter(a),Counter(b)
for i in range(1,n+1):
    if x[i]+y[i]>n:
        print("No")
        exit()
ans=b[::-1]
change=deque()
num=-1
now=deque()
for i in range(n):
    if a[i]==ans[i]:
        change.append(i)
        num=ans[i]
    else:
        now.append(i)
if change==[]:
    print("Yes")
    print(" ".join(map(str,ans)))
    exit()
while len(change):
    q=now.popleft()
    p=change.popleft()
    if a[q]!=num and ans[q]!=num:
        ans[q],ans[p]=ans[p],ans[q]
    else:
        change.appendleft(p)

print("Yes")
print(" ".join(map(str,ans)))

Solution (Python):
def Next(): return input()
def NextInt(): return int(Next())
def NextInts(): return map(int,input().split())
def Nexts(): return map(str,input().split())
def NextIntList(): return list(map(int,input().split()))
def RowInts(n): return [input() for i in range(n)]

def output(ans):
    print("Yes")
    size = len(ans)
    for i in range(size):
        if i == size-1:
            print(ans[i])
        else:
            print(ans[i], end=' ')

n = NextInt()
a = NextIntList()
b = NextIntList()[::-1]


def solve():
    border = n-1
    for i in range(n):
        if(a[i] >= b[i]):
            border = i
            break
    if a[border] != b[border]:
        output(b)
        return
    key = a[border]
    if a.count(key)+b.count(key) > n:
        print("No")
        return
    cnt = a[:border].count(key) + b[:border].count(key)

    for i in range(border, n):
        if a[i] != key or b[i] != key:
            output(b[i-1::-1] + b[:i-1:-1])
            return
        cnt += 2
        if cnt == i+1:
            output(b[i::-1] + b[:i:-1])
            return
    output(b[::-1])

solve()

Solution (Python):
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

INF = 10**18
eps = 10**-7

n = int(readline())
a = list(map(int,readline().split()))
b = list(map(int,readline().split()))

c = [0]*(n+1)
d = [0]*(n+1)

for ai in a:
    c[ai] += 1

for bi in b:
    d[bi] += 1

for i in range(n+1):
    if (c[i]+d[i] > n):
        print("No")
        exit()
print("Yes")

for i in range(n):
    c[i+1] += c[i]
    d[i+1] += d[i]

divi = 0

for i in range(1,n+1):
    divi = max(divi, c[i]-d[i-1])

ans = [-1]*n

for i in range(n):
    ans[(i+divi)%n] = b[i]

print(" ".join(map(str, ans)))

Solution (Python):
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

INF = 10**18
eps = 10**-7

n = int(readline())
a = list(map(int,readline().split()))
b = list(map(int,readline().split()))

c = [0]*(n+1)
d = [0]*(n+1)

for ai in a:
    c[ai] += 1

for bi in b:
    d[bi] += 1

for i in range(n+1):
    if (c[i]+d[i] > n):
        print("No")
        exit()
print("Yes")

for i in range(n):
    c[i+1] += c[i]
    d[i+1] += d[i]

divi = 0

for i in range(1,n+1):
    divi = max(divi, c[i]-d[i-1])

divi = n-divi

ans = b[divi:] + b[:divi]

print(" ".join(map(str, ans)))

Solution (Python):
import sys
from collections import Counter
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    counterA = Counter(A)
    counterB = Counter(B)

    for i in range(1, N + 1):
        if counterA[i] + counterB[i] > N:
            print('No')
            return

    C = [0] * (N + 1)
    D = [0] * (N + 1)
    for a in A:
        C[a] += 1
    for b in B:
        D[b] += 1

    C = list(accumulate(C))
    D = list(accumulate(D))

    x = max(C[i] - D[i - 1] for i in range(1, N + 1))

    ans = [0] * N
    for i in range(N):
        ans[i] = B[(i - x) % N]

    print('Yes')
    print(*ans)

    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    alst = list(map(int, input().split()))
    blst = list(map(int, input().split()))
    a_s = [0 for _ in range(n)]
    b_s = [0 for _ in range(n)]
    for a in alst:
        a_s[a - 1] += 1
    for b in blst:
        b_s[b - 1] += 1
    for a, b in zip(a_s, b_s):
        if a + b > n:
            print("No")
            return
    print("Yes")
    a_total = 0
    b_total = 0
    cor = -100000
    for a, b in zip(a_s, b_s):
        a_total += a
        cor = max(cor, a_total - b_total)
        b_total += b
    ans = blst[-cor:] + blst[:-cor]
    print(*ans)
        
main()

Solution (Python):
def f_contrast():
    from collections import Counter
    N = int(input())
    A = [int(i) for i in input().split()]
    B = [int(i) for i in input().split()]

    c = [0] * (N + 1)
    d = [0] * (N + 1)

    for a, b in zip(A, B):
        c[a] += 1
        d[b] += 1

    for s, t in zip(c, d):
        if s + t > N:
            return 'No'

    for i in range(1, N + 1):
        c[i] += c[i - 1]
        d[i] += d[i - 1]

    shift = max(c[i] - d[i - 1] for i in range(1, N + 1))
    ans = [B[(i + N - shift) % N] for i in range(N)]
    ret = ' '.join(map(str, ans))
    return f'Yes\n{ret}'

print(f_contrast())

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
aCnt = [0] * n
bCnt = [0] * n

for elem in a:
    aCnt[elem - 1] += 1
for elem in b:
    bCnt[elem - 1] += 1

ans1 = [-1] * n
ans2 = [-1] * n
isPossible = True

aLast = -1
aLastRemaining = 0
bLast = -1
bLastRemaining = 0
curIndex = 0

cntArray = []

for i in range(n):
    cntArray.append((aCnt[i] + bCnt[i],i))

cntArray.sort(reverse = True)

for j in range(n):
    i = cntArray[j][1]
    if aCnt[i] + bCnt[i] > n:
        isPossible = False
        break
    aRemaining = aCnt[i]
    bRemaining = bCnt[i]
    while aRemaining and curIndex < n:
        aRemaining -= 1
        ans1[curIndex] = i
        curIndex += 1
    while bRemaining and curIndex < n:
        bRemaining -= 1
        ans2[curIndex] = i
        curIndex += 1
    if curIndex >= n:
        aLast = j
        bLast = j
        aLastRemaining = aRemaining
        bLastRemaining = bRemaining
        break

if isPossible:
    for i in range(n):
        if ans1[i] == -1:
            while aLastRemaining == 0:
                aLast += 1
                aLastRemaining = aCnt[cntArray[aLast][1]]
            aLastRemaining -= 1
            ans1[i] = cntArray[aLast][1]
        if ans2[i] == -1:
            while bLastRemaining == 0:
                bLast += 1
                bLastRemaining = bCnt[cntArray[bLast][1]]
            bLastRemaining -= 1
            ans2[i] = cntArray[bLast][1]
    aCorrespondingList = []
    for _ in range(n):
        aCorrespondingList.append([])
    
    for i in range(n):
        aCorrespondingList[ans1[i]].append(ans2[i])
    newB = []
    for elem in a:
        newB.append(aCorrespondingList[elem - 1].pop() + 1)
    print("Yes")
    print(" ".join(map(str,newB)))
else:
    print("No")

Solution (Python):
"""
                            pppppppppppppppppppp
                         ppppp  ppppppppppppppppppp
                      ppppppp    ppppppppppppppppppppp
                      pppppppp  pppppppppppppppppppppp
                      pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
       ppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppp
      pppppppppppppppppppppppppppppppppppppppppppppppp  ppppppppppppppppppppp
     ppppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppppp
    ppppppppppppppppppppppppppppppppppppppppppppppp    pppppppppppppppppppppppp
   pppppppppppppppppppppppppppppppppppppppppppppp     pppppppppppppppppppppppppp
  ppppppppppppppppppppppppppppppppppppppppppppp      pppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppppppp               pppppppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppp     pppppppppppppppppppppppppppppppppppppppppppppp
  ppppppppppppppppppppppppppp    pppppppppppppppppppppppppppppppppppppppppppppppp
    pppppppppppppppppppppppp  pppppppppppppppppppppppppppppppppppppppppppppppppp
     ppppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppppp
      pppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppp
       ppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
                              pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppp  pppppppp
                              ppppppppppppppppppppp    ppppppp
                                 ppppppppppppppppppp  ppppp
                                    pppppppppppppppppppp
"""


import sys
from functools import lru_cache, cmp_to_key
from collections import defaultdict as dd, deque, Counter as C
from bisect import bisect_left as bl, bisect_right as br, bisect
from heapq import heapify, heappop, heappush
from math import ceil, log, floor, sqrt
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var, end="\n"): sys.stdout.write(str(var)+end)
def outa(*var, end="\n"): sys.stdout.write(' '.join(map(str, var)) + end)
def L(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]


n = int(data())
a = L()
b = L()
i = 0
temp = 0
r = True
for j in range(n):
    if temp != a[j]:
        i = 0
    if a[j] == b[j]:
        while i < n:
            if a[i] != a[j] and b[i] != a[j]:
                b[i], b[j] = b[j], b[i]
                break
            i += 1
        if a[j] == b[j]:
            out("No")
            exit()
    temp = a[j]
out("Yes")
outa(*b)

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

C=[0]*(N+1)
D=[0]*(N+1)
E=[0]*(N+1)

for i in range(N):
  C[A[i]]+=1
  D[B[i]]+=1
  E[A[i]]+=1
  E[B[i]]+=1

if max(E)>N:
  print("No")
  exit()
  
for i in range(1,N+1):
  C[i]+=C[i-1]
  D[i]+=D[i-1]

ans=0
for i in range(1,N+1):
  ans=max(ans, C[i]-D[i-1])
B=[B[(i-ans)%N] for i in range(N)]
  
print("Yes")
print(' '.join(map(str, B)))

Solution (Python):
import collections
import sys

def solve(n, A, B):
  ans = B[:]
  same = collections.deque()
  for i, (a, b) in enumerate(zip(A, B)):
    if a == b:
      if same and B[same[0]] != a:
        j = same.popleft()
        B[i], B[j] = B[j], B[i]
      else:
        same.append(i)
 
  
  
  if same:
    s = A[same[0]]
    to_swap = []
    possible = False
    for i, (a, b) in enumerate(zip(A, B)):
      if a != s and b!=s:
        to_swap.append(i)
      if len(to_swap) == len(same):
        possible = True
        break
    if not possible:
      print('No')
      return
    for i, j in zip(same, to_swap):
      B[i], B[j] = B[j], B[i]
  print('Yes')
  print(' '.join(map(str, B)) )


       


def main():
  input = sys.stdin.readline
  n = int(input())
  A = list(map(int, input().split()))
  B = list(map(int, input().split()))
  solve(n, A, B)

main()

Solution (Python):
import collections
import sys

def solve(n, A, B):
  ans = B[:]
  same = collections.deque()
  for i, (a, b) in enumerate(zip(A, B)):
    if a == b:
      if same and B[same[0]] != a:
        j = same.popleft()
        B[i], B[j] = B[j], B[i]
      else:
        same.append(i)
 
  
  
  if same:
    s = A[same[0]]
    to_swap = []
    possible = False
    for i, (a, b) in enumerate(zip(A, B)):
      if a != s and b!=s:
        to_swap.append(i)
      if len(to_swap) == len(same):
        possible = True
        break
    if not possible:
      print('No')
      return
    for i, j in zip(same, to_swap):
      B[i], B[j] = B[j], B[i]
  print('Yes')
  print(*B,sep=' ')


       


def main():
  input = sys.stdin.readline
  n = int(input())
  A = list(map(int, input().split()))
  B = list(map(int, input().split()))
  solve(n, A, B)

main()

Solution (Python):
from collections import deque
import sys

input = sys.stdin.buffer.readline

n = int(input())

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

ans = B[::]
same = deque()
for i, (a, b) in enumerate(zip(A, B)):
    if a == b:
        if same and B[same[0]] != a:
            j = same.popleft()
            B[i], B[j] = B[j], B[i]
        else:
            same.append(i)



if same:
    s = A[same[0]]
    to_swap = []
    possible = False
    for i, (a, b) in enumerate(zip(A, B)):
        if a != s and b != s:
            to_swap.append(i)
        if len(to_swap) == len(same):
            possible = True
            break
    if not possible:
        print("No")
        exit()
    for i, j in zip(same, to_swap):
        B[i], B[j] = B[j], B[i]
print("Yes")
print(*B, sep=" ")

Solution (Python):
import sys

sys.setrecursionlimit(10**6)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def lr(xx):
    ll=[-1]*(n+1)
    rr=[-1]*(n+1)
    for i,x in enumerate(xx):
        if ll[x]==-1:ll[x]=i
        if i==n-1 or x!=xx[i+1]:rr[x]=i+1
    return ll,rr

n=II()
aa=LI()
bb=LI()
all,arr=lr(aa)
bll,brr=lr(bb)

ca=[0]*(n+1)
cb=[0]*(n+1)
for a in aa:ca[a]+=1
for b in bb:cb[b]+=1
for i in range(n+1):
    if ca[i]+cb[i]>n:
        print("No")
        exit()
ng=[0]*(n+1)
for al,ar,bl,br in zip(all,arr,bll,brr):
    if al==-1 or bl==-1:continue
    if bl<ar:
        mn=max(0,al-br+1)
        mx=ar-bl
        ng[mn]+=1
        ng[mx]-=1
    else:
        mn=max(0,al+n-br+1)
        mx=min(n,ar+n-bl)
        ng[mn]+=1
        ng[mx]-=1

for i in range(n):ng[i+1]+=ng[i]
shift=-1
for i in range(n):
    if ng[i]==0:
        shift=i
        break
ans=bb[-shift:]+bb[:-shift]
print("Yes")
print(*ans)

Solution (Python):
def get_ints():
  return list(map(int,input().split()))

n = int(input())
a = get_ints()
b = get_ints()

cnt = [0]*(n+1)
for x in a+b:
  cnt[x] += 1

q = [set() for i in range(n+1)]
for i in range(1,n+1):
  if cnt[i] > n:
    print('No')
    exit(0)
  q[cnt[i]].add(i)
print('Yes')

def make_cnt(a):
  cnt_ = {}
  for i in range(n):
    if a[i] not in cnt_:
      cnt_[a[i]] = [i]
    else:
      cnt_[a[i]].append(i)
  return cnt_

cnt_a = make_cnt(a)
cnt_b = make_cnt(b)

def get_any(cnt_, x):
  for key in cnt_.keys():
    if key != x:
      return key
  assert False

ans = [-1]*n

for i in range(n,0,-1):
  s = list(q[i])
  x,y = 0,0
  if len(s) == 0:
    x = get_any(cnt_a, -1)
    y = get_any(cnt_b, x)
  elif len(s) == 1:
    x = s[0]
    if x not in cnt_a: 
      y = x
      x = get_any(cnt_a, y)
    else: 
      y = get_any(cnt_b, x)
  else: 
    x,y = s
    if x not in cnt_a:
      x,y = y,x

  ans[cnt_a[x][-1]] = y
  def update_cnt(cnt_, x):
    cnt_[x].pop()
    if len(cnt_[x]) == 0:
      del cnt_[x]
  def use(x):
    q[cnt[x]].remove(x)
    cnt[x] -= 1
    q[cnt[x]].add(x)
  update_cnt(cnt_a, x)
  update_cnt(cnt_b, y)
  use(x)
  use(y)

print(' '.join(map(str,ans)))

Solution (Python):
import sys
input = sys.stdin.readline

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

from collections import Counter

CA=Counter(A)
CB=Counter(B)

H=[]

for ca in CA:
    if CA[ca]>0 and CB[ca]>0:
        if CA[ca]+CB[ca]>N:
            print("No")
            sys.exit()
        H.append((-(CA[ca]+CB[ca]),ca))

import heapq
heapq.heapify(H)

ANSDICT=dict()

while len(H)>=2:
    c1,x1=heapq.heappop(H)
    c1=-c1

    c2,x2=heapq.heappop(H)
    c2=-c2

    if x1 in ANSDICT:
        ANSDICT[x1].append(x2)
    else:
        ANSDICT[x1]=[x2]

    if x2 in ANSDICT:
        ANSDICT[x2].append(x1)
    else:
        ANSDICT[x2]=[x1]

    CA[x1]-=1
    CA[x2]-=1
    CB[x1]-=1
    CB[x2]-=1

    if CA[x1]>0 and CB[x1]>0:
        heapq.heappush(H,(-(c1-2),x1))

    if CA[x2]>0 and CB[x2]>0:
        heapq.heappush(H,(-(c2-2),x2))

ANS=[-1]*N

for i in range(N):
    a=A[i]
    if a in ANSDICT and len(ANSDICT[a])!=0:
        
        x=ANSDICT[a].pop()
        ANS[i]=x

if H:
    cc,xx=H[0]
    cc=-cc

    for i in range(N):
        if ANS[i]==-1 and A[i]!=xx:
            ANS[i]=xx
            CB[xx]-=1

            if CB[xx]==0:
                break

ind=0
for cb in CB:
    while CB[cb]!=0:
        while ANS[ind]!=-1:
            ind+=1
        ANS[ind]=cb
        CB[cb]-=1

print("Yes")
print(*ANS)

Solution (Python):
'''
自宅用PCでの解答
'''
import math

import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7

dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"
INF = 1<<32-1


def main():
    n = int(ipt())
    base = n+1
    a = [int(i) for i in ipt().split()]
    b = [int(i) for i in ipt().split()]
    na = [0]*(n+1)
    nb = [0]*(n+1)
    bl = b[-1]

    for i in range(n):
        na[a[i]] += 1
        nb[b[i]] += 1

    bs = []

    for i in range(1,n+1):
        nai = na[i]
        nbi = nb[i]
        if nai+nbi > n:
            print("No")
            exit()
        elif nai+nbi != 0:
            if bl != i:
                hpq.heappush(bs,(n-nai-nbi)*base+i)

    print("Yes")
    ans = []
    q = []
    for i,ai in enumerate(a):
        na[ai] -= 1
        while True:
            hi = hpq.heappop(bs)
            hiv = hi%base
            while True:
                hi = hpq.heappushpop(bs,(n-na[hiv]-nb[hiv])*base+hiv)
                if hiv == hi%base:
                    break
                else:
                    hiv = hi%base
            if na[hiv]+nb[hiv] < n-i and nb[bl] != 0:
                ans.append(bl)
                nb[bl] -= 1
                q.append((n-na[hiv]-nb[hiv])*base+hiv)
                break
            elif hiv == ai:
                if nb[hiv] != 0:
                    q.append((n-na[ai]-nb[ai])*base+hiv)
            elif nb[hiv] != 0:
                ans.append(hiv)
                nb[hiv] -= 1
                q.append((n-na[hiv]-nb[hiv])*base+hiv)
                break
        while q:
            hpq.heappush(bs,q.pop())

    print(" ".join(map(str,ans)))

    return None

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
judge = True

A_num = [0] * (N+1)
B_num = [0] * (N+1)
C = [0] * (N+1)
D = [0] * (N+1)

for a in A:
  A_num[a] += 1
for b in B:
  B_num[b] += 1
  
for a,b in zip(A_num,B_num):
  if a+b>N:
    judge = False
    break

if judge:
  for i in range(1,N+1):
    C[i] = C[i-1] + A_num[i]
    D[i] = D[i-1] + B_num[i]
  x = -float('inf')
  for i in range(1,N+1):
    x = max(x, C[i] - D[i-1])
    
  ans = B[-x:] + B[:-x]
  print('Yes')
  print(' '.join(map(str,ans)))
  
else:
  print('No')

Solution (Python):
N = int(input())
*A, = map(int, input().split())
*B, = map(int, input().split())
B.reverse()

ac = [0]*(N+1)
bc = [0]*(N+1)

ok = True
for i in range(N):
    ac[A[i]] += 1
    bc[B[i]] += 1

for i in range(1, N+1):
    if (ac[i]+bc[i]) > N:
        ok = False
        break

if ok:
    l, r = 0, N-1
    for i in range(N):
        if A[i] == B[i]:
            if A[i] != B[l] and A[l] != B[i]:
                B[l], B[i] = B[i], B[l]
                l += 1
            elif A[i] != B[r] and A[r] != B[i]:
                B[r], B[i] = B[i], B[r]
                r -= 1
    print("Yes")
    print(*B)
else:
    print("No")

Solution (Python):


import sys
readline = sys.stdin.readline
read = sys.stdin.read

n, = map(int,readline().split())
*a, = map(int,readline().split())
*b, = map(int,readline().split())
b = b[::-1]

same = [i for i in range(n) if a[i] == b[i]]
if same:
    v = a[same[0]] 
    notv = [i for i in range(n) if a[i]!=v and b[i]!=v] 
    if len(same) > len(notv):
        print("No")
        exit()
    for i,j in zip(same,notv):
        b[i],b[j] = b[j],b[i]

print("Yes")
print(*b)

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

T = [(A[i], i) for i in range(N)]+[(B[i], N+i) for i in range(N)]
T.sort()

P = [-1]*(2*N)
for i in range(N):
  if T[i][0]==T[i+N][0]:
    print("No")
    exit(0)
  P[T[i][1]] = T[i+N][1]
  P[T[i+N][1]] = T[i][1]

bp1 = N
for ap1 in range(N):
  if P[ap1]//N==1:
    continue
  while P[bp1]//N==0:
    bp1 += 1
  ap2 = P[ap1]
  bp2 = P[bp1]
  if A[ap1]!=B[bp1-N] and A[ap2]!=B[bp2-N]:
    P[ap1] = bp1
    P[bp1] = ap1
    P[ap2] = bp2
    P[bp2] = ap2
  else:
    P[ap1] = bp2
    P[bp1] = ap2
    P[ap2] = bp1
    P[bp2] = ap1

print("Yes")
print(" ".join(str(B[P[i]-N]) for i in range(N)))

Solution (Python):


import sys
readline = sys.stdin.readline
read = sys.stdin.read

n, = map(int,readline().split())
*a, = map(int,readline().split())
*b, = map(int,readline().split())
b = b[::-1]

same = [i for i in range(n) if a[i] == b[i]]
if same:
    v = a[same[0]] 
    r = same[0]
    s = same[-1]+1
    notv = [i for i in range(n) if a[i]!=v and b[i]!=v] 
    if len(same) > len(notv):
        print("No")
        exit()
    
    for i,j in zip(same,notv):
        b[i],b[j] = b[j],b[i]

print("Yes")
print(*b)