#P1882. 接力赛跑

接力赛跑

Description

There are N(1N1000)N (1\le N\le 1000) cows (numbered from 11 to NN) participating in a special relay race, where multiple cows are allowed to run simultaneously.

At time t=0t=0, cow 11 starts running along the track.

Normally, cow ii needs Li(1Li1000)L_i (1\le L_i\le 1000) seconds to complete one lap. At the instant it crosses the start line again, it will notify Mi(1MiN)M_i (1\le M_i\le N) cows Aij(1jMi)A_{ij} (1\le j\le M_i) to start. Note that it is possible that Mi=0M_i=0 and the list AiA_i does not exist.

Every cow that starts racing will, upon returning to the start line, notify other cows to start. It is possible that multiple cows notify the same cow to start, but each cow is willing to run only one lap, so any cow starts at most once.

The farmer wants you to determine the total race time (that is, from the race start to the moment when the last cow crosses the finish line).

Input Format

The first line contains an integer NN, the number of participating cows.

Lines 22 to N+1N+1: line i+1i+1 contains multiple space-separated integers: LiL_i, MiM_i, and MiM_i integers AijA_{ij}.

Output Format

Output a single integer, the time when the entire race ends.

5
4 2 2 4
3 3 1 3 4
7 1 5
4 2 3 5
1 0
14

Hint

Translated by ChatGPT 5