Problem:
Score : 1200 points

Problem Statement

Snuke loves flags.

Snuke is placing N flags on a line.

The i-th flag can be placed at either coordinate x_i or coordinate y_i.

Snuke thinks that the flags look nicer when the smallest distance between two of them, d, is larger. Find the maximum possible value of d.

Constraints

2 ≤ N ≤ 10^{4}

1 ≤ x_i, y_i ≤ 10^{9}

x_i and y_i are integers.

Input

The input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the answer.

Sample Input 1

3
1 3
2 5
1 9

Sample Output 1

4

The optimal solution is to place the first flag at coordinate 1, the second flag at coordinate 5 and the third flag at coordinate 9. The smallest distance between two of the flags is 4 in this case.

Sample Input 2

5
2 2
2 2
2 2
2 2
2 2

Sample Output 2

0

There can be more than one flag at the same position.

Sample Input 3

22
93 6440
78 6647
862 11
8306 9689
798 99
801 521
188 206
6079 971
4559 209
50 94
92 6270
5403 560
803 83
1855 99
42 504
75 484
629 11
92 122
3359 37
28 16
648 14
11 269

Sample Output 3

17

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>

#define For(i, j, k) for(int i = j; i <= k; i++)
#define pb push_back

using namespace std;

const int N = 20010, M = 3e7 + 10;

int Begin[N], Next[M], to[M], e;

void add(int u, bool fu, int v, bool fv){
	u = u << 1 | fu, v = v << 1 | fv;
	to[++e] = v, Next[e] = Begin[u], Begin[u] = e;
}

struct Flag{
	int x, y;
}A[N];

int n, m, top, clk;
int dfn[N], low[N], bel[N], stk[N];

void Tarjan(int o){
	low[o] = dfn[o] = ++clk;
	stk[++top] = o;
	for(int i = Begin[o]; i; i = Next[i]){
		int u = to[i];
		if(!dfn[u]){
			Tarjan(u);
			low[o] = min(low[u], low[o]);
		}
		else if(!bel[u]) low[o] = min(low[o], dfn[u]);
	}
	if(low[o] == dfn[o]){
		bel[o] = ++m;
		while(stk[top] ^ o) bel[stk[top--]] = m;
		--top;
	}
}

vector<int> G[N], dif[N];
int deg[N], val[N];

bool Topsort(){
	queue<int> q;
	For(i, 1, m) if(!deg[i]) q.push(i);
	while(!q.empty()){
		int o = q.front(), sz; q.pop();
		sz = int(dif[o].size()) - 1;
		val[o] = 1;
		For(i, 0, sz) if(val[dif[o][i]] == 1){
			val[o] = 0;
			break;
		}
		if(!val[o]) For(i, 0, sz) if(val[dif[o][i]] == 0) return false;
		sz = int(G[o].size()) - 1;
		For(i, 0, sz){
			int v = G[o][i];
			if(!--deg[v]) q.push(v);
		}
	}
	return true;
}

bool check(int lim){
	e = m = top = 0;
	For(i, 2, n * 2 + 1) Begin[i] = dfn[i] = bel[i] = 0;
	For(i, 1, n) For(j, i + 1, n){
		if(abs(A[i].x - A[j].y) < lim) add(i, 0, j, 0), add(j, 1, i, 1);
		if(abs(A[i].x - A[j].x) < lim) add(i, 0, j, 1), add(j, 0, i, 1);
		if(abs(A[i].y - A[j].y) < lim) add(i, 1, j, 0), add(j, 1, i, 0);
		if(abs(A[i].y - A[j].x) < lim) add(i, 1, j, 1), add(j, 0, i, 0);
	}

	clk = 0;
	For(i, 2, n * 2 + 1) if(!dfn[i]) Tarjan(i);

	For(i, 1, m) G[i].clear(), dif[i].clear(), deg[i] = 0, val[i] = -1;
	For(i, 1, n){
		int u = bel[i << 1], v = bel[i << 1 | 1];
		if(u == v) return false;
		dif[u].pb(v), dif[v].pb(u);
	}

	For(i, 2, n * 2 + 1) for(int j = Begin[i]; j; j = Next[j])
		if(bel[i] ^ bel[to[j]]) G[bel[to[j]]].pb(bel[i]), deg[bel[i]]++;
	return Topsort();
}

int main(){
	scanf("%d", &n);
	For(i, 1, n){
		scanf("%d%d", &A[i].x, &A[i].y);
		if(A[i].x > A[i].y) swap(A[i].x, A[i].y);
	}

	int Maxd = 1e9 / n + 1;
	For(i, 1, n) For(j, i + 1, n) Maxd = min(Maxd, max(abs(A[j].y - A[i].x), abs(A[i].y - A[j].x)));

	int L = 0, R = Maxd;
	while(L < R){
		int mid = (L + R + 1) >> 1;
		if(check(mid)) L = mid;
		else R = mid - 1;
	}

	printf("%d\n", L);

	return 0;
}