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

#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_macros)]
#![allow(unused_imports)]

use std::str::FromStr;
use std::io::*;
use std::collections::*;
use std::cmp::*;

struct Scanner<I: Iterator<Item = char>> {
    iter: std::iter::Peekable<I>,
}

macro_rules! exit {
    () => {{
        exit!(0)
    }};
    ($code:expr) => {{
        if cfg!(local) {
            writeln!(std::io::stderr(), "===== Terminated =====")
                .expect("failed printing to stderr");
        }
        std::process::exit($code);
    }}
}

impl<I: Iterator<Item = char>> Scanner<I> {
    pub fn new(iter: I) -> Scanner<I> {
        Scanner {
            iter: iter.peekable(),
        }
    }

    pub fn safe_get_token(&mut self) -> Option<String> {
        let token = self.iter
            .by_ref()
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        if token.is_empty() {
            None
        } else {
            Some(token)
        }
    }

    pub fn token(&mut self) -> String {
        self.safe_get_token().unwrap_or_else(|| exit!())
    }

    pub fn get<T: FromStr>(&mut self) -> T {
        self.token().parse::<T>().unwrap_or_else(|_| exit!())
    }

    pub fn vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
        (0..len).map(|_| self.get()).collect()
    }

    pub fn mat<T: FromStr>(&mut self, row: usize, col: usize) -> Vec<Vec<T>> {
        (0..row).map(|_| self.vec(col)).collect()
    }

    pub fn char(&mut self) -> char {
        self.iter.next().unwrap_or_else(|| exit!())
    }

    pub fn chars(&mut self) -> Vec<char> {
        self.get::<String>().chars().collect()
    }

    pub fn mat_chars(&mut self, row: usize) -> Vec<Vec<char>> {
        (0..row).map(|_| self.chars()).collect()
    }

    pub fn line(&mut self) -> String {
        if self.peek().is_some() {
            self.iter
                .by_ref()
                .take_while(|&c| !(c == '\n' || c == '\r'))
                .collect::<String>()
        } else {
            exit!();
        }
    }

    pub fn peek(&mut self) -> Option<&char> {
        self.iter.peek()
    }
}

fn main() {
    let cin = stdin();
    let cin = cin.lock();
    let mut sc = Scanner::new(cin.bytes().map(|c| c.unwrap() as char));
    let N: usize = sc.get();
    let A: Vec<usize> = sc.vec(N);
    let B: Vec<usize> = sc.vec(N);
    let mut cnt = vec![0; N+1];
    for i in 0..N {
        cnt[A[i]] += 1;
        cnt[B[i]] += 1;
        if cnt[A[i]] > N || cnt[B[i]] > N {
            println!("No");
            return;
        }
    }
    let mut a_most = Vec::new();
    let mut a_rest = Vec::new();
    let mut b_most = Vec::new();
    let mut b_rest = Vec::new();
    for i in 0..N {
        if cnt[A[i]] == N {
            a_most.push(i);
        } else {
            a_rest.push(i);
        }
        if cnt[B[i]] == N {
            b_most.push(i);
        } else {
            b_rest.push(i);
        }
    }
    println!("Yes");
    let mut ans = vec![0; N];
    if a_most.len() + b_most.len() == 2 * N {
        for i in 0..N {
            ans[i] = B[N-1-i];
        }
    } else if a_most.len() == 0 && b_most.len() == 0 {
        let mut b_h: BinaryHeap<usize> = B.into_iter().collect();
        for i in 0..N {
            let mut tmp = Vec::new();
            while let Some(b) = b_h.pop() {
                if b != A[i] {
                    ans[i] = b;
                    break;
                }
                tmp.push(b);
            }
            while let Some(t) = tmp.pop() {
                b_h.push(t);
            }
        }
    } else {
        for _ in 0..N {
            if let Some(ai) = a_most.pop() {
                let bi = b_rest.pop().unwrap();
                ans[ai] = B[bi];
            } else if let Some(bi) = b_most.pop() {
                let ai = a_rest.pop().unwrap();
                ans[ai] = B[bi];
            }
        }
    }
    for i in 0..N {
        println!("{}", ans[i]);
    }
}