Shut the Box I

Interview

Time limit1sMemory limit128 MB

Summary
Given a target sum and a sorted list of open card values, choose the subset summing to the target that is lexicographically largest when sorted.
Level

Medium4 of 10

Topics
Backtracking, Array, Sorting, Brute force
Solved
No attempts yet

Problem

The Avengers' pursuit of the Collector has taken them on a long voyage through the galaxies, and they look for ways to pass the time on their ship. They discover an old game called Shut the Box, once popular with sailors.

Each player starts with 9 cards numbered 11 through 99, laid face-up on the table, and keeps playing until no move is possible.

The game uses a pair of dice, with one twist: only a single die is used when the total of the open (face-up) cards is ≤6\le 6. Initially every card is face-up. The player rolls the dice; let the total be mm. The player then chooses any set of open cards whose face values sum to exactly mm and shuts (turns face-down) that set.

For example, if the player rolls a 66 and a 22 (total 88) on the first turn, they may shut the card 88, or the cards 11 and 77, or the cards 22 and 66, or the three cards 1,2,51, 2, 5, and so on. As another example, if the open cards are 1,2,61, 2, 6 and the player rolls a 44, then no set of cards sums to 44, so no move is possible and the turn is over.

The final score is the sum of the cards still face-up; the aim is to make the score as low as possible (ideally to "shut the box", leaving no card face-up). In the case above that ends with 1,2,61, 2, 6 still open, the final score is 1+2+6=91 + 2 + 6 = 9.

Illustration of the Shut the Box game

Figure 1: Illustration of the Shut the Box game in two cases (in the right example, only the last few moves are shown). Note that only one die is used when the total of the open cards is ≤6\le 6 (the right example).

Dr. Banner (the Hulk) follows this strategy. Consider every valid set of cards that can be shut for the current roll:

  • If there is only one, take it.
  • If there are several, take the one that maximizes the smallest face value. For example, given the options {1,7}\{1, 7\} and {2,6}\{2, 6\}, take {2,6}\{2, 6\}.
  • If several still tie on the smallest face value, take the one that maximizes the second-smallest face value. For example, given {2,4,6}\{2, 4, 6\} and {2,3,7}\{2, 3, 7\}, take {2,4,6}\{2, 4, 6\}.

In general, among the valid options, select the one whose face values, listed in ascending order, form the lexicographically largest sequence.

Write a program that makes this choice for Dr. Banner (you know what happens when the Hulk is angered).

Input

The first line contains the number of test cases TT (with T<100T < 100).

Each of the next TT lines describes one test case. The line begins with the rolled total (the target), followed by the number of open cards nn, followed by the nn open card values in ascending order.

Output

For each test case, output one line.

If at least one valid move exists, print the best move (per the strategy above) in this exact form:

The best move is: c1 c2 ... ck

where c1,c2,…,ckc_1, c_2, \dots, c_k are the chosen card values in ascending order, separated by single spaces.

If no set of open cards sums to the target, print exactly:

No move found.

Examples3

  1. Example 1

    Input
    5
    10 9 1 2 3 4 5 6 7 8 9
    12 9 1 2 3 4 5 6 7 8 9
    7 6 1 2 3 4 8 9
    8 7 1 2 3 4 5 6 7 
    8 4 1 2 3 9
    
    Expected output
    The best move is: 4 6
    The best move is: 5 7
    The best move is: 3 4
    The best move is: 3 5
    No move found.
    
  2. Example 2

    Input
    1
    5 3 3 5 7
    
    Expected output
    The best move is: 5
    
  3. Example 3

    Input
    1
    6 6 1 2 3 4 5 6
    
    Expected output
    The best move is: 6