#P8684. [蓝桥杯 2019 省 B] 灵能传输
[蓝桥杯 2019 省 B] 灵能传输
Description
You control High Templars, labeled for convenience. Each High Templar needs a certain amount of psionic energy to fight. Each one has a psionic value representing how much psionic energy it has ( means this High Templar has extra psionic energy beyond the best state; means this High Templar still needs psionic energy to reach the best fighting state).
Now the system gives your High Templars an ability: transferring psionic energy. Each time, you may choose an .
- If , then the two adjacent High Templars, i.e. and , will each draw psionic energy from the -th High Templar.
- If , then the two adjacent High Templars, i.e. and , will each give psionic energy to the -th High Templar.
Formally,
$$(a_{i-1},a_i,a_{i+1})\leftarrow (a_{i-1}+a_i,-a_i,a_{i+1}+a_i).$$Psionic energy is a very efficient combat tool, but it is also dangerous and unstable. Having too much or too little psionic energy is bad. Define the instability of a group of High Templars as . Please perform the transfer operation any number of times to minimize the instability of the group you control.
Input Format
This problem contains multiple queries. The first line contains a positive integer indicating the number of queries.
Then the queries follow.
For each query, the first line contains a positive integer , indicating the number of High Templars.
The next line contains numbers .
Output Format
Output lines. Each line contains one integer, representing the answer for each query in order.
3 3
5 -2 3
4
0 0 0 0
3
1 2 3
3
0
3
3 4
-1 -2 -3 7
4
2 3 4 -8
5
-1 -1 6 -1 -1
5
7
4
见文件trans3.in。
见文件trans3.ans。
Hint
Sample Explanation
For the first query:
After performing the transfer operation on the High Templar No. , we get , , . The answer is .
For the second query:
The psionic energy of this group of High Templars is exactly enough for them to reach the best fighting state.
Constraints
For all testcases, , , .
During judging, testcases will be used to test your program, and each testcase has the following limits:

Lanqiao Cup 2019 Provincial Contest B Group, Problem J.
Translated by ChatGPT 5
京公网安备 11011102002149号