You are in a company with ‘N’ employees each with a unique ID from ‘0 to N - 1’. You are given a list of managers, where ‘manager[i]’ is the direct manager of ‘ith’ employee. You are also given an array ‘timeToInform’ where ‘timeToInform[i]’ is the time it takes for ‘ith’ employee to send information to all his subordinates. Your task is to find out how much time it will take for any piece of information to reach all employees. You are also given ‘headId’ that is the ID of the head of the company.
For example:You are given ‘manager’ = [-1, 0, 0, 1, 1], ‘timeToInform’ = [1, 1, 0, 0, 0], ‘headId’ = [0],

We can see employee ‘0’ will take 1 time to inform their subordinates, that are employee 1, and employee 2, and employee 1 will take 1 time to inform their subordinates that are employee 3 and employee 4. The total time taken is 2. Hence the answer is 2.
The first line of input contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains two space-separated integers ‘N’ and ‘headId’ representing the number of employees and the employee id of the head of the company.
The second line of each test case contains ‘N’ space-separated integers representing the elements of the ‘manager’ array.
The third line of each test case contains ‘N’ space-separated integers representing the ‘timeToInform’ array.
Output Format:
For each test case, print a single integer representing the time it takes to inform all the employees
Print a separate line for each test case.
1 <= T <= 10
1 <= N <= 10^5
0 <= headId, manager[i] < N
0 <= timeToInform[i] <= 10^4
manager[headId] = -1
Time Limit = 1 sec
2
5 0
-1 0 0 1 1
1 1 0 0 0
3 1
1 -1 0
1 2 0
2
3
For the first test case, ‘manager’ = [-1, 0, 0, 1, 1], ‘timeToInform’ = [1, 1, 0, 0, 0], ‘headId’ = [0],

We can see employee ‘0’ will take 1 time to inform their subordinates, that are employee 1, and employee 2, and employee 1 will take 1 time to inform their subordinates that are employee 3 and employee 4. The total time taken is 2. Hence the answer is 2.
For the second test case, ‘manager’ = [1, -1, 0], ‘timeToInform’ = [1, 2, 0], ‘headId’ = 1,

We can see employee ‘1’ will take 2 time to inform their subordinates, that is employee ‘0’ and employee ‘0’ will take 1 time to inform their subordinate, i.e, employee 2. Hence the total time taken is 3
2
1 0
-1
0
3 0
-1 0 0
2 0 0
0
2
Try to make a tree out of the given information
In this approach, we can create a tree out of the given information and traverse it. For any node, the time taken is the maximum time taken by its subordinates and the timeToInform of that node.
We will create a dfs(employeeId, subordinates, timeToInform), where employeeId is the id of the employee we are currently looking, ‘subordinates’ is the map containing the list of all subordinates of any employee, and ‘timeToInform is the array given in the problem.
Algorithm:
O(N), Where N is the number of employees in the company
We are performing a depth-first search, which, in a tree will cost O(N) time. Hence the final time complexity is O(N).
O(N), Where N is the number of employees in the company
The space complexity of the recursive stack in a depth-first search of a tree is O(N). Hence the overall space complexity is O(N).