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 C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

  #include<bits/stdc++.h>
using namespace std;
using ll = long long;

#ifdef LOCAL
#include<debug.h>
#else
#define db(...) 21
#endif

#define debug(x) cout << #x << " = " << x << endl;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define FOR(it, b, e) for (typeof(b) it = (b); it != (e); ++it)
#define MSET(c, v) memset(c, v, sizeof(c))

const int INF = 0x3F3F3F3F; 
const int NEGINF = 0xC0C0C0C0;
const int NULO = -1;
const double EPS = 1e-10;
const ll mod = 998244353;

inline int cmp(double x, double y = 0) 
{  
  if(fabs(x - y) < EPS)
    return 0;
  return x > y ? 1 : -1;
}

ll exp(ll a, ll b)
{
  ll ans = 1;
  while(b)
  {
    if(b & 1)
      ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}

const int maxn = 5e5 + 10;

int a[maxn];
int ans[maxn];

vector<int>graph[maxn];
int idx = 1;
map<int,int>mapa;

int tree[4* maxn];


void update(int node, int l, int r, int pos, int val)
{
  if(l > pos || r < pos)
    return;
  if( l == r && l == pos )
  {
    tree[node] += val;
    return;
  }

  int mid = (l + r) >> 1;
  update(2*node, l, mid, pos, val);
  update(2*node + 1, mid+1, r, pos, val);
  tree[node] = max(tree[2*node], tree[2*node + 1]);
}

int query(int node, int l, int r, int x, int y)
{
  if(l > y || r < x)
    return 0;
  if( x <= l && r <= y)
  {
    return tree[node];
  }
  int mid = (l + r) >> 1;
  int a = query(2*node, l, mid, x, y);
  int b = query(2*node + 1, mid+1, r, x, y);
  return max(a,b);
}


void dfs(int u, int p)
{
  ans[u] = query(1, 1, idx, 1 , mapa[a[u]]) + 1;
  update(1, 1, idx, mapa[a[u]], ans[u]);
  for(const auto &v: graph[u])
  {
    if(v == p)
      continue;
    dfs(v, u);
  }
  update(1, 1, idx, mapa[a[u]], -ans[u]);
}

int main()
{
  int n;
  ios::sync_with_stdio(false);
  cin >> n;

  for(int i = 1 ; i <= n ; i++)
  {
    cin >> a[i];
    mapa[a[i]] = 0;
    mapa[a[i] - 1] = 0;
  }

  for(auto &c : mapa)
    c.second = idx++;
  
  for(int i = 1; i < n ; i++)
  {
    int u,v;
    cin >> u >> v;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  dfs(1, 0);

  for(int i = 1; i <= n ; i++)
  {
    cout << ans[i] << endl;
  }

  return 0;
}