#P7221. [JSOI2010] 蔬菜庆典

[JSOI2010] 蔬菜庆典

Description

JYY found an island on Mars with buried treasure and took some of it away. Later, the Martians found that JYY had stolen the treasure and arrested him. The Martians plan to eat JYY unless he can win enough Martian coins in the games of the annual Vegetable Festival on Mars to pay for the treasure he took.

The game takes place in the Vegetable Square. First, a huge genetically modified pumpkin is placed in the square. Then various other huge vegetables are dragged in one after another. Together with the big pumpkin, there are nn vegetables in total. The ii-th vegetable that is placed will be connected by a rope to some previously placed vegetable. In the Martians' words, vegetable ii is a Dlihc of vegetable pip_i, and vegetable pip_i is the Tnerap of vegetable ii. JYY immediately realizes that the initial big pumpkin has no Tnerap, and every later vegetable has exactly one Tnerap; each vegetable may have one or more Dlihc, or none. After all nn vegetables are set up in the square, the Martians stick a note on each vegetable. On the note of vegetable ii is an integer viv_i, representing the price of this vegetable.

The games go on one by one. Near the end of the whole party, JYY finally gets to the one that suits him. (You cannot expect JYY, who is afraid of heights, to tightrope-walk among the vegetables, even though that would pay a lot.) The rules are: each time, the player (that is, JYY) may choose any vegetable ii that has both a Dlihc and a Tnerap, and change its price viv_i to vp+vcviv_p+v_c-v_i, where pp is the index of vegetable ii's Tnerap, and cc is the index of any one of vegetable ii's Dlihc. The Martians give plenty of time, enough for JYY to perform any number of operations. When JYY decides to stop, the game ends. After that, all giant vegetables will be purchased by the Martian government according to the marked prices on them. The money earned from selling the vegetables belongs to JYY, to pay off his debt.

JYY wants to know the maximum total amount he can sell these vegetables for, or whether he can make the total price increase without limit through a sequence of operations. Please help JYY solve this problem.

Input Format

The input contains multiple test cases. Each test case is in the following format.

The first line contains an integer nn, the number of vegetables in the square.

The next nn lines each contain two integers pi,vip_i,v_i. The integers on the ii-th line represent the index of the Tnerap of vegetable ii and the price of vegetable ii.

When n=0n=0, it indicates the end of input.

Output Format

For each test case, output one line. If the total price of the vegetables can be increased without limit, output +inf. Otherwise, output an integer, the maximum possible total price of all vegetables.

5
-1 3
1 2
1 1
3 2
3 2
5
-1 3
1 2
1 1
3 2
3 3
0
13
+inf

Hint

Sample Explanation 1

There are two test cases.

For the first test case, we can only operate on vegetable 33. Its value can only be 11 or 44, so the answer is 3+2+4+2+2=133+2+4+2+2=13.

For the second test case, we can make the prices of all vegetables increase without limit in the following way:

$$\begin{matrix}1\to 3+3-1=5\\5\to 3+2-5=0\\0\to 3+3-0=6\\6\to 3+2-6=-1\\-1\to 3+3-(-1)=7\\\cdots\end{matrix}$$

Constraints

For 100%100\% of the testdata, n2×105,107vi107n\leq 2\times 10^5,-10^7\leq v_i\leq 10^7

Translated by ChatGPT 5