atcoder#ARC152B. [ARC152B] Pass on Path
[ARC152B] Pass on Path
Score : points
Problem Statement
There is a narrow straight road of length stretching east to west. Two travelers will visit this road. Along the road, there are rest areas. The distance from the west end of the road to the -th rest area is (no rest area is at either end of the road). The road is so narrow that the two travelers cannot pass each other or walk side by side except at the rest areas.
The two travelers will take a trip along the road as follows.
- At time , each traveler starts at a rest area of their choice (the two may start at the same rest area). Then, each visits both ends of the road, and returns to their own starting rest area.
During the trip, they can walk along the road at a speed of at most , or rest at a rest area. As long as they only pass each other at the rest areas, it is always allowed to change direction. What is the smallest number of seconds needed for both travelers to visit both ends of the road and return to their starting rest areas? It can be proved that the answer is always an integer under the Constraints of this problem.
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer as an integer.
2 6
2 5
14
Let us name the travelers A and B. Also, call the -th rest area simply rest area . Here is a possible trip.
At the beginning, A starts at rest area walking to the east, and B starts at rest area walking to the east, both at a speed of , planning to visit the east end first and then the west end. Then, two seconds later, B is back at rest area after visiting the east end, but A is still halfway between rest areas and . If B rests here for one second, A will also arrive at rest area , where they can pass each other.
Afterward, if they continue to walk at a speed of and A rests at rest area for two seconds, B will be back at the starting rest area at time , and A will be back at time , completing the trip.
This trip turns out to be optimal: the answer is .
2 3
1 2
6
In this case, an optimal trip will allow both travelers to keep walking at a speed of without resting.