Get Them All

Time limit1sMemory limit128 MB

Summary
Simulate the vehicle dispatch and routing rules to find when all contestants reach the contest site, or how many arrive by the time limit.
Level

Medium7 of 10

Topics
Simulation, Implementation, Queue
Solved
No attempts yet

Problem

To make sure the contestants can easily reach the regional contest site, the organizers have prepared several robot-driven vehicles. The vehicles visit nn predetermined junctions and carry the contestants waiting there to the contest. A computer-controlled Transportation Center (TC) decides the number of seats of each vehicle and the time at which each vehicle first leaves the contest site.

Whenever a new vehicle is needed, a request is sent to the TC. As long as the seat count stays above 3, each new vehicle has fewer seats than the previous one: the ii-th vehicle has max⁡(s−(i−1)⋅t, 3)\max(s - (i-1)\cdot t,\ 3) seats (i=1,2,3,…i = 1, 2, 3, \dots). The first vehicle leaves the contest site exactly at 8:00am (time 00). When the TC receives a request for a new vehicle, it prepares one, and exactly 22 seconds after receiving the request that vehicle leaves the contest site. If several requests arrive at the same time, only one of them is honored.

At junction jj each vehicle performs the tasks below. If more than one vehicle is at jj at the same time, they perform the tasks in order of their service time: the vehicle with the longest service time goes first. A vehicle's service time is the current time minus the time at which that vehicle first left the contest site (junction 00).

  1. If j=0j = 0 (the contest site), every contestant in the vehicle gets off. Otherwise the vehicle picks up as many contestants as it can (until the vehicle is full or no contestant is left at junction jj).

  2. If, after that, any contestant is still left at junction jj (j>0j > 0), the vehicle sends a request for a new vehicle to the TC.

  3. Finally the vehicle starts moving toward the next junction kk, which the robot-driver chooses as follows (this also applies at junction 00):

    • If the vehicle is full, k=0k = 0.
    • Otherwise, if no other vehicle has left junction jj yet, k=(j+1) mod nk = (j+1) \bmod n.
    • Otherwise, k=(k0+1) mod nk = (k_0+1) \bmod n if that value is different from jj.
    • Otherwise, k=(k0+2) mod nk = (k_0+2) \bmod n.
    • (Here k0k_0 is the "next junction" chosen by the most recent vehicle to leave junction jj.)

The three tasks above happen instantly (in 00 seconds). The time needed to travel from each junction to every other junction is given. All contestants have reached a suitable junction by 8:00am and do not leave until some vehicle picks them up. Given the number of contestants waiting at each junction and a time limit, determine the time at which everyone reaches the contest, or how many contestants have reached the contest by the time limit.

Input

The input consists of several datasets. Each dataset has the following form:

  • A line with the name of the set (2 to 20 alphanumeric characters).
  • A line with three positive integers nn, ss, and tt (2<n<112 < n < 11).
  • Then nn lines, each with n−1n-1 integers. The ii-th line (i=1,2,3,…i = 1, 2, 3, \dots) gives the time (in seconds) needed to travel from junction i−1i-1 to every other junction (all except i−1i-1), listed in order of destination index 0,1,2,…,n−10, 1, 2, \dots, n-1.
  • Then n−1n-1 lines, each with one non-negative integer. The ii-th line (i=1,2,3,…i = 1, 2, 3, \dots) is the number of contestants waiting at junction ii.
  • The last line of the dataset is the time limit (in seconds, less than 1000000010000000).

Integers on the same line are separated by exactly one space. The total number of contestants is at most 10001000.

The end of the input is marked by a line containing only TheEnd.

Output

For each set, print two lines. The first line is the name of the set exactly as it appears in the input. On the second line, if the time needed to bring all contestants to the contest does not exceed the given time limit, print that time (in seconds) as <time> seconds needed. Otherwise, print the number of contestants that reached the contest by the time limit as <count> contestants reached.

Examples5

  1. Example 1

    Input
    Dhaka2000
    3 22 4
    30 8
    10 30
    28 8
    20
    20
    100
    Dhaka2001
    3 22 4
    30 8
    10 30
    28 8
    20
    20
    90
    Dhaka2002
    3 22 2
    30 8
    10 30
    28 8
    20
    20
    100
    TheEnd
    
    Expected output
    Dhaka2000
    98 seconds needed
    Dhaka2001
    22 contestants reached
    Dhaka2002
    88 seconds needed
    
  2. Example 2

    Input
    Simple
    3 3 1
    1 1
    1 1
    1 1
    1
    1
    100
    TheEnd
    
    Expected output
    Simple
    3 seconds needed
    
  3. Example 3

    Input
    Quad
    4 10 1
    1 1 1
    1 1 1
    1 1 1
    1 1 1
    1
    1
    1
    100
    TheEnd
    
    Expected output
    Quad
    4 seconds needed
    
  4. Example 4

    Input
    BigCap
    3 40 5
    30 8
    10 30
    28 8
    15
    15
    1000
    TheEnd
    
    Expected output
    BigCap
    88 seconds needed
    
  5. Example 5

    Input
    Simple
    3 3 1
    1 1
    1 1
    1 1
    1
    1
    100
    BigCap
    3 40 5
    30 8
    10 30
    28 8
    15
    15
    1000
    TheEnd
    
    Expected output
    Simple
    3 seconds needed
    BigCap
    88 seconds needed