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(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn make_pairs(a: &Vec<usize>, n: usize) -> Vec<Vec<usize>> {
    let mut a_poses = vec![vec![]; n];
    for i in 0..n {
        a_poses[a[i]].push(i);
    }
    a_poses
}

fn main() {
    let n = read::<usize>();
    let a = read_vec::<usize>()
        .iter()
        .map(|&x| x - 1)
        .collect::<Vec<_>>();
    let b = read_vec::<usize>()
        .iter()
        .map(|&x| x - 1)
        .collect::<Vec<_>>();
    let mut count = vec![0; n];
    for &num in a.iter().chain(b.iter()) {
        count[num] += 1;
    }
    if count.iter().any(|&x| x > n) {
        println!("No");
        return;
    }
    let mut count_nums = vec![HashSet::new(); n + 1];
    for i in 0..n {
        count_nums[count[i]].insert(i);
    }

    let mut a_pairs = make_pairs(&a, n);
    let mut a_numbers = HashMap::new();
    for i in 0..n {
        *a_numbers.entry(a[i]).or_insert(0) += 1;
    }

    let mut b_pairs = make_pairs(&b, n);
    let mut b_numbers = HashMap::new();
    for i in 0..n {
        *b_numbers.entry(b[i]).or_insert(0) += 1;
    }

    let mut answers = vec![0; n];
    let mut count_i = n;
    while count_i > 0 {
        let mut iter = count_nums[count_i].iter();
        if let Some(&number) = iter.next() {
            if let Some(&number2) = iter.next() {
                if !a_pairs[number].is_empty() {
                    let a_pos = a_pairs[number].pop().unwrap();
                    pop_select(&mut a_numbers, number);

                    b_pairs[number2].pop().unwrap();
                    pop_select(&mut b_numbers, number2);

                    answers[a_pos] = number2;
                } else {
                    let a_pos = a_pairs[number2].pop().unwrap();
                    pop_select(&mut a_numbers, number2);

                    b_pairs[number].pop().unwrap();
                    pop_select(&mut b_numbers, number);

                    answers[a_pos] = number;
                }
                count_nums[count_i].remove(&number);
                count_nums[count_i].remove(&number2);
                count_nums[count_i - 1].insert(number);
                count_nums[count_i - 1].insert(number2);
            } else {
                let num2;
                if !a_pairs[number].is_empty() {
                    // from a
                    let a_pos = a_pairs[number].pop().unwrap();
                    pop_select(&mut a_numbers, number);

                    num2 = pop_any(&mut b_numbers, number);
                    b_pairs[num2].pop().unwrap();
                    answers[a_pos] = num2;
                } else {
                    b_pairs[number].pop().unwrap();
                    pop_select(&mut b_numbers, number);

                    num2 = pop_any(&mut a_numbers, number);
                    let a_pos = a_pairs[num2].pop().unwrap();
                    answers[a_pos] = number;
                }
                let count2_prev = a_pairs[num2].len() + b_pairs[num2].len() + 1;
                count_nums[count2_prev].remove(&num2);
                count_nums[count2_prev - 1].insert(num2);
                count_nums[count_i].remove(&number);
                count_nums[count_i - 1].insert(number);
            }
        }

        if count_nums[count_i].len() == 0 {
            count_i -= 1;
        }
    }

    println!("Yes");
    for ans in answers {
        print!("{} ", ans + 1);
    }
    println!("");
}

fn pop_select(a_number: &mut HashMap<usize, usize>, ok: usize) {
    *a_number.get_mut(&ok).unwrap() -= 1;
    if a_number[&ok] == 0 {
        a_number.remove(&ok);
    }
}

fn pop_any(a_number: &mut HashMap<usize, usize>, ng: usize) -> usize {
    let mut ret = std::usize::MAX;
    let mut v0 = 0;
    for (k, v) in a_number.iter() {
        if *k != ng {
            ret = *k;
            v0 = *v;
            break;
        }
    }
    if v0 == 1 {
        a_number.remove(&ret);
    } else {
        *a_number.get_mut(&ret).unwrap() = v0 - 1;
    }
    ret
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}