Under construction

Still being built. Things may change

Pseudominion (Small)

Time limit5sMemory limit512 MB

Summary
Choose the order to play hand and deck cards so draws and extra turns produce the largest total score before turns run out.
Level

Medium6 of 10

Topics
Greedy, Sorting, Simulation
Solved
No attempts yet

Problem

You are playing a card game. Every card carries three bonus numbers: a card bonus cc, a score bonus ss, and a turn bonus tt. Some cards start in your hand and the rest sit in a deck on the table. You start with 1 turn left and a score of 0.

On a turn you may pick any card from your hand and play it. If the card has bonus numbers cc, ss, tt, the following happens in order.

  • The card is discarded from your hand and can never be used again.
  • You draw the top cc cards of the deck into your hand. If the deck holds fewer than cc cards, you draw all of them.
  • Your score increases by ss.
  • Your number of remaining turns increases by tt.

Playing a card first spends 1 turn and then adds tt turns. If your hand is empty at the start of a turn, nothing happens on that turn. Once no turns remain, you cannot play another card. Report the largest score you can reach before running out of turns.

The walkthrough below shows how the rules interact. It uses a cc value larger than the limits allow.

Card in handccsstt
1002
2050
3211
Card in deck (draw order)ccsstt
4110
5011
6220

The table below traces a score of 8 from these cards. The first three columns give your hand, your remaining turns, and your score just before each play, and the last column gives the card you play on that turn.

HandTurns leftScorePlay
1, 2, 3101
2, 3203
2, 4, 5212
4, 5165
4174
608none

Card bonuses and turn bonuses let you chain a long run of cards together before you have to stop.

Input

The first line of the input gives the number of test cases, TT. TT test cases follow.

Each test case begins with a line containing NN, the number of cards in your hand. The next NN lines each contain three integers cc, ss, and tt, the bonus numbers of one card in your hand.

The next line contains MM, the number of cards in the deck. The next MM lines each contain three integers cc, ss, and tt, the bonus numbers of one card in the deck. The deck cards are listed in the order you draw them.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤N1 \le N
  • 0≤M0 \le M
  • N+M≤80N + M \le 80
  • 0≤c≤10 \le c \le 1
  • 0≤s≤200 \le s \le 20
  • 0≤t≤200 \le t \le 20

Output

For each test case, output one line containing Case #x: S, where xx is the test case number starting from 1 and SS is the largest score you can obtain before running out of turns.

Examples2

  1. Example 1

    Input
    2
    4
    1 0 0
    1 1 1
    0 5 0
    1 2 0
    0
    2
    1 1 1
    0 6 0
    1
    0 1 3
    
    Expected output
    Case #1: 6
    Case #2: 8
    
  2. Example 2

    Input
    1
    1
    0 0 0
    0
    
    Expected output
    Case #1: 0