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(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];
    for count_i in (1..n + 1).rev() {
        let number1;
        let number2;
        if count_nums[count_i].len() == 2 {
            let l = count_nums[count_i].iter().collect::<Vec<_>>();
            number1 = *l[0];
            number2 = *l[1];
        } else if count_nums[count_i].len() == 1 {
            number1 = *count_nums[count_i].iter().next().unwrap();
            if a_pairs[number1].len() > 0 {
                number2 = get_any(&mut b_numbers, number1);
            } else {
                number2 = get_any(&mut a_numbers, number1);
            }
        } else {
            number1 = get_any(&mut a_numbers, std::usize::MAX);
            number2 = get_any(&mut b_numbers, number1);
        }
        let a_pos;
        let b_pos;
        if a_pairs[number1].len() > 0 {
            a_pos = a_pairs[number1].pop().unwrap();
            b_pos = b_pairs[number2].pop().unwrap();
            pop_select(&mut a_numbers, number1);
            pop_select(&mut b_numbers, number2);
        } else {
            a_pos = a_pairs[number2].pop().unwrap();
            b_pos = b_pairs[number1].pop().unwrap();
            pop_select(&mut a_numbers, number2);
            pop_select(&mut b_numbers, number1);
        }
        for &num in &[number1, number2] {
            let num_count_before = a_pairs[num].len() + b_pairs[num].len() + 1;
            count_nums[num_count_before].remove(&num);
            count_nums[num_count_before - 1].insert(num);
        }
        answers[a_pos] = b[b_pos];
    }

    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 get_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;
        }
    }
    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()
}