Critical Path of a Release Pipeline
A release pipeline consists of n tasks, numbered from 1 to n. Task i takes exactly d_i
minutes once started. There are also m dependencies; a dependency a b means that task a must
finish before task b can start. There are unlimited workers, so any number of tasks can run at the
same time, as long as every dependency is respected.
The pipeline starts at minute 0. Each task gets a start time s_i ≥ 0, and for every dependency
a b it must hold that s_b ≥ s_a + d_a. The release is finished when the last task finishes, at
minute max(s_i + d_i). Let T be the smallest possible release time.
A task is critical if it has no room to slip: starting it even one minute later than the
earliest moment its dependencies allow makes it impossible to finish the release by minute T,
no matter how the other tasks are scheduled.
Find T and all critical tasks. If the dependencies are circular, so that no valid schedule
exists, report that instead.
Input
The first line contains two integers n and m (1 ≤ n ≤ 2·10^5, 0 ≤ m ≤ 2·10^5): the number of
tasks and the number of dependencies.
The second line contains n integers d_1, d_2, ..., d_n (0 ≤ d_i ≤ 10^9): the task durations in
minutes.
Each of the next m lines contains two integers a and b (1 ≤ a, b ≤ n): task a must finish
before task b starts. The same dependency may appear more than once, and a may equal b.
Output
If no valid schedule exists, print a single line CYCLE.
Otherwise print three lines: the minimum release time T, then the number k of critical tasks,
then the k ids of the critical tasks in increasing order, separated by spaces.
Notes
In the first example, the build (task 1, 30 min) is followed by unit tests (task 2, 20 min) and integration tests (task 3, 45 min) in parallel; both must finish before signing (task 5, 5 min), and signing and docs (task 4, 10 min) must finish before publishing (task 6, 15 min). The longest chain is 1 → 3 → 5 → 6 with 30 + 45 + 5 + 15 = 95 minutes. Unit tests can slip by 25 minutes and docs by 70 minutes without delaying the release, so tasks 2 and 4 are not critical.
In the second example, tasks 2 and 3 each wait for the other, so the pipeline can never complete.
The answer T may not fit in a 32-bit integer.