atcoder#AGC024C. [AGC024C] Sequence Growing Easy

[AGC024C] Sequence Growing Easy

Score : 700700 points

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 1iN11\leq i\leq N-1. Replace the value of Xi+1X_{i+1} with the value of XiX_i plus 11.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 0Ai109(1iN)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.

4
0
1
1
2
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).
3
1
2
1
-1
9
0
1
1
0
1
2
2
1
2
8