PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000062. Trò chơi trên trục số (Game on Axis)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Trò chơi trên trục số (Game on Axis)

Game on Axis

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A game takes place on a number line with positions 1,2,…,n1,2,\ldots,n. Position ii stores an integer aia_i. The player always starts at position 11.

When the player is at a position ii inside [1,n][1,n], they must move to position i+aii+a_i. If the new position lies outside [1,n][1,n], the game ends immediately. Otherwise the player continues under the same rule. A game may run forever if the player repeats a cycle of positions.

Before starting, you must choose exactly one pair (x,y)(x,y) satisfying 1≤x≤n1\le x\le n and −n≤y≤n-n\le y\le n and replace axa_x with yy. Choosing y=axy=a_x is allowed and still counts as a pair. Count the pairs (x,y)(x,y) for which the modified game ends after finitely many moves.

Input

The first line contains nn; the second contains aia_i with −n≤ai≤n-n\le a_i\le n.

Output

Print the number of valid pairs (x,y)(x,y).

Subtasks

  • Subtask 1 (20 points): 1≤n≤401\le n\le40; −n≤ai≤n-n\le a_i\le n.
  • Subtask 2 (30 points): 1≤n≤50001\le n\le5000; −n≤ai≤n-n\le a_i\le n.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le2\cdot10^5; −n≤ai≤n-n\le a_i\le n.

Examples

Input

2
1 -1

Output

6

Explanation

Initially, play alternates between positions 11 and 22, so it never terminates. If a1a_1 is changed, choosing y=−2,−1,2y=-2,-1,2 makes play leave the interval immediately. If a2a_2 is changed, choosing y=−2,1,2y=-2,1,2 makes it leave after reaching position 22. There are 3+3=63+3=6 valid pairs (x,y)(x,y).