AtCoder Regular Contest 129
D
-1+2-1
AtCoder
⌀ 2300
原题
← CE →
翻译中…

Problem Statement

翻译中…

Given is an integer sequence of length $N$: $A=(A_1,A_2,\cdots,A_N)$.

翻译中…

You can repeat the operation below any number of times.

翻译中…
  • Choose an integer $i$ ($1 \leq i \leq N$) and add $-1, 2, -1$ to $A_{i-1},A_i,A_{i+1}$, respectively. Here, $A_0$ stands for $A_N$, and $A_{N+1}$ stands for $A_1$.
翻译中…

Determine whether it is possible to make every element of $A$ $0$. If it is possible, find the minimum number of operations needed.

翻译中…

Constraints

翻译中…
  • $3 \leq N \leq 200000$
  • $-100 \leq A_i \leq 100$
  • All values in input are integers.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

N
A_1 A_2 \cdots A_N

翻译中…

Output

翻译中…

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

翻译中…

样例

Input #1
4
3 0 -1 -2
Output #1
5
We can achieve the objective in five operations, as follows. - Do the operation with $i=2$. Now we have $A=(2,2,-2,-2)$. - Do the operation with $i=3$. Now we have $A=(2,1,0,-3)$. - Do the operation with $i=3$. Now we have $A=(2,0,2,-4)$. - Do the operation with $i=4$. Now we have $A=(1,0,1,-2)$. - Do the operation with $i=4$. Now we have $A=(0,0,0,0)$.
Input #2
3
1 0 -2
Output #2
-1
Input #3
4
1 -1 1 -1
Output #3
-1
Input #4
10
-28 -3 90 -90 77 49 -31 48 -28 -84
Output #4
962
题目信息
题号
arc129_d
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。