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)

import scala.collection.mutable.Map

object Main extends App {
  val sc = new FastScanner(System.in)
  val n = sc.nextInt()
  val a, b = Array.fill(n)(sc.nextInt() - 1)
  val count = Array.fill(n)(0)
  val idxa = Map[Int, List[Int]]().withDefaultValue(Nil)
  val idxb = Map[Int, List[Int]]().withDefaultValue(Nil)

  for (i <- 0 until n) {
    count(a(i)) += 1
    count(b(i)) += 1
    idxa(a(i)) ::= i
    idxb(b(i)) ::= 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 idxap = idxa(p)
      val idxbq = idxb(q)
      ans(idxap.head) = q + 1

      if (idxap.tail.isEmpty) {
        idxa -= p
      } else {
        idxa(p) = idxap.tail
      }

      if (idxbq.tail.isEmpty) {
        idxb -= q
      } else {
        idxb(q) = idxbq.tail
      }

      val cp = count(p)
      val cq = count(q)
      count(p) -= 1
      count(q) -= 1

      x(cp) -= p
      x(cq) -= q
      x(cp - 1) += p
      x(cq - 1) += q
    }

    def getAny(m: Map[Int, List[Int]], except: Int): Int = {
      m.keys.find(_ != except).get
    }

    for (i <- n until 0 by -1) {
      var p = -1
      var q = -1
      x(i).toList match {
        case Nil =>
          p = getAny(idxa, -1)
          q = getAny(idxb, p)
        case x1 :: Nil =>
          if (idxa.isDefinedAt(x1)) {
            p = x1
            q = getAny(idxb, p)
          } else {
            q = x1
            p = getAny(idxa, q)
          }
        case x1 :: x2 :: Nil =>
          if (idxa.isDefinedAt(x1)) {
            p = x1
            q = x2
          } else {
            p = x2
            q = x1
          }
      }
      update(p, q)
    }

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

class FastScanner(source: java.io.InputStream) {
  val buf = new Array[Byte](1024)
  var now, end = 0

  def isPrintable(b: Byte): Boolean = 33 <= b && b <= 126
  def isNum(b: Byte): Boolean = 48 <= b && b <= 57

  def hasNextByte: Boolean = {
    if (now < end) {
      true
    } else {
      now = 0
      end = source.read(buf)
      end > 0
    }
  }

  def nextByte(): Byte = {
    if (hasNextByte) {
      val b = buf(now)
      now += 1
      b
    } else {
      -1
    }
  }

  def hasNext: Boolean = {
    while (hasNextByte && !isPrintable(buf(now))) {
      now += 1
    }
    hasNextByte
  }

  def next(): String = {
    if (hasNext) {
      val sb = new StringBuilder()
      var b = nextByte()
      while (isPrintable(b)) {
        sb += b.toChar
        b = nextByte()
      }
      sb.toString
    } else {
      throw new NoSuchElementException()
    }
  }

  def nextInt(): Int = {
    nextLong().toInt
  }

  def nextLong(): Long = {
    if (hasNext) {
      var b = nextByte()
      var sign = 1
      var n = 0L
      if (b == '-') {
        sign = -1
        b = nextByte()
      }
      while (isNum(b)) {
        n = n * 10 + (b - 48)
        b = nextByte()
      }
      sign * n
    } else {
      throw new NoSuchElementException()
    }
  }

  def nextDouble(): Double = next().toDouble
}