Utilitarianism
Time limit5sMemory limit1024 MB
Pick k tree edges with no shared endpoints to maximize total value; the answer is a matching-style DP with a slope-trick lambda search over edge weights.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, Binary search, Greedy
- Solved
- No attempts yet
Problem
In RUN-land, there are cities numbered to . Some pairs of cities are connected by a bidirectional road. There are exactly roads, and between any two cities there is a unique path. Each road is assigned an integer called its value.
Today, to honor the co-founders of RUN-land, Alex, the king of RUN-land, will choose distinct roads and give one road to each of the co-founders. To avoid needless conflict, no city may be connected to more than one of the chosen roads.
Alex does not care who gets which road. He only cares about the sum of the values of the chosen roads. Choose the roads so that this sum is as large as possible.
Input
The first line contains two integers and (, ), the number of cities in RUN-land and the number of roads to choose. Each of the next lines contains three integers (, ), meaning that city and city are directly connected by a bidirectional road with value .
Output
If no choice of roads satisfies the conditions, print Impossible. Otherwise, print one integer, the maximum possible sum of the values of the chosen roads.