/*!enjoy karo yaar!*/

//CF,CC,AtC,SPOJ: hp1999
//HE: hemant269
//HR: hemant2132

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

//#define     int             long long int
#define     fast()          ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define     all(x)          (x).begin(),(x).end()
#define     rz(x)           resize(x)
#define     mem(a,b)        memset(a,b,sizeof(a))
#define     sz(x)           ((int)(x.size()))
#define     eb              emplace_back
#define     pb              push_back
#define     pf              push_front
#define     pob             pop_back
#define     pof             pop_front
#define     ins             insert
#define     vi              vector<int>
#define     pii             pair<int,int>
#define     mii             map<int,int>
#define     F               first
#define     S               second
#define     makep           make_pair
#define     maket           make_tuple
#define     remax(a,b)      a=max(a,b)
#define     remin(a,b)      a=min(a,b)
#define     bitcount(x)     __builtin_popcountll(x)
#define     iceil(n,x)      (((n)+(x)-1)/(x))
#define     gcd(a,b)        (__gcd((a),(b)))
#define     lcm(a,b)        (((a)*(b))/gcd((a),(b)))
#define     dbug(x)         cout<<#x<<": "<<(x)<<"\n"
#define     flush           fflush(stdout)
#define     show(x)         for(auto zz:x)cout<<zz<<" ";cout<<"\n";
#define     show2(x)        for(auto zz:x)cout<<zz.F<<" "<<zz.S<<"\n";

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

const ld pi=acos(-1);
const ll inf=1e18,M=1e9+7;
const int N=2e5+5;

vi adj[N],euler;

void dfs(int x,int p)
{
    euler.pb(x);

    for(auto c:adj[x])
    {
        if(c!=p)
            dfs(c,x);
    }

    euler.pb(x);
}

int tree[4*N];
multiset<int> ms[N];

int cnt=0;

void update(int node,int beg,int en,int idx,int val)
{
    if(beg==en)
    {
        //base[idx]=val;
        tree[node]=val;
        return;
    }

    int mid=(beg+en)/2;
    if(beg<=idx && idx<=mid)
        update(2*node,beg,mid,idx,val);
    else
        update(2*node+1,mid+1,en,idx,val);

    tree[node]=max(tree[2*node],tree[2*node+1]);
}

int query(int node,int beg,int en,int l,int r)
{
    if(r<beg || en<l)
        return 0;

    if(l<=beg && en<=r)
        return tree[node];

    int mid=(beg+en)/2;
    int q1=query(2*node,beg,mid,l,r);
    int q2=query(2*node+1,mid+1,en,l,r);

    return max(q1,q2);
}

void up(int ind,int val,int fl)         // multiple values at one index, need only max...
{
    int tmp;

    if(fl==1)
    {
        ms[ind].ins(val);
        update(1,1,cnt,ind,*ms[ind].rbegin());
    }
    else
    {
        ms[ind].erase(ms[ind].find(val));

        if(sz(ms[ind]))
            tmp=*ms[ind].rbegin();
        else
            tmp=0;

        update(1,1,cnt,ind,tmp);
    }
}

void solve()
{
    mem(tree,0);

    int n;
    cin>>n;

    vi v(n+1,0);
    mii mp;

    for(int i=1;i<=n;++i)
        cin>>v[i],mp[v[i]];

    for(auto& c:mp)                 // compressing values
        c.S=++cnt;

    for(int i=1;i<=n;++i)
        v[i]=mp[v[i]];

    int a,b;
    for(int i=0;i<n-1;++i)
    {
        cin>>a>>b;
        adj[a].pb(b);
        adj[b].pb(a);
    }

    dfs(1,-1);      // euler tour

    //show(v);
    //show(euler);

    vi dp(n+1,0),mark(n+1,0);
    vi ans(n+1,0);
    int tmp;
    for(auto& c:euler)
    {
        if(!mark[c])
        {
            mark[c]=1;

            tmp=query(1,1,cnt,1,v[c]-1);
            dp[c]=tmp+1;
            ans[c]=max(query(1,1,cnt,1,cnt),dp[c]);

            up(v[c],dp[c],1);
        }
        else
        {
            up(v[c],dp[c],-1);
        }

//        dbug(c);
//        for(int i=1;i<=20;++i)
//            cout<<tree[i]<<" ";
//        cout<<"\n";
    }

    for(int i=1;i<=n;++i)
        cout<<ans[i]<<"\n";

}

int32_t main()
{
    fast();

    int t=1;
    //cin>>t;
    for(int z=1;z<=t;++z)
    {
        solve();
        //cout<<"\n";
    }

    return 0;
}



Measured memory_bytes: 51956