Dacians vs Samurai

Time limit: 1s
Memory limit: 256MB
Input:
Output:

As many of you might know, during the golden age of romanian television, one of the most beloved channels was named OTV.

One of the reasons for its popularity were the polls where the spectators voted for one of two options. Maybe the most popular poll was the one asking who would win in a fight between the dacians and the samurai.

After years and years, the OTV founder, DD, is back with his new project: DD Home Cinema. In order to regain his old level of fame he wants to please the nostalgics by bringing back the famous poll, but with a modern twist!

DD gives his spectators two integer sequences AA (the strenghts of the dacians) and BB (the strengths of the samurai) and wants another sequence CC of the same length, where Ci{Ai,Bi}C_i \in \{A_i, B_i\} (either the ii-th dacian or the ii-th samurai). Because such a large sequence would not fit well on a tv display, he asks for his spectators only for the smallest difference in strength of such a sequence.

Formally, the spectators are asked for the minimum value of max(C)min(C)max(C) - min(C) among all possible sequences CC.

Input data

The first line will contain a single integer N105N \leq 10^5 - the length of the arrays AA and BB.

The following NN lines will each contain 2 integers, Ai,Bi109A_i, B_i \leq 10^9, the values that form the sequences AA and BB

Output data

The only line will contain a single integer - the minimum value of said expression.

Constraints and clarifications

  • For 55 points, it is guaranteed that N2N \leq 2;
  • For another 1515 points, it is guaranteed that N20N \leq 20;
  • For another 2020 points, it is guaranteed that N5 000N \leq 5 \ 000;
  • For the last 6060 points, we have N105N \leq 10^5.

Example 1

stdin

5
5 4 3 2 1
3
2 3
1 5
2 2

stdout

8
80
4

Explanation

The minimum value is 4, and it can be obtain by choosing C1=A1,C2=B2,C3=A3,C4=A4,C5=B5C_1=A_1, C_2=B_2 , C_3=A_3 , C_4=A_4 , C_5 = B_5.

Log in or sign up to be able to send submissions!