Relay Race
InterviewTime limit1sMemory limit128 MB
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 () cows, numbered through , taking part in an unusual relay race in which several cows may run at the same time.
Before time , 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 , cow starts running and crosses the starting line again exactly seconds later. In general, cow takes () seconds to complete one lap. The instant a cow crosses the starting line at the end of her lap, she signals () other cows 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 cows. The table lists each cow's id , her lap time , the number of cows she signals when she finishes, and the (possibly empty) list of those cows :
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 at time produces the following timeline of events:
The race therefore lasts seconds.
Input
- Line : a single integer .
- Lines : line contains the space-separated integers and , followed by the integers .
Output
- A single integer: the time at which the last cow finishes her lap.