FALTAENV - Falta Envido

Envido is an important part of the typical Argentinian game Truco. It is even more important when Falta Envido is called, because in this case the winner of the Envido wins the entire game. In this problem you are asked to calculate the envido advantage of a cheater.

Truco is played with a limited deck of spanish cards. This deck contains four suits: espada, basto, copa and oro. There are 10 cards of each suit: 1, 2, 3, 4, 5, 6, 7, 10, 11 and 12. Cards 10, 11 and 12 have an envido value of 0, while the envido value for any of the other cards is simply its number. Each player holds three cards. The winner of an Envido is the player with the higher envido score. When calculating his envido score each player can either:

  1. select a single card and have as envido score the envido value of the card; or
  2. select two cards of the same suit and have as envido score 20 plus the sum of the envido value of each selected card.

Players always select the card or cards that yield the highest envido score. For instance, a player having a 10 and a 2 of the same suit, together with a 5 of a different suit, would inform an envido score of 22 because 20 + 0 + 2 = 22.

A cheater playing Truco thinks that he can change one of his three cards without anybody else in the table noticing. If he changes more than one card, he surely will be discovered, so he does not do that. Since even changing one card is risky, he only does it when the envido score of the resulting hand is much higher than the one of his original hand.

Given the cards the cheater has in hand, you must calculate the maximum increase he can get in the envido score by exchanging one of his cards with a card from the deck. Notice that the resulting hand must be formed by two of his original cards and one new card selected from the deck, and that he cannot have two equal cards (same number and suit).

Input

The input contains several test cases. Each test case is described in a single line that contains six values N1, S1, N2, S2, N3, and S3 separated by single spaces. Each pair (Ni , Si ) describes a card in the hand of the cheater (1 ≤ i ≤ 3), where Ni is the number of the card (1, 2, 3, 4, 5, 6, 7, 10, 11 or 12), and Si is its suit (espada, basto, copa or oro). You may assume that the three cards are different. The last line of the input contains three times the number −1 and an asterisk, with the six values separated by single spaces, and should not be processed as a test case.

Output

For each test case output a single line with an integer representing the maximum increase in the envido score that can be obtained by replacing exactly one card of the input hand.

Example

Input:
12 espada 10 basto 11 basto
7 espada 1 oro 2 oro
7 espada 1 oro 6 espada
-1 * -1 * -1 *

Output:
7
10
0

Added by:Pablo Ariel Heiber
Date:2010-08-19
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS OBJC PERL6 VB.NET
Resource:FCEyN UBA ICPC Selection 2008

hide comments
2017-10-28 06:17:20
I like this kind of problems very much - no algo for it to be found in CLRS, just figure out the logic and make sure your implementation follows it. Commented code and sensible variable names help getting it right.
2012-08-15 10:09:24 game2712
hey prabhat what was the silly mistake ?
2012-06-20 17:23:58 ♘Prabhat
i had done silly mistake finally got AC :) after lots of WA
2012-05-23 05:56:44 ♘Prabhat
@aman 27
2012-05-23 05:55:17 ♘Prabhat
easy but i don'nt know why i am getting WA pls give me any critical test case
2012-05-22 11:47:52 Aman Choudhary
getting wrong answer.
what wil be d output for
1 espada 2 oro 3 basto
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.