1799. Rummikub

单点时限: 2.0 sec

内存限制: 256 MB

Rummikub is a simple game, often played by elderly people versus their grandchildren. It is a tile-based game and each of the tiles has a colored number written on it. There are four available colors: yellow, red, green and black. A tile is described by its number followed by a lower case letter, which is the first letter of its color. All tiles are unique.

The goal of game is simple: get rid of all your tiles. This sounds easier than it is, since you can only use them in two specific ways. You can get rid of tiles by constructing runs or groups. A run is composed of three or more consecutive numbers of the same color, for example 9r, 10r, 11r, or 60g, 61g, 62g, 63g, 64g, 65g. A group is composed of three or four tiles with the same numbers, and therefore different colors. Examples are 1r, 1g, 1b and 17r, 17g, 17b, 17y. In constructing the runs and groups, you may only use each tile once.

The value of a run or group is the sum of the numbers on the tiles. Your score is the sum of the values of all the runs and groups you construct. What is your maximum possible score?


The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format:

One line with one integer N, satisfying 1 <= N <= 400: the number of tiles you have.

One line with N strings separated by single spaces, each consisting of a number between 1 and 100 and a character which is either ‘y’, ‘r’, ‘g’, ‘b’: the available tiles. All tiles are unique.


For every test case in the input, the output should contain a single number, on a single line: the maximum possible score.


7g 7b 7r 8r 9r
23b 1y 24b 1r 93b 1b 100r
2y 2r 2g 2b 4g 5g 6g 7g

0 人解决,2 人已尝试。

0 份提交通过,共有 2 份提交。

9.9 EMB 奖励。

创建: 12 年,9 月前.

修改: 2 年,11 月前.

最后提交: 12 年,2 月前.

来源: The 2006 Benelux Algorithm Programming Contest