Balanced Development
Time limit5sMemory limit512 MB
Given a weighted tree and an activation permutation, find when each region activates as ancestor population inflows trigger chain activations.
- Level
Hard8 of 10
- Topics
- Tree, Simulation, DFS, Segment tree
- Solved
- No attempts yet
Problem
In the 2062 presidential election, Kyeonggwak-dang Jeong-hu won the presidency by defeating Dong-hyeon, an independent candidate.
For his balanced regional development pledge, Jeong-hu first built a tree of target regions. The tree has carefully chosen target regions as vertices and edges, each connecting two regions. Region 1, where Gyeonggi Science High School is located, is the root.
Next, Jeong-hu drew up a development plan, which is a permutation of length that gives the activation order of the target regions. The current time is 0, and at time , region is activated. During this process, an activation not in the plan can occur when a region's cumulative inflow population reaches or more. The cumulative inflow population of every region starts at 0.
Whether or not it is in the plan, when region is activated, a population movement of immediately occurs in every descendant region within distance , that is, every region that has as an ancestor and lies at distance or less from . In other words, the cumulative inflow population of every child region within distance increases by . Activations can chain. When several chain activation orders are possible, activations happen in any one of those orders.
Find the activation time of each region to help Jeong-hu review the development plan.
Input
The first line contains an integer . Each of the next lines contains three integers , , , meaning regions and are connected by a road of length . The next line contains integers , separated by spaces. Each of the next lines contains three integers , , , separated by spaces, with the -th of these lines describing region .
Output
Print integers on a single line, separated by spaces, giving the activation time of each region in order.
Constraints
- If , then .
- ,
- There are no duplicate edges.
- All given numbers are integers.
- The target regions form a tree.