#P3647. [APIO2014] 连珠线

[APIO2014] 连珠线

Description

In the era of Da Vinci, there was a popular children's game called Beads and Wires. As the name suggests, the game involves beads and wires. The wires are either red or blue, and the beads are numbered from 11 to nn. The game starts with a single bead, and each time a new bead is added using one of the following operations:

Append(w, v): Connect a new bead ww to an already added bead vv with a red wire.

Insert(w, u, v): Insert a new bead ww between two beads uu and vv that are connected by a red wire. Specifically, remove the red wire between uu and vv, and connect uu to ww and ww to vv with blue wires.

Each wire has a length. After the game ends, your final score is the sum of the lengths of all blue wires.

You are given the final configuration of the Beads and Wires game: which beads are connected and the length of each wire. However, you are not told the color of each wire.

Your task is to write a program to find the maximum possible score. That is, among all games that could lead to the given final configuration, find the one with the highest score and output that maximum possible score.

Input Format

The first line contains a positive integer nn, the number of beads. The beads are numbered from 11 to nn.

Each of the next n1n - 1 lines contains three integers ai,bi,cia_i, b_i, c_i. It is guaranteed that 1ai<bin1 \leq a_i < b_i \leq n and 1ci100001 \leq c_i \leq 10000. This means there is a wire of length cic_i between beads aia_i and bib_i.

Output Format

Output a single integer, the maximum possible score.

5
1 2 10
1 3 40
1 4 15
1 5 20
60
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
140

Hint

[Sample Description 1]

A score of 6060 can be achieved as follows: start from bead 33.

  • Connect 55 and 33. (length arbitrary)
  • Insert 11 between 33 and 55. (the lengths are 4040 and 2020 respectively)
  • Connect 22 to 11 with a wire of length 1010.
  • Connect 44 to 11 with a wire of length 1515.

[Constraints]

  • Subtask 1 (13 points): 1n101 \leq n \leq 10.
  • Subtask 2 (15 points): 1n2001 \leq n \leq 200.
  • Subtask 3 (29 points): 1n100001 \leq n \leq 10000.
  • Subtask 4 (43 points): 1n2000001 \leq n \leq 200000.

Translated by ChatGPT 5