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 in C++ (GCC 9.2.1) (judge verdict: Runtime Error)

#include <iostream>
#include <vector>
#include <set>
#include <map>

typedef long long ll;

struct union_find {
  std::vector<int> p; union_find(int n) : p(n, -1) {}
  int find(int x) { return p[x] < 0 ? x : p[x] = find(p[x]); }
  int unite(int x, int y) {
    int xp = find(x), yp = find(y);
    if (xp == yp)     return false;
    // x -> y
    p[xp] = yp;
  }
};

int N, A[200005], B[200005], ans[200005];
std::map<int, int> start, next;

int main() {
  std::cin >> N;

  union_find uf(N);
  std::set<int> uniq;
  for (int i = 0; i <= N; ++i)
    start[i] = -1;

  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
    if (start[A[i]] == -1)  start[A[i]] = i;
    uniq.insert(A[i]);
  }

  // problem might occur if uniqv.size() == 1
  std::vector<int> uniqv(uniq.begin(), uniq.end());
  for (int i = 1; i < uniqv.size(); ++i)
    next[uniqv[i-1]] = uniqv[i];
  next[uniqv.back()] = uniqv[0];

  /*for (int x : uniqv)
    std::cout << x << " " << next[x] << " " << start[x] << "\n";
  std::cout << "--\n";*/

  bool kaya = true;
  for (int i = 0; i < N; ++i) {
    int x;  std::cin >> x;
    int p = start[x], numjumps = 0;
    while (true) {
      int pp = uf.find(p);
      if (pp != p) {
        p = pp;
      } else if (A[p] == x) {
        //std::cout << p << "->" << start[next[A[p]]] << "\n";
        p = start[next[A[p]]];
      } else
        break;
      numjumps++;
      if (numjumps >= 2*N) {
        //std::cout << "extra\n";
        kaya = false;
        break;
      }
    }
    if (!kaya)
      break;
    //std::cout << i << " " << p << " " << x << "\n";
    ans[p] = x;
    uf.unite(p, (p+1) % N);
  }

  if (kaya) {
    std::cout << "Yes\n";
    for (int i = 0; i < N; ++i)
      std::cout << ans[i] << " ";
    std::cout << "\n";
  } else
    std::cout << "No\n";

  return 0;
}