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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 1000000000
#define LLINF 100000000000000000ll
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(),(x).end()
#define sq(x) ((x)*(x))
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define repn(i,a,n) for(int (i)=(a);(i)<(int)(n);(i)++)
#define EQ(a,b) (abs((a)-(b))<eps)
template<class T> void chmin(T& a,const T& b){if(a>b)a=b;}
template<class T> void chmax(T& a,const T& b){if(a<b)a=b;}
int N;
ll x[200100],y[200100];
P p[200100];
ll A[200100],B[200100];
ll ans = 0ll;
int main(){
	cin >> N;
	ll mi = LLINF,ma = -LLINF;
	ll a = -LLINF, b= LLINF;
	for(int i=0;i<N;i++){
		cin >> x[i] >> y[i];
		mi = min(mi,x[i]);
		mi = min(mi,y[i]);
		ma = max(ma,x[i]);
		ma = max(ma,y[i]);
		if(x[i]>y[i])swap(x[i],y[i]);
		p[i].fi = x[i];
		p[i].sec= y[i];
		a = max(a,x[i]);
		b = min(b,y[i]);
	}
	ans = (a-mi)*(ma-b);
	sort(p,p+N);
	A[0]=B[0]=p[0].sec;
	for(int i=1;i<N;i++){
		A[i]=min(A[i-1],p[i].sec);
		B[i]=max(B[i-1],p[i].sec);
	}
	for(int i=0;i<N-1;i++){
		ll ret = (max(p[N-1].fi,B[i])-min(p[i].fi,A[i]))*(ma-mi);
		ans = min(ans,ret);
	}
	ans = min(ans,(p[N-1].fi-p[0].fi)*(ma-mi));
	ans = min(ans,(B[N-1]-A[N-1])*(ma-mi));
	cout << ans << endl;
	return 0;
}