#P1113. [USACO02FEB] 杂务

[USACO02FEB] 杂务

Problem Description

Farmer John's family pitches in with the chores during milking, doing all the chores as quickly as possible. At FJ's house, some chores cannot be started until others have been completed, e.g., it is impossible to wash the cows until they are in the stalls.

Farmer John has a list of N(3N10,000)N (3 \leq N \leq 10,000) chores that must be completed. Each chore requires an integer time (1length of time100)(1 \leq \text{length of time} \leq 100) to complete and there may be other chores that must be completed before this chore is started. We will call these prerequisite chores. At least one chore has no prerequisite: the very first one, number 1. Farmer John's list of chores is nicely ordered, and chore K(K>1)K (K > 1) can have only chores 1K11 \dots K-1 as prerequisites. Write a program that reads a list of chores from 1 to N with associated times and all perquisite chores. Now calculate the shortest time it will take to complete all N chores. Of course, chores that do not depend on each other can be performed simultaneously.

Input Format

  • Line 11: One integer, NN.

  • Lines 2N+12 \dots N+1: NN lines, each with several space-separated integers. Line 22 contains chore 11; line 33 contains chore 22, and so on. Each line contains the chore index (1n)(1 \dots n), the length of time to complete the chore, and the prerequisites (range 1N1 \dots N, of course), the number of the prerequisites 100\leq 100.

Output Format

A single line with an integer which is the least amount of time required to perform all the chores.

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

23

Hint

Hint

Here is one task schedule:

  • Chore 11 starts at time 00, ends at time 55.
  • Chore 22 starts at time 55, ends at time 66.
  • Chore 33 starts at time 66, ends at time 99.
  • Chore 44 starts at time 55, ends at time 1111.
  • Chore 55 starts at time 1111, ends at time 1212.
  • Chore 66 starts at time 1111, ends at time 1919.
  • Chore 77 starts at time 1919, ends at time 2323.