#P7232. [JSOI2014] 支线剧情 2

[JSOI2014] 支线剧情 2

Description

The otaku JYY really likes playing RPG games, and JYY always wants to spend the least time to watch all side plots. Recently, JYY bought a new genuine RPG game, so JYY can finally “load” and “save”!

In the RPG game JYY is playing now, there are nn plot points, numbered from 11 to nn. At plot point ii, depending on JYY’s choices, JYY may go through different side plots and reach kik_i different new plot points. Of course, if kik_i is 00, it means that plot point ii is an ending of the game.

Watching a side plot takes some time. JYY starts at plot point 11, which is the beginning of the game. This game is special: from the beginning to any plot point, the side-plot path is unique. Also, every plot point is reachable from plot point 11.

Just like in the previous RPG game, JYY can choose to restart the game at any time, i.e. return to plot point 11. However, something is different this time: since JYY bought the genuine software, whenever JYY reaches a plot point, JYY can “save” and “load”. Unfortunately, this RPG game has only one save slot: each “save” operation overwrites the previous save. For example, if JYY performs “save” at plot point xx, then afterward, when JYY reaches any plot point (including an ending), JYY can “load” and immediately return to plot point xx. But if later JYY performs “save” at plot point yy, then when JYY “loads” again, JYY can only return to plot point yy.

At the beginning, the save slot stores no information, so JYY can only “load” after performing a “save” at least once. Also, “load”, “save”, and “restart” take no time.

Rewatching plots that have already been watched is very painful. JYY wants to spend the least time to watch all different side plots.

Input Format

The first line contains a positive integer nn.

The next nn lines describe the information of plot points. Line ii describes plot point ii.

In each line, the first non-negative integer is kik_i. Then follow kik_i pairs of integers bi,jb_{i,j} and ti,jt_{i,j}, meaning that from plot point ii one can go to plot point bi,jb_{i,j}, and watching this side plot takes ti,jt_{i,j} time.

Output Format

Output one integer in one line, the minimum time required for JYY to finish watching all side plots.

9
2 5 1 2 1
2 3 1 6 1
2 7 1 4 1
2 8 1 9 1
0
0
0
0
0
8

Hint

Sample Explanation 1

The best route is:

Go from 11 to 55, then restart.

Go from 11 to 22, save at 22, then go to 66, and load.

Go from 22 to 33, save at 33, then go to 77, and load.

Go from 33 to 44, save at 44, then go to 88, and load.

Finally go to 99. The total time is 88. It can be proven that there is no answer smaller than 88.

Constraints

For 100%100\% of the testdata, n106n\leq 10^6, 1ti,j1041\leq t_{i,j}\leq 10^4, i=1nki=n1\sum\limits_{i=1}^{n}k_i=n-1.

Translated by ChatGPT 5