eolymp
bolt
Try our new interface for solving problems
Problems

Islands

Islands

\includegraphics{https://static.e-olymp.com/content/8e/8e76e21e23f0b735c19f0a5c00759fdbba7280ba.jpg} You are visiting a park which has \textbf{N} islands. From each island i, exactly one bridge was constructed. The length of that bridge is denoted by \textbf{L_i}. The total number of bridges in the park is \textbf{N}. Although each bridge was built from one island to another, now every bridge can be traversed in both directions. Also, for each pair of islands, there is a unique ferry that travels back and forth between them. Since you like walking better than riding ferries, you want to maximize the sum of the lengths of the bridges you cross, subject to the constraints below. \begin{enumerate} \item You can start your visit at an island of your choice. \item You may not visit any island more than once. \item At any time you may move from your current island \textbf{S} to another island \textbf{D} that you have not visited before. You can go from \textbf{S} to \textbf{D} either by: \begin{enumerate} \item \textbf{Walking}: Only possible if there is a bridge between the two islands. With this option the length of the bridge is added to the total distance you have walked, or \item \textbf{Ferry}: You can choose this option only if \textbf{D} is not reachable from \textbf{S} using any combination of bridges and/or previously used ferries. (When checking whether it is reachable or not, you consider all paths, including paths passing through islands that you have already visited.) Note that you do not have to visit all the islands, and it may be impossible to cross all the bridges. \end{enumerate} \end{enumerate} Write a program that, given the N bridges along with their lengths, computes the maximum distance you can walk over the bridges obeying the rules described above. \textbf{2} <= \textbf{N} <= \textbf{1,000,000} The number of islands in the park. \textbf{1 }<= \textbf{L_i} <= \textbf{100,000,000} The length of bridge \textbf{i}. \InputFile Your program must read from the standard input the following data: \begin{enumerate} \item Line \textbf{1} contains the integer \textbf{N}, the number of islands in the park. Islands are numbered from \textbf{1} to \textbf{N}, inclusive. \item Each of the next \textbf{N} lines describes a bridge. The ith of these lines describes the bridge constructed from island \textbf{i} using two integers separated by a single space. The first integer represents the island at the other endpoint of the bridge, the second integer represents the length \textbf{L_i} of the bridge. You may assume that for each bridge, its endpoints are always on two different islands. \end{enumerate} \OutputFile Your program must write to the standard output a single line containing one integer, the maximum possible walking distance. \textbf{Note 1}: For some of the test cases the answer will not fit in a \textbf{32}‐bit integer, you might need \textbf{int64} in Pascal or \textbf{long long} in C/C++ to score full points on this problem. \textbf{Note 2}: When running Pascal programs in the contest environment, it is significantly slower to read in \textbf{64}‐bit data types than \textbf{32}‐bit data types from standard input even when the values being read in fit in \textbf{32} bits. We recommend that you read the input data into \textbf{32}‐bit data types. For some cases worth \textbf{40} points, \textbf{N} will not exceed \textbf{4,000}.
Time limit 5 seconds
Memory limit 256 MiB
Input example #1
7
3 8
7 2
4 2
1 4
1 9
3 4
2 3
Output example #1
24
Source 2008 IOI