atcoder#AGC025C. [AGC025C] Interval Game
[AGC025C] Interval Game
Score : points
Problem Statement
Takahashi and Aoki will play a game with a number line and some segments. Takahashi is standing on the number line and he is initially at coordinate . Aoki has segments. The -th segment is , that is, a segment consisting of points with coordinates between and (inclusive).
The game has steps. The -th step proceeds as follows:
- First, Aoki chooses a segment that is still not chosen yet from the segments and tells it to Takahashi.
- Then, Takahashi walks along the number line to some point within the segment chosen by Aoki this time.
After steps are performed, Takahashi will return to coordinate and the game ends.
Let be the total distance traveled by Takahashi throughout the game. Aoki will choose segments so that will be as large as possible, and Takahashi walks along the line so that will be as small as possible. What will be the value of in the end?
Constraints
- and are integers.
Input
Input is given from Standard Input in the following format:
:
Output
Print the total distance traveled by Takahashi throughout the game when Takahashi and Aoki acts as above. It is guaranteed that is always an integer when are integers.
3
-5 1
3 7
-4 -2
10
One possible sequence of actions of Takahashi and Aoki is as follows:
- Aoki chooses the first segment. Takahashi moves from coordinate to , covering a distance of .
- Aoki chooses the third segment. Takahashi stays at coordinate .
- Aoki chooses the second segment. Takahashi moves from coordinate to , covering a distance of .
- Takahashi moves from coordinate to , covering a distance of .
The distance covered by Takahashi here is (because Takahashi didn't move optimally). It turns out that if both players move optimally, the distance covered by Takahashi will be .
3
1 2
3 4
5 6
12
5
-2 0
-2 0
7 8
9 10
-2 -1
34