#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<iostream>
#define pb push_back
#define fi first
#define se second
#define sz(x)  x.size()
#define cl(x)  x.clear()
#define all(x)  x.begin() , x.end()
#define r(x)  scanf("%d", &x)
#define rr(x , y)  scanf("%d%d" , &x , &y)
#define rst(s)  scanf("%s" , s)
#define rl(x)  scanf("%lld" , &x)
#define rrl(x , y)  scanf("%lld%lld" , &x , &y)
#define w(x)  printf("%d" , x)
#define ww(x , y)  printf("%d %d" , x , y)
#define wl(x)  printf("%lld" , x)
#define wwl(x , y)  printf("%lld %lld" , x , y)
#define sp()  printf(" ")
#define en()  puts("")
#define rep(i , x , n)  for(int i = x ; i <= n ; i ++)
#define per(i , n , x)  for(int i = n ; i >= x ; i --)
#define mem0(x)  memset(x , 0 , sizeof(x))
#define mem_1(x)  memset(x , -1 , sizeof(x))
#define mem_inf(x)  memset(x , inf , sizeof(x))
#define mem__inf(x)  memset(x , -inf , sizeof(x))
#define p_queue priority_queue
#define debug(x)  cout << '*' << x << '\n'
#define ddebug(x , y)  cout << '*' << x << ' ' << y << '\n'
#define ios std::ios::sync_with_stdio(false) , cin.tie(0)
using namespace std ;
//mt19937  rnd(chrono::high_resolution_clock::now().time_since_epoch().count()) ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair<int , int> pii ;
typedef pair<ll , ll> pll ;
typedef pair<int , ll> pil ;
typedef pair<ll , int> pli ;
typedef pair<double , double> pdd ;
const int inf = 0x3f3f3f3f ;
const ll mod = 998244353 ;
const int maxn = 2e5 + 5 ;
const int maxm = 30 ;
const double eps = 1e-6 ;
const double pi = acos(-1.0) ;
int n ;
struct node
{
	int x , id ;
	bool operator < (const node &s) const
	{
		if(x != s.x)  return x < s.x ;
		else  return id < s.id ;
	}
} a[maxn] ;
vector<int> g[maxn] ;
int son[maxn] , dfn[maxn] , fa[maxn] ;
int cnt = 0 ;
int dep[maxn] , siz[maxn] , top[maxn] ;
int ans[maxn] ;
void dfs1(int x , int f , int deep)
{
   dep[x] = deep ; 
   fa[x] = f ; 
   siz[x] = 1 ; 
   son[x] = 0 ;
   int maxson = -1 ; 
   for(auto y : g[x])
   {
     if(y == f) continue ; 
     dfs1(y , x , deep + 1) ; 
     siz[x] += siz[y] ; 
     if(siz[y] > maxson)
     son[x] = y , maxson = siz[y] ;
   }
}
void dfs2(int x , int topf)
{
   dfn[x] = ++ cnt ;
   top[x] = topf ; 
   if(!son[x]) return ; 
   dfs2(son[x] , topf) ;
   for(auto y : g[x])
   {
     if(y == fa[x] || y == son[x]) continue ;
     dfs2(y , y) ; 
   }
}
int max1[maxn << 2] ;
int ls(int x)
{
   return x << 1 ;
}
int rs(int x)
{
   return x << 1 | 1 ;
}
void update(int id , int l , int r , int x , int y)
{
   int mid = (l + r) / 2 ;
   if(l == r && l == x){max1[id] = y ; return ;}
   if(x <= mid)  update(ls(id) , l , mid , x , y) ;
   else  update(rs(id) , mid + 1 , r , x , y) ;
   max1[id] = max(max1[ls(id)] , max1[rs(id)]) ;
}
int query(int id , int l , int r , int x , int y)
{
   int ans = 0 ;
   int mid = (l + r) / 2 ;
   if(y < x)  return -1 ; //不合法的询问
   if(x <= l && r <= y)  return max1[id] ;
   if(x <= mid)  ans = max(ans , query(ls(id) , l , mid , x , y)) ;
   if(y > mid)  ans = max(ans , query(rs(id) , mid + 1 , r , x , y)) ;
   return ans ;
}
int q_range(int x , int y)
{
   int ans = 0 ;
   while(top[x] != top[y])
   {
     if(dep[top[x]] < dep[top[y]])
     swap(x , y) ;
     ans = max(ans , query(1 , 1 , n , dfn[top[x]] , dfn[x])) ;
     x = fa[top[x]] ;
   }
   if(dep[x] > dep[y])
   swap(x , y) ;
   ans = max(ans , query(1 , 1 , n , dfn[x] , dfn[y])) ;
   return ans ;
}
void solve()
{
	rep(i , 1 , n)
	{
		int j = i ;
		while(j + 1 <= n && a[j + 1].x == a[j].x)  j ++ ;
		rep(k , i , j)
		{
			int x = q_range(1 , a[k].id) ;
			ans[a[k].id] = x + 1 ;
		}
		rep(k , i , j)
		  update(1 , 1 , n , dfn[a[k].id] , ans[a[k].id]) ;
		i = j ;
	}
}
void dfs3(int fa , int u)
{
	for(auto v : g[u])
    {
       if(v == fa)  continue ;
       ans[v] = max(ans[v] , ans[u]) ;
       dfs3(u , v) ; 
    }
}
int main()
{
	r(n) ;
	rep(i , 1 , n)  r(a[i].x) , a[i].id = i ;
	sort(a + 1 , a + n + 1) ;
	rep(i , 1 , n - 1)
	{
		int u , v ;
		rr(u , v) ;
		g[u].pb(v) , g[v].pb(u) ;
	}
	dfs1(1 , 0 , 1) ;
	dfs2(1 , 1) ;
	mem0(max1) ;
	solve() ;
	dfs3(1 , 1) ;
	rep(i , 1 , n)  w(ans[i]) , en() ; 
	return 0 ;
}

Measured memory_bytes: 39184