#P1135. Strange Elevator
Strange Elevator
Strange Elevator
One day, you dreamed of a very unusual elevator. In a building with N floors, each floor i (1 ≤ i ≤ N) has a number Ki (0 ≤ Ki ≤ N) written on it. The elevator has only four buttons: On, Off, Up, and Down. When you press the Up or Down button at floor i, the elevator attempts to move exactly Ki floors up or down respectively. However, if the resulting floor is invalid (i.e. less than 1 or greater than N), that button does nothing.
Starting from floor A, determine the minimum number of button presses needed to reach floor B. Note that while the elevator has both On and Off buttons, they do not affect your current floor – only the Up and Down buttons change floors, and since unnecessary presses increase the count, you should use them only if needed.
If it is impossible to reach floor B from floor A by valid moves, output -1.
inputFormat
The first line of input contains three space-separated integers: N, A, and B, where N is the number of floors, A is the starting floor, and B is the destination floor.
The second line contains N integers: K1, K2, …, KN, where Ki is the number written on the i-th floor.
outputFormat
Output a single integer indicating the minimum number of button presses required to reach floor B from floor A. If it is not possible, output -1.
sample
5 1 5
3 3 1 2 5
3