PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000035. Tài liệu trích dẫn (Citations)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tài liệu trích dẫn (Citations)

Citations

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn books forming a citation tree rooted at book 11. Book ii needs kik_i 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 00. Minimize the sum of return times.

Input

The first line contains nn. For each i=1..ni=1..n, one line contains ki,fik_i,f_i followed by its fif_i child book indices. Every book except 11 appears exactly once as a child.

Output

Print the minimum total borrowing time.

Constraints

  • 1≤n≤1051\le n\le 10^5.
  • 1≤ki≤1091\le k_i\le 10^9 for 1≤i≤n1\le i\le n.
  • 0≤fi≤n−10\le f_i\le n-1 và ∑i=1nfi=n−1\sum_{i=1}^n f_i=n-1.
  • The child references form a rooted tree with root 11..

Subtasks

  • Subtask 1 (20 points): n≤8n\le 8.
  • Subtask 2 (30 points): n≤30n\le 30.
  • 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.