Performance Review
Time limit2sMemory limit512 MB
For each employee, sum t_j over all descendants j whose rank r_j is lower than the employee's rank.
- Level
Medium7 of 10
- Topics
- Tree, DFS, Segment tree, Prefix sum
- Solved
- No attempts yet
Problem

In the engineering division of a company, every employee except the director reports to one manager, and every employee reports to the director directly or through a chain of managers. Maria runs the performance review system of the division. Letting managers assess their direct reports did not work well, so Maria built a new system that gives each employee a technical rank.
Under the new system an employee reviews only the subordinates whose technical rank is lower. The subordinates of employee are the employees who report to directly and the employees who report to through a chain of managers. Employee writes a summary of recent work and estimates , the time one review of that summary takes. Every superior of whose technical rank is higher than spends writing the review of . A superior with the same technical rank writes no review.
Maria wants to know the load this creates. Given the structure of the division, compute the total time each employee spends writing reviews.
Input
The first line contains one integer , the number of employees, who are numbered 1 to . The next lines describe employee 1 up to employee in that order. Line contains three space separated integers , , : the manager, the technical rank, and the expected time of one review of employee . The director has no manager and is given as . Every other employee has between 1 and .
Output
Print lines. Line holds the total time employee spends writing reviews.
Constraints
- , the number of employees
- , the technical rank of each employee
- , the expected time of each review
- Exactly one employee has , and the manager relation forms a tree rooted at that employee.