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 Scala (2.13.1) (judge verdict: Time Limit Exceeded)

object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt()
  val a, b = Array.fill(n)(sc.nextInt() - 1)
  val count = Array.fill(n)(0)
  var idxa = Map[Int, List[Int]]().withDefaultValue(Nil)
  var idxb = Map[Int, List[Int]]().withDefaultValue(Nil)
  for (i <- 0 until n) {
    count(a(i)) += 1
    count(b(i)) += 1
    idxa += a(i) -> (i :: idxa(a(i)))
    idxb += b(i) -> (i :: idxb(b(i)))
  }

  if (count.max > n) {
    println("No")
  } else {
    val x = Array.fill(n+1)(Set[Int]())
    for (i <- 0 until n) {
      x(count(i)) += i
    }

    val ans = Array.fill(n)(-1)

    def update(p: Int, q: Int): Unit = {
      val idx = idxa(p).head
      ans(idx) = q + 1
      idxa += p -> idxa(p).tail
      idxb += q -> idxb(q).tail
      x(count(p)) -= p
      x(count(q)) -= q
      count(p) -= 1
      count(q) -= 1
      x(count(p)) += p
      x(count(q)) += q
    }

    def getAny(m: Map[Int, List[Int]], except: Int): Int = {
      m.find{case (k, v) => k != except && v.nonEmpty}.get._1
    }

    for (i <- n until 0 by -1) {
      var p = -1
      var q = -1
      x(i).size match {
        case 0 =>
          p = getAny(idxa, -1)
          q = getAny(idxb, p)
        case 1 =>
          if (idxa(x(i).head).nonEmpty) {
            p = x(i).head
            q = getAny(idxb, p)
          } else {
            q = x(i).head
            p = getAny(idxa, q)
          }
        case 2 =>
          if (idxa(x(i).head).nonEmpty) {
            p = x(i).head
            q = x(i).last
          } else {
            p = x(i).last
            q = x(i).head
          }
      }
      update(p, q)
    }

    println("Yes")
    println(ans.mkString(" "))
  }
}