#P8092. [USACO22JAN] Drought B
[USACO22JAN] Drought B
Problem Description
The grass in Farmer John’s pasture has all dried up in a severe drought. After hours of despair and thinking, Farmer John comes up with a brilliant idea: buy corn to feed his precious cows.
FJ has cows () standing in a line. The -th cow in the line has hunger level (). Since cows are social animals, they insist on eating together. The only way for FJ to decrease the cows’ hunger levels is to choose two adjacent cows and and feed each of them one bag of corn, which decreases both of their hunger levels by .
FJ wants to feed his cows until all cows have the same non-negative hunger level. Please help FJ find the minimum number of bags of corn needed to reach this state, or output if it is impossible.
Input Format
Each test contains multiple independent subtasks, and all of them must be answered correctly to pass the entire test.
The first line of input contains (), the number of subtasks you need to solve.
Then follow subtasks. Each subtask contains two lines.
The first line contains , and the second line contains . The input guarantees that the sum of over all subtasks does not exceed .
The value of may be different for each subtask.
Output Format
Output lines, one line for each subtask.
5
3
8 10 5
6
4 6 4 4 6 4
3
0 1 0
2
1 2
3
10 9 9
14
16
-1
-1
-1
Hint
Sample Explanation.
For the first subtask, feed cows and two bags of corn each, then feed cows and five bags of corn each. This makes all cows have hunger level .
For the second subtask, feed cows and two bags of corn each, cows and two bags each, cows and two bags each, and cows and two bags each. This makes all cows have hunger level .
For the remaining subtasks, it is impossible to make the cows’ hunger levels equal.
Constraints.
-
For all subtasks in test point 2, and .
-
For all subtasks in test points 3-8, and .
-
For all subtasks in test points 9-14, .
-
Test point 15 has no additional constraints.
-
In addition, in test points 3-5 and 9-11, is even, and in test points 6-8 and 12-14, is odd.
Translated by ChatGPT 5