Dune
On the desert planet of Zarith, the Camlmen are responsible for harvesting elixir, a valuable substance essential for the galaxy. The harvesting and processing of elixir involves multiple stages across different processing facilities scattered throughout the vast desert. Each stage has dependencies that must be followed, where certain stages must be completed before others can begin.
While that in itself is not a big challenge, the Camlmen face threats from the harsh desert environment and the monstrous sandsnakes. Time is of the essence, and they must act fast to harvest and transport the precious elixir.
To efficiently and safely harvest the elixir, Patrick, the leader of the Camlmen, must determine the minimum time to complete all stages. Stages that do not have dependencies can be processed in parallel, but tasks that are dependent on each other must be completed in sequence.
Input
The first line contains two integers and
, the number of stages in the elixir processing, and the number of dependencies between processing stages.
The next lines describe the dependencies. Each line contains two integers
and
, indicating that stage
must be completed before stage
.
The last line contains space-separated integers, where the
th integer
represents the time required to complete stage
.
Output
Output a single integer, the minimum time required to complete all stages, assuming an optimal path and maximising parallel processing.
Clarifications
- There are no circular dependencies.
- The stages can be processed in parallel if no dependencies exist between them.
Example
Input 1
6 6
1 2
1 3
2 4
3 4
4 5
4 6
10 5 8 7 6 4
Output 1
31
The stages and their dependencies are:
1 → 2 → 4 → 5
↘︎ 3 → 4 → 6
The optimal execution will be:
- Stage
finishes at time
- Stage
and
will finish at time
and
respectively
- Stage
depends on both stage
and
, so it will only begin at time
and finish at time
- Stage
will finish at time
- Stage
will finish at time
Since stage finishes last at time
, we output
.
Input 2
4 3
1 2
1 3
3 4
5 3 2 7
Output 2
14
The stages and their dependencies are:
1 → 2
↘︎ 3 → 4
The optimal execution will be:
- Stage
finishes at time
- Stages
and
can be processed in parallel after stage
, finishing at time
and
respectively
- Stage
starts at time
after stage
finishes, and finishes at time
Comments