#K38477. Kangaroo's Minimum Jumps

    ID: 26207 Type: Default 1000ms 256MiB

Kangaroo's Minimum Jumps

Kangaroo's Minimum Jumps

You are given a sequence of rocks. Each rock has a non-negative integer that represents the maximum distance (in terms of the number of rocks) the kangaroo can jump from that rock. The rocks are arranged linearly from the starting rock (index 0) to the ending rock (index n). The value n indicates the index of the last rock, and the list of jump capacities has a length of n+1.

Your task is to compute the minimum number of jumps required for the kangaroo to reach the last rock from the first rock. If it is impossible to reach the last rock, output \( -1 \).

More formally, if we denote the jump capacity at rock \( i \) as \( a_i \), you need to find the minimum number of jumps such that starting from index 0 and repeatedly moving to any index in the range \( [i+1, \min(n, i+a_i)] \), you eventually reach index \( n \). If there is no such sequence of jumps, return \( -1 \).

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  • The first line contains a single integer \( n \) representing the index of the last rock (note that there are \( n+1 \) rocks, including the starting rock at index 0).
  • The second line contains \( n+1 \) space-separated non-negative integers, where the \( i \)-th integer represents the maximum distance the kangaroo can jump from rock \( i \).

outputFormat

Output a single integer to standard output (stdout): the minimum number of jumps required to reach the last rock. If it is impossible to reach the last rock, output \( -1 \).

## sample
5
4 2 0 0 2 1
2