Problem:
Score : 600 points

Problem Statement

10^9 contestants, numbered 1 to 10^9, will compete in a competition.
There will be two contests in this competition.

The organizer prepared N problems, numbered 1 to N, to use in these contests.
When Problem i is presented in a contest, it will be solved by all contestants from Contestant L_i to Contestant R_i (inclusive), and will not be solved by any other contestants.

The organizer will use these N problems in the two contests.
Each problem must be used in exactly one of the contests, and each contest must have at least one problem.

The joyfulness of each contest is the number of contestants who will solve all the problems in the contest.
Find the maximum possible total joyfulness of the two contests.

Constraints

2 \leq N \leq 10^5

1 \leq L_i \leq R_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

Print the maximum possible total joyfulness of the two contests.

Sample Input 1

4
4 7
1 4
5 8
2 5

Sample Output 1

6

The optimal choice is:

Use Problem 1 and 3 in the first contest. Contestant 5, 6, and 7 will solve both of them, so the joyfulness of this contest is 3.

Use Problem 2 and 4 in the second contest. Contestant 2, 3, and 4 will solve both of them, so the joyfulness of this contest is 3.

The total joyfulness of these two contests is 6. We cannot make the total joyfulness greater than 6.

Sample Input 2

4
1 20
2 19
3 18
4 17

Sample Output 2

34

Sample Input 3

10
457835016 996058008
456475528 529149798
455108441 512701454
455817105 523506955
457368248 814532746
455073228 459494089
456651538 774276744
457667152 974637457
457293701 800549465
456580262 636471526

Sample Output 3

540049931

Solution in C (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <inttypes.h>

static unsigned int x=62526377;
static unsigned int y=5463654;
static unsigned int z=87602543;
static unsigned int w=13597657;

void seed(unsigned int a,unsigned int b,unsigned int c,unsigned int d) {
	if((a|b|c|d)==0)return;
	x=a;y=b;z=c;w=d;
}

unsigned int randint(void) {
	unsigned int t;
	t=(x^(x<<11));
	x=y;y=z;z=w;
	w=(w^(w>>19))^(t^(t>>8));
	return w;
}

#define MAX 200000

int32_t N;
struct lr_t {
	int32_t L, R;
} LR[MAX];

int32_t solve(int32_t seed1, int32_t seed2) {
	int32_t i;
	int32_t al, ar, bl, br;
	/* 損が少ない方に入れる貪欲 */
	al = LR[seed1].L; ar = LR[seed1].R;
	bl = LR[seed2].L; br = LR[seed2].R;
	for (i = 0; i < N; i++) {
		int32_t al2 = al, ar2 = ar, bl2 = bl, br2 = br;
		int32_t psa = ar - al, psb = br - bl, asa, asb;
		if (i == seed1 || i == seed2) continue;
		if (al2 < LR[i].L) al2 = LR[i].L;
		if (LR[i].R < ar2) ar2 = LR[i].R;
		if (bl2 < LR[i].L) bl2 = LR[i].L;
		if (LR[i].R < br2) br2 = LR[i].R;
		if (ar2 < al2) ar2 = al2;
		if (br2 < bl2) br2 = bl2;
		asa = ar2 - al2;
		asb = br2 - bl2;
		if ((psa - asa) <= (psb - asb)) {
			al = al2; ar = ar2;
		} else {
			bl = bl2; br = br2;
		}
	}

	return (ar - al) + (br - bl);
}

int main(void) {
	int32_t i;
	int32_t ai = 0, bi = 0, asmax = -1, bsmax = -1;
	int32_t best = 0, candidate;
	if (scanf("%" SCNd32, &N) != 1) return 1;
	for (i = 0; i < N; i++) {
		if  (scanf("%" SCNd32 "%" SCNd32, &LR[i].L, &LR[i].R) != 2) return 1;
		LR[i].R++; /* [L, R) に変換 */
	}

	/* 一番長いのを選ぶ */
	asmax = -1;
	for (i = 0; i < N; i++) {
		int32_t as = LR[i].R - LR[i].L;
		if (as > asmax) { ai = i; asmax = as; }
	}
	/* それと一番共通部分が少ないのを選ぶ */
	bsmax = MAX;
	for (i = 0; i < N; i++) {
		int32_t l = LR[ai].L < LR[i].L ? LR[i].L : LR[ai].L;
		int32_t r = LR[i].R < LR[ai].R ? LR[i].R : LR[ai].R;
		int32_t bs;
		if (i == ai) continue;
		bs = l < r ? r - l : 0;
		if (bs < bsmax) { bi = i; bsmax = bs; }
	}
	candidate = solve(ai, bi);
	if (best < candidate) best = candidate;

	/* Lが一番左のを選ぶ */
	asmax = MAX;
	for (i = 0; i < N; i++) {
		int32_t as = LR[i].L;
		if (as < asmax) { ai = i; asmax = as; }
	}
	/* Rが一番右のを選ぶ */
	bsmax = -1;
	for (i = 0; i < N; i++) {
		int32_t bs;
		if (i == ai) continue;
		bs = LR[i].R;
		if (bs > bsmax) { bi = i; bsmax = bs; }
	}
	candidate = solve(ai, bi);
	if (best < candidate) best = candidate;

	/* Rが一番左のを選ぶ */
	asmax = MAX;
	for (i = 0; i < N; i++) {
		int32_t as = LR[i].R;
		if (as < asmax) { ai = i; asmax = as; }
	}
	/* Lが一番右のを選ぶ */
	bsmax = -1;
	for (i = 0; i < N; i++) {
		int32_t bs;
		if (i == ai) continue;
		bs = LR[i].L;
		if (bs > bsmax) { bi = i; bsmax = bs; }
	}
	candidate = solve(ai, bi);
	if (best < candidate) best = candidate;

	/* 一番短いのを選ぶ */
	asmax = MAX;
	for (i = 0; i < N; i++) {
		int32_t as = LR[i].R - LR[i].L;
		if (as < asmax) { ai = i; asmax = as; }
	}
	/* 二番目に短いのを選ぶ */
	bsmax = MAX;
	for (i = 0; i < N; i++) {
		int32_t bs;
		if (i == ai) continue;
		bs = LR[i].R - LR[i].L;
		if (bs > bsmax) { bi = i; bsmax = bs; }
	}
	candidate = solve(ai, bi);
	if (best < candidate) best = candidate;

	/* やけくそ乱択 */
	for (i = 0; i < 100; i++) {
		ai = randint() % N;
		bi = randint() % (N - 1);
		if (ai <= bi) bi++;
		candidate = solve(ai, bi);
		if (best < candidate) best = candidate;
	}

	printf("%" PRId32 "\n", best);

	return 0;
}

/*
各コンテスト最初の1問を除いて、問題をコンテストに入れたとき
* 必ず解ける人は減るか変わらない (増えない)
* 必ず範囲の左端は変わらないか右に動く
* 必ず範囲の右端は変わらないか左に動く

予想：一番長いのと一番短いのは別のコンテストにするべき
これは偽
3
1 10
9 15
11 100

-> 92

入力例1
12345678
   ****
****
    ****
 ****

もし、共通部分が無い問題の組があるとき
同じコンテストに入れる -> 楽しさ0になる -> もう1方は残りから一番長い1問を取ってほかを全部ぶちこむ
違うコンテストに入れる -> ？

1. 各コンテスト最初の1問を決める
2. 残りを適当に割り振る

「1本とそれ以外」を超えるパフォーマンスを出せるか？

*/

/*

4
1 10
11 101
2 100
12 13

-> 99

**********
 ******************************************
          **********************************
           *


*/