Flower's Land
Time limit8sMemory limit2048 MB
For each city z, find the largest flower total over a connected set of exactly k cities that includes z, where z is the root of the tree.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Tree, Divide and conquer
- Solved
- No attempts yet
Statement
The Kingdom of Flowers has cities, and city grows flowers. There are roads, and the -th road connects cities and . Any two cities are connected by a path.
The kingdom wants to hold a flower exhibition. First, choose a city to build the exhibition hall. Then select exactly cities and transport their flowers to city .
The organizers require that if city is selected, then every city on the path from to must also be selected. In particular, city must be selected.
For each , find the maximum number of flowers that can be transported when city is chosen for the exhibition hall.
Input
The first line contains two integers and (, ).
The second line contains integers ().
Each of the next lines contains two integers and (, ), meaning there is an edge between vertices and . The given graph is guaranteed to be a tree.
Output
Output a single line with integers , where is the answer for .