//For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456 a.cpp -o a.exe in cmd
    #include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstring>
    #include <chrono>
    #include <complex>
    #define ll long long
    #define ld long double
    #define vi vector<int>
    #define vll vector<ll>
    #define vvi vector < vi >
    #define pii pair<int,int>
    #define pll pair<long long, long long>
    #define mod 1000000007
    #define inf 1000000000000000001;
    #define all(c) c.begin(),c.end()
    #define mp(x,y) make_pair(x,y)
    #define mem(a,val) memset(a,val,sizeof(a))
    #define eb emplace_back
    #define pb push_back
    #define f first
    #define s second
    
    using namespace std;

    ll tree[5000000];

    void build(int node,int l,int r)
    {
        if(l==r)
        {
            tree[node]=0;   //Change for min/sum
            return;
        }
        build(node*2,l,(l+r)/2);
        build(node*2+1,(l+r)/2+1,r);
        tree[node]=max(tree[node*2],tree[node*2+1]);    //Change for min/sum
    }

    void update(int node,int l,int r,int i,ll x)
    {
        if(i<l || r<i)
            return;
        if(l==r)
        {
            tree[node]=x;
            return;
        }
        update(node*2,l,(l+r)/2,i,x);
        update(node*2+1,(l+r)/2+1,r,i,x);
        tree[node]=max(tree[node*2],tree[node*2+1]);    //Change for min/sum
    }
    
    ll query(int node,int l,int r,int i,int j)
    {
        if(j<l || r<i)
            return -2e18;   //Change for min/sum
        if(i<=l && r<=j)
        {

            return tree[node];
        }
        ll q1=query(node*2,l,(l+r)/2,i,j);
        ll q2=query(node*2+1,(l+r)/2+1,r,i,j);
        return max(q1,q2);  //Change for min/sum
    }

    vector<int> v[200001];
    int ar[200001];
    int ans[200001],n;

    void dfs(int i,int p)
    {
        int x=query(1,0,n,0,ar[i]-1)+1;
        int z=query(1,0,n,ar[i],ar[i]);
        update(1,0,n,ar[i],x);
        ans[i]=query(1,0,n,0,n);
        for(int j=0;j<v[i].size();++j)
        {
            if(v[i][j]!=p)
            {
                dfs(v[i][j],i);
            }
        }
        // cout<<ar[i]<<":::"<<z<<endl;
        update(1,0,n,ar[i],z);
    }

    int main()
    {
        std::ios::sync_with_stdio(false);
        int T;
        T=1;
        // cin.ignore(); must be there when using getline(cin, s)
        while(T--)
        {
            int i,j,a,b;
            cin>>n;
            map<int,int> mp;
            set<int> st;
            for(i=1;i<=n;++i)
            {
                cin>>ar[i];
                st.insert(ar[i]);
            }
            for(i=1;i<=n-1;++i)
            {
                cin>>a>>b;
                v[a].pb(b);
                v[b].pb(a);
            }
            int ct=1;
            build(1,0,n);
            for(auto it:st)
            {
                mp[it]=ct;
                ++ct;
            }
            
            for(i=1;i<=n;++i)
            {
                ar[i]=mp[ar[i]];
            }
            dfs(1,-1);
            for(i=1;i<=n;++i)
            {
                cout<<ans[i]<<endl;
            }
        }
        return 0;
    }


Measured memory_bytes: 48580