题库练习 Andrew and Stones
← 上一题 下一题 →

A14964 | Andrew and Stones

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

Andrew has $n$ piles with stones. The $i$ -th pile contains $a_i$ stones. He wants to make his table clean so he decided to put every stone either to the $1$ -st or the $n$ -th pile.

Andrew can perform the following operation any number of times: choose $3$ indices $1 \le i < j < k \le n$ , such that the $j$ -th pile contains at least $2$ stones, then he takes $2$ stones from the pile $j$ and puts one stone into pile $i$ and one stone into pile $k$ .

Tell Andrew what is the minimum number of operations needed to move all the stones to piles $1$ and $n$ , or determine if it's impossible.

输入格式

The input contains several test cases. The first line contains one integer $t$ ( $1 \leq t \leq 10\,000$ ) — the number of test cases.

The first line for each test case contains one integer $n$ ( $3 \leq n \leq 10^5$ ) — the length of the array.

The second line contains a sequence of integers $a_1, a_2, \ldots, a_n$ ( $1 \leq a_i \leq 10^9$ ) — the array elements.

It is guaranteed that the sum of the values $n$ over all test cases does not exceed $10^5$ .

输出格式

For each test case print the minimum number of operations needed to move stones to piles $1$ and $n$ , or print $-1$ if it's impossible.

输入输出样例

输入 #1
4
5
1 2 2 3 6
3
1 3 1
3
1 2 1
4
3 1 1 2
输出 #1
4
-1
1
-1
C++ 编辑器
输入
输出