Problem:
Score : 700 points

Problem Statement

There are N bags, each containing two white balls. The i-th box contains two balls with integers x_i and y_i written on them, respectively.

For each of these bags, you will paint one of the balls red, and paint the other blue.

Afterwards, the 2N balls will be classified according to color.

Then, we will define the following:

R_{max}: the maximum integer written on a ball painted in red

R_{min}: the minimum integer written on a ball painted in red

B_{max}: the maximum integer written on a ball painted in blue

B_{min}: the minimum integer written on a ball painted in blue

Find the minimum possible value of (R_{max} - R_{min}) \times (B_{max} - B_{min}).

Constraints

1 ≤ N ≤ 200,000

1 ≤ x_i, y_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the minimum possible value.

Sample Input 1

3
1 2
3 4
5 6

Sample Output 1

15

The optimal solution is to paint the balls with x_1, x_2, y_3 red, and paint the balls with y_1, y_2, x_3 blue.

Sample Input 2

3
1010 10
1000 1
20 1020

Sample Output 2

380

Sample Input 3

2
1 1
1000000000 1000000000

Sample Output 3

999999998000000001

Solution in Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports, unused_variables, dead_code)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

trait InputValue {
    fn parse(s: &str) -> Self;
}

fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);

// ===

fn solve0(xy: &Vec<(i64, i64)>) -> i64 {
    // fix rmin and bmax

    let n = xy.len();
    if n == 1 {
        return 0
    }

    let mut rmin = xy[0].0;
    let mut rmax = xy[0].0;
    let mut bmin = xy[0].1;
    let mut bmax = xy[0].1;

    let mut bi = 0;
    for i in 0..n {
        if bmax <= xy[i].1 {
            bmax = xy[i].1;
            bi = i;
        }
    }
    rmin = min(rmin, xy[bi].0);
    rmax = max(rmax, xy[bi].0);

    for i in 0..n {
        if i == 0 || i == bi {
            continue
        }
        bmin = min(bmin, xy[i].1);
        rmax = max(rmax, xy[i].0);
    }

    (rmax - rmin) * (bmax - bmin)
}

fn solve1(xy: &Vec<(i64, i64)>) -> i64 {
    // fix rmin and rmax
    let n = xy.len();
    if n == 1 {
        return 0
    }

    let mut rmin = xy[0].0;
    let mut rmax = xy[0].0;
    let mut bmin = xy[0].1;
    let mut bmax = xy[0].1;

    let mut bi = 0;
    for i in 1..n {
        if rmax <= xy[i].1 {
            rmax = xy[i].1;
            bi = i;
        }
    }
    bmin = min(bmin, xy[bi].0);
    bmax = max(bmax, xy[bi].0);

    if n == 2 {
        return (rmax - rmin) * (bmax - bmin)
    }

    let mut pairs = vec![];
    for i in 0..n {
        if i != 0 && i != bi {
            pairs.push(xy[i]);
        }
    }

    let pn = pairs.len();

    let mut right_min = 1e11 as i64;
    let mut right_max = 0;
    let mut best_blue = (0, 1e11 as i64);
    for i in 0..pn {
        let bmin = min(min(bmin, pairs[i].0), right_min);
        let bmax = max(max(bmax, right_max), pairs[pn-1].0);
        if best_blue.1 - best_blue.0 >= bmax - bmin {
            best_blue = (bmin, bmax);
        }
        right_min = min(right_min, pairs[i].1);
        right_max = max(right_max, pairs[i].1);
    }

    (rmax - rmin) * (best_blue.1 - best_blue.0)
}

fn main() {
    let n: usize = read();
    let mut xy: Vec<(i64, i64)> = readn(n);

    for i in 0..n {
        if xy[i].1 < xy[i].0 {
            xy[i] = (xy[i].1, xy[i].0);
        }
    }
    xy.sort();

    // println!("{:?}", xy);
    println!("{}", min(solve0(&xy), solve1(&xy)));
}