//#include <ext/pb_ds/assoc_container.hpp> 
 //#include <ext/pb_ds/tree_policy.hpp>
// #include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <iterator>
#include <functional>
#include <limits>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <stack>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define trace(x) cerr << #x << ": " << x << " " << endl;

typedef int64_t ll;
 
#define endl '\n'
#define int ll
ll mod=1e17;
ll mod1=1e9+5;
ll power(ll a,ll b)
{
    if(b==0) return 1;
    else if(b%2==0)
        return power(((((a)%mod)*((a)%mod))%mod),b/2)%mod;
    else return (((a)%mod)*(power(((((a)%mod)*((a)%mod))%mod),b/2)%mod))%mod;
}
inline ll inverse(ll a,ll md) {
  a %= md;
  if (a < 0) a += md;
  ll b = md, u = 0, v = 1;
  while (a) {
    ll t = b / a;
    b -= t * a; swap(a, b);
    u -= t * v; swap(u, v);
  }
  assert(b == 1);
  if (u < 0) u += md;
  return u;
}
  
  
 //using namespace __gnu_pbds;
 //#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update>
//find_by_order(k)  returns iterator to kth element starting from 0;
//order_of_key(k) returns count of elements strictly smaller than k;
//erase,insert same as normal set
// mt19937 mrand(chrono::high_resolution_clock::now().time_since_epoch().count()); for randomized solution
const int ce=1e9+7;
ll fast_mod(ll input)
{
    return input < ce ? input : input%ce;
}
#define pi acos(-1)
int a[200006];
vector<int> v[200006];
int fin[200006];
int tree[800017];
int dp[200006];
void update(int node, int start, int end, int idx, int val)
{
    if(start == end)
    {
        tree[node] = val;
    }
    else
    {
        int mid = (start + end) / 2;
        if(start <= idx and idx <= mid)
        {
            update(2*node, start, mid, idx, val);
        }
        else
        {
            update(2*node+1, mid+1, end, idx, val);
        }
        tree[node] = max(tree[2*node] ,tree[2*node+1]);
    }
}
int query(int node, int start, int end, int l, int r)
{
    if(r < start or end < l)
    {
        return 0;
    }
    if(l <= start and end <= r)
    {
        return tree[node];
    }
    int mid = (start + end) / 2;
    int p1 = query(2*node, start, mid, l, r);
    int p2 = query(2*node+1, mid+1, end, l, r);
    return max(p1 , p2);
}
void dfs(int u,int par)
{
    int begin=dp[a[u]];
    int ne=query(1,1,200006,1,a[u]-1);
    dp[a[u]]=max(dp[a[u]],ne+1);
    update(1,1,200006,a[u],dp[a[u]]);
    fin[u]=query(1,1,200006,1,200006);
    for(auto i:v[u])
    {
        if(i==par)
            continue;
        dfs(i,u);
    }
    dp[a[u]]=begin;
    update(1,1,200006,a[u],dp[a[u]]);
}
int32_t main()
{
    IOS


    int n;
    cin>>n;
    map<int,int> m;
    set<int> s;
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        a[i]=x;
        s.insert(x);
    }
    int c=1;
    for(auto i:s)
    {
        m[i]=c;
        c++;
    }
    for(int i=1;i<=n;i++)
        a[i]=m[a[i]];
    // for(int i=1;i<=n;i++)
    //     cout<<a[i]<<" ";
    memset(tree,0,sizeof(tree));
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n-1;i++)
    {
        int x,y;
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(1,0);
    for(int i=1;i<=n;i++)
        cout<<fin[i]<<" ";

}       


Measured memory_bytes: 55188