Performance Review

For each employee, sum t_j over all descendants j whose rank r_j is lower than the employee's rank.

Medium7TreeDFSSegment treePrefix sumNo attempts yetTime limit2sMemory limit512 MB

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 ii are the employees who report to ii directly and the employees who report to ii through a chain of managers. Employee jj writes a summary of recent work and estimates tjt_j, the time one review of that summary takes. Every superior of jj whose technical rank is higher than rjr_j spends tjt_j writing the review of jj. 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 EE, the number of employees, who are numbered 1 to EE. The next EE lines describe employee 1 up to employee EE in that order. Line ii contains three space separated integers mim_i, rir_i, tit_i: the manager, the technical rank, and the expected time of one review of employee ii. The director has no manager and is given as mi=1m_i = -1. Every other employee has mim_i between 1 and EE.

Output

Print EE lines. Line ii holds the total time employee ii spends writing reviews.

Constraints

  • 1E1000001 \le E \le 100000, the number of employees
  • 1ri1000001 \le r_i \le 100000, the technical rank of each employee
  • 1ti1000001 \le t_i \le 100000, the expected time of each review
  • Exactly one employee has mi=1m_i = -1, and the manager relation forms a tree rooted at that employee.