Description

Given n courses, prerequisite relations, and time to complete each course, return minimum time to complete all courses working in parallel.

Examples

Input:n = 3, relations = [[1,3],[2,3]], time = [3,2,5]
Output:8
Explanation:

3+5 = 8.

Input:n = 1, relations = [], time = [5]
Output:5
Explanation:

With only one course and no prerequisites, the minimum time to complete all courses is simply the time for that course: 5.

Input:n = 5, relations = [[1,2],[1,3],[2,4],[3,4],[4,5]], time = [4,3,6,2,5]
Output:17
Explanation:

The critical path is 1 -> 3 -> 4 -> 5, so the total time is 4 + 6 + 2 + 5 = 17.

Constraints

  • 1 ≤ n ≤ 5 × 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!