Problem:
Score : 600 points

Problem Statement

We have a tree with N vertices, whose i-th edge connects Vertex u_i and Vertex v_i.
Vertex i has an integer a_i 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 A_{i_1} , A_{i_2} , ... , A_{i_M} with the greatest possible value of M such that 1 \leq i_1 < i_2 < ... < i_M \leq L and A_{i_1} < A_{i_2} < ... < A_{i_M}.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq a_i \leq 10^9

1 \leq u_i , v_i \leq N

u_i \neq v_i

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}

Output

Print N lines. 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.

Sample Input 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

Sample Output 1

1
2
3
3
4
4
5
2
2
3

For example, the sequence A obtained from the shortest path from Vertex 1 to Vertex 5 is 1,2,5,3,4. Its longest increasing subsequence is A_1, A_2, A_4, A_5, with the length of 4.

Solution in JavaScript (Node.js 12.16.1) (judge verdict: Accepted)

"use strict";
var input=require("fs").readFileSync("/dev/stdin","utf8");
var cin=input.split(/ |\n/),cid=0;
function next(a){return a?cin[cid++]:+cin[cid++];}
function nexts(n,a){return a?cin.slice(cid,cid+=n):cin.slice(cid,cid+=n).map(a=>+a);}
function nextm(h,w,a){var r=[],i=0;if(a)for(;i<h;i++)r.push(cin.slice(cid,cid+=w));else for(;i<h;i++)r.push(cin.slice(cid,cid+=w).map(a=>+a));return r;}
function xArray(v){var a=arguments,l=a.length,r="Array(a["+--l+"]).fill().map(x=>{return "+v+";})";while(--l)r="Array(a["+l+"]).fill().map(x=>"+r+")";return eval(r);}
function lower_bound(d,e,a=0,b=d.length){for(var c;1<b-a;)d[c=a+b>>1]<e?a=c:b=c;return d[a]<e?b:a};

var x = xArray("[]",2e5+1);
var ans, dp;
var g, a;
var myOut = main();
if(myOut !== undefined)console.log(myOut);

function main(){
  var n = next();
  a = nexts(n);
  g = xArray("[]",n);
  for(var i = 1; i < n; i++){
    var u = next()-1, v = next()-1;
    g[u].push(v);
    g[v].push(u);
  }
  ans = xArray(0,n);
  dp = xArray(2e9,n+1);
  var iv = Array(n), ix = Array(n);
  dp[0] = 0;
  var q = [0,-1];
  while(q.length){
    var p = q.pop(), v = q.pop();
    q.push(~v);
    ix[v] = lower_bound(dp,a[v]), iv[v] = dp[ix[v]];
    dp[ix[v]] = a[v];
    ans[v] = lower_bound(dp,2e9)-1;
    for(var i = 0; i < g[v].length; i++){
      var V = g[v][i];
      if(V === p)continue;
      q.push(V,v);
    }
    while(q[q.length-1] < 0){
      var t = ~q.pop();
      dp[ix[t]] = iv[t];
    }
  }
  return ans.join("\n");
}