#agc024c. [agc024_c]Sequence Growing Easy

[agc024_c]Sequence Growing Easy

Problem Statement

There is a sequence XX of length NN, where every element is initially 00. Let XiX_i denote the ii-th element of XX.

You are given a sequence AA of length NN. The ii-th element of AA is AiA_i. Determine if we can make XX equal to AA by repeating the operation below. If we can, find the minimum number of operations required.

  • Choose an integer ii such that 1leqileqN11\\leq i\\leq N-1. Replace the value of Xi+1X_{i+1} with the value of XiX_i plus 11.

Constraints

  • 1leqNleq2times1051 \\leq N \\leq 2 \\times 10^5
  • 0leqAileq109(1leqileqN)0 \\leq A_i \\leq 10^9(1\\leq i\\leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN A1A_1 :: ANA_N

Output

If we can make XX equal to AA by repeating the operation, print the minimum number of operations required. If we cannot, print \-1\-1.


Sample Input 1

4
0
1
1
2

Sample Output 1

3

We can make XX equal to AA as follows:

  • Choose i=2i=2. XX becomes (0,0,1,0)(0,0,1,0).
  • Choose i=1i=1. XX becomes (0,1,1,0)(0,1,1,0).
  • Choose i=3i=3. XX becomes (0,1,1,2)(0,1,1,2).

Sample Input 2

3
1
2
1

Sample Output 2

-1

Sample Input 3

9
0
1
1
0
1
2
2
1
2

Sample Output 3

8