Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq
from functools import lru_cache


# \n
def input():
    return sys.stdin.readline().rstrip()


def main():
    K = 0
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    n = N // 3

    @lru_cache(maxsize=None)
    def diff(s):
        count = 0
        for i in range(N):
            if A[i] == B[(i + s) % N]:
                count += 1

        if count == 0:
            print("Yes")
            L = []
            for i in range(N):
                L.append(B[(i + s) %N])
            print(*L)
            exit()

        return count

    l = 0
    r = N - 1
    diff(l)
    diff(r)

    while r - l > 1:
        a = diff(l + (r - l) // 3)
        b = diff(l + 2 * (r - l) // 3)
        if a > b:
            l = l + (r - l) // 3
        else:
            r =l + 2 * (r - l) // 3

    print("No")


if __name__ == "__main__":
    main()


Fixed submission:
import sys
import heapq
from functools import lru_cache


# \n
def input():
    return sys.stdin.readline().rstrip()


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

    n = N // 3

    @lru_cache(maxsize=None)
    def diff(s):
        count = 0
        for i in range(N):
            if A[i] == B[(i + s) % N]:
                count += 1

        if count == 0:
            print("Yes")
            L = []
            for i in range(N):
                L.append(B[(i + s) %N])
            print(*L)
            exit()

        return count

    l = 0
    r = N - 1
    diff(l)
    diff(r)

    while r - l > 2:
        a = diff(l + (r - l) // 3)
        b = diff(l + 2 * (r - l) // 3)
        if a > b:
            l = l + (r - l) // 3
        else:
            r =l + 2 * (r - l) // 3
    diff((l+r)//2)

    print("No")


if __name__ == "__main__":
    main()

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


# \n
def input():
    return sys.stdin.readline().rstrip()


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

    AA = Counter(A)
    BB = Counter(B)

    max_con = 1
    conA = 0

    for key, value in AA.items():
        if not BB[key]:
            continue

        if BB[key] + value > max_con:
            max_con =BB[key] + value
            conkey = key

        if BB[key] + value > N:
            print("No")
            exit()

    print("Yes")

    A_key = bisect.bisect_right(A, conkey)
    B_key = bisect.bisect_left(B, conkey)


    S=[0]*N

    for i in range(N):
        S[(i+A_key)%N] =B[(B_key+i)%N]

    print(*S)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
import heapq
from functools import lru_cache


# \n
def input():
    return sys.stdin.readline().rstrip()


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

    n = N // 3

    @lru_cache(maxsize=None)
    def diff(s):
        count = 0
        for i in range(N):
            if A[i] == B[(i + s) % N]:
                count += 1

        if count == 0:
            print("Yes")
            L = []
            for i in range(N):
                L.append(B[(i + s) %N])
            print(*L)
            exit()

        return count

    l = 0
    r = N - 1
    diff(l)
    diff(r)

    while r - l > 2:
        a = diff(l + (r - l) // 3)
        b = diff(l + 2 * (r - l) // 3)
        if a > b:
            l = l + (r - l) // 3
        else:
            r =l + 2 * (r - l) // 3
    diff((l+r)//2)

    print("No")


if __name__ == "__main__":
    main()