Problem:
Problem C:
K Poker
Description
時は200X年、K大学で活動する謎のサークルKはその大学の文化祭の最中にK東4Fで彼らが作成した新たなゲーム、Kポーカーを発表した。
このゲームは一見すると普通のポーカーであるが、カードに基本点という要素をつけてポーカーをより奥の深いゲームにしたものである。
カードの基本点はそれぞれのカードに書かれている絵柄によって決まり、手札の点数は手札にある5枚の基本点の合計に手札の役の倍率を掛けたものになる。
この基本点を導入した事により、例えばフルハウスなどの強い役を作ったとしても基本点が0であればブタと同様に扱われてしまい、ワンペアに負けることさえある。
それ以外のルールは通常のポーカーと同じである。
このKポーカーをPCに移植しようと思ったあなたは手札の点数を計算するプログラムを書く事にした。
なおこのゲームは18歳未満の人はプレイ出来ない。
Input
入力は複数のテストケースからなる。
各テストケースの最初の行にはチェックする手札の数Nが書かれている。
次の4行には13個の整数が並びカードの基本点が1-13の順に並んで書かれている。4行のスーツは上から順にスペード、クローバー、ハート、ダイヤの順に並んでいる。
次の行には9個の整数が並び、順にワンペア、ツーペア、スリーカード、ストレート、フラッシュ、フルハウス、フォーカード、ストレートフラッシュ、ロイヤルストレートフラッシュの倍率を表している。役の倍率は必ず昇順となっている。ブタ(役無し、ノーペア)の倍率は常に0である。
次のN行には手札を表す5個の長さ2の異なる文字列が並んでいる。1文字目はカードの数値を表し,A,2,3,4,5,6,7,8,9,T,J,Q,Kのいずれかである。2文字目はカードのスーツを表し、S,C,H,Dのいずれかである。各アルファベットはAはエース、Tは10、Jはジャック、Qはクイーン、Kはキング、Sはスペード、Cはクローバー、Hはハート、Dはダイヤを表す。
入力の数値は全て[0,10000]の範囲に収まっている。
入力はEOFで終わる。
Output
各手札の点数を1行ずつ出力せよ。
連続する2つのテストケースの間には空行を1つ入れること。
役については
wikipediaのポーカー
のページを参照のこと。
なお、ストレートはエースとキングをまたぐ場合があるが、手札が10,ジャック、クイーン、キング、エースの場合にのみストレートとみなされる。
Sample Input
3
0 1 0 1 0 0 0 0 0 1 0 0 0
1 0 0 0 0 0 1 0 0 1 0 0 2
0 1 1 0 1 1 1 0 0 0 0 5 5
3 1 1 1 0 1 0 0 1 0 3 0 2
1 1 2 4 5 10 20 50 100
7H 6H 2H 5H 3H
9S 9C 9H 8H 8D
KS KH QH JD TS
10
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9
AS 3D 2C 5D 6H
6S 6C 7D 8H 9C
TS TD QC JD JC
KD KH KC AD 2C
3D 4H 6D 5H 7C
2S KS QS AS JS
TS TD JD JC TH
8H 8D TC 8C 8S
4S 5S 3S 7S 6S
KD QD JD TD AD
Output for Sample Input
25
0
14

0
5
10
15
20
25
30
35
40
45

Submitted solutions:

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    int fc = 0;
    while (cin >> n) {
        if (fc++) cout << endl;
        vector<vector<int>> p(4, vector<int>(14));
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                cin >> p[i][j];
            }
        }
        vector<int> s(10);
        for (int i = 1; i < 10; i++) {
            cin >> s[i];
        }
        auto is_flush = [](vector<pair<int,int>> v) -> bool {
            for (int i = 1; i < 5; i++) {
                if (v[i].second != v[0].second) return false;
            }
            return true;
        };
        auto is_straight = [](vector<pair<int,int>> v) -> bool {
            sort(v.begin(), v.end());
            for (int i = 1 + (v[0].first == 1 and v[1].first == 10); i < 5; i++) {
                if (v[i].first != v[i-1].first + 1) return false;
            }
            return true;
        };
        auto cal = [&](vector<pair<int,int>> v) -> int {
            bool ff = is_flush(v);
            bool ss = is_straight(v);
            sort(v.begin(), v.end());
            if (ff and ss) {
                if (v[0].first == 1 and v[1].first == 10) return 9;
                else return 8;
            }
            if (v[0].first == v[3].first or v[1].first == v[4].first) {
                return 7;
            }
            if (v[0].first == v[2].first and v[3].first == v[4].first) {
                return 6;
            }
            if (v[0].first == v[1].first and v[2].first == v[4].first) {
                return 6;
            }
            if (ff) {
                return 5;
            }
            if (ss) {
                return 4;
            }
            {
                for (int i = 0; i < 3; i++) {
                    if (v[i].first == v[i+2].first) return 3;
                }
            }
            {
                int cnt = 0;
                for (int i = 0; i < 4; i++) {
                    if (v[i].first == v[i+1].first) cnt += 1;
                }
                return cnt;
            }
        };
        for (int i = 0; i < n; i++) {
            int a = 0, b = 0;
            vector<pair<int,int>> v(5);
            for (int j = 0; j < 5; j++) {
                char c,cc; cin >> c >> cc;
                if (cc == 'S') v[j].second = 0;
                else if (cc == 'C') v[j].second = 1;
                else if (cc == 'H') v[j].second = 2;
                else v[j].second = 3;
                if (isdigit(c)) {
                    v[j].first = c - '0';
                }
                else {
                    if (c == 'A') v[j].first = 1;
                    else if (c == 'T') v[j].first = 10;
                    else if (c == 'J') v[j].first = 11;
                    else if (c == 'Q') v[j].first = 12;
                    else v[j].first = 13;
                }
                b += p[v[j].second][v[j].first];
            }
            a = cal(v);
            //cout << a << " " << b << endl;
            cout << s[a]*b << endl;
        }
    }
}
accuracy: 1
time_ms: 50
memory_kb: 3296
final_rank: 11

code_snippet: #include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>

bool is_royal(const std::vector<std::pair<int, char>> &cards) {
  std::set<int> s;
  for (const auto &p: cards) {
    s.insert(p.first);
  }
  return s.count(10) && s.count(11) && s.count(12) && s.count(13) && s.count(1);
}

bool is_flush(const std::vector<std::pair<int, char>> &cards) {
  std::set<char> s;
  for (const auto &p: cards) {
    s.insert(p.second);
  }
  return (s.size() == 1);
}

bool is_straight(const std::vector<std::pair<int, char>> &cards) {
  std::set<int> s;
  std::vector<int> t;
  for (const auto &p: cards) {
    s.insert(p.first);
    t.push_back(p.first);
  }
  if (s.size() != 5) return false;
  std::sort(t.begin(), t.end());
  return (t[4]-t[0] == 4);
}

std::vector<int> multi(const std::vector<std::pair<int, char>> &cards) {
  std::map<int, int> s;
  for (const auto &p: cards) {
    ++s[p.first];
  }
  std::vector<int> res;
  for (const auto &p: s) {
    if (p.second >= 2) {
      res.emplace_back(p.second);
    }
  }
  return res;
}

int testcase_ends(bool first) {
  int N;
  if (scanf("%d", &N) == EOF)
    return 1;

  if (!first)
    printf("\n");

  std::map<char, std::vector<int>> score;
  for (char ch: {'S', 'C', 'H', 'D'}) {
    score[ch].emplace_back(0);
    for (int i=1; i<=13; ++i) {
      int x;
      scanf("%d", &x);
      score[ch].emplace_back(x);
    }
  }

  std::vector<int> hands{0};
  for (int i=0; i<9; ++i) {
    int x;
    scanf("%d", &x);
    hands.emplace_back(x);
  }

  for (int i=0; i<N; ++i) {
    std::vector<std::pair<int, char>> cards;
    int base=0;
    for (int j=0; j<5; ++j) {
      char n, s;
      scanf(" %c%c", &n, &s);
      int num=0;
      switch (n) {
      case 'A':
        num = 1;
        break;
      case 'T':
        num = 10;
        break;
      case 'J':
        num = 11;
        break;
      case 'Q':
        num = 12;
        break;
      case 'K':
        num = 13;
        break;
      default:
        num = n-'0';
      }

      cards.emplace_back(num, s);
      base += score[s][num];
    }

    bool royal=is_royal(cards);
    bool straight=is_straight(cards);
    bool flush=is_flush(cards);
    std::vector<int> mul=multi(cards);
    std::sort(mul.begin(), mul.end());

    intmax_t res=base;
    if (royal && flush) {
      res *= hands[9];
    } else if (straight && flush) {
      res *= hands[8];
    } else if (mul.size() == 1 && mul[0] == 4) {
      res *= hands[7];
    } else if (mul.size() == 2 && mul[0] == 2 && mul[1] == 3) {
      res *= hands[6];
    } else if (flush) {
      res *= hands[5];
    } else if (straight || royal) {
      res *= hands[4];
    } else if (mul.size() == 1 && mul[0] == 3) {
      res *= hands[3];
    } else if (mul.size() == 2 && mul[0] == 2 && mul[1] == 2) {
      res *= hands[2];
    } else if (mul.size() == 1 && mul[0] == 2) {
      res *= hands[1];
    } else {
      res = 0;
    }
    printf("%jd\n", res);
  }
  return 0;
}

