This page is still under construction.

Parts of this page are still being built. What you see may change.

Bus

Time limit2sMemory limit256 MB

Summary
Pick one rider each day to pay the full rent so the largest overpayment above each rider's fair share is as small as possible.
Level

Medium7 of 10

Topics
Graph, Binary search
Solved
No attempts yet

Problem

A company runs one shuttle bus that takes employees home at the end of each day. To ride it, an employee registers online in advance. Every morning the company checks that the bus capacity is not exceeded and posts the list of riders for that day.

The rent of the bus is pp per day, and it does not depend on how many people ride. By a rule everyone accepted, exactly one of the people on the bus that day pays the whole rent to the driver. The name of the person who pays is announced daily as well.

Write a program that picks the person who pays on each day so that the assignment is fair in the sense defined below.

Let L1,L2,…,LdL_1, L_2, \dots, L_d be the lists of employees on the bus on day 1 through day dd, and let nin_i be the size of LiL_i. If an employee AA rides the bus on days t1,t2,…,tkt_1, t_2, \dots, t_k, the correct share of AA is

PA=p(1nt1+1nt2+⋯+1ntk)P_A = p \left( \frac{1}{n_{t_1}} + \frac{1}{n_{t_2}} + \cdots + \frac{1}{n_{t_k}} \right)

If AA is chosen to pay the rent rr times, AA actually pays QA=r×pQ_A = r \times p, which is EA=QA−PAE_A = Q_A - P_A more than the correct share. The unfairness of an assignment is the maximum of EAE_A over all employees AA. An assignment with the smallest unfairness is a fair assignment.

Input

The input holds several test cases. The first line of each test case has three positive integers nn, dd and pp, where nn is the number of employees, dd is the number of days, and pp is the daily rent of the bus (n,d≤500n, d \le 500, p≤109p \le 10^9).

The next dd lines describe one day each. Every such line starts with the number of employees on the bus that day, followed by that many employee IDs, each an integer between 11 and nn. No ID appears twice on the same line, and at least one employee rides on every day. To keep the arithmetic simple, pp is chosen so that the share of one employee is an integer on every day.

The last line of the input is 0 0 0 and must not be processed.

Output

For each test case, print the unfairness of a fair assignment on its own line.

Examples2

  1. Example 1

    Input
    3 2 1000
    2 1 2
    2 1 3
    4 4 3000
    2 1 2
    2 1 3
    2 2 3
    3 2 3 4
    0 0 0
    
    Expected output
    500
    2000
    
  2. Example 2

    Input
    4 1 12
    4 1 2 3 4
    0 0 0
    
    Expected output
    9