This page is still under construction.

Parts of this page are still being built. What you see may change.

Milk Scheduling

Interview

Time limit1sMemory limit128 MB

Summary
Given task durations and precedence constraints that form a DAG, find the minimum makespan when unlimited workers milk cows in parallel.
Level

Medium5 of 10

Topics
Graph, Topological sort, Dynamic programming, DFS
Solved
No attempts yet

Problem

Farmer John's NN cows (1≤N≤10,0001 \le N \le 10{,}000) are numbered 11 through NN. Milking cow ii takes T(i)T(i) units of time. Because of the layout of the barn, some cows must be milked before others: if cow AA must be milked before cow BB, then John must completely finish milking AA before he can start milking BB.

To finish as quickly as possible, John has hired enough farmhands to milk any number of cows at the same time. Even so, the ordering constraints limit how fast the whole process can go. Compute the minimum total time needed to milk all of the cows.

Input

  • Line 1: Two space-separated integers NN (the number of cows) and MM (the number of milking constraints, 1≤M≤50,0001 \le M \le 50{,}000).
  • Lines 2 through N+1N+1: Line i+1i+1 contains T(i)T(i) (1≤T(i)≤100,0001 \le T(i) \le 100{,}000).
  • Lines N+2N+2 through N+M+1N+M+1: Each line contains two space-separated integers AA and BB, meaning that cow AA must be completely milked before cow BB can be started. These constraints never form a cycle, so a solution always exists.

Output

  • Line 1: The minimum amount of time required to milk all of the cows.

Hint

In the first example there are 33 cows, and milking each of them takes 1010, 55, and 66 units of time respectively. Cow 33 must be completely milked before cow 22 can start.

Cows 11 and 33 can be milked at the same time from the beginning. Once cow 33 is finished, cow 22 can start. All cows finish being milked after 1111 units of time.

Examples1

  1. Example 1

    Input
    3 1
    10
    5
    6
    3 2
    
    Expected output
    11