Under construction

Still being built. Things may change

Jump Jump 2

Interview

Time limit2sMemory limit512 MB

Summary
Given a walk that adds or subtracts A_i at each step, count the stones reachable from start s, excluding s unless a cycle returns to it.
Level

Medium5 of 10

Topics
Graph, BFS, Implementation, Math
Solved
No attempts yet

Problem

Youngwoo the frog is on a stone bridge made of nn stones in a row. The stones are numbered 11 to nn from the left, and stone ii has a single number AiA_i written on it. From stone ii Youngwoo can jump exactly AiA_i stones to the left or to the right, landing on stone i−Aii - A_i or on stone i+Aii + A_i. He cannot jump to a position numbered below 11 or above nn.

Youngwoo starts on stone ss. There is no limit on the number of jumps, and he may land on the same stone more than once. Call a stone visitable if Youngwoo can land on it after one or more jumps. The starting stone ss is visitable only when he can land back on it after at least one jump.

Count the visitable stones.

Input

The first line contains the number of stones nn (1≤n≤1000001 \le n \le 100000).

The second line contains A1,A2,…,AnA_1, A_2, \dots, A_n, separated by spaces (1≤Ai≤1000001 \le A_i \le 100000).

The third line contains the starting position ss (1≤s≤n1 \le s \le n).

Output

Print the number of visitable stones on the first line.

Examples8

  1. Example 1

    Input
    5
    1 4 2 2 1
    3
    
    Expected output
    4
    
  2. Example 2

    Input
    1
    1
    1
    
    Expected output
    0
    
  3. Example 3

    Input
    2
    1 1
    1
    
    Expected output
    2
    
  4. Example 4

    Input
    5
    1 1 1 1 1
    3
    
    Expected output
    5
    
  5. Example 5

    Input
    6
    3 3 3 3 3 3
    1
    
    Expected output
    2
    
  6. Example 6

    Input
    3
    100000 100000 100000
    2
    
    Expected output
    0
    
  7. Example 7

    Input
    7
    2 2 2 2 2 2 2
    1
    
    Expected output
    4
    
  8. Example 8

    Input
    10
    5 3 1 2 4 6 2 8 1 7
    4
    
    Expected output
    9