#P1882. Relay Race Among Cows

    ID: 15165 Type: Default 1000ms 256MiB

Relay Race Among Cows

Relay Race Among Cows

There are \(N\) cows (\(1 \le N \le 1000\)) numbered from \(1\) to \(N\) participating in a special relay race. In this race, several cows can run concurrently.

At time \(t=0\), cow \(1\) starts running along the track. Normally, cow \(i\) takes \(L_i\) seconds (\(1 \le L_i \le 1000\)) to complete one lap. The moment cow \(i\) crosses the starting line again, she notifies \(M_i\) cows (possibly zero, i.e. \(M_i=0\)) with indices \(A_{ij}\) (for \(1 \le j \le M_i\)) to start running. Note that if a cow is notified more than once, she will only run once.

The total race time is defined as the time from the beginning of the race until the last cow (that runs) crosses the finish line. Compute the total race time.

inputFormat

The first line contains a single integer \(N\) representing the number of cows.

Then \(N\) lines follow. The \(i\)-th line contains two integers: \(L_i\) and \(M_i\), followed by \(M_i\) integers \(A_{i1}, A_{i2}, \dots, A_{iM_i}\). If \(M_i=0\), then no additional numbers appear on that line.

outputFormat

Output a single integer representing the total race time.

sample

1
5 0
5

</p>