#BS0000349. Nhảy qua các đoạn (Jumping Through Segments)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 closed intervals on a number line. Interval has endpoints . 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 . After move , the position must satisfy .
The level is completed after all moves in the specified order. Find the smallest nonnegative integer permitting completion.
Input
The first line contains an integer , the number of intervals.
Each of the next lines contains two integers , the endpoints of interval .
Output
Print one integer: the minimum value of .
Subtasks
- Subtask 1 (20 points): ; for all .
- Subtask 2 (30 points): ; for all .
- Subtask 3 (50 points): ; for all .
Examples
Input 1
5
1 5
3 4
5 6
8 10
0 1
Output 1
7
Explanation
A valid route is , 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 is optimal.