#GD0000035. Tài liệu trích dẫn (Citations)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tài liệu trích dẫn (Citations)
Citations
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
There are books forming a citation tree rooted at book . Book needs minutes for its content; opening it costs one additional minute to read its citation list. All cited child books must then be read recursively in an order of your choice, after which the book itself is read and returned. All books are borrowed at time . Minimize the sum of return times.
Input
The first line contains . For each , one line contains followed by its child book indices. Every book except appears exactly once as a child.
Output
Print the minimum total borrowing time.
Constraints
- .
- for .
- và .
- The child references form a rooted tree with root ..
Subtasks
- Subtask 1 (20 points): .
- Subtask 2 (30 points): .
- Subtask 3 (50 points): Full constraints.
Examples
Input
3
2 2 2 3
1 0
3 0
Output
19
Explanation
The sample follows the ordering rule described in the Vietnamese explanation and produces the shown optimum.