Beads and Wires
Time limit1sMemory limit128 MB
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 through . You start with one bead and may add beads using:
- Append(w, v): connect new bead to existing bead with a red thread.
- Insert(w, u, v): insert new bead between beads and that are connected by red thread. Remove the red thread - and replace it with blue threads - and -.
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: ().
Next lines: , , (, ). Beads and are connected by a thread of length .
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.