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

#GD0000224. Tổng trọng số lớn nhất trên đường đi (Sum of Maximum Weights)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng trọng số lớn nhất trên đường đi (Sum of Maximum Weights)

Sum of Maximum Weights

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a weighted tree with vertices numbered 11 to nn, and all edge weights are positive integers. For two different vertices u,vu,v, let f(u,v)f(u,v) be the maximum edge weight along their unique simple path.

Compute the sum of f(u,v)f(u,v) over every unordered pair 1≤u<v≤n1\le u<v\le n.

Input

  • The first line contains integer nn.
  • Each of the following n−1n-1 lines contains ui,vi,wiu_i,v_i,w_i, describing one tree edge.

Output

Print one integer: the requested sum.

Subtasks

  • Subtask 1 (20 points): 2≤n≤202\le n\le 20; 1≤ui,vi≤n, ui≠vi1\le u_i,v_i\le n,\ u_i\ne v_i; 1≤wi≤1071\le w_i\le 10^7; các cạnh tạo thành cây.
  • Subtask 2 (30 points): 2≤n≤20002\le n\le 2000; 1≤ui,vi≤n, ui≠vi1\le u_i,v_i\le n,\ u_i\ne v_i; 1≤wi≤1071\le w_i\le 10^7; các cạnh tạo thành cây.
  • Subtask 3 (50 points): 2≤n≤1000002\le n\le 100000; 1≤ui,vi≤n, ui≠vi1\le u_i,v_i\le n,\ u_i\ne v_i; 1≤wi≤1071\le w_i\le 10^7; các cạnh tạo thành cây.

Examples

Input

3
1 2 10
2 3 20

Output

50

Explanation

The three pairs are (1,2)(1,2) with value 1010, (2,3)(2,3) with value 2020, and (1,3)(1,3), whose path contains weights 10,2010,20 and thus has value 2020. Their sum is 10+20+20=5010+20+20=50.