By Jadson José Monteiro Oliveira, Faculdade de Balsas Brazil
Nhonho, son of Mr. Barriga is participating of a new game created by the townhouse’s children, the game is well know by “cylinders Tower”. The game is to build the tallest tower made up of cylinders, respecting all the rules, of course! Just below it is described the operation of the game.
Will be arranged on the table, an amount of N cylinders.
Each cylinder has one color: Red, orange, green or blue.
Each cylinder has one heigth h and a base with radius of size r.
To the build the tower, the cylinders should to be stacked, and the top cylinder should ever have the base smaller that the base of the cylinder below it. Except the first cylinder, it can have the base of any size, since there is no other cylinder below it.
There are also some restrictions very interesting on colors of the cylinders. They are described below.
A red cylinder cannot to be put on an orange cylinder
An orange cylinder cannot to be put on a blue cylinder
A blue cylinder cannot to be put on a green cylinder
A green cylinder cannot to be put on a red cylinder
Tired of seeing his son complain of losing forever and consequently to hear the phrase "Look at him huh! Look at him huh! Look at him ... " multiple times, Mr. Barriga want to give a small tip for Nhonho, telling the height greater than can be built given the list with the characteristics of cylinders available for construction of the tower. However, although being good at math and collect the rent properly, Mr. Barriga was unable to calculate the maximum height that the tower may have due to the large amount of available cylinders. So he hired you to write a program able to inform this value.
To help you better understand the problem, Mr. Barriga showed him through a drawing the height of the highest possible tower to be built with five parts, following all the rules of the game. Below you can see the drawing of Mr. Barriga.
The input contains several test cases. The first line of each test case contains an integer N (1 ≤ N ≤ 10³), representing the number of cylinders arranged on the table, following N rows, each row having a height h (1 ≤ h ≤ 1000) of the cylinder in centimeters, the radius r (1 ≤ r ≤ 1000) of the cylinder base and a word p, representing the color of the cylinder. The word can be: VERMELHO (red in portuguese), LARANJA (orange in portuguese), VERDE (green in portuguese), or AZUL (blue in portuguese). The end of input is indicated as N == 0, which should not be processed.
For each test case, your program should print a single line with the value the height of the largest cylinders tower that can be built, followed by the word "centimetro(s)” (centimeters in portuguese).
Input Sample | Output Sample |
5 5 3 VERMELHO 4 2 LARANJA 1 1 VERDE 3 5 LARANJA 2 4 AZUL 3 10 10 LARANJA 5 10 VERDE 6 5 VERMELHO 0 |
15 centimetro(s) 11 centimetro(s) |