#include using namespace std; const string order = "AKQT98765432J"; inline int getval(string s) { sort(s.begin(), s.end()); vector groups; char last = '\0'; int nj = 0; for (char c : s) { if (c == 'J') { nj++; continue; } if (c != last) { groups.push_back(1); last = c; } else { groups.back()++; } } switch (groups.size()) { case 0: case 1: return 1; case 2: return (min(groups[0], groups[1]) == 1) ? 2 : 3; case 3: return (max(groups[0], max(groups[1], groups[2])) + nj == 3) ? 4 : 5; case 4: return 6; default: return 7; } } inline int geti(char c) { return find(order.begin(), order.end(), c) - order.begin(); } bool comp(pair& a, pair& b) { string aa = a.first, bb = b.first; int av = getval(aa), bv = getval(bb); if (av > bv) return true; if (av < bv) return false; for (int i = 0; i < 5; i++) { int ai = geti(aa[i]), bi = geti(bb[i]); if (ai > bi) return true; if (ai < bi) { return false; } } return false; } int main() { ios::sync_with_stdio(0); cin.tie(0); vector> hands; string line; while (getline(cin, line)) { istringstream ss(line); string h; int bid; ss >> h >> bid; hands.push_back(make_pair(h, bid)); } sort(hands.begin(), hands.end(), comp); int ret = 0; for (int i = 0; i < hands.size(); i++) { ret += hands[i].second * (i + 1); } cout << ret << '\n'; }