Under construction

Still being built. Things may change

Lingering Regret

Time limit3sMemory limit1024 MB

Summary
For each valley, the task counts how many valleys Uk-je can reach by jumping down the source-rooted tree under the bounce rule, starting from that valley.
Level

Hard8 of 10

Topics
Tree, DFS, Math
Solved
No attempts yet

Problem

Uk-je regrets the problem he wrote, Valley Overflow, so he is imagining what his life will be like in the future as a poached chicken seller.

In a deep mountain valley of Nonsan Training Center, Uk-je lives after a gift wrapping factory went under in the Fourth Industrial Revolution. He sold poached chicken by the valley until he was drafted as a trainee.

Nonsan Valley has NN terraced valleys connected in a tree shape by N−1N - 1 streams. Any two valleys are joined by exactly one path. Valley ii is at altitude HiH_i meters. Valley 1 is the source, and every stream flows away from the source.

During shooting practice, Uk-je lost NN shell casings, one of which fell into each valley. He jumps to search for them and collect them.

When he jumps from a valley at altitude HaH_a to a valley at altitude HbH_b (where Ha≥HbH_a \ge H_b), he bounces up (Ha−Hb)/2(H_a - H_b) / 2 meters from HbH_b. He then rises to altitude Hb+(Ha−Hb)/2H_b + (H_a - H_b) / 2 and can move to a valley cc with Hc≤Hb+(Ha−Hb)/2H_c \le H_b + (H_a - H_b) / 2. Then he bounces up again by (Hb+(Ha−Hb)/2−Hc)/2(H_b + (H_a - H_b) / 2 - H_c) / 2 meters. Uk-je can only jump in the same direction as the streams. Jumps never change valley heights or stream directions.

a→ba \to b means Uk-je jumps from aa to bb, where aa and bb stand for the altitudes of the two valleys. For 8→28 \to 2, Uk-je bounces (8−2)/2=3(8 - 2) / 2 = 3 meters and rises to altitude 5. He can then reach 8→2→58 \to 2 \to 5 at most. 8→2→68 \to 2 \to 6 and 8→2→78 \to 2 \to 7 are impossible. For 8→2→28 \to 2 \to 2, he jumps to altitude 5 via 8→28 \to 2, falls into the valley at altitude 2, and bounces (5−2)/2=1.5(5 - 2) / 2 = 1.5 meters, ending at altitude 3.5. In the example above, starting from 8 lets him reach every valley.

Uk-je does not even know which valley he is in. Assuming Uk-je starts in each valley, find how many shell casings he can recover. Uk-je can clone himself without limit, so each clone may take a different path to recover shells.

Input

The first line contains the number of valleys NN.

The second line contains the altitudes H1,H2,…,HNH_1, H_2, \ldots, H_N, separated by spaces.

Each of the next N−1N - 1 lines contains the two valley numbers connected by one stream, separated by a space.

Output

Print NN numbers on one line, separated by spaces. The ii-th number is the count of shell casings Uk-je can recover if he starts in valley ii.

Constraints

1≤N≤316,4021 \le N \le 316,402

1≤Hi≤1081 \le H_i \le 10^8 (1≤i≤N1 \le i \le N)

The N−1N - 1 streams connect the valleys in a tree shape.

Examples1

  1. Example 1

    Input
    5
    8 2 2 3 2
    1 2
    2 3
    3 4
    2 5
    
    Expected output
    5 3 1 1 1