PivotOJ

Cribbage On Steroids

시간 제한: 1000ms메모리 제한: 1024MB출처: ICPC ECNA 2022-2023BOJ 27611

문제

Cribbage is a two-person card game where players score points for various combinations of cards. A standard 5252-card deck is used where cards have one of 44 suits (not important in this problem) and one of 1313 ranks. The card ranks, from lowest to highest, are Ace (A), 22, 33, 44, 55, 66, 77, 88, 99, 1010 (T), Jack (J), Queen (Q), King (K). In normal cribbage, players make combinations from a hand consisting of five cards (four in their hand and one common card). The possible combinations and their point values are the following:

  • 15's: any combination of card ranks that total exactly 1515 scores 22 points. The values of Kings, Queens and Jacks are 1010, the value of Aces is 11 and all other card values are the same as their rank.
  • pairs: any two cards with the same rank scores 22 points. Note that three cards with the same rank will score 66 points since it contains three separate pairs; four cards with the same rank scores 1212 points; five cards with the same rank\ldots but we're getting ahead of ourselves.
  • runs: for each disjoint run of length three or more, each set of cards that generates the longest run scores 11 point per card. So for example, the cards 2,2,3,4,5,8,92,2,3,4,5,8,9, T, J, Q will score 88 points for two four-card sequences (there are 22 ways to make 2,3,4,52,3,4,5) and 55 points for a five-card sequence (there is one way to make 8,98,9, T, J, Q).

The total score across each of these three categories is the hand's score.

For example, a five-card hand consisting of 4,5,5,5,64,5,5,5,6 will score 2323 points: 88 points for 1515's (three 44-55-66 combinations and one 55-55-55), 66 points for pairs (three pairs of 55's) and 99 points for runs (three 44-55-66 runs). The five-card hand T,T,J,Q,Q scores 1616: 44 points for pairs (T's and Q's) and 1212 points for runs (four different runs of T-J-Q). There are other ways to score as well but the three rules above will suffice for this problem.

Now as we said, in normal cribbage you look for combinations in five-card hands. But we're far from normal. Your task is to determine the value of an nn-card hand when nn can be significantly larger than 55.

입력

Input starts with a integer nn (5n1005 \leq n \leq 100) indicating the number of cards in the hand. Following this are nn characters taken from the set {A,2,3,4,5,6,7,8,9,T,J,Q,K} indicating the ranks of the nn cards. These characters will be on one or more lines with one space between any two characters on the same line. The card ranks will not necessarily be in sorted order. Note that unlike a standard deck of cards, there may be more than four cards of any rank.

출력

Output the total score achieved by the nn-card hand.

예제

예제 1

입력
5
4 5 6 5 5
출력
23

예제 2

입력
13
A 2 3 4 5
6 7 8 9 T
J Q K
출력
71

예제 3

입력
10
2 2 3 4 5 8 9 T J Q
출력
45
코드를 제출하려면 로그인하세요.