Score : $500$ points
There are $N$ balls, called Ball $1$ through $N$, on a number line.
Ball $i$ is at the coordinate $X_i$.
Each ball has a color represented by an integer ID between $1$ and $N$ (inclusive); the ID of the color of Ball $i$ is $C_i$.
You are now at the coordinate $0$. You will collect all the balls by moving along the line at the speed of $1$ per second, and then return to the coordinate $0$.
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 $0$, collect all the balls, and return to the coordinate $0$.
Input is given from Standard Input in the following format:
$N$ $X_1$ $C_1$ $X_2$ $C_2$ $X_3$ $C_3$ $\hspace{15pt} \vdots$ $X_N$ $C_N$
Print the number of seconds needed.
5 2 2 3 1 1 3 4 2 5 3
12
The optimal strategy is:
Here, we collected the balls in a non-descending order of their IDs: $1, 2, 2, 3, 3$.
9 5 5 -4 4 4 3 6 3 -5 5 -3 2 2 2 3 3 1 4
38