#P10948. 升降梯上
升降梯上
Problem Description
After turning on the power of the elevator, the explorers enter the vertical tunnel where the elevator runs. In front of them is a track leading straight to the top of the tower, an elevator parked at the bottom of the track, and a huge lever inside the elevator that controls its movement up and down.
The Tower has a total of floors, and the elevator has a stop on every floor.
The lever has control slots. Next to the -th slot there is a number , satisfying .
If , it means that when the lever is moved to this slot, the elevator will go up floors. If , it means that when the lever is moved to this slot, the elevator will go down floors. Also, there is guaranteed to be some , and the lever initially stays at this slot.
Note that the elevator can only move within floors . Therefore, it is not allowed to move the lever to a slot that would make the elevator go below floor or above floor .
For each floor the elevator moves, it takes seconds. Moving the lever from one control slot to an adjacent slot takes second.
The explorers are currently on floor and want to reach floor as quickly as possible. They want to know the minimum time needed to get from floor to floor .
Input Format
The first line contains two positive integers .
The second line contains integers .
Output Format
Output one integer, the answer: the minimum time required.
If it is impossible to reach, output .
6 3
-1 0 2
19
Hint
Constraints: It is guaranteed that , , .
Translated by ChatGPT 5