<p>You are given an integer <code>n</code>, which indicates that there are <code>n</code> courses labeled from <code>1</code> to <code>n</code>. You are also given a 2D integer array <code>relations</code> where <code>relations[j] = [prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> denotes that course <code>prevCourse<sub>j</sub></code> has to be completed <strong>before</strong> course <code>nextCourse<sub>j</sub></code> (prerequisite relationship). Furthermore, you are given a <strong>0-indexed</strong> integer array <code>time</code> where <code>time[i]</code> denotes how many <strong>months</strong> it takes to complete the <code>(i+1)<sup>th</sup></code> course.</p>

<p>You must find the <strong>minimum</strong> number of months needed to complete all the courses following these rules:</p>

<ul>
	<li>You may start taking a course at <strong>any time</strong> if the prerequisites are met.</li>
	<li><strong>Any number of courses</strong> can be taken at the <strong>same time</strong>.</li>
</ul>

<p>Return <em>the <strong>minimum</strong> number of months needed to complete all the courses</em>.</p>

<p><strong>Note:</strong> The test cases are generated such that it is possible to complete every course (i.e., the graph is a directed acyclic graph).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<strong><img alt="" src="https://assets.leetcode.com/uploads/2021/10/07/ex1.png" style="width: 392px; height: 232px;" /></strong>

<pre>
<strong>Input:</strong> n = 3, relations = [[1,3],[2,3]], time = [3,2,5]
<strong>Output:</strong> 8
<strong>Explanation:</strong> The figure above represents the given graph and the time required to complete each course. 
We start course 1 and course 2 simultaneously at month 0.
Course 1 takes 3 months and course 2 takes 2 months to complete respectively.
Thus, the earliest time we can start course 3 is at month 3, and the total time required is 3 + 5 = 8 months.
</pre>

<p><strong class="example">Example 2:</strong></p>
<strong><img alt="" src="https://assets.leetcode.com/uploads/2021/10/07/ex2.png" style="width: 500px; height: 365px;" /></strong>

<pre>
<strong>Input:</strong> n = 5, relations = [[1,5],[2,5],[3,5],[3,4],[4,5]], time = [1,2,3,4,5]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The figure above represents the given graph and the time required to complete each course.
You can start courses 1, 2, and 3 at month 0.
You can complete them after 1, 2, and 3 months respectively.
Course 4 can be taken only after course 3 is completed, i.e., after 3 months. It is completed after 3 + 4 = 7 months.
Course 5 can be taken only after courses 1, 2, 3, and 4 have been completed, i.e., after max(1,2,3,7) = 7 months.
Thus, the minimum time needed to complete all the courses is 7 + 5 = 12 months.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>0 &lt;= relations.length &lt;= min(n * (n - 1) / 2, 5 * 10<sup>4</sup>)</code></li>
	<li><code>relations[j].length == 2</code></li>
	<li><code>1 &lt;= prevCourse<sub>j</sub>, nextCourse<sub>j</sub> &lt;= n</code></li>
	<li><code>prevCourse<sub>j</sub> != nextCourse<sub>j</sub></code></li>
	<li>All the pairs <code>[prevCourse<sub>j</sub>, nextCourse<sub>j</sub>]</code> are <strong>unique</strong>.</li>
	<li><code>time.length == n</code></li>
	<li><code>1 &lt;= time[i] &lt;= 10<sup>4</sup></code></li>
	<li>The given graph is a directed acyclic graph.</li>
</ul>

class Solution:
    def minimumTime(self, n: int, relations: List[List[int]], time: List[int]) -> int:
        # a node takes x months. We can and will start taking classes as soon as possible, as in
        # whenever all prereqs are met. How much time did these prereqs take? Whichever one ended
        # the latest dictates the time at which the next class can start. 

        # Procedure: Topological sort, take a class (visit node) as soon as its prereqs are met. 
        # What we want to find out: If we start at time 0, how much time passes until each class
        # is done? Can solve for each class, at what time is it done, return the max value amongst all
        # classes. Any given class takes x months, these months can start at y time where y is the 
        # time at which all prereqs are met. Ex: in Example 1, node 3 takes 5 + 3 months because
        # its prereqs finish at month 3. Node 1 finishes at 3 months and 2 at 2 months, the max value
        # is 8 return 8.

        # In Example 2, nodes 1, 2, 3 finish at months 1, 2, 3 respectively. node 4 then finishes at
        # 4 + 3 months = month 7. Amongst all prereqs of 5, node 4 ends the lastest so node 5 
        # finishes at 5 + 7 = 12 months, this is the largest value, return it.

        # Modify a topological traversal so each time a node traverses an edge (ie a prereq tells 
        # tells a postreq the prereq has been taken), it also tells it what time the prereq finished.
        # Then, when the postreq is added to the queue, it knows what time it finishes (the time the
        # class takes plus the latest time it took to finish any of the prereqs)

        graph = defaultdict(set)
        indegrees = defaultdict(int)
        

        for prereq, postreq in relations:
            prereq_edges = graph[prereq]
            prereq_edges.add(postreq)
            graph[prereq] = prereq_edges

            indegrees[postreq] += 1

        # key is a class, value is the time the class can start
        time_started = defaultdict(int)
        ready_to_take = set()

        for node in range(1, n+1):
            if indegrees[node] == 0:
                ready_to_take.add(node)

        max_finished_time = 0

        while len(ready_to_take) > 0:
            course = ready_to_take.pop()
            

            finished_at = time_started[course] + time[course-1]

            max_finished_time = max(max_finished_time, finished_at)

            # tell all outgoing neighbors that the course has been taken (decrease prereqs by 1,
            # if neighbors now have 0 indegree they are ready to take). Also tell prereqs what time
            # the current course was finished at, check if that means the postreq must start later
            for postreq in graph[course]:
                time_started[postreq] = max(time_started[postreq], finished_at)
                indegrees[postreq] -= 1

                if indegrees[postreq] == 0:
                    ready_to_take.add(postreq)

            
        return max_finished_time
52956