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 9.2.1) (judge verdict: Accepted)

#include <stdio.h>

const int sup = 1 << 30;

typedef struct {
	int id;
	long long key;
} data;

typedef struct {
	data obj[100001];
	int size;
} max_heap;

void push(data x, max_heap* h)
{
	int i = h->size;
	data tmp;
	h->obj[(h->size)++] = x;
	while (1) {
		if (i == 0) break;
		else if (h->obj[i].key > h->obj[(i-1)/2].key) {
			tmp = h->obj[(i-1)/2];
			h->obj[(i-1)/2] = h->obj[i];
			h->obj[i] = tmp;
			i = (i - 1) / 2;
		} else break;
	}
}

data pop(max_heap* h)
{
	int i = 0, j;
	data output = h->obj[0], tmp;
	h->obj[0] = h->obj[--(h->size)];
	while (1) {
		if (i * 2 + 1 >= h->size) break;
		else if (i * 2 + 2 >= h->size) j = i * 2 + 1;
		else if (h->obj[i*2+1].key > h->obj[i*2+2].key) j = i * 2 + 1;
		else j = i * 2 + 2;
		if (h->obj[j].key > h->obj[i].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
		} else break;
	}
	return output;
}

int main()
{
	int i, N, L[100001], R[100001];
	scanf("%d", &N);
	for (i = 1; i <= N; i++) scanf("%d %d", &(L[i]), &(R[i]));

	int min = R[1], argmin = 1;
	for (i = 2; i <= N; i++) {
		if (R[i] < min) {
			min = R[i];
			argmin = i;
		}
	}

	data d;
	max_heap h;
	h.size = 0;
	for (i = 1; i <= N; i++) {
		if (i == argmin) continue;
		d.key = (long long)L[i] * sup + R[i];
		push(d, &h);
	}
	
	long long ans = 0, l[2] = {L[argmin], h.obj[0].key / sup}, r[2] = {R[argmin], h.obj[0].key % sup};
	while (h.size > 0 && h.obj[0].key / sup > r[0]) {
		d = pop(&h);
		l[1] = (l[1] < d.key / sup)? d.key / sup: l[1];
		r[1] = (r[1] > d.key % sup)? d.key % sup: r[1];
	}
	while (h.size > 0) {
		l[0] = h.obj[0].key / sup;
		if (r[0] - l[0] + 1 + ((r[1] >= l[1])? r[1] - l[1] + 1: 0) > ans) ans = r[0] - l[0] + 1 + ((r[1] >= l[1])? r[1] - l[1] + 1: 0);
		while (h.size > 0 && h.obj[0].key / sup == l[0]) {
			d = pop(&h);
			r[1] = (r[1] > d.key % sup)? d.key % sup: r[1];
		}
	}
	l[0] = L[argmin];
	if (r[0] - l[0] + 1 + ((r[1] >= l[1])? r[1] - l[1] + 1: 0) > ans) ans = r[0] - l[0] + 1 + ((r[1] >= l[1])? r[1] - l[1] + 1: 0);
	
	int max = R[argmin] - L[argmin], argmax = argmin;
	for (i = 1; i <= N; i++) {
		if (R[i] - L[i] > max) {
			max = R[i] - L[i];
			argmax = i;
		}
	}
	if (argmax != argmin) {
		for (i = 1, max = 0; i <= N; i++) {
			if (i == argmax) continue;
			if (L[i] > max) max = L[i];
		}
		if (R[argmax] - L[argmax] + 1 + ((max <= min)? min - max + 1: 0) > ans) ans = R[argmax] - L[argmax] + 1 + ((max <= min)? min - max + 1: 0);
	}
	
	printf("%lld\n", ans);
	fflush(stdout);
	return 0;
}