Under construction

Still being built. Things may change

Recruiting New SCCC Members

Interview

Time limit2sMemory limit1024 MB

Summary
Assign applicants in descending score order to K groups of capacity X, accepting each one only if all accepted applicants can still be seated, moving earlier ones if needed.
Level

Medium5 of 10

Topics
Greedy, Graph
Solved
No attempts yet

Problem

Chansol, the president of SCCC, is running the selection process for new members in the first semester of 2023. In that semester, SCCC runs KK study groups, each with a capacity of XX. Each new member must join exactly one of the KK study groups. An applicant who cannot join any study group cannot be accepted, so every applicant selects at least one study group they can join.

Chansol gave the NN applicants distinct scores after a fair and strict review. To assign the new members to study groups, he sorts the applicants from the highest score to the lowest and assigns them one at a time.

Consider the moment when the applicant with the ii-th highest score is about to be assigned. If the applicant can be added while keeping the set of people already assigned to the studies, the applicant is assigned. Otherwise, if no seat remains for the applicant no matter how the people already assigned are moved, the applicant cannot be assigned.

For example, let N=5N=5, K=2K=2, X=2X=2, and let the study groups each applicant can join, from the highest score, be {1},{1,2},{1},{1},{2}\{1\}, \{1,2\}, \{1\}, \{1\}, \{2\}.

Suppose the highest and second highest applicants are both assigned to group 1, and the third applicant is next. Move the second applicant to group 2, then assign the third applicant to group 1. The people already assigned remain in the study groups, so the third applicant is accepted.

On the other hand, suppose group 1 holds the first and third applicants and group 2 holds the second applicant. No arrangement lets the fourth applicant be assigned. The lowest-scoring applicant can still join group 2. The final result is that group 1 holds the first and third applicants, and group 2 holds the second and fifth applicants.

Chansol wants to assign the applicants by these rules. Write a program that finds the number of people assigned to study groups and the list of applicants in each study group.

Input

The first line contains the number of applicants NN, the number of study groups KK, and the capacity of each study group XX, separated by spaces.

From the second line onward, the ii-th of the NN lines gives the information for the ii-th applicant (1≤i≤N1\le i\le N).

Each line starts with the number of study groups CiC_i the applicant can join, followed by the CiC_i study group numbers Ai,1,Ai,2,⋯ ,Ai,CiA_{i,1},A_{i,2},\cdots ,A_{i,C_i}, separated by spaces.

The line N+2N+2 contains the scores B1,B2,⋯ ,BNB_1,B_2,\cdots ,B_N of the students, separated by spaces.

Output

Print KK lines. The ii-th line describes group ii (1≤i≤K1\le i\le K).

Each line starts with the number of people assigned to that study group, followed by the numbers of the assigned people, separated by spaces.

If several answers exist, you may print any of them. The order of the assigned people in each line does not matter.

Constraints

  • 1≤N,K,X≤151\leq N,K,X\leq 15
  • 1≤K×X≤151\leq K\times X\leq 15
  • 1≤Ci≤K1\leq C_i\leq K
  • 1≤Ai,j≤K1\leq A_{i,j}\leq K (1≤i≤N,1≤j≤Ci)(1\le i\le N,1\le j\le C_i)
  • If j≠kj\ne k, then Ai,j≠Ai,kA_{i,j}\ne A_{i,k}. (1≤i≤N,1≤j,k≤Ci)(1\le i\le N,1\le j,k\le C_i) In other words, every element in a row is distinct.
  • 1≤Bi≤1091\leq B_i\leq 10^9 (1≤i≤N)(1\le i\le N)
  • If i≠ji\ne j, then Bi≠BjB_i\ne B_j. (1≤i,j≤N)(1\le i,j\le N) In other words, every element of BB is distinct.
  • All numbers in the input are integers.

Examples1

  1. Example 1

    Input
    5 2 2
    1 1
    2 1 2
    1 1
    1 1
    1 2
    5 4 3 2 1
    
    Expected output
    2 1 3
    2 2 5