Winter Forest and the Magic Flame
Time limit1.5sMemory limit512 MB
Given a weighted tree rooted at village 1, find the smallest possible farthest distance from the root after shortening edges within a magic budget B, for each query.
Problem
The villages in the winter forest are connected by undirected roads. Every road has a positive integer length, and the road network forms a tree.
The first village has a magic flame, and the people of the winter forest live on the warmth of this flame. The distance from the village with the flame to another village is the sum of the road lengths on the path to that village.
The king of the forest is also its most powerful wizard. Using magic, he can shorten the roads. For each road, spending units of magic power, where is a positive integer, shortens that road by . However, no matter how much magic power is spent, a road cannot be shortened below .
The king wants to shorten the distance from the flame village to the farthest village as much as possible. If he uses at most units of magic power, how far can this distance be reduced?
Input
The first line contains the number of villages ().
Each of the next lines contains two endpoints , (, ) and the length () of a road.
The next line contains the number of magic power queries ().
Each of the next lines contains the magic power for one query ().
Output
For each query, print the minimum possible distance between the village with the flame and the farthest village, given the magic power available for that query. Print one answer per line.