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# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCorder
{
	public class Program
	{
		static void Main(string[] args)
		{
			var cin = new Scanner2();
			int n = cin.Int();
			LR[] lrs = new LR[n];
			for (int i = 0; i < n; i++) {
				long l = cin.Long();
				long r = cin.Long();
				lrs[i] = new LR(l, r);
			}

			int maxIndex = 0;
			long maxCount = 0;
			for (int i = 0; i < n; i++) {
				long count = lrs[i].R - lrs[i].L + 1;
				if (count > maxCount) {
					maxIndex = i;
					maxCount = count;
				}
			}

			long remain = Count(lrs, maxIndex);
			long ans = maxCount + remain;

			int maxLIndex = 0;
			int minRIndex = 0;
			long minR = long.MaxValue;
			long maxL = -1;
			for (int i = 0; i < n; i++) {
				if (lrs[i].L > maxL) {
					maxLIndex = i;
					maxL = lrs[i].L;
				}

				if (lrs[i].R < minR) {
					minRIndex = i;
					minR = lrs[i].R;
				}
			}

			var diffsTemp = new List<LR>();
			for (int i = 0; i < n; i++) {
				if (i != maxLIndex && i != minRIndex) {
					diffsTemp.Add(new LR(Math.Max(0, minR - lrs[i].L + 1), Math.Max(0, lrs[i].R - maxL + 1)));
				}
			}
			
			var diffs = diffsTemp.OrderByDescending(x => x.L).ThenBy(x => x.R).ToArray();
			MultiSet<long> rs = new MultiSet<long>(diffs.Length);
			for (int i = 0; i < diffs.Length; i++) {
				rs.Add(diffs[i].R);
			}

			for (int i = 0; i <= diffs.Length; i++) {
				LR[] lr1 = new LR[Math.Max(i, 1)];

				if (i != 0) {
					rs.Remove(diffs[i  - 1].R);
				}

				for (int j = 0; j < i; j++) {
					lr1[j] = (diffs[j]);
				}

				long count1 = i == 0
					? lrs[minRIndex].R - lrs[minRIndex].L + 1
					: lr1[lr1.Length - 1].L;
				long count2 = i == diffs.Length
					? lrs[maxLIndex].R - lrs[maxLIndex].L + 1
					: rs.Min;

				ans = Math.Max(ans, count1 + count2);
			}

			Console.WriteLine(ans);
		}

		public static long Count(IReadOnlyList<LR> lrs)
		{
			if (lrs.Count == 0) {
				return 0;
			}

			long l = lrs[0].L;
			long r = lrs[0].R;
			for (int i = 1; i < lrs.Count; i++) {
				l = Math.Max(l, lrs[i].L);
				r = Math.Min(r, lrs[i].R);
				if (r < l) {
					return 0;
				}
			}

			return r - l + 1;
		}

		public static long Count(LR[] lrs, int exclude)
		{
			int first = exclude == 0 ? 1 : 0;
			if (lrs.Length <= first) {
				return 0;
			}

			long l = lrs[first].L;
			long r = lrs[first].R;
			for (int i = 1; i < lrs.Length; i++) {
				if (i == exclude) {
					continue;
				}

				l = Math.Max(l, lrs[i].L);
				r = Math.Min(r, lrs[i].R);
				if (r < l) {
					return 0;
				}
			}

			return r - l + 1;
		}

		public static long Count(LR[] lrs, int start, int end)
		{
			if (end - start <= 0) {
				return 0;
			}

			long l = lrs[start].L;
			long r = lrs[start].R;
			for (int i = 1; i < end; i++) {
				l = Math.Max(l, lrs[i].L);
				r = Math.Min(r, lrs[i].R);
				if (r < l) {
					return 0;
				}
			}

			return r - l + 1;
		}

		public struct LR
		{
			public long L { get; set; }
			public long R { get; set; }
			public LR(long l, long r)
			{
				L = l;
				R = r;
			}
		}
	}

	public class MultiSet<T> :
		IEnumerable<T>,
		System.Collections.IEnumerable,
		ICollection<T>,
		System.Collections.ICollection,
		IReadOnlyCollection<T>
	{
		private readonly SortedSet<T> set_;
		private readonly Dictionary<T, int> counts_ = new Dictionary<T, int>();

		public MultiSet(
			int capacity,
			IEnumerable<T> collection = null,
			IComparer<T> comparer = null)
		{
			counts_ = new Dictionary<T, int>(capacity);
			if (collection == null && comparer == null) {
				set_ = new SortedSet<T>();
			} else if (collection == null) {
				set_ = new SortedSet<T>(comparer);
			} else if (comparer == null) {
				set_ = new SortedSet<T>(collection);
			} else {
				set_ = new SortedSet<T>(collection, comparer);
			}
		}

		public IComparer<T> Comparer => set_.Comparer;
		public int Count { get; private set; }
		public T Max => set_.Max;
		public T Min => set_.Min;

		public bool IsReadOnly => false;

		object System.Collections.ICollection.SyncRoot
			=> (set_ as System.Collections.ICollection).SyncRoot;

		bool System.Collections.ICollection.IsSynchronized
			=> (set_ as System.Collections.ICollection).IsSynchronized;

		public bool Add(T item)
		{
			if (counts_.ContainsKey(item) == false) {
				counts_[item] = 1;
			} else {
				counts_[item]++;
			}

			set_.Add(item);
			++Count;
			return true;
		}

		public bool Remove(T item)
		{
			if (counts_.ContainsKey(item) == false) {
				return false;
			}

			counts_[item]--;
			Count--;
			if (counts_[item] == 0) {
				counts_.Remove(item);
				set_.Remove(item);
			}

			return true;
		}

		public void Clear()
		{
			counts_.Clear();
			Count = 0;
			set_.Clear();
		}

		public bool Contains(T item) => set_.Contains(item);

		public IEnumerator<T> GetEnumerator()
		{
			foreach (var s in set_) {
				for (int i = 0; i < counts_[s]; i++) {
					yield return s;
				}
			}
		}

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
			=> GetEnumerator();

		void ICollection<T>.Add(T item) => Add(item);

		public void CopyTo(T[] array, int arrayIndex)
		{
			int index = arrayIndex;
			foreach (var s in set_) {
				for (int i = 0; i < counts_[s]; i++) {
					array[index] = s;
					++index;
				}
			}
		}

		void System.Collections.ICollection.CopyTo(Array array, int index)
		{
		}
	}

	public class Scanner2
	{
		private readonly char[] delimiter_ = new char[] { ' ' };
		private readonly string filePath_;
		private string[] buf_;
		private int index_;
		Func<string> reader_;

		public Scanner2(string file = "")
		{
			if (string.IsNullOrWhiteSpace(file)) {
				reader_ = Console.ReadLine;
			} else {
				filePath_ = file;
				var fs = new StreamReader(file);
				reader_ = fs.ReadLine;
			}
			buf_ = new string[0];
			index_ = 0;
		}

		public string Next()
		{
			if (index_ < buf_.Length) {
				return buf_[index_++];
			}

			string st = reader_();
			while (st == "") {
				st = reader_();
			}

			buf_ = st.Split(delimiter_, StringSplitOptions.RemoveEmptyEntries);
			if (buf_.Length == 0) {
				return Next();
			}

			index_ = 0;
			return buf_[index_++];
		}

		public int Int() => int.Parse(Next());
		public long Long() => long.Parse(Next());
		public double Double() => double.Parse(Next());

		public int[] ArrayInt(int N, int add = 0)
		{
			int[] Array = new int[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Int() + add;
			}
			return Array;
		}

		public long[] ArrayLong(int N, long add = 0)
		{
			long[] Array = new long[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Long() + add;
			}
			return Array;
		}

		public double[] ArrayDouble(int N, double add = 0)
		{
			double[] Array = new double[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Double() + add;
			}
			return Array;
		}

		public void Save(string text)
		{
			if (string.IsNullOrWhiteSpace(filePath_)) {
				return;
			}

			File.WriteAllText(filePath_ + "_output.txt", text);
		}
	}
}