Light It Up (Remix)
Natasha has just been assigned a very important job: illuminating the main square of Melbourne.
Naturally, she begins the only reasonable way: by putting on ...Baby One More Time by Britney Spears, turning the volume up, and getting to work. The square is huge, the night is getting darker, and Natasha is determined to make every part of it shine.
Around the square, there are streetlights, numbered from
to
in clockwise order. Each streetlight has a cost to turn on, but the city budget is not unlimited. Luckily, each streetlight is powerful enough to light not only the area beneath itself, but also the areas beneath its two neighbouring streetlights.
The streetlights are arranged in a circle:
- if the next streetlight after
is needed, it is considered to be streetlight
;
- if the previous streetlight before
is needed, it is considered to be streetlight
.
You are given the cost of turning on each streetlight
. Natasha wants to choose some streetlights to turn on so that the area under every streetlight is lit, while minimizing the total cost.
For each test case, determine the minimum possible total cost.
Input
The first line contains an integer
, the number of test cases.
For each test case:
- The first line contains an integer
, the number of streetlights.
- The second line contains
integers
, where
is the cost of turning on streetlight
.
The sum of over all test cases is at most
.
Output
For each test case, print a single integer: the minimum total cost needed to make the whole square lit.
Sample Input
5
5
1 1 1 2 2
3
3 4 7
5
7 5 4 1 1
1
100
7
4 4 4 4 4 4 4
Sample Output
2
3
5
100
12
Explanation
In the first test case, we can turn on streetlights and
. The total cost is
, and every area is lit.
In the second test case, turning on only streetlight is enough to light all areas, so the answer is
.
In the third test case, we can turn on streetlights and
. The total cost is
.
In the fourth test case, there is only one streetlight, so we must turn it on. The answer is .
In the fifth test case, at least three streetlights are needed. Turning on streetlights ,
, and
gives total cost
.
Comments