#ABC214D. [ABC214D] Sum of Maximum Weights

[ABC214D] Sum of Maximum Weights

提示:该题测试数据补录中,暂不支持评测提交。

No testdata at current.

No submission language available for this problem.

Score : 400400 points

Problem Statement

We have a tree with NN vertices numbered 1,2,…,N1, 2, \dots, N. The ii-th edge (1≤i≤N−1)(1 \leq i \leq N - 1) connects Vertex uiu_i and Vertex viv_i and has a weight wiw_i.

For different vertices uu and vv, let f(u,v)f(u, v) be the greatest weight of an edge contained in the shortest path from Vertex uu to Vertex vv.

Find $\displaystyle \sum_{i = 1}^{N - 1} \sum_{j = i + 1}^N f(i, j)$.

Constraints

  • 2≤N≤1052 \leq N \leq 10^5
  • 1≤ui,vi≤N1 \leq u_i, v_i \leq N
  • 1≤wi≤1071 \leq w_i \leq 10^7
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

u1u_1 v1v_1 w1w_1

⋮\vdots

uN−1u_{N - 1} vN−1v_{N - 1} wN−1w_{N - 1}

Output

Print the answer.

3
1 2 10
2 3 20
50

We have f(1,2)=10f(1, 2) = 10, f(2,3)=20f(2, 3) = 20, and f(1,3)=20f(1, 3) = 20, so we should print their sum, or 5050.

5
1 2 1
2 3 2
4 2 5
3 5 14
76