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 C (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#define inf (int)(1e9 + 1)

int max(int a, int b){
	if(a > b){
		return a;
	}
	else{
		return b;
	}
}

int min(int a, int b){
	if(a < b){
		return a;
	}
	else{
		return b;
	}
}

typedef struct {
	int x;
	int y;
}pair;

#define datatype pair

int compare(datatype a, datatype b){
	if(a.y < b.y){
		return 1;
	}
	else if(a.y > b.y){
		return -1;
	}
	else{
		return 0;
	}
}

datatype *sort_sub(datatype *origin, int N){
	datatype *ans = (datatype *)malloc(sizeof(datatype) * N);
	if(N == 1){
		ans[0] = origin[0];
	}
	else{
		int N1 = N / 2;
		int N2 = (N + 1) / 2;
		datatype *sub1 = sort_sub(&origin[0] , N1);
		datatype *sub2 = sort_sub(&origin[N1], N2);
		int i, j, k;
		for(i = 0, j = 0, k = 0; i < N; i++){
			if((compare(sub1[j], sub2[k]) == 1 && j != N1) || (k == N2)){
				ans[i] = sub1[j];
				j++;
			}
			else{
				ans[i] = sub2[k];
				k++;
			}
		}
		free(sub1);
		free(sub2);
	}
	return ans;
}

datatype *sort(datatype *origin, int N){
	datatype *ans = sort_sub(origin, N);
	free(origin);
	return ans;
}

int main(){
	int N, i, tmp, xmax = 0, xmin = inf, ymax = 0, ymin = inf;
	scanf("%d", &N);
	pair *ball = (pair *)malloc(sizeof(pair) * N);
	for(i = 0; i < N; i++){
		scanf("%d%d", &ball[i].x, &ball[i].y);
		if(ball[i].x < ball[i].y){
			tmp = ball[i].x;
			ball[i].x = ball[i].y;
			ball[i].y = tmp;
		}
		if(ball[i].x > xmax){
			xmax = ball[i].x;
		}
		if(ball[i].x < xmin){
			xmin = ball[i].x;
		}
		if(ball[i].y > ymax){
			ymax = ball[i].y;
		}
		if(ball[i].y < ymin){
			ymin = ball[i].y;
		}
	}
	long long int ans1, ans2;
	ans1 = (long long int)(xmax - xmin) * (ymax - ymin);
	ball = sort(ball, N);
	int dy = ball[N - 1].y - ball[0].y, _ymax = ball[N - 1].y, _ymin = inf;
	for(i = 0; ball[i].y < _ymin && ball[i].x != xmax; i++){
		_ymax = max(ball[i].x, _ymax);
		_ymin = min(ball[i].x, _ymin);
		dy = min(_ymax - min(ball[i + 1].y, _ymin), dy);
	}
	ans2 = (long long int)(xmax - ymin) * dy;
	if(ans1 < ans2){
		printf("%lld\n", ans1);
	}
	else{
		printf("%lld\n", ans2);
	}
	return 0;
}