Under construction

Still being built. Things may change

Card Counting Club

Time limit8sMemory limit1024 MB

Summary
Given N hands of cards and a penalty P per mark, print the order in which players run out of cards in the counting-out game.
Level

Medium6 of 10

Topics
Heap, Simulation, Greedy
Solved
No attempts yet

Problem

The Card Counting Club wanted to combine its card counting practice with choosing the player order for its games. They settled on a counting-out game played as follows.

  1. At the start, each player is dealt a hand with the same number of cards as everyone else.
  2. Each turn, every player shows the card with the smallest value in their hand to the other players.
  3. The player with the smallest card discards it.
    1. Only one player discards their card. Ties are broken by choosing the player whose name comes first in standard dictionary (lexicographic) order.
    2. Every player who does not discard leaves a small mark on their card and returns it to their hand. Each mark increases the card's value by the agreed penalty amount P.
  4. A player who runs out of cards is counted out.
  5. The game continues until all players are counted out.

The order in which the players are counted out sets the order in which club members play other games.

Input

The first line contains three integers NN, MM, and PP (2≤N≤352 \leq N \leq 35, 1≤M≤170001 \leq M \leq 17000, 1≤P≤101 \leq P \leq 10). NN is the number of players, MM is the hand size of each player, and PP is the penalty added for each mark.

The next NN lines each contain a player's name, followed by MM space-separated integers x1,x2,…,xMx_1, x_2, \ldots, x_M (1≤xi≤2000001 \leq x_i \leq 200000) that are the card values in that player's hand. Values may repeat within a hand and across hands. Each name consists only of uppercase letters and has a length between 2 and 10.

Output

On a single line, print the player names in the order in which they are counted out during the game. Separate consecutive names with a single space.

Examples2

  1. Example 1

    Input
    3 3 3
    BOB 5 2 1
    EVE 1 4 3
    ALICE 3 4 2
    
    Expected output
    EVE ALICE BOB
    
  2. Example 2

    Input
    3 1 5
    WYNKEN 7
    BLYNKEN 7
    NOD 7
    
    Expected output
    BLYNKEN NOD WYNKEN