#ABC197E. [ABC197E] Traveler

[ABC197E] Traveler

Score : 500500 points

Problem Statement

There are NN balls, called Ball 11 through NN, on a number line. Ball ii is at the coordinate XiX_i. Each ball has a color represented by an integer ID between 11 and NN (inclusive); the ID of the color of Ball ii is CiC_i. You are now at the coordinate 00. You will collect all the balls by moving along the line at the speed of 11 per second, and then return to the coordinate 00. Here, you have to collect the balls in a non-descending order of their IDs. When collecting a ball, you have to be at the coordinate of that ball, but it is not mandatory to collect it when you are there. Find the minimum time needed to start at the coordinate 00, collect all the balls, and return to the coordinate 00.

Constraints

  • 1N2×1051 \le N \le 2 \times 10^5
  • Xi109|X_i| \le 10^9
  • XiXj(ij)X_i \neq X_j (i \neq j)
  • Xi0X_i \neq 0
  • 1CiN1 \le C_i \le N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

X1X_1 C1C_1

X2X_2 C2C_2

X3X_3 C3C_3

\hspace{15pt} \vdots

XNX_N CNC_N

Output

Print the number of seconds needed.

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

The optimal strategy is:

  • spend 33 seconds to reach the coordinate 33 and collect Ball 22;
  • spend 11 second to reach the coordinate 22 and collect Ball 11;
  • spend 22 seconds to reach the coordinate 44 and collect Ball 44;
  • spend 11 second to reach the coordinate 55 and collect Ball 55;
  • spend 44 seconds to reach the coordinate 11 and collect Ball 33;
  • spend 11 second to return to the coordinate 00.

Here, we collected the balls in a non-descending order of their IDs: 1,2,2,3,31, 2, 2, 3, 3.

9
5 5
-4 4
4 3
6 3
-5 5
-3 2
2 2
3 3
1 4
38