atcoder#ARC077C. [ARC077E] guruguru
[ARC077E] guruguru
Score : points
Problem Statement
Snuke is buying a lamp. The light of the lamp can be adjusted to levels of brightness, represented by integers from through , by the two buttons on the remote control.
The first button is a "forward" button. When this button is pressed, the brightness level is increased by , except when the brightness level is , in which case the brightness level becomes .
The second button is a "favorite" button. When this button is pressed, the brightness level becomes the favorite brightness level , which is set when the lamp is purchased.
Snuke is thinking of setting the favorite brightness level so that he can efficiently adjust the brightness. He is planning to change the brightness times. In the -th change, the brightness level is changed from to . The initial brightness level is . Find the number of times Snuke needs to press the buttons when is set to minimize this number.
Constraints
- , and are integers.
Input
Input is given from Standard Input in the following format:
…
Output
Print the minimum number of times Snuke needs to press the buttons.
4 6
1 5 1 4
5
When the favorite brightness level is set to , , , , and , Snuke needs to press the buttons , , , , and times, respectively. Thus, Snuke should set the favorite brightness level to . In this case, the brightness is adjusted as follows:
- In the first change, press the favorite button once, then press the forward button once.
- In the second change, press the forward button twice.
- In the third change, press the favorite button once.
10 10
10 9 8 7 6 5 4 3 2 1
45