This page is still under construction.

Parts of this page are still being built. What you see may change.

Beads and Wires

Time limit1sMemory limit128 MB

Summary
You choose append and insert orders that build the given weighted tree to maximize the total length of insert-created edges.
Level

Hard8 of 10

Topics
Dynamic programming, Tree, Greedy, Sorting
Solved
No attempts yet

Problem

In the bead-threading game, thread comes in red and blue. Beads are numbered 11 through nn. You start with one bead and may add beads using:

  • Append(w, v): connect new bead ww to existing bead vv with a red thread.
  • Insert(w, u, v): insert new bead ww between beads uu and vv that are connected by red thread. Remove the red thread uu-vv and replace it with blue threads uu-ww and ww-vv.

Every thread has a length. When the game ends, the score is the sum of blue thread lengths.

You are given a final connection state: each thread connects two beads with a length, but colors are unknown. Among all ways to produce this state, output the maximum possible final score.

Input

Line 1: nn (1≤n≤200 0001 \le n \le 200\,000).

Next n−1n-1 lines: aia_i, bib_i, cic_i (1≤ai<bi≤n1 \le a_i < b_i \le n, 1≤ci≤10 0001 \le c_i \le 10\,000). Beads aia_i and bib_i are connected by a thread of length cic_i.

Output

Print the maximum possible final score.

Hint

In the sample, start at bead 3, connect 5, insert 1 between 3 and 5, then append 2 and 4 to 1 for a score of 60. No larger score exists.

Examples6

  1. Example 1

    Input
    5
    1 2 10
    1 3 40
    1 4 15
    1 5 20
    
    Expected output
    60
    
  2. Example 2

    Input
    2
    1 2 7
    
    Expected output
    0
    
  3. Example 3

    Input
    3
    1 2 5
    2 3 8
    
    Expected output
    13
    
  4. Example 4

    Input
    4
    1 2 10
    1 3 20
    1 4 30
    
    Expected output
    50
    
  5. Example 5

    Input
    6
    1 2 1
    2 3 2
    3 4 3
    4 5 4
    5 6 5
    
    Expected output
    14
    
  6. Example 6

    Input
    7
    1 2 100
    1 3 50
    2 4 25
    2 5 75
    3 6 10
    3 7 90
    
    Expected output
    315