Islands

Time limit2sMemory limit128 MB

Summary
Each island has one undirected weighted edge; find the maximum-weight walk choosing one edge per component/tree path structure respecting ferry reachability rules.
Level

Hard8 of 10

Topics
Graph, Greedy, Union-find, Implementation
Solved
No attempts yet

Problem

You are visiting a park with NN islands, numbered 11 through NN. From each island ii, exactly one bridge was built, connecting island ii to some other island; that bridge has length LiL_i. There are therefore NN bridges in total. Although each bridge was built starting from one island, every bridge can be crossed in both directions. In addition, for every pair of islands there is a ferry that shuttles back and forth between them.

Because you enjoy walking more than riding ferries, you want to maximize the total length of the bridges you cross, subject to the following rules:

  • You may start on any island of your choice.
  • You may never visit the same island twice.
  • From your current island SS you may move to an island DD that you have not visited yet, in one of two ways:
    • Walk: allowed only if a bridge directly connects SS and DD. The bridge's length is added to your total walking distance.
    • Ferry: allowed only if DD is not reachable from SS using any combination of bridges and ferries you have already used. (When checking reachability, consider every path, including paths that pass through islands you have already visited.)

You do not have to visit every island, and it may be impossible to cross every bridge.

Given the NN bridges and their lengths, compute the maximum total distance you can walk while obeying the rules above.

Input

  • The first line contains the integer NN, the number of islands (2≤N≤1,000,0002 \le N \le 1{,}000{,}000). Islands are numbered from 11 to NN.
  • Each of the next NN lines describes one bridge. Line ii (for i=1,2,…,Ni = 1, 2, \dots, N) contains two space-separated integers: the island at the other endpoint of the bridge built from island ii, followed by that bridge's length LiL_i (1≤Li≤100,000,0001 \le L_i \le 100{,}000{,}000). The two endpoints of every bridge are always different islands.

Output

Print a single line containing one integer: the maximum possible total walking distance.

Note: for some inputs the answer does not fit in a 32-bit integer, so use a 64-bit integer type (for example long long in C/C++ or a normal integer in Python).

Note

In the sample, the N=7N = 7 bridges are (1-3)(1\text{-}3), (2-7)(2\text{-}7), (3-4)(3\text{-}4), (4-1)(4\text{-}1), (5-1)(5\text{-}1), (6-3)(6\text{-}3) and (7-2)(7\text{-}2). Note that there are two different bridges connecting islands 22 and 77.

One way to achieve the maximum walking distance is:

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

You finish on island 22 with a total walking distance of 9+8+4+3=249 + 8 + 4 + 3 = 24. The only island left unvisited is island 44, and you can no longer reach it: not by walking (there is no bridge between island 22 and island 44), and not by ferry (island 44 is reachable from island 22 via the bridge (2-7)(2\text{-}7), then the ferry you already used from island 77 to island 66, then the bridges (6-3)(6\text{-}3) and (3-4)(3\text{-}4)).

Examples1

  1. Example 1

    Input
    7
    3 8
    7 2
    4 2
    1 4
    1 9
    3 4
    2 3
    
    Expected output
    24