Closing the Loop (Small)
InterviewTime limit5sMemory limit512 MB
Pick equal numbers of red and blue segments with the largest lengths and subtract one centimeter per knot to get the longest alternating loop.
Problem
A bag holds rope segments. Each segment is blue (B) or red (R), and its length is an integer number of centimeters. You tie segments together into one closed loop, and the colors must alternate all the way around the loop. Because of that rule you do not have to use every segment in the bag. If the bag holds only one color, no knot can be tied and the answer is 0.
Each knot shortens the loop by one centimeter, taking half a centimeter from each of the two segments it joins. A loop built from segments has knots, so it loses centimeters in total. The smallest possible loop is two segments joined by two knots.
A segment of length 1 may give all of its length to its two knots and leave 0 centimeters behind. That is allowed, and such a segment still counts as used.
Find the greatest loop length you can build.
Input
The first line contains , the number of test cases.
Each test case takes two lines. The first line contains , the number of rope segments. The second line contains space separated values. Each value is an integer length followed immediately by the letter B or R giving the color.
Limits
Output
For each test case, print one line of the form Case #x: y, where is the test case number starting at 1 and is the maximum loop length.