#P10569. 「Daily OI Round 4」Snow
「Daily OI Round 4」Snow
Problem Description
It is snowing. Little Y stacked snow pillars in a row. Naughty little X wants to knock down these snow pillars. He wants to knock them all down in the least time, so he asks you to help him plan.
Each snow pillar stacked by little Y has height units. The time to manually push down one snow pillar equals the height of that pillar. Little X can only push snow pillars from the two ends of the row *, with no limit on the number of pushes. The time for little X to move can be ignored. In this way, a pillar can fall onto other pillars, knocking down another pillar (the time for knocking down is ignored), and then a chain reaction happens, saving more time **.
Let the initial potential energy be the height of the currently manually pushed pillar , i.e. . During this chain reaction, when the -th pillar falls toward the -th pillar:
- If , then the -th pillar is also knocked down, and set .
- If , then the height of the -th pillar decreases (it is not knocked down), and the entire chain reaction stops. Set .
Please find the minimum total time to knock down all snow pillars.
* Each time, you either push the leftmost pillar from the left side, or push the rightmost pillar from the right side.
** The falling direction depends on the pushing direction. If you push from the left, the pillars will fall to the right in order (the -th pillar falls toward the -th pillar), and vice versa.
Input Format
This problem has multiple test cases.
The first line contains an integer , the number of test cases.
For each test case:
The first line contains an integer , the number of snow pillars.
The second line contains integers, the heights of the snow pillars.
Output Format
For each test case, output one integer per line, the minimum time to knock down all snow pillars.
3
5
2 3 1 4 5
6
6 6 6 6 6 6
6
1 1 4 5 1 4
7
6
8
Hint
Sample Explanation
- For the first test case:
Push from the left the first time, costing units of time, and the heights of the snow pillars become: .
Push from the right the second time, costing units of time, and all pillars are knocked down.
The total time is units.
- For the second test case:
Pushing from either the left or the right can finish in one push, costing units of time in total.
- For the third test case:
Push from the right the first time, costing units of time, and the heights of the snow pillars become: .
Push from the right the second time, costing units of time, and all pillars are knocked down.
The total time is units.
Constraints
This problem uses bundled testdata.
| Score | ||
|---|---|---|
For all testdata, it is guaranteed that , , and .
Translated by ChatGPT 5