Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

    solve(N, As, Bs)

def test():
    import random
    for _ in range(10000):
        As = [random.randint(0, 10) for _ in range(10)]
        Bs = [random.randint(0, 10) for _ in range(10)]
        try:
            solve(10, As, Bs)
        except:
            print(10)
            print(*As)
            print(*Bs)
            solve(10, As, Bs)


def solve(N, As, Bs):
    from collections import Counter
    As_counter = Counter(As)
    Bs_counter = Counter(Bs)

    A_indices = dict()
    for i, A in enumerate(As):
        if A in A_indices:
            A_indices[A].append(i)
        else:
            A_indices[A] = [i]
    B_indices = dict()
    for i, B in enumerate(Bs):
        if B in B_indices:
            B_indices[B].append(i)
        else:
            B_indices[B] = [i]

    B_count = sorted(Bs_counter.items(), key=lambda t: t[1], reverse=True)

    if max((As_counter + Bs_counter).values()) > N:
        print("No")
    else:
        print("Yes")
        filling_order = []
        for elem, count in B_count[N//2:] + B_count[:N//2]:
            if elem in A_indices:
                filling_order += A_indices[elem]

        delset = set(filling_order)
        filling_order += list(set(range(N)) - delset)

        index = 0
        ans = [0] * N
        for elem, _ in B_count:
            count = Bs_counter[elem]
            for i in range(index, index + count):
                ans[filling_order[i]] = elem
            index += count

        import random
        while True:
            for i, (a, b) in enumerate(zip(ans, As)):
                if a == b:
                    t = random.randint(0, N)
                    ans[i], ans[i-t] = ans[i-t], ans[i]
                    break
            else:
                break

        print(*ans)

main()

Fixed submission:
# Rotationによる解。
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()

# Bs[-index:] > As[:index] を満たすようなindexを二分探索
# 参考: https://twitter.com/catupper/status/1305163805802917889?s=20

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]))

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

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

As_counter = Counter(As)
Bs_counter = Counter(Bs)

A_indices = dict()
for i, A in enumerate(As):
    if A in A_indices:
        A_indices[A].append(i)
    else:
        A_indices[A] = [i]
B_indices = dict()
for i, B in enumerate(Bs):
    if B in B_indices:
        B_indices[B].append(i)
    else:
        B_indices[B] = [i]

all_count = sorted((As_counter + Bs_counter).items(), key=lambda t: t[1], reverse=True)

if all_count[0][1] > N:
    print("No")
else:
    print("Yes")
    filling_order = []
    for elem, count in all_count[1:]:
        filling_order += A_indices[elem]
    filling_order += A_indices[all_count[0][0]]

    index = 0
    ans = [0] * N
    for elem, _ in all_count:
        count = Bs_counter[elem]
        for i in range(index, index + count):
            ans[filling_order[i]] = elem
        index += count

    print(*ans)
    assert not any(a == b for a, b in zip(ans, As))

Fixed submission:
# Rotationによる解。
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()

# Bs[-index:] > As[:index] を満たすようなindexを二分探索
# 参考: https://twitter.com/catupper/status/1305163805802917889?s=20

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]))