atcoder#AGC025C. [AGC025C] Interval Game

[AGC025C] Interval Game

Score : 700700 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 00. Aoki has NN segments. The ii-th segment is [Li,Ri][L_i,R_i], that is, a segment consisting of points with coordinates between LiL_i and RiR_i (inclusive).

The game has NN steps. The ii-th step proceeds as follows:

  • First, Aoki chooses a segment that is still not chosen yet from the NN 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 NN steps are performed, Takahashi will return to coordinate 00 and the game ends.

Let KK be the total distance traveled by Takahashi throughout the game. Aoki will choose segments so that KK will be as large as possible, and Takahashi walks along the line so that KK will be as small as possible. What will be the value of KK in the end?

Constraints

  • 1N1051 \leq N \leq 10^5
  • 105Li<Ri105-10^5 \leq L_i < R_i \leq 10^5
  • LiL_i and RiR_i are integers.

Input

Input is given from Standard Input in the following format:

NN

L1L_1 R1R_1

:

LNL_N RNR_N

Output

Print the total distance traveled by Takahashi throughout the game when Takahashi and Aoki acts as above. It is guaranteed that KK is always an integer when Li,RiL_i,R_i 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 00 to 4-4, covering a distance of 44.
  • Aoki chooses the third segment. Takahashi stays at coordinate 4-4.
  • Aoki chooses the second segment. Takahashi moves from coordinate 4-4 to 33, covering a distance of 77.
  • Takahashi moves from coordinate 33 to 00, covering a distance of 33.

The distance covered by Takahashi here is 1414 (because Takahashi didn't move optimally). It turns out that if both players move optimally, the distance covered by Takahashi will be 1010.

3
1 2
3 4
5 6
12
5
-2 0
-2 0
7 8
9 10
-2 -1
34