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: Runtime Error)

#[allow(dead_code)]
fn main() {
    let stdin = stdin();
    solve(StdinReader::new(stdin.lock()));
}

pub fn solve<R: BufRead>(mut reader: StdinReader<R>) {
    let n = reader.u();
    let a = reader.uv(n);
    let b = reader.uv(n);

    let mut cnt_a = HashMap::new();
    let mut cnt_b = HashMap::new();
    let mut pos_a = HashMap::new();
    let mut pos_b = HashMap::new();
    for i in 0..n {
        *cnt_a.entry(a[i]).or_insert(0usize) += 1;
        pos_a.entry(a[i]).or_insert(VecDeque::new()).push_front(i);
        *cnt_b.entry(b[i]).or_insert(0usize) += 1;
        pos_b.entry(b[i]).or_insert(VecDeque::new()).push_front(i);
    }
    for (key, count) in &cnt_a {
        if !cnt_b.contains_key(key) {
            continue;
        }
        if cnt_b.get(key).unwrap() + *count > n {
            println!("{}", "No");
            return;
        }
    }

    let mut rest_a = BTreeMap::new();
    let mut rest_b = BTreeMap::new();

    for (key, count) in cnt_a {
        rest_a
            .entry(Reverse(count))
            .or_insert(VecDeque::new())
            .push_front(key);
    }
    for (key, count) in cnt_b {
        rest_b
            .entry(Reverse(count))
            .or_insert(VecDeque::new())
            .push_front(key);
    }

    let mut ans = vec![0; n];

    // dbg!(&rest_a, &rest_b);
    let mut c = n;
    while rest_a.len() > 0 {
        // dbg!(&ans);
        let (&Reverse(a_count), _) = rest_a.iter().next().unwrap();
        let a_keys = rest_a.get_mut(&Reverse(a_count)).unwrap();
        let cur = a_keys.pop_front().unwrap();
        if a_keys.len() == 0 {
            rest_a.remove(&Reverse(a_count));
        }
        let mut count = a_count;

        while count > 0 && c > 0 {
            let (&Reverse(b_count), _) = rest_b.iter().next().unwrap();
            let b_keys = rest_b.get_mut(&Reverse(b_count)).unwrap();
            let mut b_cur = b_keys.pop_front().unwrap();
            let mut bc = b_count;

            if b_cur == cur {
                if b_keys.len() != 0 {
                    b_keys.push_back(b_cur);
                    b_cur = b_keys.pop_front().unwrap();
                } else {
                    rest_b.remove(&Reverse(b_count));
                    let (&Reverse(b_count2), _) = rest_b.iter().next().unwrap();
                    rest_b
                        .entry(Reverse(b_count))
                        .or_insert(VecDeque::new())
                        .push_front(b_cur);
                    let b_keys2 = rest_b.get_mut(&Reverse(b_count2)).unwrap();
                    b_cur = b_keys2.pop_front().unwrap();
                    if b_keys2.len() == 0 {
                        rest_b.remove(&Reverse(b_count2));
                    }
                    bc = b_count2;
                }
            } else {
                if b_keys.len() == 0 {
                    rest_b.remove(&Reverse(b_count));
                }
            }

            // dbg!(cur, count, a_count, bc, b_cur);
            while count > 0 && bc > 0 {
                let a_pos = pos_a.get_mut(&cur).unwrap().pop_front().unwrap();
                ans[a_pos] = b_cur;
                count -= 1;
                bc -= 1;
                c -= 1;
            }
            if bc > 0 {
                rest_b
                    .entry(Reverse(b_count))
                    .or_insert(VecDeque::new())
                    .push_front(b_cur);
            }
        }
    }
    println!("{}", "Yes");
    println!("{}", ans.iter().join(" "));
}

#[allow(unused_imports)]
use itertools::Itertools;
#[allow(unused_imports)]
use std::{cmp::*, collections::*, io::*, num::*, str::*};
#[allow(unused_imports)]
use stdin_reader::StdinReader;

#[allow(dead_code)]
pub mod stdin_reader {
    use std::{fmt::Debug, io::*, str::*};

