This page is still under construction.

Parts of this page are still being built. What you see may change.

Winter Forest and the Magic Flame

Time limit1.5sMemory limit512 MB

Summary
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.
Level

Hard8 of 10

Topics
Tree, Greedy, Sorting
Solved
No attempts yet

Problem

The NN villages in the winter forest are connected by N−1N - 1 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 KK units of magic power, where KK is a positive integer, shortens that road by KK. However, no matter how much magic power is spent, a road cannot be shortened below 11.

The king wants to shorten the distance from the flame village to the farthest village as much as possible. If he uses at most BB units of magic power, how far can this distance be reduced?

Input

The first line contains the number of villages NN (2≤N≤200,0002 \leq N \leq 200,000).

Each of the next N−1N - 1 lines contains two endpoints AjA_{j}, BjB_{j} (1≤Aj,Bj≤N1 \leq A_{j}, B_{j} \leq N, Aj≠BjA_{j} \neq B_{j}) and the length WjW_{j} (1≤Wj≤1091 \leq W_{j} \leq 10^{9}) of a road.

The next line contains the number of magic power queries QQ (1≤Q≤200,0001 \leq Q \leq 200,000).

Each of the next QQ lines contains the magic power BiB_{i} for one query (0≤Bi≤2×10140 \leq B_{i} \leq 2 \times 10^{14}).

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.

Examples1

  1. Example 1

    Input
    8
    1 2 7
    2 5 3
    1 3 3
    3 6 2
    6 8 4
    3 7 8
    1 4 12
    3
    1
    40
    6
    
    Expected output
    11
    3
    9