atcoder#AGC018D. [AGC018D] Tree and Hamilton Path

[AGC018D] Tree and Hamilton Path

Score : 11001100 points

Problem Statement

There is a tree with NN vertices, numbered 11 through NN. The ii-th edge in this tree connects Vertices AiA_i and BiB_i and has a length of CiC_i.

Joisino created a complete graph with NN vertices. The length of the edge connecting Vertices uu and vv in this graph, is equal to the shortest distance between Vertices uu and vv in the tree above.

Joisino would like to know the length of the longest Hamiltonian path (see Notes) in this complete graph. Find the length of that path.

Notes

A Hamiltonian path in a graph is a path in the graph that visits each vertex exactly once.

Constraints

  • 2N1052 \leq N \leq 10^5
  • 1Ai<BiN1 \leq A_i < B_i \leq N
  • The given graph is a tree.
  • 1Ci1081 \leq C_i \leq 10^8
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 B1B_1 C1C_1

A2A_2 B2B_2 C2C_2

::

AN1A_{N-1} BN1B_{N-1} CN1C_{N-1}

Output

Print the length of the longest Hamiltonian path in the complete graph created by Joisino.

5
1 2 5
3 4 7
2 3 3
2 5 2
38

The length of the Hamiltonian path 5533114422 is 5+8+15+10=385+8+15+10=38. Since there is no Hamiltonian path with length 3939 or greater in the graph, the answer is 3838.

8
2 8 8
1 5 1
4 8 2
2 5 4
3 8 6
6 8 9
2 7 12
132