A company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company is the one with headID.

Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also, it is guaranteed that the subordination relationships have a tree structure.

The head of the company wants to inform all the company employees of an urgent piece of news. He will inform his direct subordinates, and they will inform their subordinates, and so on until all employees know about the urgent news.

The i-th employee needs informTime[i] minutes to inform all of his direct subordinates (i.e., After informTime[i] minutes, all his direct subordinates can start spreading the news).

Return the number of minutes needed to inform all the employees about the urgent news.

&nbsp;
Example 1:

Input: n = 1, headID = 0, manager = [-1], informTime = [0]
Output: 0
Explanation: The head of the company is the only employee in the company.


Example 2:

Input: n = 6, headID = 2, manager = [2,2,-1,2,2,2], informTime = [0,0,1,0,0,0]
Output: 1
Explanation: The head of the company with id = 2 is the direct manager of all the employees in the company and needs 1 minute to inform them all.
The tree structure of the employees in the company is shown.


&nbsp;
Constraints:


	1 &lt;= n &lt;= 105
	0 &lt;= headID &lt; n
	manager.length == n
	0 &lt;= manager[i] &lt; n
	manager[headID] == -1
	informTime.length == n
	0 &lt;= informTime[i] &lt;= 1000
	informTime[i] == 0 if employee i has no subordinates.
	It is guaranteed that all the employees can be informed.


import queue
class Solution:
	def numOfMinutes(self, n: int, headID: int, manager: List[int],time: List[int]) -> int:
		nodes = []
		for i in range(n): nodes.append([])
		for i in range(n): 
			if i != headID: nodes[manager[i]].append(i)

		q = queue.LifoQueue()
		q.put([headID,0])
		ans = 0
		while not q.empty():
			cur = q.get()
			for nxt in nodes[cur[0]]:
				q.put([nxt,cur[1]+time[cur[0]]])
				ans = max(ans,cur[1]+time[cur[0]])
		return ans
class Solution {
    public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) {
        Map<Integer, List<Integer>> graph = new HashMap<>();
        for(int i=0; i<n; i++) {
            graph.putIfAbsent(manager[i], new ArrayList<>());
            graph.get(manager[i]).add(i);
        }
        return dfs(graph, headID, informTime);
    }

    int dfs(Map<Integer, List<Integer>> graph, int curHead, int[] informTime) {
        int curMax = 0;
        if(!graph.containsKey(curHead)){
            return curMax;
        }
        for(int subordinate : graph.get(curHead)) {
            curMax = Math.max(curMax, dfs(graph, subordinate, informTime));
        }
        return curMax + informTime[curHead];
    }
}
class Solution {
public:
    int ans = INT_MIN, tmp=0;
    unordered_map<int, vector<int>> mp;//manager to each subordination
    vector<int>* pInformTime;
    int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {
        pInformTime = &informTime;
        for(int i = 0; i<n;i++) mp[manager[i]].push_back(i);
        dfs(headID);
        return ans;
    }
    void dfs(int i) {
        if(mp.find(i) == mp.end()) {
            ans = max(ans, tmp);
        } else {
            tmp+=(*pInformTime)[i];
            for(auto&c: mp[i]) dfs(c);
            tmp-=(*pInformTime)[i];
        }
    }
};
var numOfMinutes = function(n, headID, manager, informTime) {

// Build the tree structure
    let tree = {}

// {manager: direct employee}
    for (let i = 0 ; i < manager.length ; i++){
// the head of the company does not have a manager
        if (i === headID){
            continue
        }
        let m = manager[i]
        if (!tree[m]){
            tree[m] = []
        }
        tree[m].push(i)
    }

// BFS
// [employee, time to inform the head]
    let queue = [[headID, 0]]
    let res = 0

    while (queue.length){
        let [emp, currTime] = queue.shift()
        let children = tree[emp] || []
        for (let child of children){
            res = Math.max(res, informTime[emp] + currTime)
            queue.push([child, informTime[emp] + currTime])
        }
    }

    return res
};
Time Needed to Inform All Employees