Under construction

Still being built. Things may change

Yanggallae Rescue

Interview

Time limit1sMemory limit1024 MB

Summary
Given a tree rooted at room 1, find the minimum total thickness of vines to cut so that no leaf room stays connected to room 1.
Level

Medium4 of 10

Topics
Tree, Dynamic programming, DFS
Solved
No attempts yet

Problem

The peaceful village of Yanggallae is in danger! Poison is spreading through the vines and moving toward the room where Yanggallae is.

The poison spreads from every room except room 1 that is connected to exactly one vine.

There is exactly one path between any pair of rooms, and there are N−1N-1 vines in total. Each end of a vine is strongly attached to its room, so the vines must be cut to keep the poison from reaching the room where Yanggallae is.

Cutting a vine takes effort equal to its thickness VV. Shiyeon wants to cut the vines using as little total effort as possible.

Find the minimum total effort needed to cut the vines.

Input

The first line contains the number of rooms NN. (2≤N≤100 0002 \leq N \leq 100\,000)

Each of the next N−1N-1 lines contains three integers AA, BB, and VV. (1≤A,B≤N1 \leq A, B \leq N, 1≤V≤1 0001 \leq V \leq 1\,000)

This means the vine connecting room AA and room BB has thickness VV.

The room where Yanggallae is tied is numbered 11.

Output

Print the minimum total effort needed to cut the vines.

Hint

In the figure, cutting the vine between 1 and 2, the vine between 3 and 6, and the vine between 3 and 7 gives the minimum total of 3+1+2 = 6.

Examples1

  1. Example 1

    Input
    7
    1 2 3
    1 3 4
    2 4 2
    2 5 2
    3 6 1
    3 7 2
    
    Expected output
    6