eolymp
bolt
Try our new interface for solving problems
Problems

LIS on Tree

LIS on Tree

We have a tree with n vertices, whose i-th edge connects Vertex ui and Vertex vi. Vertex i has an integer ai written on it. For every integer k from 1 through n, solve the following problem:

We will make a sequence by lining up the integers written on the vertices along the shortest path from Vertex 1 to Vertex k, in the order they appear. Find the length of the longest increasing subsequence of this sequence.

Here, the longest increasing subsequence of a sequence A of length L is the subsequence Ai1, Ai2, ..., Aim with the greatest possible value of m such that 1i1 < i2 < ...< imL and Ai1 < Ai2 < ... < Aim.

Input

The first line contains number n (2n2 * 105). The second line contains numbers a1, a2, ..., an (1ai109). Each of the next n - 1 lines contains an edge (ui, vi) (1ui, vin).

Output

Print n lines. In the k-th line, print the length of the longest increasing subsequence of the sequence obtained from the shortest path from Vertex 1 to Vertex k.

prb10657.gif

Time limit 1 second
Memory limit 128 MiB
Input example #1
10
1 2 5 3 4 6 7 3 2 4
1 2
2 3
3 4
4 5
3 6
6 7
1 8
8 9
9 10
Output example #1
1
2
3
3
4
4
5
2
2
3