Hot Path Through Microservices

medium graphs dfs dp topological-sort 1500 ms · 256 MB

Hot Path Through Microservices

Your platform runs n microservices, numbered 1 to n. Cold-starting service i costs c_i milliseconds of CPU: the first time a request reaches the service, the container has to warm up its caches. After that, any further calls the same request makes into that service are free.

The tracing system has recorded m call edges. An edge u v means service u may call service v while handling a request. The call graph is directed and may contain cycles (services call each other back, retries loop, and so on), self-loops and repeated edges.

A request enters at some service and then follows call edges one after another, as long as it likes. It may revisit services and edges any number of times, and it may stop anywhere, including at the entry service. Its cold-start cost is the sum of c_i over all distinct services it reached, the entry service included.

For capacity planning you need the worst case: for every possible entry service s, find the largest cold-start cost a single request entering at s can incur.

Input

The first line contains two integers n and m (1 ≤ n ≤ 2·10^5, 0 ≤ m ≤ 2·10^5).

The second line contains n integers c_1, c_2, ..., c_n (0 ≤ c_i ≤ 10^9).

Each of the next m lines contains two integers u and v (1 ≤ u, v ≤ n): service u may call service v. Self-loops (u = v) and repeated edges are allowed.

Output

Print n integers separated by spaces on one line: the s-th of them is the largest cold-start cost of a request entering at service s.

Notes

In the first example, services 1, 2 and 3 call each other in a loop, and from that loop a request can go either to service 4 (and on to 5) or to service 6. A request entering at 1 can tour the whole loop for 1 + 2 + 3 = 6, then go to 4 and 5 for 4 + 5 more, total 15. It cannot also reach 6 on the same request, because nothing leads back from 4 or 5. Going to 6 instead would give only 6 + 7 = 13.

In the second example there are no edges, so every request only reaches its entry service.

A single request is a single chain of calls: it cannot fan out into several branches at once. The answers can exceed 2^31.

Example 1

Input

6 7
1 2 3 4 5 7
1 2
2 3
3 1
3 4
4 5
2 6
5 5

Output

15 15 15 9 5 7

Example 2

Input

3 0
5 0 1000000000

Output

5 0 1000000000

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

Hint 1

A request can loop around a cycle of services as many times as it likes, paying each service's cost only once. Once a request reaches one service of a group that all call each other, which services can it collect?

Hint 2

Group services that all reach each other into components. A request that enters a component can collect all of it, and it can never come back to a component it has left. What kind of graph do the components form, and what do you want to compute on it for each starting component?

Hint 3

Find the components with an SCC algorithm such as Tarjan's, which outputs them in reverse topological order, then take the heaviest path from each component in that order. Use an iterative DFS so a long chain of services does not overflow the stack, and use 64-bit totals.

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

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