14463 - Take 6!   

Description

"Take 6" (also known as "6 Nimmt!") is a card game consisting of 104 cards, each with a number (ranging from 1 to 104) and a certain number of cattle head symbols. The number of cattle headson a card varies, with higher-numbered cards typically carrying more cattle heads. Players aim to finish the game with the fewest cattle heads, as each cattle head counts as a penalty point. The game is played in rounds.

Each card in the game has a certain number of "cattle heads" (penalty points). The distribution of cattle heads across the cards is as follows:

  • 1 card with 7 cattle heads—number 55
  • 8 cards with 5 cattle heads—multiples of 11 (except 55): 11, 22, 33, 44, 66, 77, 88, 99
  • 10 cards with 3 cattle heads—multiples of ten: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
  • 9 cards with 2 cattle heads—multiples of five that are not multiples of ten (except 55): 5, 15, 25, 35, 45, 65, 75, 85, 95
  • 76 cards with 1 cattle head—the rest of the cards from 1 through 104

Game Rules

  • Each player is dealt M cards, and the game consists of M rounds.
  • The game starts with P rows of cards, each row beginning with a single starting card.
  • During each round:
    • All players simultaneously choose a card from their hand to play.
    • Once revealed, the cards are arranged in ascending order based on their values, starting with the smallest number.
    • Each card must be placed in a row where it is higher than the last card in that row but closest in value.
    • If a row already contains five cards, the player placing the sixth card must take all five cards in that row as a penalty. Their card then becomes the new first card in that row.
    • If a player's card is smaller than the last card in any row, the player must take all the cards from the row with the highest last card (as a penalty) and place their card as the new first card of that row.
  • The game continues until all players have played all their cards.

The goal is to avoid collecting cattle heads, as they represent penalty points. The player with the fewest cattle heads at the end of the game wins.

Input

The input starts with a line containing three integers , , and P , which denote the number of players, the number of cards each player holds, and the number of rows on the table.

The next P lines each contain one integer Di​ , representing the starting card in row i on the table.

The following N lines each contain M integers Ci,1 ,Ci,2 ,...,Ci,M , where Ci,j​ represents the card that player i plays in round j. Each player will play one card per round.

 

Constraints

  • 2 ≤ ≤ 10
  • 1 ≤ M ≤ 50
  • 1 ≤ N×M ≤ 100
  • 1 ≤ P ≤ 4
  • 1 ≤ Di , Ci,j ≤ N×M + P
  • for all triple (i, j, k) , Di  Cj,k

Subtasks

  • Testcases 1~2: P = 1
  • Testcases 3~4: M = 1
  • Testcases 5~6: N = 2
  • Testcases 7~10: No additional restrictions.

Output

The output should consist of N lines, where each line i contains a single integer representing the total number of cattle head for player i at the end of the game. Each player's cattle head count reflects the penalties incurred throughout the game.

 

Sample explanation

At the beginning, there are two rows of cards on the table, with 11 and 6 cards respectively.

In the first round, the four players each play a card: 12, 1, 7, and 8.

Player 2 (green) plays card 1 first. Since there are no cards on the table smaller than 1, this player must take all the cards from the row with the highest last card.The player takes the card numbered 11 and earns 5 cattle heads.

Next, the cards are placed in order: 7 is placed after 6, 8 is placed after 7, and 12 is placed after 8.

In the second round, the four players each play a card: 5, 13, 2, and 14.

Player 3 (purple) plays card 2 first, and the cards are placed in order: 2 is placed after 1, 5 is placed after 2, 13 is placed after 12, and 14 is placed after 13.

Since Player 4 (gray)’s card (14) is the sixth card in the row, as a penalty, they must take the previous five cards in the row and earn 5 cattle heads. 

The two new rows will become the ones shown in the image below.

In the third round, the four players each play a card: 9, 10, 4, and 3. 

Player 4 (gray) plays card 3 first, but since there are no cards on the table smaller than 3, this player must take all the cards from the row with the highest last card.

The player takes the card numbered 14 and earns 1 cattle head.

Next, the cards are placed in order: 4 is placed after 3, 9 is placed after 5, and 10 is placed after 9.

Sample Input  Download

Sample Output  Download

Tags




Discuss