cho.sh
Notes
Loading...

Tree Diameter

Time limit

2s

Memory limit

256 MB

Problem

In a tree, the distance between two vertices is the sum of the edge weights on the unique path connecting them. The diameter of a tree is the maximum distance over all pairs of vertices. Given a weighted tree, compute its diameter.

Input

The first line contains the number of vertices V (2 <= V <= 100000). The vertices are numbered from 1 to V.

The next V lines describe the edges adjacent to each vertex. Each line starts with a vertex number, followed by zero or more pairs neighbor distance. The line ends with -1. Every distance is a positive integer no greater than 10000.

Output

Print the diameter of the tree.