← 返回 amazon 的题目列表Employee Importance
类型:online_judge
You are given a list of employees employees. Each employee has:
id: unique employee id
importance: the employee's importance value
subordinates: a list of ids of the employee's direct subordinates
You are also given an integer id representing a target employee.
Return the total importance of that employee: their own importance plus the importance of all of their direct and indirect subordinates.
Assume the subordinate relationship forms a tree (no cycles), and id is guaranteed to exist.
Input (stdin)
Line 1: integer n, the number of employees. Next n lines: each line encodes one employee as:
id importance k sub1 sub2 ... subk
where k is the number of subordinates.
Last line: the target employee id.
Output (stdout)
Print one integer: the total importance of the target employee.
Constraints
1 <= n <= 2*10^5
1 <= id <= 2*10^5
0 <= k <= n-1
1 <= importance <= 1000
Example
Input
3
1 5 2 2 3
2 3 0
3 3 0
1
Output
11