Critical Path of a Release Pipeline

medium graphs topological-sort dp 1000 ms · 256 MB

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.

Example 1

Input

6 6
30 20 45 10 5 15
1 2
1 3
2 5
3 5
4 6
5 6

Output

95
4
1 3 5 6

Example 2

Input

4 4
5 10 10 3
1 2
2 3
3 2
1 4

Output

CYCLE

Hints · reveal one at a time if you're stuck

Hint 1

Treat each task as a vertex and each dependency as a directed edge. Before any timing, what must be true of the graph for a valid schedule to exist, and how can you check it and get an order to process tasks in?

Hint 2

Process tasks in that topological order and compute the earliest start of each task as the latest finish among its prerequisites. The release time is then the largest finish time over all tasks.

Hint 3

Sweep backwards from the release time T to get each task's latest start, where a task with nothing depending on it must finish by T. A task is critical exactly when its earliest and latest starts coincide, which also catches tasks on any tied longest chain. Durations may be zero, so detect cycles by counting, not by timing.

report an issue Prefer your own editor? sooks prep 47 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.