#P1796. 汤姆斯的天堂梦

汤姆斯的天堂梦

Description

Thomas lives on a planet at level 00. The environment there is extremely harsh: 1212 hours of work every day and piles of garbage are unbearable. He longs for the heavenly life on a planet at level NN.

There are flights that take people from a lower-level planet to a planet one level higher. Sometimes you need to pay a certain amount to the pilot, and sometimes you can receive some money.

Thomas knows in advance all routes from the level 00 planet to the level NN planet and the amount to pay (or receive) for each route. He wants to find a route with the lowest total price (or the highest total earnings).

Input Format

  • The first line contains a positive integer NN (N100N \le 100). The following data is divided into NN sections. In each section, the first line contains an integer KiK_i (Ki100K_i \le 100), indicating that there are KiK_i planets at level ii.
  • In the next KiK_i lines, the jj-th line describes the planets at level i1i - 1 that are connected to the planet at level ii with index jj, followed by the cost for each such flight (a positive number means paying, a negative number means earning; the absolute value does not exceed 10001000). Each line consists of several “index cost” pairs and ends with a single 00. Each line contains at most 100100 routes.

Output Format

Output the required (or obtained) total cost. A positive number means paying, and a negative number means earning.

3
2
1 15 0
1 5 0
3
1 -5 2 10 0
1 3 0
2 40 0
2
1 1 2 5 3 -5 0
2 -19 3 -20 0
-1

Hint

For 100%100\% of the testdata, 1N1001 \le N \le 100, 1Ki1001 \le K_i \le 100.

Sample explanation:

Translated by ChatGPT 5