Problem A. Closing the Loop
问题A.关闭的环
Problem 问题
Given a bag full of rope segments, you will build the longest loop of rope while alternating colors. The bag contains S segments and each segment will either be blue (B) or red (R). You are required to alternate between colors and because of this requirement you might not use every segment in the bag. If you only have segments of a single color, you will not be able to tie any knots and should output 0. Each segment length is provided in centimeters and each knot in the loop consumes one centimeter of length from the loop. In other words, a knot consumes one-half of a centimeter from of the two segment it connects.
给了一包绳子,一段一段的,你需要把它们连接成一个最长的环,而且颜色要分隔开。这个包里包含了S段绳子,每一段绳子或者是B蓝色的,或者是R红色的。你需要使用交替的颜色,因此可能有的绳子你用不到。如果你只有一种颜色的绳子你就不能打结,因此应该输出0.绳子给出的长度按厘米算,环中的每一个扣消耗一厘米。换句话说一个扣对于两段绳子来讲各消耗半厘米的长度。
Note that pieces of string that have length 1, if used in making the cycle, might get reduced to just a pair of knots of total length 0. This is allowed, and each such piece counts as having been used.
注意如果绳子长度为1,用它来形成一个圈,可能导致仅形成了两个扣,总长度为1.这是允许的,每一个这样的绳子按照被使用了计算。
Input 输入
The first line of input gives the number of cases, N.
输入的第一行给出数字N,表示有N个例子。
N test cases follow. For each test case there will be:
下面接着N个例子。每一个测试用例将会是:
One line containing the value S, the number of rope segments in the bag.
一行包含着S,表示包中的绳子段
One line containing a space separated list of S values. Each value L indicates the segment length in centimeters followed by the letter B or R to indicate the segment color.
一行包含着用空隔分隔的S值的列表。L表示绳子的长度,R或者B表示段的颜色
Output 输出
For each test case, output one line containing "Case #x: " followed by the maximum length of the rope loop that can be generated with the rope segments provided.
对于每个测试用例,输出行包含"Case #x: "跟着最大长度
Limits 限制
1 ≤ number of rope segments (S) ≤ 1000 S的范围大于等于1,小于等于1000
1 ≤ length of a rope segment (L) ≤ 100 L的范围大于等于1,小于等于100
Small dataset 小数据集
测试用例小于等于5
N ≤ 5
Large dataset 大数据集
测试用例小于等于50
N ≤ 50
Sample 例子
Input 输入
4 4个测试用例
1 1段
5B 5厘米,蓝色
4 4段
6R 1B 7R 3B 6厘米红色,1厘米蓝色,7厘米红色,3厘米蓝色
7 7段
5B 4R 3R 2R 5R 4R 3R
2 2段
20B 20R
Output 输出结果
Case #1: 0
Case #2: 13
Case #3: 8
Case #4: 38