PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000349. Nhảy qua các đoạn (Jumping Through Segments)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Nhảy qua các đoạn (Jumping Through Segments)

Jumping Through Segments

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Polycarp designs a game level with nn closed intervals on a number line. Interval ii has endpoints li,ril_i,r_i. Intervals may overlap.

The player begins at coordinate 0. On each move, the player may move left, right or remain still, provided the distance traveled is at most kk. After move ii, the position xx must satisfy li≤x≤ril_i\le x\le r_i.

The level is completed after all nn moves in the specified order. Find the smallest nonnegative integer kk permitting completion.

Input

The first line contains an integer nn, the number of intervals.

Each of the next nn lines contains two integers li,ril_i,r_i, the endpoints of interval ii.

Output

Print one integer: the minimum value of kk.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; 0≤li≤ri≤200\le l_i\le r_i\le 20 for all 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 0≤li≤ri≤1090\le l_i\le r_i\le 10^9 for all 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 0≤li≤ri≤1090\le l_i\le r_i\le 10^9 for all 1≤i≤n1\le i\le n.

Examples

Input 1

5
1 5
3 4
5 6
8 10
0 1

Output 1

7

Explanation

A valid route is 0→1→3→5→8→10\to1\to3\to5\to8\to1, with lengths 1, 2, 2, 3 and 7. The fourth position is at least 8 and the fifth at most 1, requiring a final jump of at least 7.

Input 2

3
0 2
0 1
0 3

Output 2

0

Explanation

All three intervals contain 0. Staying there for all three moves is valid, so k=0k=0 is optimal.