#P12605. 求和

    ID: 11656 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>贪心洛谷原创前缀和洛谷月赛

求和

Description

You are given an integer sequence aa of length nn.

You can perform the following operations any number of times (possibly, zero):

Select indices 1i,jn1 \le i, j \le n such that iji \ne j, and perform the operation: aiai+1a_i \leftarrow a_i + 1, ajaj1a_j \leftarrow a_j - 1 simultaneously.

Your task is to compute the minimum number of operations needed to make the sum of the prefix sums of aa equal to the sum of the suffix sums of aa.

That is, let si=a1+a2++ai,ti=ai+ai+1++ans_i=a_1+a_2+\dots+a_i,t_i=a_i+a_{i+1}+\dots+a_n, you need to make s1+s2++sns_1+s_2+\dots+s_n equal to t1+t2++tnt_1+t_2+\dots+t_n.

Note that aia_i can become negative after several operations.

Input Format

The first line contains a single integer nn.

The second line contains nn integers a1,a2,,ana_1,a_2,\dots,a_n.

Output Format

Print a single integer — the minimum number of operations needed.

If it is impossible to satisfy the condition within 1010010^{100} operations, print 1-1.

5
1 2 3 4 5
3
6
2 3 7 4 5 8
-1

Hint

This problem has subtasks.

  • Subtask 1 (30 points): 1n21 \le n \le 2
  • Subtask 2 (30 points): ai=ia_i = i
  • Subtask 3 (5 points): ai=1a_i = 1
  • Subtask 4 (35 points): No additional constraints

It is guaranteed that for all testcases, 1n,ai1061 \le n,a_i \le 10^6.