Under construction

Still being built. Things may change

Blackjack

Time limit1sMemory limit128 MB

Summary
Given the exact order of the remaining deck, decide which hands to play, how much to bet, and when to hit or stand, to maximize total profit.
Level

Hard8 of 10

Topics
Dynamic programming, Game theory, Probability, Implementation
Solved
No attempts yet

Problem

You play blackjack against the dealer with a standard deck. The deck contains the cards 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, and ace; suits do not affect the game in any way. Cards 2 through 10 are worth their face value, jack, queen, and king are each worth 10, and each ace may count as either 1 or 11.

For a hand, its low value is the sum of all of its cards counting every ace as 1. Its high value is the largest sum that is at most 21, counting each ace as 1 or 11. (Two aces counted as 11 already reach 22, so at most one ace can be counted as 11.)

This is a simplified variant: there is no splitting, doubling, insurance, or natural blackjack bonus.

Your goal is to maximize your total profit (equivalently, to minimize your losses). A single hand is played as follows:

  • You place a bet of xx euros, where the minimum bet is pp and the maximum bet is qq, so p≤x≤qp \le x \le q.
  • You receive one card, then the dealer receives one card; then you receive a second card, and finally the dealer receives a second card.
  • While your low value is below 22, you may either Hit (take one more card from the deck) or Stand (take no more cards).
  • If your low value ever exceeds 21, you lose your bet and the hand ends immediately.
  • The dealer then plays: while the dealer's high value is below 17, the dealer takes another card.
  • If the dealer's low value exceeds 21 (the dealer busts), you receive 2x2x euros, for a profit of xx.
  • If the dealer's high value is greater than your high value, you lose your bet.
  • If the dealer's high value equals your high value, your bet is returned (no gain, no loss).
  • If the dealer's high value is less than your high value, you receive 2x2x euros, for a profit of xx.
  • After the hand, all used cards are discarded and never return to the deck.

If at any moment the deck is empty and either you choose to take a card or the dealer must take a card, the current hand is cancelled and your bet is returned.

The exact order of the remaining cards in the deck is known to you in advance. Cards are always dealt from the top of the deck in this fixed order, and you may play consecutive hands, stopping whenever you like (you may even stop before playing any hand). Determine the maximum total profit you can obtain by playing optimally.

Input

The first line contains an integer nn, the number of test cases. Each test case is given as follows:

  • One line with three integers cc, pp, and qq (0≤c≤100000 \le c \le 10000, 0≤p≤q≤1000 \le p \le q \le 100): the number of cards left in the deck, the minimum bet, and the maximum bet.
  • The next ⌈c/60⌉\lceil c/60 \rceil lines list the cc cards in order, using the characters 2-9, T, J, Q, K, A, where T denotes the 10 card. The first character of the first line is the card dealt first. Each of these lines contains exactly 60 characters, except possibly the last, which may contain fewer.

Output

For each test case, print a single line containing one integer: the maximum total profit obtainable by playing optimally.

Examples5

  1. Example 1

    Input
    1
    4 1 15
    TTA8
    
    Expected output
    15
    
  2. Example 2

    Input
    1
    4 3 7
    TTTT
    
    Expected output
    0
    
  3. Example 3

    Input
    1
    5 1 15
    6T57T
    
    Expected output
    15
    
  4. Example 4

    Input
    1
    5 4 20
    TTT68
    
    Expected output
    20
    
  5. Example 5

    Input
    3
    4 1 15
    TTA8
    4 3 7
    TTTT
    5 1 15
    6T57T
    
    Expected output
    15
    0
    15