Watchtowers
InterviewTime limit1sMemory limit128 MB
Find the maximum sum over any nonempty block of consecutive towers arranged in a circle.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
Let us travel back to a time when the finest means of transport was a steed, castles towered over cities, princesses were beautiful and knights were brave. In those very times lived our new hero, Captain Lucjusz.
Captain Lucjusz manages the watchtowers that keep one of the royal cities safe. There are watchtowers, arranged in a circle: the first watchtower is adjacent to the second and to the -th, the second is adjacent to the first and the third, and so on.
In Captain Lucjusz's day there were no ministries yet, but bureaucracy already existed. At any moment the captain expects a visit from the royal inspectors who check compliance with norms and regulations. The officials will want to inspect some of the watchtowers under his command. The captain may pick any connected fragment of his network of watchtowers (that is, one made up of consecutively adjacent watchtowers) to be the subject of the inspection.
Captain Lucjusz has assigned to every watchtower an integer (negative, zero, or positive) describing the impression that, in his opinion, a visit to that watchtower will make on the inspectors. The score of the whole inspection is the sum of the impressions the inspectors gather in the watchtowers they visit. Compute the maximum score Captain Lucjusz can hope for.
The fragment chosen for inspection must contain at least one watchtower and, in the extreme case, may contain all of them.
Input
The first line of input contains a natural number (), the number of test sets. The test sets then follow one after another.
The first line of a test set contains a natural number (), the number of watchtowers under Captain Lucjusz's command.
The second line of the set contains space-separated integers (), the impression each watchtower is expected to make on the inspectors.
Output
For each test set, print on its own line the maximum inspection score that can be achieved.