#C12400. Organization Salary Sum Calculation

    ID: 41824 Type: Default 1000ms 256MiB

Organization Salary Sum Calculation

Organization Salary Sum Calculation

Given a company hierarchy, calculate the total salary sum of a target employee's entire organization. The organization of an employee is defined as the employee itself plus all of its direct or indirect subordinates. Formally, if the salary of an employee is \( s_i \) and the target employee's organization is a set \( O \), the output is \( \sum_{i \in O} s_i \).

The company structure is provided in the form of employee records where each record contains a unique employee id, the id of their manager (use -1 for the top manager), and their salary. You need to output the total salary sum for the organization of the target employee.

inputFormat

The input is read from standard input (stdin) and is structured as follows:

  • The first line contains an integer \( N \) representing the number of employees.
  • The following \( N \) lines each contain three space-separated integers: id, manager_id, and salary.
  • The last line contains a single integer representing the target_id.

You may assume that the employee ids are unique and that the input describes a valid hierarchy.

outputFormat

Output a single integer to standard output (stdout), which is the total salary sum of the target employee's organization.

## sample
5
1 -1 100
2 1 90
3 1 80
4 2 70
5 2 60
1
400