Islands#275

Islands

5 seconds
256 megabytes
Hard

You are visiting a park that contains islands. From each island , exactly one bridge has been constructed. The length of the bridge from island is denoted by L_i. The total number of bridges in the park is . Although each bridge was built to connect two islands, now each bridge can be traversed in both directions. In addition, for each pair of islands, there is a unique ferry running between them in both directions.

Since you prefer walking to taking the ferry, you want to maximize the sum of the lengths of the bridges you cross, subject to the rules below.

  1. You can start your walk from any island of your choice.

  2. You may not visit any island more than once.

  3. At any time, you may move from your current island to another island that you have not yet visited. You can move from to in the following ways:

    1. Walking: This is only possible if there is a bridge between the two islands. In this case, the length of the bridge is added to your walking total.

    2. Ferry: You can use the ferry only if it is not possible to get from to using any combination of bridges and/or previously used ferries, even by passing through islands you have already visited. Note that you do not have to visit all islands, and it may not be possible to cross all bridges.

Given the bridges and their lengths, write a program that calculates the maximum total walking distance you can achieve, following the rules above.

2 \leq N \leq 1\,000\,000
1 \leq L_i \leq 100\,000\,000

Input

Your program should read from standard input:

  1. The first line contains the integer — the number of islands in the park. Islands are numbered from to .

  2. The next lines each describe a bridge. The -th of these lines describes the bridge from island with two space-separated integers: the first integer is the index of the other island the bridge connects to, and the second integer is L_i, the length of the bridge. The endpoints of each bridge are always two different islands.

Output

Output a single integer — the maximum possible total walking distance.

Examples

Example 1

Loading
Loading

Note

Note 1. For some test cases, the answer may not fit in a -bit integer. Use in Pascal or long~long in C/C++ to get full points for this problem.

Note 2. In the contest environment, -bit integer types are read much more slowly in Pascal than -bit types, even if the value fits in bits. It is recommended to use -bit types for reading input. For some cases worth points, does not exceed .