atcoder#ABC197E. [ABC197E] Traveler
[ABC197E] Traveler
Score : points
Problem Statement
There are balls, called Ball through , on a number line. Ball is at the coordinate . Each ball has a color represented by an integer ID between and (inclusive); the ID of the color of Ball is . You are now at the coordinate . You will collect all the balls by moving along the line at the speed of per second, and then return to the coordinate . 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 , collect all the balls, and return to the coordinate .
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the number of seconds needed.
5
2 2
3 1
1 3
4 2
5 3
12
The optimal strategy is:
- spend seconds to reach the coordinate and collect Ball ;
- spend second to reach the coordinate and collect Ball ;
- spend seconds to reach the coordinate and collect Ball ;
- spend second to reach the coordinate and collect Ball ;
- spend seconds to reach the coordinate and collect Ball ;
- spend second to return to the coordinate .
Here, we collected the balls in a non-descending order of their IDs: .
9
5 5
-4 4
4 3
6 3
-5 5
-3 2
2 2
3 3
1 4
38