#arc129d. [arc129_d]-1+2-1

[arc129_d]-1+2-1

Problem Statement

Given is an integer sequence of length NN: A=(A1,A2,cdots,AN)A=(A_1,A_2,\\cdots,A_N).

You can repeat the operation below any number of times.

  • Choose an integer ii (1leqileqN1 \\leq i \\leq N) and add \-1,2,1\-1, 2, -1 to Ai1,Ai,Ai+1A_{i-1},A_i,A_{i+1}, respectively. Here, A0A_0 stands for ANA_N, and AN+1A_{N+1} stands for A1A_1.

Determine whether it is possible to make every element of AA 00. If it is possible, find the minimum number of operations needed.

Constraints

  • 3leqNleq2000003 \\leq N \\leq 200000
  • \-100leqAileq100\-100 \\leq A_i \\leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN A1A_1 A2A_2 cdots\\cdots ANA_N

Output

If it is impossible to make every element of AA 00, print -1. If it is possible to do so, print the minimum number of operations needed.


Sample Input 1

4
3 0 -1 -2

Sample Output 1

5

We can achieve the objective in five operations, as follows.

  • Do the operation with i=2i=2. Now we have A=(2,2,2,2)A=(2,2,-2,-2).
  • Do the operation with i=3i=3. Now we have A=(2,1,0,3)A=(2,1,0,-3).
  • Do the operation with i=3i=3. Now we have A=(2,0,2,4)A=(2,0,2,-4).
  • Do the operation with i=4i=4. Now we have A=(1,0,1,2)A=(1,0,1,-2).
  • Do the operation with i=4i=4. Now we have A=(0,0,0,0)A=(0,0,0,0).

Sample Input 2

3
1 0 -2

Sample Output 2

-1

Sample Input 3

4
1 -1 1 -1

Sample Output 3

-1

Sample Input 4

10
-28 -3 90 -90 77 49 -31 48 -28 -84

Sample Output 4

962