    pub struct StdinReader<R: BufRead> {
        reader: R,
        buf: Vec<u8>,
        // Should never be empty
        pos: usize, // Should never be out of bounds as long as the input ends with '\n'
    }

    impl<R: BufRead> StdinReader<R> {
        pub fn new(reader: R) -> StdinReader<R> {
            let (buf, pos) = (Vec::new(), 0);
            StdinReader { reader, buf, pos }
        }

        pub fn n<T: FromStr>(&mut self) -> T
        where
            T::Err: Debug,
        {
            if self.buf.is_empty() {
                self._read_next_line();
            }
            let mut start = None;
            while self.pos != self.buf.len() {
                match (self.buf[self.pos], start.is_some()) {
                    (b' ', true) | (b'\n', true) => break,
                    (_, true) | (b' ', false) => self.pos += 1,
                    (b'\n', false) => self._read_next_line(),
                    (_, false) => start = Some(self.pos),
                }
            }
            match start {
                Some(s) => from_utf8(&self.buf[s..self.pos]).unwrap().parse().unwrap(),
                None => panic!("入力された数を超えた読み込みが発生しています"),
            }
        }

        fn _read_next_line(&mut self) {
            self.pos = 0;
            self.buf.clear();
            if self.reader.read_until(b'\n', &mut self.buf).unwrap() == 0 {
                panic!("Reached EOF");
            }
        }

        pub fn str(&mut self) -> String {
            self.n()
        }
        pub fn s(&mut self) -> Vec<char> {
            self.n::<String>().chars().collect()
        }
        pub fn i(&mut self) -> i64 {
            self.n()
        }
        pub fn i2(&mut self) -> (i64, i64) {
            (self.n(), self.n())
        }
        pub fn i3(&mut self) -> (i64, i64, i64) {
            (self.n(), self.n(), self.n())
        }
        pub fn u(&mut self) -> usize {
            self.n()
        }
        pub fn u2(&mut self) -> (usize, usize) {
            (self.n(), self.n())
        }
        pub fn u3(&mut self) -> (usize, usize, usize) {
            (self.n(), self.n(), self.n())
        }
        pub fn u4(&mut self) -> (usize, usize, usize, usize) {
            (self.n(), self.n(), self.n(), self.n())
        }
        pub fn u5(&mut self) -> (usize, usize, usize, usize, usize) {
            (self.n(), self.n(), self.n(), self.n(), self.n())
        }
        pub fn u6(&mut self) -> (usize, usize, usize, usize, usize, usize) {
            (self.n(), self.n(), self.n(), self.n(), self.n(), self.n())
        }
        pub fn f(&mut self) -> f64 {
            self.n()
        }
        pub fn f2(&mut self) -> (f64, f64) {
            (self.n(), self.n())
        }
        pub fn c(&mut self) -> char {
            self.n::<String>().pop().unwrap()
        }
        pub fn iv(&mut self, n: usize) -> Vec<i64> {
            (0..n).map(|_| self.i()).collect()
        }
        pub fn iv2(&mut self, n: usize) -> Vec<(i64, i64)> {
            (0..n).map(|_| self.i2()).collect()
        }
        pub fn iv3(&mut self, n: usize) -> Vec<(i64, i64, i64)> {
            (0..n).map(|_| self.i3()).collect()
        }
        pub fn uv(&mut self, n: usize) -> Vec<usize> {
            (0..n).map(|_| self.u()).collect()
        }
        pub fn uv2(&mut self, n: usize) -> Vec<(usize, usize)> {
            (0..n).map(|_| self.u2()).collect()
        }
        pub fn uv3(&mut self, n: usize) -> Vec<(usize, usize, usize)> {
            (0..n).map(|_| self.u3()).collect()
        }
        pub fn uv4(&mut self, n: usize) -> Vec<(usize, usize, usize, usize)> {
            (0..n).map(|_| self.u4()).collect()
        }
        pub fn fv(&mut self, n: usize) -> Vec<f64> {
            (0..n).map(|_| self.f()).collect()
        }
        pub fn cmap(&mut self, h: usize) -> Vec<Vec<char>> {
            (0..h).map(|_| self.s()).collect()
        }
    }
}