This page is still under construction.

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

Relay Race

Interview

Time limit1sMemory limit128 MB

Summary
Each cow runs one lap, then signals other cows to start; find the time when the last cow finishes, ignoring repeat signals.
Level

Medium5 of 10

Topics
Graph, BFS, Simulation, Queue
Solved
No attempts yet

Problem

There are NN (1≤N≤10001 \le N \le 1000) cows, numbered 11 through NN, taking part in an unusual relay race in which several cows may run at the same time.

Before time t=0t = 0, every cow waits at the starting line. Each cow runs exactly one lap around a circular track whose finish line is the same as its starting line.

At time t=0t = 0, cow 11 starts running and crosses the starting line again exactly L1L_1 seconds later. In general, cow ii takes LiL_i (1≤Li≤10001 \le L_i \le 1000) seconds to complete one lap. The instant a cow crosses the starting line at the end of her lap, she signals MiM_i (0≤Mi≤N0 \le M_i \le N) other cows Ai1,Ai2,…,AiMiA_{i1}, A_{i2}, \dots, A_{iM_i} to start running immediately.

Each signaled cow begins her own lap at that moment and, when she finishes, performs her own signaling. A cow may be signaled by several different cows, but she runs only one lap, so every signal after the first one she receives is ignored. Every cow is guaranteed to be signaled at least once.

Determine the total race time: the moment at which the last cow finishes her lap.

Consider a race with 55 cows. The table lists each cow's id ii, her lap time LiL_i, the number of cows MiM_i she signals when she finishes, and the (possibly empty) list of those cows Ai∗A_{i*}:

i   L_i  M_i   A_i*
1    4    2    2 4
2    3    3    1 3 4
3    7    1    5
4    4    2    3 5
5    1    0

Starting cow 11 at time 00 produces the following timeline of events:

TimeEvent
0Cow 1 starts running
4Cow 1 finishes and signals cows 2 and 4
4Cow 2 starts running (finishes at 4 + 3 = 7)
4Cow 4 starts running (finishes at 4 + 4 = 8)
7Cow 2 finishes and signals cows 1, 3, and 4
7Cows 1 and 4 ignore the repeated signal
7Cow 3 starts running (finishes at 7 + 7 = 14)
8Cow 4 finishes and signals cows 3 and 5
8Cow 3 ignores the repeated signal
8Cow 5 starts running (finishes at 8 + 1 = 9)
9Cow 5 finishes and has no cow to signal
14Cow 3 finishes and signals cow 5
14Cow 5 ignores the repeated signal
14All cows have finished

The race therefore lasts 1414 seconds.

Input

  • Line 11: a single integer NN.
  • Lines 2…N+12 \dots N+1: line i+1i+1 contains the space-separated integers LiL_i and MiM_i, followed by the MiM_i integers Ai1,…,AiMiA_{i1}, \dots, A_{iM_i}.

Output

  • A single integer: the time at which the last cow finishes her lap.

Examples3

  1. Example 1

    Input
    5
    4 2 2 4
    3 3 1 3 4
    7 1 5
    4 2 3 5
    1 0
    
    Expected output
    14
    
  2. Example 2

    Input
    1
    5 0
    
    Expected output
    5
    
  3. Example 3

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