int main() {
  for (int i=0; true; ++i) {
    if (testcase_ends(i == 0)) return 0;
  }
}
accuracy: 1
time_ms: 60
memory_kb: 2804
final_rank: 9

code_snippet: #include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<cmath>
#include<algorithm>
using namespace std;
typedef vector<int> vi;

int main()
{
  int buckets[14];
  int buckets2[5];
  bool firsttime=true;
  int n;
  while(cin >> n)
  {
    if(!n) break;
    if(!firsttime) cout << endl;
    if(firsttime) firsttime=false;
    int cardscore[14][5];
    int handscore[9];
      for(int j=1;j<=4;j++)
      {
        for(int k=1;k<=13;k++)
        {
          cin >> cardscore[k][j];
        }
      }
      for(int j=0;j<9;j++)
      {
        cin >> handscore[j];
      }
    
    for(int i=0;i<n;i++)
    {
      int times=0;
      long long score=0;
      vector<string> card(5);
      vector<pair<int,int> > tmpc(5);
      for(int j=0;j<5;j++)
      {
        cin >> card[j];
        if(card[j][0]=='A')
        {
          tmpc[j].first=1;
        }
        else if(card[j][0]=='T')
        {
          tmpc[j].first=10;
        }
        else if(card[j][0]=='J')
        {
          tmpc[j].first=11;
        }
        else if(card[j][0]=='Q')
        {
          tmpc[j].first=12;
        }
        else if(card[j][0]=='K')
        {
          tmpc[j].first=13;
        }
        else
        {
          tmpc[j].first=(int)(card[j][0]-'0');
        }
        if(card[j][1]=='S')
        {
          tmpc[j].second=1;
        }
        else if(card[j][1]=='C')
        {
          tmpc[j].second=2;
        }
        else if(card[j][1]=='H')
        {
          tmpc[j].second=3;
        }
        else if(card[j][1]=='D')
        {
          tmpc[j].second=4;
        }
        score+=cardscore[tmpc[j].first][tmpc[j].second];

      }
      int onep=0,threec=0,four=0;
      bool flash=false;
      bool straight=true;
      sort(tmpc.begin(),tmpc.end());
      memset(buckets,0,sizeof(buckets));
      memset(buckets2,0,sizeof(buckets2));
      for(int j=0;j<5;j++)
      {
        buckets[tmpc[j].first]++;
        buckets2[tmpc[j].second]++;
      }
      for(int j=1;j<=13;j++)
      {
        if(buckets[j]==2)
        {
          onep++;
        }
        else if(buckets[j]==3)
        {
          threec++;
        }
        else if(buckets[j]==4)
        {
          four++;
        }
      }
      for(int j=1;j<=4;j++)
      {
        if(buckets2[j]==5)
        {
          flash=true;
          times=handscore[4];
        }
      }
      if(onep==1 && threec==0)
      {
        times=handscore[0];
      }
      else if(onep==2)
      {
        times=handscore[1];
      }
      else if(threec==1 && onep==0)
      {
        times=handscore[2];
      }
      else if(threec==1 && onep==1)
      {
        times=handscore[5];
      }
      else if(four==1)
      {
        times=handscore[6];
      }
      if(tmpc[0].first==1 && tmpc[1].first==10 && tmpc[2].first==11 && tmpc[3].first==12 && tmpc[4].first==13)
      {
        if(flash) times=handscore[8];
        else times=handscore[3];
      }
      else
      {
        for(int j=0;j<4;j++)
        {
          if(tmpc[j+1].first!=(tmpc[j].first+1)) straight=false;
        }
        if(straight)
        {
          if(flash) times=handscore[7];
          else times=handscore[3];
        }
      }
      cout << score*times << endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 3156
final_rank: 18

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>

#define REP(i,n) for(int i=0;i<n;++i)
#define REPR(i,n) for(int i=n;i>=0;--i)
#define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(),v.end()
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

using namespace std;

//suit S:0 C:1 H:2
int conNum(char c){
  if(c == 'K') return 13;
  else if(c == 'Q') return 12;
  else if(c == 'J') return 11;
  else if(c == 'T') return 10;
  else if(c == 'A') return 1;
  else{
    return c - '0';
  }
}

int suitTonum(char c){
  if(c == 'S') return 0;
  else if(c == 'C') return 1;
  else if(c == 'H') return 2;
  else return 3;
}

struct hand{
  int num[5];
  int suit[5];
};

int main(){

  int n;
  bool hoge = false;
  while(cin >> n){
    if(hoge) cout << "\n";
    hoge = true;
    llong card[4][13] = {};
    REP(i,4){
      REP(j,13){
        cin >> card[i][j];
      }
    }
    llong role[9];
    REP(i,9){
      cin >> role[8 - i];
    }

    vector<hand> v(n);
    REP(i,n){
      REP(j,5){
        string tmp;
        cin >> tmp;
        v[i].num[j] = conNum(tmp[0]) - 1;
        v[i].suit[j] = suitTonum(tmp[1]);

        //cout << "#i" << i << " " << v[i].num[i] << " " << v[i].suit[i] << "\n";
      }
    }

    vector<llong> base(n);
    REP(i,n){
      base[i] = 0;
      REP(j,5){
        int a = v[i].num[j], b = v[i].suit[j];
        base[i] += card[b][a];
      }
      sort(v[i].num, v[i].num + 5);
    }
    REP(i,n){

      //flush
      bool end = false;
      bool isFl = true;
      REP(j,4){
        if(v[i].suit[j+1] != v[i].suit[0]){
          isFl = false;
          break;
        }
      }
      //st
      bool isSt = true;
      int last = v[i].num[0];
      REP(j,4){
        if(v[i].num[j+1] != last + 1){
          isSt = false;
          break;
        }
        last = v[i].num[j+1];
      }
      
      bool isRoyal = false;
      if(v[i].num[0] == 0 && v[i].num[1] == 9 && v[i].num[2] == 10 && v[i].num[3] == 11 && v[i].num[4] == 12){
        isSt = true;
        if(isFl){
          cout << base[i] * role[0] << "\n";
          isRoyal = true;
        }
      }
      if(isRoyal) continue;
      if(isFl && isSt){
        cout << base[i] * role[1] << "\n";
        continue;
      }

      map<int, int> mp;
      REP(j,5){
        mp[v[i].num[j]]++;
      }
      //four
      REPI(itr,mp){
        if(itr->second == 4){
          cout << base[i] * role[2] << "\n";
          end = true;
          break;
        }
      }
      if(end) continue;
      
      //full
      int th = 0, tw = 0;
      REPI(itr,mp){
        if(itr->second == 3){
          th++;
        }else if(itr->second == 2){
          tw++;
        }
      }
      if(th > 0 && tw){
        cout << base[i] * role[3] << "\n";
        continue;
      }

      if(isFl){
        cout << base[i] * role[4] << "\n";
        continue;
      }
      if(isSt){
        cout << base[i] * role[5] << "\n";
        continue;
      }
      if(th > 0){
        cout << base[i] * role[6] << "\n";
        continue;
      }
      if(tw == 2){
        cout << base[i] * role[7] << "\n";
        continue;
      }
      if(tw == 1){
        cout << base[i] * role[8] << "\n";
        continue;
      }
      cout << "0\n";
    }
  }
  return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3160
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
int nton[256];
int ston[256];
int point[4][13];
int rate[9];

int calc(vector<pair<int, int> > &a){
	int s[5], n[5];
	for(int i=0; i<5; i++){
		s[i] = a[i].second;
		n[i] = a[i].first;
	}
	bool samesuit = false;
	if(s[0]==s[1] && s[0]==s[2] && s[0]==s[3] && s[0]==s[4]){
		samesuit = true;
	}
	bool rstraight = false;
	bool straight = false;
	if(0 <= n[0] && n[0] <= 8){
		bool succ = true;
		for(int i=0; i<4; i++){
			if((n[i]+1)%13 != n[i+1]%13) succ = false;
		}
		if(succ){
			straight = true;
		}
	}
	if(n[0]==0 && n[1]==9 && n[2]==10 && n[3]==11 && n[4]==12) rstraight = true;
	if(samesuit && rstraight) return rate[8];
	if(samesuit && straight) return rate[7];
	if(straight || rstraight) return rate[3];
	if(samesuit) return rate[4];
	
	int pair1=0, pair2=0;
	for(int i=0; i<5; i++){
		int lim = 1;
		while(i+lim<5 && n[i]==n[i+lim]) lim++;
		if(lim>1){
			if(pair1!=0){
				pair2 = lim;
			}else{
				pair1 = lim;
			}
			i += lim-1;
		}
	}
	if(pair1 == 4) return rate[6];
	if(pair1 == 2 && pair2 == 3) return rate[5];
	if(pair1 == 3 && pair2 == 2) return rate[5];
	if(pair1 == 3) return rate[2];
	if(pair2 == 2) return rate[1];
	if(pair1 == 2) return rate[0];
	return 0;
}

int main(){
	for(int i=0; i<256; i++){
		nton[i] = i-'1';
	}
	nton['A'] = 0;
	nton['T'] = 9;
	nton['J'] = 10;
	nton['Q'] = 11;
	nton['K'] = 12;
	ston['S'] = 0;
	ston['C'] = 1;
	ston['H'] = 2;
	ston['D'] = 3;
	
	bool first = true;
	int n;
	while(cin >> n){
		if(!first){
			cout << endl;
		}else{
			first = false;
		}
		
		for(int i=0; i<4; i++){
			for(int j=0; j<13; j++){
				cin >> point[i][j];
			}
		}
		for(int i=0; i<9; i++){
			cin >> rate[i];
		}
		
		for(int i=0; i<n; i++){
			vector<pair<int, int> > a(5);
			int psum = 0;
			for(int j=0; j<5; j++){
				string s;
				cin >> s;
				a[j].first = nton[(int)s[0]];
				a[j].second = ston[(int)s[1]];
				psum += point[a[j].second][a[j].first];
			}
			sort(a.begin(), a.end());
			cout << psum *calc(a) << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3016
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

int n, base[4][13], rate[10];
string mark = "SCHD";

int getNumber(char c) {
	if (c == 'A') return 0;
	if (c == 'T') return 9;
	if (c == 'J') return 10;
	if (c == 'Q') return 11;
	if (c == 'K') return 12;
	return c - '0' - 1;
}

bool isStraight(string card) {
	int tmp[5];
	for (int i=0; i<5; ++i) tmp[i] = getNumber(card[2 * i]);
	sort(tmp, tmp + 5);
	for (int i=0; i<4; ++i) if (tmp[i]+1 != tmp[i+1]) return false;
	return true;
}

bool isFlush(string card) {
	char tmp = card[1];
	for (int i=1;i<5; ++i) if (tmp != card[2*i+1]) return false;
	return true;
}

bool isRoyal(string card) {
	set< char > num_set;
	for (int i=0; i<5; ++i) num_set.insert(card[2*i]);
	set< char > yes = {'A','T','J','Q','K'};
	return num_set == yes;
}

int getID(string card) {
	int cnt[13];
	memset(cnt, 0, sizeof(cnt));
	for (int i=0; i<5; ++i) ++cnt[getNumber(card[2 * i])];
	
	int pair_num = 0, max_card = 0;
	for (int i=0; i<13; ++i) {
		if (cnt[i] == 2) ++pair_num;
		max_card = max(max_card, cnt[i]);
	}
	
	if (max_card == 2 && pair_num == 1) return 0; // one pair
	if (max_card == 2 && pair_num == 2) return 1; // two pair
	
	if (max_card == 3) {
		if (pair_num == 0) return 2; // three card
		return 5; // full house
	}
	
	bool royal = isRoyal(card), straight = isStraight(card), flush = isFlush(card);
	
	if (royal && flush) return 8;
	if (straight && flush) return 7;	
	if (straight || royal) return 3;
	if (flush) return 4;
	
	if (max_card == 4) return 6;
	
	return 9;
}

void solve() {
	for (int i=0; i<n; ++i) {
		string card;
		int sum = 0;
		for (int j=0; j<5; ++j) {
			string cj;
			cin >> cj;
			card += cj;
			sum += base[mark.find(cj[1])][getNumber(cj[0])];
		}
		cout << sum * rate[getID(card)] << endl;
	}
}

int main() {
	rate[9] = 0;
	int i = 0;
	while (cin >> n) {
		if (i > 0) cout << endl;
		for (int i=0; i<4; ++i) for (int j=0; j<13; ++j) cin >> base[i][j];
		for (int i=0; i<9; ++i) cin >> rate[i];
		solve();
		++i;
	}
}
accuracy: 1
time_ms: 160
memory_kb: 3164
final_rank: 19

code_snippet: #include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>

bool is_royal(const std::vector<std::pair<int, char>> &cards) {
  std::set<int> s;
  for (const auto &p: cards) {
    s.insert(p.first);
  }
  return s.count(10) && s.count(11) && s.count(12) && s.count(13) && s.count(1);
}

bool is_flush(const std::vector<std::pair<int, char>> &cards) {
  std::set<char> s;
  for (const auto &p: cards) {
    s.insert(p.second);
  }
  return (s.size() == 1);
}

bool is_straight(const std::vector<std::pair<int, char>> &cards) {
  std::set<int> s;
  std::vector<int> t;
  for (const auto &p: cards) {
    s.insert(p.first);
    t.push_back(p.first);
  }
  if (s.size() != 5) return false;
  std::sort(t.begin(), t.end());
  return (t[4]-t[0] == 4);
}

std::vector<int> multi(const std::vector<std::pair<int, char>> &cards) {
  std::map<int, int> s;
  for (const auto &p: cards) {
    ++s[p.first];
  }
  std::vector<int> res;
  for (const auto &p: s) {
    if (p.second >= 2) {
      res.emplace_back(p.second);
    }
  }
  return res;
}

int testcase_ends(bool first) {
  int N;
  if (scanf("%d", &N) == EOF)
    return 1;

  if (!first)
    printf("\n");

  std::map<char, std::vector<int>> score;
  for (char ch: {'S', 'C', 'H', 'D'}) {
    score[ch].emplace_back(0);
    for (int i=1; i<=13; ++i) {
      int x;
      scanf("%d", &x);
      score[ch].emplace_back(x);
    }
  }

  std::vector<int> hands{0};
  for (int i=0; i<9; ++i) {
    int x;
    scanf("%d", &x);
    hands.emplace_back(x);
  }

  for (int i=0; i<N; ++i) {
    std::vector<std::pair<int, char>> cards;
    int base=0;
    for (int j=0; j<5; ++j) {
      char n, s;
      scanf(" %c%c", &n, &s);
      int num=0;
      switch (n) {
      case 'A':
        num = 1;
        break;
      case 'T':
        num = 10;
        break;
      case 'J':
        num = 11;
        break;
      case 'Q':
        num = 12;
        break;
      case 'K':
        num = 13;
        break;
      default:
        num = n-'0';
      }

      cards.emplace_back(num, s);
      base += score[s][num];
    }

    bool royal=is_royal(cards);
    bool straight=is_straight(cards);
    bool flush=is_flush(cards);
    std::vector<int> mul=multi(cards);
    std::sort(mul.begin(), mul.end());

    intmax_t res=base;
    if (royal && flush) {
      res *= hands[9];
    } else if (straight && flush) {
      res *= hands[8];
    } else if (mul.size() == 1 && mul[0] == 4) {
      res *= hands[7];
    } else if (mul.size() == 2 && mul[0] == 2 && mul[1] == 3) {
      res *= hands[6];
    } else if (flush) {
      res *= hands[5];
    } else if (straight || royal) {
      res *= hands[4];
    } else if (mul.size() == 1 && mul[0] == 3) {
      res *= hands[3];
    } else if (mul.size() == 2 && mul[0] == 2 && mul[1] == 2) {
      res *= hands[2];
    } else if (mul.size() == 1 && mul[0] == 2) {
      res *= hands[1];
    } else {
      res = 0;
    }
    printf("%jd\n", res);
  }
  return 0;
}

int main() {
  for (int i=0; true; ++i) {
    if (testcase_ends(i == 0)) return 0;
  }
}
accuracy: 1
time_ms: 70
memory_kb: 2672
final_rank: 8

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;

int solve(vector<pair<int, int>>cards) {
	sort(cards.begin(),cards.end());
	bool flush_flag = all_of(cards.begin(), cards.end(), [&](const pair<int, int>&p) {
		return p.second==cards[0].second;
	});
	bool str_flag;
	{
		bool ok=true;
		for (int i = 0; i < 4; ++i) {
			if (cards[i].first + 1 != cards[i + 1].first) {
				ok=false;
			}
			if(cards[0].first==1&&cards[1].first==10&&cards[2].first==11&&cards[3].first==12&&cards[4].first==13)ok=true;
		}
		str_flag=ok;
	}
	if(str_flag&&flush_flag&&cards[0].first==1&&cards[1].first==10)return 9;
	if(str_flag&&flush_flag)return 8;
	if(str_flag)return 4;
	if(flush_flag)return 5;


	map<int,int>mp;
	for (int i = 0; i < 5; ++i) {
		mp[cards[i].first]++;
	}
	vector<int>v;
	for (auto m : mp) {
		v.push_back(m.second);
	}
	sort(v.begin(),v.end(),greater<int>());
	if (v == vector<int>{4, 1})return 7;
	if (v == vector<int>{3, 2})return 6;
	if (v == vector<int>{1, 1, 1, 1, 1})return 0;
	if (v == vector<int>{2, 1, 1, 1})return 1;
	if (v == vector<int>{2, 2, 1})return 2;
	if (v == vector<int>{3, 1, 1})return 3;
}

int main() {
	int N;
	bool flag=false;
	while (cin>>N) {
		if(flag)cout<<endl;
		flag=true;
		vector<vector<int>>costs(4,vector<int>(13));
		for (int i = 0; i < 4; ++i) {
			for (int j = 0; j < 13; ++j) {
				cin>>costs[i][j];
			}
		}
		vector<int>yaku_cost(10);
		for (int i = 0; i < 9; ++i) {
			cin>>yaku_cost[i+1];
		}
		for (int q = 0; q < N; ++q) {
			map<char, int>type_mp{
				{'S',0},
				{'C',1},
				{'H',2},
				{'D',3},
			};
			map<char, int>num_mp{
				{'T',10},
				{'J',11},
				{'Q',12},
				{'K',13},
				{'A',1}
			};

			int sum=0;
			vector<pair<int,int>>cards;
			for (int i = 0; i < 5; ++i) {
				string st;cin>>st;
				int num=isdigit(st[0])?st[0]-'0':num_mp[st[0]];
				int type=type_mp[st[1]];
				sum+=costs[type][num-1];
				cards.emplace_back(num,type);
			}
			int yaku=solve(cards);
			int ans=sum*yaku_cost[yaku];
			cout<<ans<<endl;
		}

	}
	return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 3128
final_rank: 20

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


int LOC[128];
int value[4][14],mult[9];

int getNUM(char ch){

	if(ch >= '2' && ch <= '9')return ch - '0';

	switch(ch){
	case 'A':return 1;
	case 'T':return 10;
	case 'J':return 11;
	case 'Q':return 12;
	case 'K':return 13;
	}
	return -1; //must not reach here
}

bool is_straight(int array[5]){

	if(array[4]-array[0] == 4)return true;

	if(array[0] == 1 && array[1] == 10)return true;

	return false;
}


int main(){

	LOC['S'] = 0;
	LOC['C'] = 1;
	LOC['H'] = 2;
	LOC['D'] = 3;

	int N,num_2,num_3,num_4,num_suit,point;
	bool isFirst = true;
	char buf[3];
	int num_table[14],suit_table[4],tmp_num,tmp_suit,array[5],index;

	while(scanf("%d",&N) != EOF){

		if(isFirst){
			isFirst = false;
		}else{
			printf("\n");
		}

		for(int i = 0; i < 4; i++){
			for(int k = 1; k <= 13; k++)scanf("%d",&value[i][k]);
		}

		for(int i = 0; i < 9; i++)scanf("%d",&mult[i]);

		for(int loop = 0; loop < N; loop++){
			for(int i = 1; i <= 13; i++)num_table[i] = 0;
			for(int i = 0; i < 4; i++)suit_table[i] = 0;

			num_2 = num_3 = num_4 = num_suit = point = 0;

			for(int k = 0; k < 5; k++){
				scanf("%s",buf);

				tmp_suit = LOC[buf[1]];
				tmp_num = getNUM(buf[0]);

				point += value[tmp_suit][tmp_num];
				num_table[tmp_num]++;
				suit_table[tmp_suit]++;
			}

			for(int k = 0; k < 4; k++){
				if(suit_table[k] > 0)num_suit++;
			}

			for(int k = 1; k <= 13; k++){
				if(num_table[k] == 0)continue;

				switch(num_table[k]){
				case 2:
					num_2++;
					break;
				case 3:
					num_3++;
					break;
				case 4:
					num_4++;
					break;
				}
			}

			if(num_2 == 1){
				if(num_3 != 1){
					printf("%d\n",point*mult[0]);
					continue;
				}else{
					printf("%d\n",point*mult[5]);
					continue;
				}
			}

			if(num_2 == 2){
				printf("%d\n",point*mult[1]);
				continue;
			}
			if(num_3 == 1){
				printf("%d\n",point*mult[2]);
				continue;
			}

			if(num_4 == 1){
				printf("%d\n",point*mult[6]);
				continue;
			}

			index = 0;
			for(int k = 1; k <= 13; k++){
				if(num_table[k] == 0)continue;
				array[index++] = k;
			}

			if(num_suit >= 2){

				if(is_straight(array)){
					printf("%d\n",point*mult[3]);
				}else{
					printf("0\n");
				}

			}else{

				if(is_straight(array)){
					if(array[0] == 1 && array[4] == 13){
						printf("%d\n",point*mult[8]);
					}else{
						printf("%d\n",point*mult[7]);
					}
				}else{
					printf("%d\n",point*mult[4]);
				}
			}
		}
	}

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3196
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>

#define i64 long long
#define ui64 unsigned long long
using namespace std;

int c_points[5][14];
int h_points[10];

struct card{
  int num;
  int mark;
  card(char n_,char m_):num(),mark(){
    switch(m_){
      case 'S': mark = 0; break;
      case 'H': mark = 2; break;
      case 'D': mark = 3; break;
      case 'C': mark = 1; break;
    }
    if(('0'<=n_)&&(n_<='9'))num=n_-'0';
    else{
      switch(n_){
        case 'T': num = 10; break;
        case 'J': num = 11; break;
        case 'Q': num = 12; break;
        case 'K': num = 13; break;
        case 'A': num = 1; break;
      }
    }
  }
  bool operator< (const card& a){
    return num==a.num?mark<a.mark:num<a.num;
  }
};

bool straight(vector<card>& hand){
  for(int i=0;i<4;i++){
    if((hand[i+1].num-hand[i].num==1)||\
        ((!i)&&(hand[4].num==13&&hand[i].num==1)));
    else return false;
  }
  return true;
}

bool flush(vector<card>& hand){
  for(int i=0;i<4;i++){
    if(hand[i].mark!=hand[i+1].mark)return false;
  }
  return true;
}

bool no_pair(vector<card>& hand){
  for(int i=0;i<4;i++){
    if(hand[i].num==hand[i+1].num)return false;
  }
  return true;
}

bool one_pair(vector<card>& hand){
  int p;
  for(int i=0;i<=4;i++){
    if(i==4)return false;
    if(hand[i].num==hand[i+1].num){
      p=i;break;
    }
  }
  for(int i=p+1;i<4;i++){
    if(hand[i].num==hand[i+1].num)return false;
  }
  return true;
}

bool two_pair(vector<card>& hand){
  int p;
  for(int i=0;i<=2;i++){
    if(i==2)return false;
    if(hand[i].num==hand[i+1].num){
      p=i;break;
    }
  }
  if(p==0){
    return hand[2].num==hand[3].num?hand[3].num!=hand[4].num:hand[3].num==hand[4].num;
  }
  if(p==1){
    return hand[3].num==hand[4].num;
  }
}

bool three_card(vector<card>& hand){
  if(hand[0].num==hand[2].num&&hand[0].num!=hand[3].num)return true;
  if(hand[0].num!=hand[1].num&&hand[1].num==hand[3].num&&hand[3].num!=hand[4].num)return true;
  if(hand[0].num!=hand[1].num&&hand[1].num!=hand[2].num&&hand[2].num==hand[4].num)return true;
  return false;
}

bool fullhouse(vector<card>& hand){
  int p;
  if(hand[1].num!=hand[2].num){
    if(hand[0].num!=hand[1].num)return false;
    for(int i=2;i<4;i++){
      if(hand[i].num!=hand[i+1].num)return false;
    }
    return true;
  }else{
    if(hand[3].num!=hand[4].num)return false;
    for(int i=0;i<2;i++){
      if(hand[i].num!=hand[i+1].num)return false;
    }
    return true;
  }
}

bool straight_flush(vector<card>& hand){
  return straight(hand)&&flush(hand);
}

bool four_card(vector<card>& hand){
  return hand[0].num==hand[3].num||hand[1].num==hand[4].num;
}

bool royal_straight_flush(vector<card>& hand){
  if(straight_flush(hand)){
    if(hand[0].num==1&&hand[4].num==13)return true;
  }
  return false;
}


i64 solve(vector<card>& hand){
  sort(hand.begin(),hand.end());
  i64 res=0LL;
  
  for(int i=0;i<5;i++){
    int m_=hand[i].mark;
    int n_=hand[i].num;
    res+=(i64)c_points[m_][n_-1];
  }
  if(royal_straight_flush(hand)){
    res*=(i64)h_points[8];
    return res;
  }
  if(straight_flush(hand)){
    res*=(i64)h_points[7];
    return res;
  }
  if(four_card(hand)){
    res*=(i64)h_points[6];
    return res;
  }
  if(straight(hand)){
    res*=(i64)h_points[3];
    return res;
  }
  if(flush(hand)){
    res*=(i64)h_points[4];
    return res;
  }
  if(fullhouse(hand)){
    res*=(i64)h_points[5];
    return res;
  }
  if(one_pair(hand)){
    res*=(i64)h_points[0];
    return res;
  }
  if(two_pair(hand)){
    res*=(i64)h_points[1];
    return res;
  }
  if(three_card(hand)){
    res*=(i64)h_points[2];
    return res;
  }
  if(no_pair(hand)){
    return (i64)0;
  }
  
  return -1;
}

int main(){
  int n;
  bool flag = false;
  while(1){
    if(!flag){
      cin >> n;
      if(cin.eof())return 0; 
      flag=true;
    }
    fill(&c_points[0][0],&c_points[4][13],0);
    fill(&h_points[0],&h_points[9],0);
    for(int i=0;i<4;i++){
      for(int k=0;k<13;k++){
        cin >> c_points[i][k];
      }
    }
    for(int i=0;i<9;i++){
      cin >> h_points[i];
    }
    char a[3];
    vector<card> hands;
    for(int i=0;i<n;i++){
      for(int k=0;k<5;k++){
        cin >> a;
        hands.push_back(card(a[0],a[1]));
      }
      cout << solve(hands) << endl;
      hands.clear();
    }
    if(flag){
      cin >> n;
      if(cin.eof())return 0; 
      else cout << endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3012
final_rank: 14

code_snippet: #include <iostream>
#include <deque>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>

#define i64 long long
#define ui64 unsigned long long
#define REP(i,n) for(int (i)=0;(i)<(n);i++)
#define REP2(i,k,n) for(int (i)=(k);(i)<(n);i++)
#define ATCODER 1000000007
using namespace std;
 
////////////////////////

int c_points[5][14];
int h_points[10];

struct card{
  int num;
  int mark;
  card(char n_,char m_):num(),mark(){
    switch(m_){
      case 'S': mark = 0; break;
      case 'H': mark = 2; break;
      case 'D': mark = 3; break;
      case 'C': mark = 1; break;
    }
    if(('0'<=n_)&&(n_<='9'))num=n_-'0';
    else{
      switch(n_){
        case 'T': num = 10; break;
        case 'J': num = 11; break;
        case 'Q': num = 12; break;
        case 'K': num = 13; break;
        case 'A': num = 1; break;
      }
    }
  }
  bool operator< (const card& a){
    return num==a.num?mark<a.mark:num<a.num;
  }
};

bool straight(vector<card>& hand){
  for(int i=0;i<4;i++){
    if((hand[i+1].num-hand[i].num==1)||\
        ((!i)&&(hand[4].num==13&&hand[i].num==1)));
    else return false;
  }
  return true;
}

bool flush(vector<card>& hand){
  for(int i=0;i<4;i++){
    if(hand[i].mark!=hand[i+1].mark)return false;
  }
  return true;
}

bool no_pair(vector<card>& hand){
  for(int i=0;i<4;i++){
    if(hand[i].num==hand[i+1].num)return false;
  }
  return true;
}

bool one_pair(vector<card>& hand){
  int p;
  for(int i=0;i<=4;i++){
    if(i==4)return false;
    if(hand[i].num==hand[i+1].num){
      p=i;break;
    }
  }
  for(int i=p+1;i<4;i++){
    if(hand[i].num==hand[i+1].num)return false;
  }
  return true;
}

bool two_pair(vector<card>& hand){
  int p;
  for(int i=0;i<=2;i++){
    if(i==2)return false;
    if(hand[i].num==hand[i+1].num){
      p=i;break;
    }
  }
  if(p==0){
    return hand[2].num==hand[3].num?hand[3].num!=hand[4].num:hand[3].num==hand[4].num;
  }
  if(p==1){
    return hand[3].num==hand[4].num;
  }
}

bool three_card(vector<card>& hand){
  if(hand[0].num==hand[2].num&&hand[0].num!=hand[3].num)return true;
  if(hand[0].num!=hand[1].num&&hand[1].num==hand[3].num&&hand[3].num!=hand[4].num)return true;
  if(hand[0].num!=hand[1].num&&hand[1].num!=hand[2].num&&hand[2].num==hand[4].num)return true;
  return false;
}

bool fullhouse(vector<card>& hand){
  int p;
  if(hand[1].num!=hand[2].num){
    if(hand[0].num!=hand[1].num)return false;
    for(int i=2;i<4;i++){
      if(hand[i].num!=hand[i+1].num)return false;
    }
    return true;
  }else{
    if(hand[3].num!=hand[4].num)return false;
    for(int i=0;i<2;i++){
      if(hand[i].num!=hand[i+1].num)return false;
    }
    return true;
  }
}

bool straight_flush(vector<card>& hand){
  return straight(hand)&&flush(hand);
}

bool four_card(vector<card>& hand){
  return hand[0].num==hand[3].num||hand[1].num==hand[4].num;
}

bool royal_straight_flush(vector<card>& hand){
  if(straight_flush(hand)){
    if(hand[0].num==1&&hand[4].num==13)return true;
  }
  return false;
}


i64 solve(vector<card>& hand){
  sort(hand.begin(),hand.end());
  // for(int i=0;i<5;i++){
  //   cout << hand[i].num << " " << hand[i].mark << endl;
  // }
  i64 res=0LL;
  
  for(int i=0;i<5;i++){
    int m_=hand[i].mark;
    int n_=hand[i].num;
    res+=(i64)c_points[m_][n_-1];
  }
  // cout << res << endl;
  if(royal_straight_flush(hand)){
    res*=(i64)h_points[8];
    // cout << 0 << endl;
    return res;
  }
  if(straight_flush(hand)){
    res*=(i64)h_points[7];
    // cout << 8 << endl;
    return res;
  }
  if(four_card(hand)){
    res*=(i64)h_points[6];
    // cout << 7 << endl;
    return res;
  }
  if(straight(hand)){
    res*=(i64)h_points[3];
    // cout << 4 << endl;
    return res;
  }
  if(flush(hand)){
    res*=(i64)h_points[4];
    // cout << 5 << endl;
    return res;
  }
  if(fullhouse(hand)){
    res*=(i64)h_points[5];
    // cout << 6 << endl;
    return res;
  }
  if(one_pair(hand)){
    res*=(i64)h_points[0];
    // cout << 1 << endl;
    return res;
  }
  if(two_pair(hand)){
    res*=(i64)h_points[1];
    // cout << 2 << endl;
    return res;
  }
  if(three_card(hand)){
    res*=(i64)h_points[2];
    // cout << 3 << endl;
    return res;
  }
  if(no_pair(hand)){
    return (i64)0;
  }
  
  return -1;
}

int main(){
  int n;
  bool flag = false;
  while(1){
    if(!flag){
      cin >> n;
      if(cin.eof())return 0; 
      flag=true;
    }
    fill(&c_points[0][0],&c_points[4][13],0);
    fill(&h_points[0],&h_points[9],0);
    for(int i=0;i<4;i++){
      for(int k=0;k<13;k++){
        cin >> c_points[i][k];
      }
    }
    for(int i=0;i<9;i++){
      cin >> h_points[i];
    }
    char a[3];
    vector<card> hands;
    for(int i=0;i<n;i++){
      for(int k=0;k<5;k++){
        cin >> a;
        hands.push_back(card(a[0],a[1]));
      }
      cout << solve(hands) << endl;
      hands.clear();
    }
    if(flag){
      cin >> n;
      if(cin.eof())return 0; 
      else cout << endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3068
final_rank: 13

code_snippet: #include <iostream>
#include <algorithm>
#include <cstring>
 
using namespace std;
 
int arr[4][13], rank[9], card[5], cnt[13];
string str[5];
 
int getMark(char ch){
    if(ch == 'S'){
	return 0;
    }else if(ch == 'C'){
	return 1;
    }else if(ch == 'H'){
	return 2;
    }
    return 3;
}
 
int getNum(char ch){
    if('2' <= ch && ch <= '9'){
	return (ch-'0')-1;
    }else if(ch == 'A'){
	return 0;
    }else if(ch == 'T'){
	return 9;
    }else if(ch == 'J'){
	return 10;
    }else if(ch == 'Q'){
	return 11;
    }
    return 12;
}
 
int judge(){
    bool same_mark = true;
    char ch = str[0][1];
    for(int i = 1 ; i < 5 ; i++){
	if(ch != str[i][1]){
	    same_mark = false;
	    break;
	}
    }
 
    if(same_mark){
	if(card[0] == 1 && card[1] == 10 && card[2] == 11 &&
	   card[3] == 12 && card[4] == 13){
	    return rank[8];
	}else{
	    bool check = true;
	    for(int i = 1 ; i < 5 ; i++){
		if(card[i] - card[i-1] != 1){
		    check = false;
		    break;
		}
	    }
	    if(check){
		return rank[7];
	    }else{
		return rank[4];
	    }
	}
    }
 
    sort(cnt, cnt+13, greater<int>());
    if(cnt[0] == 4){
	return rank[6];
    }else if(cnt[0] == 3){
	if(cnt[1] == 2){
	    return rank[5];
	}else{
	    return rank[2];
	}
    }else if(cnt[0] == 2){
	if(cnt[1] == 2){
	    return rank[1];
	}else{
	    return rank[0];
	} 
    }else{
	bool check = true;
	for(int i = 1 ; i < 5 ; i++){
	    if(card[i] - card[i-1] != 1){
		check = false;
		break;
	    }
	}
	if(check){
	    return rank[3];
	}
	if(card[0] == 1 && card[1] == 10 && card[2] == 11 &&
	   card[3] == 12 && card[4] == 13){
	    return rank[3];
	}
    }
 
    return 0;
}
 
int main(){
    int N;
    bool space = false;
 
    while(cin >> N){
	if(space){
	    cout << endl;
	}else{
	    space = true;
	}
 
	for(int i = 0 ; i < 4 ; i++){
	    for(int j = 0 ; j < 13 ; j++){
		cin >> arr[i][j];
	    }
	}
	for(int i = 0 ; i < 9 ; i++){
	    cin >> rank[i];
	}
 
	while(N--){
	    int point = 0, m,n;
	    memset(cnt, 0, sizeof(cnt));
	    for(int i = 0 ; i < 5 ; i++){
		cin >> str[i];
		m = getMark(str[i][1]);
		n = getNum(str[i][0]);
		point += arr[m][n];
		card[i] = n+1;
		cnt[n]++;
	    }
	    sort(card, card+5);
	    cout << point*judge() << endl;
	}
    }
    return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 1220
final_rank: 3

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n, a[4][13], p[10], s[5], t[5], z; char c; string s1 = "A23456789TJQK", s2 = "SCHD";
int judge() {
	vector<int> v; int w = 1, f = 1, g = 1, h = 1;
	for (int i = 1; i < 5; i++) {
		if (s[i - 1] != s[i]) v.push_back(w), w = 0;
		if (s[i - 1] + 1 != s[i]) g = 0;
		if (t[i - 1] != t[i]) f = 0;
		w++;
	}
	v.push_back(w);
	sort(v.begin(), v.end());
	if (v.size() == 2) return v.back() + 3;
	for (int i = 0; i < 5; i++) h &= (s[i] == (i ? (i + 8) % 13 : 0) ? 1 : 0);
	if (f) return h ? 9 : g ? 8 : 5;
	if (g | h) return 4;
	if (v.back() == 3) return 3;
	if (v.back() == 2) return 5 - v.size();
	return 0;
}
int main() {
	while (cin >> n) {
		if (z++) cout << endl;
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 13; j++) cin >> a[i][j];
		}
		for (int i = 1; i < 10; i++) cin >> p[i];
		for (int i = 0; i < n; i++) {
			int cnt = 0;
			for (int j = 0; j < 5; j++) {
				cin >> c; s[j] = s1.find(c);
				cin >> c; t[j] = s2.find(c);
				cnt += a[t[j]][s[j]];
			}
			sort(s, s + 5);
			sort(t, t + 5);
			cout << cnt * p[judge()] << endl;
		}
	}
}
accuracy: 1
time_ms: 100
memory_kb: 3016
final_rank: 15

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n, a[4][13], p[10], z; char c; pair<int, int> s[5]; string s1 = "A23456789TJQK", s2 = "SCHD";
int judge() {
	vector<int> v; int w = 1;
	for (int i = 1; i < 5; i++) {
		if (s[i].first != s[i - 1].first) v.push_back(w), w = 0;
		w++;
	}
	v.push_back(w);
	sort(v.begin(), v.end());
	if (v.back() == 4) return 7;
	if (v.back() == 3 && v.size() == 2) return 6;
	int f = 1, g = 1, h = 1;
	for (int i = 1; i < 5; i++) {
		if (s[i].second != s[i - 1].second) f = 0;
	}
	for (int i = 1; i < 5; i++) {
		if (s[i - 1].first + 1 != s[i].first) g = 0;
	}
	for (int i = 0; i < 5; i++) {
		if (s[i].first != (i ? (i + 8) % 13 : 0)) h = 0;
	}
	if (f) {
		if (h) return 9;
		if (g) return 8;
		return 5;
	}
	if (g | h) return 4;
	if (v.back() == 3) return 3;
	if (v.back() == 2 && v[v.size() - 2] == 2) return 2;
	if (v.back() == 2) return 1;
	return 0;
}
int main() {
	while (cin >> n) {
		if (z++) cout << endl;
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 13; j++) cin >> a[i][j];
		}
		for (int i = 1; i < 10; i++) cin >> p[i];
		for (int i = 0; i < n; i++) {
			int cnt = 0;
			for (int j = 0; j < 5; j++) {
				cin >> c; s[j].first = s1.find(c);
				cin >> c; s[j].second = s2.find(c);
				cnt += a[s[j].second][s[j].first];
			}
			sort(s, s + 5);
			cout << cnt * p[judge()] << endl;
		}
	}
}
accuracy: 1
time_ms: 100
memory_kb: 3016
final_rank: 15

code_snippet: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int N, a[4][14], b[9];
char T[15] = "-A23456789TJQK";
char U[5] = "SCHD";
string S; pair<int, int>d[5];
int poker() {
	int e[14], f[9];
	int g[14], h[9];
	for (int i = 0; i < 14; i++) { e[i] = 0; g[i] = 0; }
	for (int i = 0; i < 9; i++) { f[i] = 0; h[i] = 0; }
	for (int i = 0; i < 5; i++) {
		e[d[i].first]++;
		f[d[i].second]++;
	}
	for (int i = 0; i < 14; i++) { g[e[i]]++; }
	for (int i = 0; i < 4; i++) { h[f[i]]++; }
	if (h[5] == 1) {
		if (d[0].first != 1) { goto E; }
		if (d[1].first != 10) { goto E; }
		if (d[2].first != 11) { goto E; }
		if (d[3].first != 12) { goto E; }
		if (d[4].first != 13) { goto E; }
		return 8; E:;
		for (int i = 1; i < 5; i++) {
			if (d[i].first != d[i - 1].first + 1) {
				goto F;
			}
		}
		return 7; F:;
		return 4;
	}
	if (g[4] == 1) { return 6; }
	if (g[2] == 1 && g[3] == 1) { return 5; }
	for (int i = 1; i < 5; i++) {
		if (d[i].first != d[i - 1].first + 1) {
			goto G;
		}
	}
	return 3; G:;
	if (d[0].first != 1) { goto H; }
	if (d[1].first != 10) { goto H; }
	if (d[2].first != 11) { goto H; }
	if (d[3].first != 12) { goto H; }
	if (d[4].first != 13) { goto H; }
	return 3; H:;
	if (g[3] == 1) { return 2; }
	if (g[2] == 2) { return 1; }
	if (g[2] == 1) { return 0; }
	return 9;
}
int main() {
	int cnt = 0;
	while (cin >> N) {
		if (cnt) { cout << endl; }
		cnt++;
		for (int i = 0; i < 4; i++) {
			for (int j = 1; j <= 13; j++) {
				cin >> a[i][j];
			}
		}
		for (int i = 0; i < 9; i++) { cin >> b[i]; }
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < 5; j++) {
				cin >> S;
				for (int k = 0; k < 14; k++) {
					if (T[k] == S[0]) { d[j].first = k; }
				}
				for (int k = 0; k < 4; k++) {
					if (U[k] == S[1]) { d[j].second = k; }
				}
			}
			sort(d, d + 5);
			int A = b[poker()];
			int B = 0;
			for (int j = 0; j < 5; j++) {
				B += a[d[j].second][d[j].first];
			}
			cout << A*B << endl;
		}
	}
}
accuracy: 1
time_ms: 170
memory_kb: 1204
final_rank: 5

code_snippet: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

/* typedef */

typedef pair<int,int> pii;

/* global variables */

int bscs[4][13], mps[9];
string crds[5];
pii cs[5];

/* subroutines */

pii parse(string crd) {
  int d, s;

  switch (crd[0]) {
  case 'A': d = 0; break;
  case 'T': d = 9; break;
  case 'J': d = 10; break;
  case 'Q': d = 11; break;
  case 'K': d = 12; break;
  default: d = crd[0] - '1';
  }

  switch (crd[1]) {
  case 'S': s = 0; break;
  case 'C': s = 1; break;
  case 'H': s = 2; break;
  case 'D': s = 3; break;
  }

  return pii(d, s);
}

bool flush() {
  int s = cs[0].second;
  for (int i = 1; i < 5; i++)
    if (cs[i].second != s) return false;
  return true;
}

bool straight() {
  if (cs[0].first == 0 && cs[1].first == 9) {
    for (int i = 2; i < 5; i++)
      if (cs[i].first != 8 + i) return false;
    return true;
  }

  int d = cs[0].first;
  for (int i = 1; i < 5; i++)
    if (cs[i].first != d + i) return false;
  return true;
}

void pairs(int *pns) {
  int cns[13];
  memset(cns, 0, sizeof(cns));
  memset(pns, 0, sizeof(int) * 5);

  for (int i = 0; i < 5; i++) cns[cs[i].first]++;
  for (int i = 0; i < 13; i++) pns[cns[i]]++;
}

int hand() {
  bool sf = straight();
  bool ff = flush();

  if (sf && ff) {
    // Royal Straight Flush
    if (cs[0].first == 0 && cs[1].first == 9) return 8;
    // Straight Flush
    return 7;
  }

  int pns[5];
  pairs(pns);

  // Four Cards
  if (pns[4] == 1) return 6;
  // Full House
  if (pns[3] == 1 && pns[2] == 1) return 5;

  // Flush
  if (ff) return 4;

  // Straight
  if (sf) return 3;

  // Three Card
  if (pns[3] == 1) return 2;

  // Two Pair
  if (pns[2] == 2) return 1;

  // One Pair
  if (pns[2] == 1) return 0;

  // Buta
  return -1;
}

/* main */

int main() {
  for (bool first = true;; first = false) {
    int n;
    cin >> n;
    if (cin.eof()) break;
    
    for (int i = 0; i < 4; i++)
      for (int j = 0; j < 13; j++) cin >> bscs[i][j];

    for (int i = 0; i < 9; i++) cin >> mps[i];

    if (! first) cout << endl;
    
    while (n--) {
      for (int i = 0; i < 5; i++) {
	cin >> crds[i];
	cs[i] = parse(crds[i]);
      }
      sort(cs, cs + 5);

      int h = hand();
      int sc = 0;

      if (h >= 0) {
	int sum = 0;
	for (int i = 0; i < 5; i++)
	  sum += bscs[cs[i].second][cs[i].first];
	sc = sum * mps[h];
      }

      cout << sc << endl;
    }
  }

  return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 1212
final_rank: 6

code_snippet: #include <string> 
#include <algorithm> 
#include <cfloat> 
#include <climits> 
#include <cmath> 
#include <complex> 
#include <cstdio> 
#include <cstdlib> 
#include <cstring> 
#include <functional> 
#include <iostream> 
#include <map> 
#include <memory> 
#include <queue> 
#include <set> 
#include <sstream> 
#include <stack> 
#include <utility> 
#include <vector> 

#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define ALL(x) (x).begin(),(x).end() 
typedef long long ll;
using namespace std; 
const double eps = 1e-10;

int has[4][20];
int cost[4][20], mul[10];
int getVal(string s){
    if('2' <= s[0] && s[0] <= '9') return s[0]-'0';
    else if(s[0] == 'A') return 1;
    else if(s[0] == 'T') return 10;
    else if(s[0] == 'J') return 11;
    else if(s[0] == 'Q') return 12;
    else return 13;
}
int getMark(string s){
    if(s[1] == 'S') return 0;
    else if(s[1] == 'C') return 1;
    else if(s[1] == 'H') return 2;
    else return 3;
}
int st(){
    for(int i = 1; i <= 10; ++i){
        bool f = true;
        for(int k = 0; k < 5; ++k){
            bool h = false;
            for(int j=0;j<4;++j) h = h || has[j][i+k];
            f = f && h;
        }
        if(f) return i;
    }
    return 0;
}
int fl(){
    for(int i = 0; i < 4; ++i){
        int cnt = 0;
        for(int j = 1; j <= 13; ++j) if(has[i][j])++cnt;
        if(cnt == 5) return true;
    }
    return false;
}
int four(){
    for(int j = 1; j <= 13;++j){
        bool f = true;
        for(int i = 0; i < 4; ++i) f = f && has[i][j];
        if(f) return true;
    }
    return false;
}
int thr(){
    for(int j = 1; j <= 13;++j){
        int f = 0;
        for(int i = 0; i < 4; ++i) f += has[i][j];
        if(f == 3) return true;
    }
    return false;
}
int par(){
    int res = 0;
    for(int j = 1; j <= 13; ++j){
        int f = 0;
        for(int i = 0; i < 4; ++i) f += has[i][j];
        if(f == 2) ++res;
    }
    return res;
}
int judge(){
    int s = st();
    int f = fl();
    int fo = four();
    int th = thr();
    int p = par();
    // cout << ">" << p << endl;
    if(s && f) return s == 10? 9: 8;
    else if(fo) return 7;
    else if(th && p) return 6;
    else if(f) return 5;
    else if(s) return 4;
    else if(th) return 3;
    else return p;
}
void solve(const vector<string> &vs){
    ll sum = 0;
    memset(has, 0, sizeof(has));
    for(int i = 0; i < 5; ++i){
        int val = getVal(vs[i]);
        int mark = getMark(vs[i]);
        // cout << val << "," << mark << endl;
        has[mark][val]++;
        if(val == 1) has[mark][14]++;
        sum += cost[mark][val];
    }
    /*
    cout << ">" << sum << endl;
    for(int i = 0; i < 4; ++i){
        for(int j = 1; j <= 14; ++j) cout << has[i][j];
        cout << endl;
    }
    */
    cout << sum * mul[judge()] << endl;
}
int main(){
    bool flag = true;
    int N;
    for(;cin >> N;){
        if(!flag) cout << endl;
        for(int i = 0; i < 4; ++i) for(int j = 1; j <= 13; ++j)
            cin >> cost[i][j];
        for(int i = 1; i <= 9; ++i){
            cin >> mul[i];
        }
        for(int i = 0; i < N; ++i){
            vector<string> vs(5);
            for(int j = 0; j < 5; ++j) cin >> vs[j];
            solve(vs);
        }
        flag = false;
    }
    return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 1208
final_rank: 10

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
int c[4][15];
char st[7]="SCHD";
char nm[17]=".23456789TJQKA";
int d[20];
char in[4];
pair<int,int>p[10];
int calc(){
	bool flash=true;
	for(int i=0;i<5;i++)for(int j=i+1;j<5;j++)
		if(p[i].second!=p[j].second)flash=false;
	bool st=true;
	for(int i=0;i<4;i++)if(p[i].first+1!=p[i+1].first)st=false;
	bool st2=true;
	for(int i=0;i<4;i++)if(p[i].first!=i+1)st2=false;
	if(p[4].first!=13)st2=false;
	if(st2)st=true;
	if(st&&flash&&p[0].first==9)return d[8];
	if(st&&flash)return d[7];
	if(p[0].first==p[3].first||p[1].first==p[4].first)return d[6];
	if((p[0].first==p[1].first&&p[2].first==p[4].first)||(p[0].first==p[2].first&&p[3].first==p[4].first))return d[5];
	if(flash)return d[4];
	if(st)return d[3];
	if(p[0].first==p[2].first||p[1].first==p[3].first||p[2].first==p[4].first)return d[2];
	if((p[0].first==p[1].first&&p[2].first==p[3].first)||
	(p[0].first==p[1].first&&p[3].first==p[4].first)||
	(p[1].first==p[2].first&&p[3].first==p[4].first))return d[1];
	for(int i=0;i<4;i++)if(p[i].first==p[i+1].first)return d[0];
	return 0;
}
int main(){
	int a;
	bool tc=false;
	while(~scanf("%d",&a)){
		if(tc)printf("\n");
		for(int i=0;i<4;i++)for(int j=0;j<13;j++)
			scanf("%d",&c[i][j]);
		for(int i=0;i<4;i++)c[i][13]=c[i][0];
		for(int i=0;i<9;i++)scanf("%d",d+i);
		while(a--){
			int ks=0;
			for(int i=0;i<5;i++){
				scanf("%s",in);
				for(int j=1;j<=13;j++){
					if(in[0]==nm[j])p[i].first=j;
				}
				for(int j=0;j<4;j++){
					if(in[1]==st[j])p[i].second=j;
				}
				ks+=c[p[i].second][p[i].first];
			//	printf("(%d %d) ",p[i].first,p[i].second);
			}
			std::sort(p,p+5);
			int sc=calc();
			printf("%d\n",ks*sc);
		}
		tc=true;
	//	printf("\n");
	}
}
accuracy: 1
time_ms: 40
memory_kb: 1036
final_rank: 1

code_snippet: #include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <functional>

using namespace std;

int main() {
	bool hantei = false;
	int n;
	while (cin >> n) {
		if (hantei) {
			cout << endl;
		}
		hantei = true;
		int hyou[4][13];
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 13; j++) {
				cin >> hyou[i][j];
			}
		}
		int data[9];
		for (int i = 0; i < 9; i++) {
			cin >> data[i];
		}
		string a;
		string abc = "SCHD";
		for (int i = 0; i < n; i++) {
			int counter = 0;
			bool hantei2 = true;
			int p;
			priority_queue<int, vector<int>, greater<int> > pq;
			for (int j = 0; j < 5; j++) {
				cin >> a;
				int b;
				if (a[0] == 'A') {
					b = 1;
				} else if (a[0] == 'T') {
					b = 10;
				} else if (a[0] == 'J') {
					b = 11;
				} else if (a[0] == 'Q') {
					b = 12;
				} else if (a[0] == 'K') {
					b = 13;
				} else {
					b = a[0]-'0';
				}
				for (int k = 0; k < 4; k++) {
					if (a[1] == abc[k]) {
						if (j == 0) {
							p = k;
						} else {
							if (p != k) {
								hantei2 = false;
							}
						}
						counter += hyou[k][b-1];
						break;
					}
				}
				pq.push(b);
			}
			int cdata[5];
			for (int j = 0; j < 5; j++) {
				cdata[j] = pq.top();
				pq.pop();
			}
			if (cdata[0] == 1 && cdata[1] == 10 && cdata[2] == 11 && cdata[3] == 12 && cdata[4] == 13) {
				if (hantei2) {
					counter *= data[8];
				} else {
					counter *= data[3];
				}
			} else {
				if (cdata[0]+1 == cdata[1] && cdata[1]+1 == cdata[2] && cdata[2]+1 == cdata[3] && cdata[3]+1 == cdata[4]) {
					if (hantei2) {
						counter *= data[7];
					} else {
						counter *= data[3];
					}
				} else if ((cdata[0] == cdata[1] && cdata[0] == cdata[2] && cdata[0] == cdata[3]) || (cdata[1] == cdata[2] && cdata[1] == cdata[3] && cdata[1] == cdata[4])) {
					counter *= data[6];
				} else if ((cdata[0] == cdata[1] && cdata[2] == cdata[3] && cdata[2] == cdata[4]) || (cdata[0] == cdata[1] && cdata[0] == cdata[2] && cdata[3] == cdata[4])) {
					counter *= data[5];
				} else if (hantei2) {
					counter *= data[4];
				} else if ((cdata[0] == cdata[1] && cdata[0] == cdata[2]) || (cdata[1] == cdata[2] && cdata[1] == cdata[3]) || (cdata[2] == cdata[3] && cdata[2] == cdata[4])){
					counter *= data[2];
				} else if ((cdata[0] == cdata[1] && cdata[2] == cdata[3]) || (cdata[0] == cdata[1] && cdata[3] == cdata[4]) || (cdata[1] == cdata[2] && cdata[3] == cdata[4])) {
					counter *= data[1];
				} else if ((cdata[0] == cdata[1]) || (cdata[1] == cdata[2]) || (cdata[2] == cdata[3]) || (cdata[3] == cdata[4])) {
					counter *= data[0];
				} else {
					counter *= 0;
				}
			}
			cout << counter << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 1232
final_rank: 4

code_snippet: #include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

int arr[4][13], rank[9], card[5], cnt[13];
string str[5];

int getMark(char ch){
  if(ch == 'S'){
    return 0;
  }else if(ch == 'C'){
    return 1;
  }else if(ch == 'H'){
    return 2;
  }
  return 3;
}

int getNum(char ch){
  if('2' <= ch && ch <= '9'){
    return (ch-'0')-1;
  }else if(ch == 'A'){
    return 0;
  }else if(ch == 'T'){
    return 9;
  }else if(ch == 'J'){
    return 10;
  }else if(ch == 'Q'){
    return 11;
  }
  return 12;
}

int judge(){
  bool same_mark = true;
  char ch = str[0][1];
  for(int i = 1 ; i < 5 ; i++){
    if(ch != str[i][1]){
      same_mark = false;
      break;
    }
  }

  if(same_mark){
    if(card[0] == 1 && card[1] == 10 && card[2] == 11 &&
       card[3] == 12 && card[4] == 13){
      return rank[8];
    }else{
      bool check = true;
      for(int i = 1 ; i < 5 ; i++){
        if(card[i] - card[i-1] != 1){
          check = false;
          break;
        }
      }
      if(check){
        return rank[7];
      }else{
        return rank[4];
      }
    }
  }

  sort(cnt, cnt+13, greater<int>());
  if(cnt[0] == 4){
    return rank[6];
  }else if(cnt[0] == 3){
    if(cnt[1] == 2){
      return rank[5];
    }else{
      return rank[2];
    }
  }else if(cnt[0] == 2){
    if(cnt[1] == 2){
      return rank[1];
    }else{
      return rank[0];
    } 
  }else{
    bool check = true;
    for(int i = 1 ; i < 5 ; i++){
      if(card[i] - card[i-1] != 1){
        check = false;
        break;
      }
    }
    if(check){
      return rank[3];
    }
    if(card[0] == 1 && card[1] == 10 && card[2] == 11 &&
       card[3] == 12 && card[4] == 13){
      return rank[3];
    }
  }

  return 0;
}

int main(){
  int N;
  bool space = false;

  while(cin >> N){
    if(space){
      cout << endl;
    }else{
      space = true;
    }

    for(int i = 0 ; i < 4 ; i++){
      for(int j = 0 ; j < 13 ; j++){
        cin >> arr[i][j];
      }
    }
    for(int i = 0 ; i < 9 ; i++){
      cin >> rank[i];
    }

    while(N--){
      int point = 0, m,n;
      memset(cnt, 0, sizeof(cnt));
      for(int i = 0 ; i < 5 ; i++){
        cin >> str[i];
        m = getMark(str[i][1]);
        n = getNum(str[i][0]);
        point += arr[m][n];
        card[i] = n+1;
        cnt[n]++;
      }
      sort(card, card+5);
      cout << point*judge() << endl;
    }
  }

  return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 1220
final_rank: 2