#P1795E. Explosions?
Explosions?
No submission language available for this problem.
Description
You are playing yet another game where you kill monsters using magic spells. There are cells in the row, numbered from to . Initially, the -th cell contains the -th monster with health.
You have a basic spell that costs MP and deals damage to the monster you choose. You can cast it any number of times. Also, you have a special scroll with "Explosion" spell you can use only once. You want to finish killing monsters with explosion, that's why you, firstly, cast the basic spell several times (possibly, zero), and then after that, you cast one "Explosion".
How does "Explosion" spell work? Firstly, you choose the power of the spell: if you pour MP into it, "Explosion" will deal damage. Secondly, you choose some monster , which will be targeted by the spell. That's what happens next:
- if its current health , then he stays alive with health decreased by ;
- if , the -th monster dies with an explosion that deals damage to monsters in the neighboring cells and , if these cells exist and monsters inside are still alive;
- if the damage dealt by the explosion is enough to kill the monster (or ), i. e. the current (or ), then that monster also dies creating a secondary explosion of power (or ) that may deals damage to their neighbors, and so on, until the explosions end.
Your goal is to kill all the remaining monsters with those "chaining" explosions, that's why you need a basic spell to decrease of some monsters or even kill them beforehand (monsters die when their current health becomes less or equal to zero). Note that monsters don't move between cells, so, for example, monsters and will never become neighbors.
What is the minimum total MP you need to kill all monsters in the way you want? The total MP is counted as the sum of the number of basic spells you cast and the power of explosion scroll you've chosen.
The first line contains one integer () — the number of test cases.
The first line of each test case contains the single integer () — the number of cells in the row, i. e. the number of monsters.
The second line of each test case contains integers () — the initial health of the monsters.
It's guaranteed that the sum of over all test cases doesn't exceed .
For each test case, print one integer — the minimum total MP you need to kill all monsters by finishing them with explosion.
Input
The first line contains one integer () — the number of test cases.
The first line of each test case contains the single integer () — the number of cells in the row, i. e. the number of monsters.
The second line of each test case contains integers () — the initial health of the monsters.
It's guaranteed that the sum of over all test cases doesn't exceed .
Output
For each test case, print one integer — the minimum total MP you need to kill all monsters by finishing them with explosion.
Note
In the first test case, you can, for example, use basic spell on monsters and (once per monster) to kill them. After that, you cast "Explosion" of power on monster to kill it. The total MP you need is .
In the second test case, it's optimal to cast basic spell times onto monster to kill it. After that, you can cast "Explosion" of power onto monster . It dies, creating an explosion of power that kills monsters and . The total MP you need is .
In the third test case, you cast "Explosion" of power onto monster . Explosion of the -rd monster (of power ) kills monsters and . Secondary explosion of monster (of power ) kills monster .