Citations

Order the reading of a citation tree rooted at book 1 so that the sum of all book return times is minimized.

Hard8TreeGreedyDFSSortingInterviewNo attempts yetTime limit1sMemory limit1024 MB

Problem

Grace wants to read one science book. To understand it fully, she reads every book it cites, then every book those books cite, and so on. There are NN books in total, numbered from 11 to NN. Reading book ii itself and returning it takes KiK_i minutes. Book ii contains a citation list of FiF_i books. The book she originally wanted to read is book 11. Every book except book 11 appears in exactly one citation list, and there is no cycle of citations. Hence the citations form a tree rooted at book 11.

Reading one book proceeds as follows.

  • Open the book and read its citation list, which takes 11 minute.
  • Read all books in the list, in any order she chooses.
  • Read the main text and return the book, which takes KiK_i minutes.

All books are already borrowed at time 00. The borrow time of book ii is the moment it is returned. Choose the reading order so that the sum of borrow times over all books is minimized.

Input

The first line contains the integer NN (1N1000001 \le N \le 100000). The next NN lines describe books i=1i = 1 through NN in order. Each line contains KiK_i (1Ki10001 \le K_i \le 1000), FiF_i (0Fi<N0 \le F_i < N), followed by FiF_i book numbers cited by book ii. Every book number except 11 appears exactly once across all citation lists.

Output

Print a single positive integer, the minimum possible total borrow time over all books.

Hint

Since every book is borrowed at time 00, the answer equals the sum of the moments at which the books are closed. For a book uu with children vv, the total time of its subtree does not depend on the order and equals T(u)=1+Ku+T(v)T(u) = 1 + K_u + \sum T(v), so the whole process always takes N+KiN + \sum K_i. The problem therefore reduces to choosing the order of children at each node, and the total is the sum inside the child subtrees plus the waiting time between siblings.