Critical path makespan

Write codeMedium30 minGoogle style

Your task

A build system runs n tasks (0..n-1), each with a positive duration durations[i]. Dependencies are pairs [a, b] meaning task b can only start after task a finishes; independent tasks run fully in parallel. Return the makespan, the minimum total time to finish all tasks given unlimited parallelism, which equals the longest dependency chain weighted by durations (the critical path). If the dependencies form a cycle, return -1.

Examples

critical_path(durations: list[int], deps: list[list[int]]) -> int
Example 1
Inputcritical_path([3, 2, 5, 1], [[0, 1], [1, 3], [0, 2], [2, 3]])
Output9