B. Death's Blessing
You are playing a computer game. To pass the current level, you have to kill a big horde of monsters. In this horde, there are monsters standing in the row, numbered from to . The -th monster has health and a special "Death's Blessing" spell of strength attached to it.
You are going to kill all of them one by one. It takes exactly seconds to kill a monster with health .
When the -th monster dies, it casts its spell that increases the health of its neighbors by (the neighbors of the -th monster in the row are the monsters on places and . The first and the last monsters have only one neighbor each).
After each monster is killed, the row shrinks, so its former neighbors become adjacent to each other (so if one of them dies, the other one is affected by its spell). For example, imagine a situation with monsters with health and spells . One of the ways to get rid of the monsters is shown below:
As a result, we can kill all monsters in seconds. Note that it's only an example and may not be the fastest way to get rid of the monsters.
What is the minimum time required to kill all monsters in the row?
The first line contains a single integer () — the number of test cases.
The first line of each test case contains a single integer () — the number of monsters in the row.
The second line contains integers () — the initial health of corresponding monsters.
The third line contains integers (), where is the strength of the spell for the -th monster.
It's guaranteed that the sum of doesn't exceed .
For each test case, print one integer — the minimum possible total time to kill all monsters.
411003100 1 1001 100 142 6 7 33 6 0 521000000000 10000000001000000000 1000000000
10 203 26 3000000000
In the first test case, there is only one monster that will be killed in seconds.
In the second test case, it's optimal to kill the first monster, the last monster and then the middle one. It will take seconds.
In the third test case, it's optimal to kill the first monster, then the third one, then the fourth one and finally the second one. It will take seconds.
0 Comments
If you have any doubts/suggestion/any query or want to improve this article, you can comment down below and let me know. Will reply to you soon.