#P3074. [USACO13FEB] Milk Scheduling S

[USACO13FEB] Milk Scheduling S

Description

Farmer John has NN cows (1N1041 \leq N \leq 10^4), numbered from 11 to NN. Milking cow ii takes TiT_i units of time. Due to the barn layout, some cows must finish milking before others can start. For example, if cow AA must be milked before cow BB, then cow AA must be completely milked before starting cow BB.

To finish as quickly as possible, John has hired many workers and can milk any number of cows simultaneously. However, the precedence constraints must still be respected. Compute the minimum total time to finish all milking.

Input Format

  • The first line: two integers NN (number of cows) and MM (number of constraints, 1M5×1041 \leq M \leq 5\times 10^4).
  • Lines 22 through N+1N+1: each line contains one integer TiT_i, the milking time of cow ii (1Ti1051 \leq T_i \leq 10^5).
  • Lines N+2N+2 through N+M+1N+M+1: each line contains two integers AA and BB, meaning cow AA must be completely milked before starting cow BB. All constraints are acyclic, so a solution is guaranteed.

Output Format

  • One line: the minimum total time to complete all milking.
3 1 
10 
5 
6 
3 2 

11 

Hint

There are 33 cows with milking times 10,5,610, 5, 6. Cow 33 must finish before cow 22.

Initially, cows 11 and 33 can be milked simultaneously (taking 1010 and 66). After cow 33 finishes, start cow 22 (total time 6+5=116 + 5 = 11).

Translated by ChatGPT 5