~/algor $ cat problems/ochkoz-eng-kam-sakrashlar
Fewest jumps
Medium
1000 ms
128 MB
64%
There are $n$ cells in a row, and the $i$-th cell holds the number $a_{i}$: from this cell you may jump forward by at most $a_{i}$ cells (that is, to one of the cells $i + 1, \dots, i + a_{i}$). What is the smallest number of jumps needed to get from the first cell to the last one? If it cannot be reached, print $-1$.
Input
The first line contains a positive integer $n$.
The second line contains $n$ integers $a_{i}$.
Output
One integer — the smallest number of jumps or $-1$.
Note
$1 \le n \le 10^{5}$
$0 \le a_{i} \le 10^{5}$
Examples
Input #1
5 2 3 1 1 4
Output #1
2
Input #2
5 3 2 1 0 4
Output #2
-1