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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

	public static void main(String args[]) {
		FastScanner cin = new FastScanner();
		PrintWriter cout = new PrintWriter(System.out);
		solve(cin, cout);
		cout.flush();
	}

	private static void solve(FastScanner cin, PrintWriter cout) {
		assert solve(3, new int[][] { { 1, 2 }, { 3, 4 }, { 5, 6 } }) == 15;
		assert solve(3, new int[][] { { 1010, 10 }, { 1000, 1 }, { 20, 1020 } }) == 380;
		assert solve(2, new int[][] { { 1, 1 }, { 1000000000, 1000000000 } }) == 999999998000000001L;
		int n = cin.nextInt();
		int[][] xys = new int[n][];
		for (int i = 0; i < n; i++)
			xys[i] = new int[] { cin.nextInt(), cin.nextInt() };
		long ans = -1;
		ans = solve(n, xys);
		cout.println(ans);
	}

	private static long solve(int n, int[][] xys) {
		if (n == 1)
			return 0;
		int[][] balls = new int[2 * n][];
		for (int i = 0; i < n; i++) {
			balls[2 * i] = new int[] { xys[i][0], i };
			balls[2 * i + 1] = new int[] { xys[i][1], i };
		}
		Arrays.sort(balls, (ball1, ball2) -> Integer.compare(ball1[0], ball2[0]));

		int[] notFound = new int[n];
		int toFind = 0;

		long[] maxOfMin = new long[2];
		for (int j = 0; j < 2; j++) {
			Arrays.fill(notFound, 1);
			toFind = n;
			for (int i = j; i < balls.length; i++) {
				if (toFind == 0)
					break;
				toFind -= notFound[balls[i][1]];
				notFound[balls[i][1]] = 0;
				maxOfMin[j] = balls[i][0];
			}
		}

		long[] minOfMax = new long[2];
		for (int j = 0; j < 2; j++) {
			Arrays.fill(notFound, 1);
			toFind = n;
			for (int i = balls.length - 1 - j; i >= 0; i--) {
				if (toFind == 0)
					break;
				toFind -= notFound[balls[i][1]];
				notFound[balls[i][1]] = 0;
				minOfMax[j] = balls[i][0];
			}
		}

		long ans = Long.MAX_VALUE;
		// min-allpairs, max-allpairs
		ans = Math.min(ans, (maxOfMin[0] - balls[0][0]) * (balls[balls.length - 1][0] - minOfMax[0]));
		// min-max, 2ndlargest-allpairs
		ans = Math.min(ans, (balls[balls.length - 1][0] - balls[0][0]) * (balls[balls.length - 2][0] - minOfMax[1]));
		// min-max, 2ndsmallest-allpairas
		ans = Math.min(ans, (balls[balls.length - 1][0] - balls[0][0]) * (maxOfMin[1] - balls[1][0]));
		return ans;
	}

	/**
	 * How to read input in Java — tutorial <br />
	 * By Flatfoot<br />
	 * http://codeforces.com/blog/entry/7018
	 */
	static class FastScanner {
		private BufferedReader br;
		private StringTokenizer st;

		FastScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements())
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					throw new IllegalStateException(e);
				}
			return st.nextToken();
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				throw new IllegalStateException(e);
			}
			return str;
		}

		long nextLong() {
			return Long.parseLong(next());
		}
	}

}