Execution Time
Time limit2sMemory limit1024 MB
In a DAG where tasks run in parallel and each waits for all predecessors, choose exactly K tasks (excluding the first and last) to zero out so the total finish time is minimized.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Topological sort, Greedy, Graph
- Solved
- No attempts yet
Problem
You developed a program that runs tasks in parallel. The tasks all have different execution times, and the tasks are ordered.
When a task finishes, it signals the next tasks. A task that receives a signal can run only after it has received signals from all of its preceding tasks.

As in the figure above, there are tasks , , , , and their order is fixed. We first run task and continue through task . There is always exactly one task that must start first, like task , and exactly one task that runs last, like task .
Suppose the execution times of tasks through are 1 second, 2 seconds, 3 seconds, 2 seconds, 1 second, in that order. The five tasks run as follows.
- Time 0: first, task runs.
- Time 1: after running for 1 second, task signals tasks , , that it has finished.
- Time 1: tasks , , receive the signals, satisfy their run condition, and start at the same time.
- Time 3: tasks and have execution time 2 seconds, so 2 seconds later they signal the next task, task .
- Time 3: task receives the signals but has not received a signal from task among its preceding tasks, so it waits.
- Time 4: task has execution time 3 seconds, so 3 seconds after task starts it signals task .
- Time 4: task has received all signals, so it runs. Task has execution time 1 second.
- Time 5: task finishes, and the execution of all tasks ends.
Therefore, task finishes 5 seconds later.
However, it was confirmed that in this program, forcibly changing the execution times of exactly of the remaining tasks, excluding the task that must start first and the task that runs last, to 0 seconds causes no problem in the program.
Find the minimum time needed for all tasks to complete when the execution times of exactly tasks are forcibly changed to 0 seconds.
Input
The number of tasks , the number of task-order relations , and the number of tasks whose execution time may be forcibly changed to 0 seconds, , are given, separated by spaces. A task-order relation tells which task runs after a task finishes.
The second line gives the execution times of the tasks, separated by spaces.
From the third line to line , task-order information is given. Each piece of information consists of two integers , separated by a space, meaning that task runs after task finishes.
The starting task number is always 1, and running this task guarantees that all tasks run.
Given tasks , , , the task order is guaranteed not to contain a cycle such as .
Output
Print the minimum time needed for all tasks to complete when the execution times of exactly tasks are changed to 0 seconds.
Constraints
- execution time , execution time is an integer