Fence
Time limit3sMemory limit1024 MB
For each height b from 1 to M, compute the total length of the odd-numbered fence pieces when every plank is cut into pieces of length b.
- Level
Medium6 of 10
- Topics
- Math, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
Bajtazar is widely believed to be the greatest scrooge in the borough. One can find many examples to support this claim, and the least important one is that his estate has no fence at all. However, he recently found old planks in his basement, so he decided to build at least a fragment of a fence.
He stacked the planks one over another so that their lengths, from the top, are . He took the first plank, cut off a piece of length , and nailed it as the leftmost piece of the fence. He then cut another piece of length and nailed it next to the previous one. He kept doing this until what was left in his hand was a piece of length (). He nailed that short piece to the fence too, since it would be a pity to waste such a good plank. Then he took the second plank from the stack and repeated the whole procedure, and so on for every plank.
When the job was done, he decided the short pieces were a poor idea. He painted the whole fence white, but then he noticed that painting every other plank, starting from the leftmost one, uses about half as much paint and still looks coherent. So he painted only every second plank, starting from the leftmost one. (He could have started from the second one, after all.)
That night he wondered whether a different height would have needed less paint. For every possible height , where is the largest , compute the total length of planks he would have painted white.
Input
The first line contains the number of test cases ().
The first line of each test case contains the number of planks (). The next integers are the plank lengths (, ).
Output
For each test case, output lines, where is the largest in that test case. On line , output , the total length of planks Bajtazar would have painted white if he had chosen the fence height .
Hint
For height , the fence pieces are 4 4 2 4 3 2 4 4. He would paint pieces with total length , so the answer on line 4 is 13.
For height , the fence pieces are 5 5 5 2 2 5 3. He would paint pieces with total length , so the answer on line 5 is 15.