Editorial
The four hole cards and three flop cards already occupy cards, so exactly cards are available for the turn or river. Since the two positions are ordered and must use different physical cards, there are only
possible turn-river pairs. We can check all of them directly.
For each pair, evaluate the final seven-card hand of each player. A poker hand uses five of those seven cards, so enumerate all
five-card subsets and take the strongest one.
A five-card hand can be encoded as an integer tuple
Give stronger categories larger category numbers, and store the remaining values in the exact tiebreak order used by Texas Hold'em. Then ordinary lexicographic comparison of the tuples determines which hand is stronger. For example, four of a kind can be represented as
When detecting straights, handle both ordinary Ace-high usage and the five-high wheel A2345. Fill the comparison tuple similarly for flushes, full houses, three of a kind, two pair, one pair, and high-card hands.
If the chosen turn is exactly the originally scheduled turn card, its cost is ; otherwise pay the cost of the chosen card's rank. The river is handled in the same way.
While scanning every turn-river pair, maintain the minimum cost among winning outcomes and the minimum cost among draws separately. If any winning outcome exists, output a minimum-cost winning choice. Otherwise output a minimum-cost draw if one exists. If neither exists, output -1.
The amount of work per test case is bounded by a fixed constant, so the time and extra space complexities per case are . Over the whole input, the running time is .
Solution written by GPT5.6