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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Comparator<Integer[]> comparator_L = new Comparator<Integer[]>() {
            @Override
            public int compare(Integer[] o1, Integer[] o2) {
                return Integer.compare(o1[0], o2[0]);
            }
        };
        Comparator<Integer[]> comparator_R = new Comparator<Integer[]>() {
            @Override
            public int compare(Integer[] o1, Integer[] o2) {
                return Integer.compare(o2[1], o1[1]);
            }
        };
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        int N = in.nextInt();
        int[] flag = new int[N];
        int max_edge = 0;
        ArrayList<Integer[]> edge = new ArrayList<Integer[]>();
        for (int i=0;i<N;i++) {
            int L = in.nextInt();
            int R = in.nextInt();
            max_edge = Math.max(max_edge, R-L);
            Integer[] add = {L, R, i};
            edge.add(add);
        }
        ArrayList<Integer[]> edge_L = new ArrayList<Integer[]>(edge);
        ArrayList<Integer[]> edge_R = new ArrayList<Integer[]>(edge);
        Collections.sort(edge_L, comparator_L);
        Collections.sort(edge_R, comparator_R);

        int l_i=0;
        int r_i=0;
        long l_left = edge_L.get(0)[0];
        long LL = l_left;
        long l_right = edge_L.get(0)[1];
        flag[edge_L.get(l_i)[2]]=1;
        long r_left = edge_R.get(0)[0];
        long r_right = edge_R.get(0)[1];
        long RR = r_right;
        if (flag[edge_R.get(r_i)[2]]==1) {
            r_i++;
        }
        flag[edge_R.get(r_i)[2]]=1;
        for (int i=0;i<N-2;i++) {
            // if (l_i==N || r_i==N) {
            //     break;
            // }
            // System.out.println(Arrays.toString(edge_L.get(l_i)));
            // System.out.println(flag[edge_L.get(l_i)[2]]);
            while (flag[edge_L.get(l_i)[2]]!=0) {
                l_i++;
            }
            while (flag[edge_R.get(r_i)[2]]!=0) {
                r_i++;
            }
            // System.out.println("i: " + i + " l_dif: " + (edge_L.get(l_i)[0]-l_left) + " r_diff: " + (r_right-edge_R.get(r_i)[1]));
            // if (edge_L.get(l_i)[0]-l_left <= r_right-edge_R.get(r_i)[1]) {
            if (edge_L.get(l_i)[0]-LL <= RR-edge_R.get(r_i)[1]) {
                l_left=edge_L.get(l_i)[0];
                // l_right=edge_L.get(l_i)[1];
                l_right=Math.min(l_right, edge_L.get(l_i)[1]);
                flag[edge_L.get(l_i)[2]]=1;
                // System.out.println("l_index: " + edge_L.get(l_i)[2]);
                l_i++;
            } else {
                // r_left = edge_R.get(r_i)[0];
                r_left = Math.max(r_left, edge_R.get(r_i)[0]);
                r_right = edge_R.get(r_i)[1];
                flag[edge_R.get(r_i)[2]]=1;
                // System.out.println("r_index: " + edge_R.get(r_i)[2]);
                r_i++;
            }
        }
        // System.out.println("l_left: " + l_left + " l_right: " + l_right);
        // System.out.println("r_left: " + r_left + " r_right: " + r_right);
        long ans = 0L;
        if (l_left<=l_right && r_left<=r_right) {
            ans = (long)l_right-(long)l_left+1 + (long)r_right-(long)r_left+1;
        }
        // }
         // else {
            // ans = r_right-l_left+1;
        // }
        out.println(Math.max(ans, max_edge+1));
        // for (Integer[] i : edge_L) {
        //     out.println(Arrays.toString(i));
        // }


        out.close();
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}