#include        "algorithm"
#include        "iostream"
#include        "numeric"
#include        "iomanip"
#include        "cstring"
#include        "math.h"
#include        "bitset"
#include        "string"
#include        "vector"
#include        "ctime"
#include        "queue"
#include        "stack"
#include        "map"
#include        "set"

#include        "ext/pb_ds/assoc_container.hpp" // Common file
#include        "ext/pb_ds/tree_policy.hpp" // Including tree_order_statistics_node_update
#include        "ext/pb_ds/detail/standard_policies.hpp"

using namespace std;
using namespace __gnu_pbds;


#define          f first
#define          lgn 25
#define          endl '\n'
#define          sc second
#define          pb push_back
#define          N (int)2e5+5
#define          PI acos(-1.0)
#define          int long long 
#define          vi vector<int>
#define          mod 1000000007
#define          ld long double
#define          eb emplace_back
#define          mii map<int,int>
#define          vpii vector<pii>
#define          pii pair<int,int>
#define          pq priority_queue
#define          BLOCK (int)sqrt(N)
#define          test(x) while(x--)
#define          all(x) begin(x),end(x)
#define          allr(x) x.rbegin(),x.rend()
#define          fo(i,a,n) for(int i=a;i<n;i++)
#define          rfo(i,n,a) for(int i=n;i>=a;i--)
#define          FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define          time() cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n"
#define            bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)

typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update > 
OS ;

template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
     const char* comma = strchr (names + 1, ',');
     cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;

int n,m,k,q;
string s;
vi adj[N];
vi a , ans;
mii mp;

          
struct T
{
     int ans;
};

struct SegmentTree
{
     T segTree[4*N+1];

     T combine( T a , T b)
     {
          T now;
          now.ans = max( a.ans , b.ans );
          return now;
     }

     void build(int v = 1 , int tl = 0, int tr = n-1 )
     {
          if( tl == tr )
          {
               segTree[v].ans = 0;
               return;
          }

          int tm = tl+tr>>1;

          build( v<<1 , tl , tm);
          build( v<<1|1 , tm+1 , tr);

          segTree[v] = combine( segTree[v<<1] , segTree[v<<1|1] );
     }

     T query( int l , int r , int v = 1 , int tl = 0 , int tr = n)
     {
          if( tl > r or tr < l ) return {0};

          if( tl == l and tr == r ) return segTree[v];
          
          int tm = tl+tr>>1;

          T aa = query( l , min( r , tm) , v<<1 , tl , tm );
          T bb = query( max( l, tm+1) , r , v<<1|1 , tm+1 , tr );

          return combine(aa , bb);
     }

     void update( int pos , int x , int v = 1 , int tl = 0 , int tr = n)
     {
          if( tl == tr )
          {
               segTree[v].ans = x;
               return;
          }

          int tm = tl+tr>>1;

          if( tm >= pos ) update( pos , x , v<<1 , tl , tm  );
          else            update( pos , x , v<<1|1 , tm+1 , tr);

          segTree[v] = combine( segTree[v<<1] , segTree[v<<1|1] );
     }

}seg;


void dfs(int i = 0 , int p = -1)
{
     int len = seg.query(0, a[i] - 1).ans;
     int now = seg.query( a[i] , a[i] ).ans;

     seg.update( a[i] , len + 1);
     int res = seg.query(0 , n).ans;

     ans[i] = res;

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

     seg.update(a[i] , now );

}

void Compress()
{
     mp.clear();
     int id = 0;

     fo(i,0,n) mp[a[i]] = 1;

     for( auto &j : mp )
     {
          j.sc = id++;
     }

     fo(i,0,n) a[i] = mp[a[i]];
}

void go()
{
     cin >> n;
     a.resize(n);
     ans.assign(n, 1);

     fo(i,0,n) cin >> a[i];

     Compress();

     fo(i,0,n-1)
     {
          int u , v;
          cin >> u >> v;
          --u , --v;

          adj[u].pb(v);
          adj[v].pb(u);
     }

     dfs();

     fo(i,0,n) cout << ans[i] << endl;
}

int32_t main()
{
     FAST;     
     int t=1; 
     // cin>>t;
     test(t) go();
}

 

Measured memory_bytes: 45524