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 Rust (1.15.1) (judge verdict: Wrong Answer)

use std::cmp::{min, max};

fn merge(a: (u32, u32), b: (u32, u32)) -> (u32, u32){
    let l = max(a.0, b.0);
    let r = min(a.1, b.1);
    (l, r)
}
fn area((l, r): (u32, u32)) -> u32{
    if l > r{
        0
    }else{
        r - l + 1
    }
}
fn marea(a: (u32, u32), b: (u32, u32)) -> u32{
    area(merge(a, b))
}

fn main(){
    let n: u32 = linp();
    let mut qs: Vec<(u32, u32)> = Vec::new();
    qs.push(linp());
    qs.push(linp());
    let (mut c1, mut c2)
        = if qs[0].0 < qs[1].0{
            (qs[0], qs[1])
        }else{
            (qs[1], qs[0])
        };
    for _ in 2 .. n{
        let q: (u32, u32) = linp();
        if q.0 < c1.0{
            c1 = q;
        }else if q.0 > c2.0{
            c2 = q;
        }
        qs.push(q);
    }
    for q in qs{
        if marea(c1, q) + area(c2) < marea(c2, q) + area(c1){
            c2 = merge(c2, q);
        }else{
            c1 = merge(c1, q);
        }
    }
    println!("{}", area(c1) + area(c2));
}

// easy IOs
trait SimpleParse{
    fn simple_parse(s: &str) -> Self;
    fn get_slice_index(s: &str) -> Option<usize>{
        s.find(|c|{c == ' ' || c == '\t' || c == '\n'})
    }
    fn next_vec_item<'a>(s: &'a str) -> Option<&'a str>{
        match s.find(|c|{!(c == ' ' || c == '\t' || c == '\n')}){
            Some(i) => Some(&s[i..]),
            None => None
        }
    }
}
macro_rules! new_parse{
    ( $t:ty ) => {
        impl SimpleParse for $t{
            fn simple_parse(s: &str) -> Self{
                s.trim().parse().expect("input parse error")
            }
        }
    }
}
macro_rules! new_tparse{
    ( $a:ident: $A:ident; $($x:ident: $X:ident, $y:ident: $Y:ident);* ) => {
        impl<$A, $($Y),*> SimpleParse for ($A, $($Y),*)
        where $A: SimpleParse, $($Y: SimpleParse),* {
            fn simple_parse($a: &str) -> Self{
                $(
                    let $x = $x.trim();
                    let ($x, $y) = $x.split_at(
                            $X::get_slice_index($x)
                            .expect("not enough inputs")
                    );
                )*
                ($A::simple_parse($a), $($Y::simple_parse($y)),*)
            }
            fn get_slice_index(s: &str) -> Option<usize>{
                let ret = $A::get_slice_index(s);
                $(
                    let ret = ret.expect("not enough inputs");
                    let ret = (&s[ret..])
                        .find(|c|{!(c == ' ' || c == '\t' || c == '\n')})
                        .expect("not enough inputs");
                    let ret = $Y::get_slice_index(&s[ret..]);
                )*
                ret
            }
        }
    }
}
impl SimpleParse for String{
    fn simple_parse(s: &str) -> String{
        String::from(s.trim())
    }
}
impl SimpleParse for char{
    fn simple_parse(s: &str) -> char{
        if s.len() != 1{
            panic!("char input error");
        }
        s.chars().nth(0).unwrap()
    }
    fn get_slice_index(s: &str) -> Option<usize>{
        if s.len() < 1{
            None
        }else{
            Some(1)
        }
    }
    fn next_vec_item<'a>(s: &'a str) -> Option<&'a str>{
        if s.len() == 0{
            None
        }else{
            Some(s)
        }
    }
}
impl<T> SimpleParse for Vec<T>
where T: SimpleParse{
    fn simple_parse(s: &str) -> Vec<T>{
        let mut ret: Vec<T> = Vec::new();
        let mut s = s.trim();
        if s.len() == 0{
            return ret;
        }
        loop{
            match T::get_slice_index(s){
                Some(i) => {
                    let (a, b) = s.split_at(i);
                    ret.push(T::simple_parse(a));
                    match T::next_vec_item(b){
                        Some(ss) => {s = ss;}
                        None => {return ret;}
                    }
                }
                None => {
                    ret.push(T::simple_parse(s));
                    return ret;
                }
            }
        }
    }
    fn get_slice_index(_: &str) -> Option<usize>{
        None
    }
}
impl<A> SimpleParse for (A,)
where A: SimpleParse {
    fn simple_parse(s: &str) -> Self{
        (A::simple_parse(s),)
    }
}
new_parse!(i8);
new_parse!(u8);
new_parse!(i16);
new_parse!(u16);
new_parse!(i32);
new_parse!(u32);
new_parse!(i64);
new_parse!(u64);
new_parse!(f32);
new_parse!(f64);
new_parse!(isize);
new_parse!(usize);
new_tparse!(a:A; a:A,b:B);
new_tparse!(a:A; a:A,b:B; b:B,c:C);
new_tparse!(a:A; a:A,b:B; b:B,c:C; c:C,d:D);
new_tparse!(a:A; a:A,b:B; b:B,c:C; c:C,d:D; d:D,e:E);
new_tparse!(a:A; a:A,b:B; b:B,c:C; c:C,d:D; d:D,e:E; e:E,f:F);
new_tparse!(a:A; a:A,b:B; b:B,c:C; c:C,d:D; d:D,e:E; e:E,f:F; f:F,g:G);
new_tparse!(a:A; a:A,b:B; b:B,c:C; c:C,d:D; d:D,e:E; e:E,f:F; f:F,g:G; g:G,h:H);
#[allow(dead_code)]
fn lin() -> String{
    let mut input = String::new();
    let _ = std::io::stdin().read_line(&mut input).expect("input error");
    input
}
#[allow(dead_code)]
fn linp<A: SimpleParse>() -> A{
    A::simple_parse(lin().trim())
}