Tree Edit

Cut one weighted edge of a tree and reattach it elsewhere with the same weight; find the maximum possible diameter.

Medium7TreeDFSGreedyNo attempts yetTime limit2sMemory limit512 MB

Problem

A tree TT has NN vertices, numbered 00 through N1N-1.

  • Exactly one simple path connects any two vertices of a tree.
  • The distance between two vertices is the sum of the edge weights on that path.
  • The diameter of a tree is the largest distance over all pairs of vertices.

Hongjun deletes one edge of TT and then adds one edge. The added edge must have the same weight as the deleted edge, and the graph must still be a tree afterwards. He may add back exactly the edge he deleted.

Write a program that finds the largest diameter among the trees Hongjun can build.

Input

The first line contains the number of vertices NN. (2N20002 \le N \le 2000)

Each of the next N1N-1 lines contains one edge as three integers fromfrom, toto, and costcost, meaning the edge joining vertex fromfrom and vertex toto has weight costcost. (0from,toN10 \le from, to \le N-1, fromtofrom \ne to, 1cost1091 \le cost \le 10^9)

The given graph is always a tree.

Output

Print the largest diameter among the trees Hongjun can build.

Hint

The tree in the first example has 4 vertices and 3 edges. Its original diameter is the path between vertex 2 and vertex 3, of length 8+4=128 + 4 = 12. Delete the edge between vertex 1 and vertex 0, then join vertex 3 and vertex 1 with an edge of weight 2. The diameter becomes the path between vertex 2 and vertex 1, of length 8+4+2=148 + 4 + 2 = 14.

In the second example, adding back the deleted edge is optimal.