#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 to . 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 to an already added bead with a red wire.
Insert(w, u, v): Insert a new bead between two beads and that are connected by a red wire. Specifically, remove the red wire between and , and connect to and to 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 , the number of beads. The beads are numbered from to .
Each of the next lines contains three integers . It is guaranteed that and . This means there is a wire of length between beads and .
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 can be achieved as follows: start from bead .
- Connect and . (length arbitrary)
- Insert between and . (the lengths are and respectively)
- Connect to with a wire of length .
- Connect to with a wire of length .
[Constraints]
- Subtask 1 (13 points): .
- Subtask 2 (15 points): .
- Subtask 3 (29 points): .
- Subtask 4 (43 points): .
Translated by ChatGPT 5
京公网安备 11011102002149号