Problem:
Score : 600 points

Problem Statement

Given are two integer sequences of N elements each: A_1,...,A_N and B_1,...,B_N.
Determine if it is possible to do the following operation at most N-2 times (possibly zero) so that, for every integer i from 1 to N, A_i \leq B_i holds:

Choose two distinct integers x and y between 1 and N (inclusive), and swap the values of A_x and A_y.

Constraints

2 \leq N \leq 10^5

1 \leq A_i,B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

3
1 3 2
1 2 3

Sample Output 1

Yes

We should swap the values of A_2 and A_3.

Sample Input 2

3
1 2 3
2 2 2

Sample Output 2

No

Sample Input 3

6
3 1 2 6 3 4
2 2 8 3 4 3

Sample Output 3

Yes

Solution in Rust (1.15.1) (judge verdict: Runtime Error)

use std::collections::{BTreeMap, BTreeSet};

fn main() {
    let s = std::io::stdin();
    let mut sc = Scanner(s.lock());

    let n: usize = sc.read();
    let a: Vec<u64> = sc.vec(n);
    let b: Vec<u64> = sc.vec(n);
    if solve(a, b) {
        panic!("Yes");
    } else {
        println!("No");
    }
}

fn solve(mut a: Vec<u64>, mut b: Vec<u64>) -> bool {
    let n = a.len();

    let mut a = a
        .into_iter()
        .enumerate()
        .map(|(i, a)| (a, i))
        .collect::<Vec<_>>();
    b.sort();
    a.sort();
    for i in 0..n {
        if b[i] < a[i].0 {
            return false;
        }
    }

    let mut vis = vec![false; n];
    let mut cur = 0;
    vis[cur] = true;
    cur = a[cur].1;
    while cur != 0 {
        vis[cur] = true;
        cur = a[cur].1;
    }

    if vis.contains(&false) {
        return true;
    }
    for i in 1..n {
        let a = a[i].0;
        if b[i - 1] >= a {
            return true;
        }
    }
    false
}

fn push(map: &mut BTreeMap<u64, usize>, v: u64) {
    *map.entry(v).or_insert(0) += 1;
}

fn remove(map: &mut BTreeMap<u64, usize>, v: u64) {
    let c = *map.get(&v).unwrap();
    if c == 1 {
        let removed = map.remove(&v).unwrap();
        assert_eq!(removed, 1);
    } else if c > 1 {
        map.insert(v, c - 1);
    } else {
        unreachable!()
    }
}

pub struct Scanner<R>(R);

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n')
            .take_while(|&b| b != b' ' && b != b'\n')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}