This page is still under construction.

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

Primary Factor

Time limit3sMemory limit1024 MB

Summary
Given a graph with node heights, compute for each node the minimum descent needed to reach a strictly taller node, or its own height if none exists.
Level

Medium7 of 10

Topics
Graph, Greedy, Union-find, Sorting
Solved
No attempts yet

Problem

"Which is the highest mountain in the world? The highest point on the summit of Mount Everest. OK, but which is the second highest mountain in the world? The second highest point on the summit of Mount Everest, of course."

With that logic, the list of the world's highest mountains becomes very silly. But there is a solution: introduce the concept of a primary factor. The primary factor of a mountain is the smallest height difference you must descend from the mountain to reach a strictly higher mountain. It works as a kind of measure of how independent a mountain is, and if you remove every point with a primary factor less than 200 m, you get rid of all the silly little mountains that really sit on higher mountains. This problem is about finding all primary factors in a graph.

We have a graph with nn nodes and mm edges, where each node ii has a non-negative integer h(i)h(i), the height of the node. The primary factor P(i)P(i) of a node is the smallest height you must descend from the node to reach a node of strictly greater height. Here is a slightly more mathematical definition: let G(i)G(i) be the set of all paths from node ii to some other node jj such that h(j)>h(i)h(j) > h(i). The primary factor of ii is defined as

P(i)=min⁡γ∈G(i){h(i)−min⁡k∈γ(h(k))}P(i) = \min_{\gamma \in G(i)} \left\{ h(i) - \min_{k\in \gamma}(h(k)) \right\}

If G(i)=∅G(i) = \emptyset, that is, if it is impossible to reach a node of greater height at all, we say that the primary factor is h(i)h(i).

Given a graph, find the primary factors of all nodes.

Input

One line with two integers, nn and mm. One line with nn integers 0≤h(i)≤1090 \leq h(i) \leq 10^9, the heights of the nodes. mm lines with two integers, aa and bb (1≤a,b≤n1 \leq a,b \leq n), meaning that an edge goes between nodes aa and bb.

Output

One line with nn integers, the primary factors of the nodes.

Constraints

  • n≤100 000n \le 100\,000
  • m≤400 000m \le 400\,000

Examples2

  1. Example 1

    Input
    5 4
    3 2 5 1 6
    1 2
    2 3
    3 4
    4 5
    
    Expected output
    1 0 4 0 6 
    
  2. Example 2

    Input
    6 7
    1 2 3 4 5 6
    1 2
    1 3
    1 6
    2 3
    2 4
    2 5
    3 4
    
    Expected output
    0 0 0 2 4 6