#graph402. Island

Island

Island

Problem Source

IOI 2008

Problem Description

You are going to tour a park consisting of NN islands. The local administration has built from each island ii a bridge of length LiL_i to another island, though the bridge can be walked in both directions. In addition, for every pair of islands there is a dedicated ferry running between the two. Compared to taking a ferry, you prefer walking. You want the total length of bridges you walk to be as large as possible, subject to the following restrictions:

  • You may freely choose an island to start the tour.
  • No island may be visited more than once.
  • At any time, from your current island SS you may go to another not-yet-visited island DD by either:
    • Walking: only possible when there is a bridge between SS and DD. In this case, the bridge's length is added to your total walking distance.
    • Ferry: you may choose this option only when there is no combination of bridges and previously used ferries that can take you from SS to DD (when checking reachability, you should consider all paths, including those through islands you have already visited).

Note that you do not have to visit all islands, and you may not be able to walk all the bridges.

Write a program that, given the NN bridges and their lengths, computes the maximum sum of bridge lengths you can walk under the rules above.

Input Format

The first line contains one integer NN (2≤N≤1062 \leq N \leq 10^6), the number of islands in the park.

Then NN lines follow, each describing one island. The ii-th line contains two integers separated by a single space, describing the bridge built from island ii. The first integer is the island at the other end of the bridge, and the second integer is the bridge's length LiL_i (1≤Li≤1081 \leq L_i \leq 10^8). You may assume that for every bridge its endpoints are always on different islands.

Output Format

Output a single integer: the maximum possible walking distance.

Sample Input

7
3 8
7 2
4 2
1 4
1 9
3 4
2 3

Sample Output

24

Sample Explanation

sample illustration

The sample has N=7N = 7 bridges, namely (1−3),(2−7),(3−4),(4−1),(5−1),(6−3)(1-3), (2-7), (3-4), (4-1), (5-1), (6-3) and (7−2)(7-2). Note that two different bridges connect islands 22 and 77.

One way to achieve the maximum walking distance is:

  • Start at island 55.
  • Walk the bridge of length 99 to island 11.
  • Walk the bridge of length 88 to island 33.
  • Walk the bridge of length 44 to island 66.
  • Take the ferry from island 66 to island 77.
  • Walk the bridge of length 33 to island 22.

Finally you arrive at island 22, and your total walking distance is 9+8+4+3=249 + 8 + 4 + 3 = 24.

Only island 44 is not visited. Note that at the end of the tour above you cannot visit this island. More precisely:

  • You cannot walk there, because there is no bridge connecting island 22 (your current island) and island 44.
  • You cannot take a ferry there, because you can already reach island 44 from your current island 22. One such way: walk the (2−7)(2-7) bridge, then take the ferry you already used from island 77 to island 66, then walk the (6−3)(6-3) bridge, and finally walk the (3−4)(3-4) bridge.