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

/**
SXR0aXAkI0JwbXptI3FhI3Z3I293bCNqY2IjUG0jMCNicG0jVHFkcXZvLyNCcG0jQW10bjBhY2phcWFicXZvLyNNYm16dml0MSNWdyNhdGN1am16I2tpdiNhbXF9bSNQcXUjVnd6I0F0bW14MSNQcWEjaXptI2l0dCNicHF2b2EjUXYjYnBtI3BtaWRtdmEjaXZsI3d2I21pemJwMSNFcHcjcWEjYnBtem0ja2l2I3F2Ym16a21sbSNRdiNQcWEjeHptYW12a20jbXtrbXhiI0lhI3BtI3htenVxYmJtYnBHI1BtI3N2d2VtYnAjRXBpYiMraXh4bWl6bWJwI2J3I1BxYSNrem1pYmN6bWEjSWEsI0ptbnd6bSN3eiNJbmJteiN3eiNKbXBxdmwjYnBtdTEjVnd6I2FwaXR0I2JwbXwja3d1eGlhYSNJY29wYiN3biNwcWEjc3Z3ZXRtbG9tI017a214YiNpYSNQbSNlcXR0bWJwMSNQcWEjYnB6d3ZtI2x3YnAjbXtibXZsI1dkbXojYnBtI3BtaWRtdmEjSXZsI3d2I21pemJwLyNpdmwjUG0jbm1tdG1icCNWdyNuaWJxb2NtI3F2I29jaXpscXZvI0l2bCN4em1hbXpkcXZvI2JwbXUvI053eiNQbSNxYSNicG0jVXdhYiNQcW9wMSNCcG0jQWN4em11bSMrcXYjb3R3enwsMQ==
*/
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back

const long long MOD = 1e9 + 7;
const long long MAXN = 1e6 + 1;
using namespace std;

typedef long long ll;

long long readInt() {
    bool minus1 = false;
    long long result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus1 = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus1)
        return -result;
    else
        return result;
}

pair<int,int> a[MAXN];

int main() {
    int n = readInt();
    for (int i = 1; i <= n; i++) {
        a[i] = {readInt(), readInt()};
    }
    sort(a + 1, a + 1 + n);
    multiset <int> s1;
    multiset <int> s2;
    for (int i = 1; i <= n; i++) {
        s2.insert(a[i].S);
    }
    ll ans = 0;
    for (int i = 1; i < n; i ++) {
        /// min(R) - max(L) + 1
        s2.erase(s2.find(a[i].S));
        s1.insert(a[i].S);
        int min_R_1 = *s1.begin();
        int min_R_2 = *s2.begin();
        if (min_R_1 <= min_R_2) {
            ///checking for >
            auto nxt = s1.upper_bound(min_R_2);
            if (nxt == s1.end()) {
                //correct
              	ans = max(ans, max(1ll * min_R_1 - a[i].F + 1, 0ll) + max(1ll * min_R_2 - a[n].F + 1, 0ll));
            } else {
                ans = max(ans, max(1ll * (*nxt) - a[i].F + 1, 0ll) + max(1ll * min_R_1 - a[n].F + 1, 0ll));
            }
            /// checking for =
            if (s1.count(min_R_2) > 1) {
				ans = max(ans, max(1ll * min_R_2 - a[i].F + 1, 0ll) + max(1ll * min_R_2 - a[n].F + 1, 0ll));            		
            }
        } else {
            ans = max(ans, max(1ll * min_R_1 - a[i].F + 1, 0ll) + max(1ll * min_R_2 - a[n].F + 1, 0ll));
        }
        if (min_R_1 >= min_R_2) {
        	//correct
          	auto nxt = s1.end();
        	nxt--;
			ans = max(ans, max(1ll * (*nxt) - a[i].F + 1, 0ll) + max(1ll * min_R_2 - a[n].F + 1, 0ll));
        }
        if (a[i].F == a[n].F) assert(0);
    }
    cout << ans;
    return 0;
}