Problems > Fewest Jumps to the End

Fewest Jumps to the End

Difficulty: 1600 3000 ms 128 MB

greedy

Same jumping rule (from i you may advance up to ai), but now print the minimum number of jumps to reach position N, or −1 if it is unreachable.

Input: the first line has N; the second line has N non-negative integers.

Output: the minimum number of jumps, or −1.

Constraints:

  • 1 ≤ N ≤ 105
  • 0 ≤ ai ≤ 105

Sample Tests

Input 1
5
2 3 1 1 4
Output 1
2
Input 2
5
3 2 1 0 4
Output 2
-1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.