#include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iomanip>
#include <fstream>
#include <unordered_map>
#include <unordered_set>
#include <assert.h>
using namespace std;


#define REP(i, n) for(ll i = 0;i < n;i++)
#define REPR(i, n) for(ll i = n;i >= 0;i--)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
#define FORR(i, m, n) for(ll i = m;i >= n;i--)
#define REPO(i, n) for(ll i = 1;i <= n;i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(),n.end()
#define MOD 1000000007
#define P pair<ll, ll>

struct SegTree { //せぐつりー！
	ll sz;
	vector<ll> dat;
	SegTree(ll n) {
		sz = 1;
		while (sz < n)sz *= 2;
		dat.resize(2 * sz - 1);//
	}
	void update(ll a, ll x) {
		a += sz - 1;//
		dat[a] = x;
		while (a > 0) {
			a = (a - 1) / 2;
			dat[a] = max(dat[a * 2 + 1], dat[a * 2 + 2]);//
		}
	}
	ll query(ll a, ll b, ll k = 0, ll l = 0, ll r = -1) {
		if (r < 0) r = sz;
		if (r <= a or b <= l)return 0;//
		if (a <= l and r <= b)return dat[k];//
		ll q1, q2;
		q1 = query(a, b, k * 2 + 1, l, (l + r) / 2);
		q2 = query(a, b, k * 2 + 2, (l + r) / 2, r);
		return max(q1, q2);//
	}
};
struct HLD {//いつもの
	vector<ll> g[510000];
	vector<ll> sz, in, out, head, rev, par, dep; //rev = in -> index
	HLD(ll n) : sz(n), in(n), out(n), head(n), rev(n), par(n), dep(n) {};
	void dfs_sz(ll a, ll p, ll b) {
		par[a] = p;
		sz[a] = 1;
		dep[a] = b;
		if (g[a].size() && g[a][0] == p) swap(g[a][0], g[a].back());
		for (auto& to : g[a]) {
			if (to == p)continue;
			dfs_sz(to, a, b + 1);
			sz[a] += sz[to];
			if (sz[g[a][0]] < sz[to])swap(g[a][0], to);
		}
	}
	void dfs_hld(ll a, ll p, ll& t) {
		in[a] = t++;
		rev[in[a]] = a;
		for (auto& to : g[a]) {
			if (to == p)continue;
			head[to] = (g[a][0] == to ? head[a] : to);
			dfs_hld(to, a, t);
		}
		out[a] = t;
	}
	void edgeset(ll a, ll b) {
		g[a].push_back(b);
		g[b].push_back(a);
	}
	void build() {
		dfs_sz(0, -1, 0);
		ll t = 0;
		dfs_hld(0, -1, t);
	}
	void input(ll n) {
		REP(i, n - 1) {
			ll a, b;
			cin >> a >> b;
			a--; b--;
			edgeset(a, b);
		}
		build();
	}
	ll la(ll a, ll x) {
		while (1) {
			ll h = head[a];
			if (in[a] - x >= in[h])return rev[in[a] - x];
			x -= in[a] - in[h] + 1;
			a = par[h];
		}
	}
	ll lca(ll a, ll b) {
		for (;; b = par[head[b]]) {
			if (in[a] > in[b])swap(a, b);
			if (head[a] == head[b])return a;
		}
	}
	template< typename Q, typename F >
	ll query(ll a, ll b, ll ti, const Q& q, const F& f, bool edge = false) {
		ll l = ti, r = ti;
		for (;; b = par[head[b]]) {
			if (in[a] > in[b])swap(a, b), swap(l, r);
			if (head[a] == head[b])break;
			l = f(q(in[head[b]], in[b] + 1), l);
		}
		return f(f(q(in[a] + edge, in[b] + 1), l), r);
	}
	template < typename Q >
	void addpath(ll a, ll b, const Q& q, bool edge = false) { //path
		for (;; b = par[head[b]]) {
			if (in[a] > in[b])swap(a, b);
			if (head[a] == head[b])break;
			q(in[head[b]], in[b] + 1);
		}
		return q(in[a] + edge, in[b] + 1);
	}
	template < typename Q >
	void addst(ll a, ll x, const Q& q) { //subtree
		q(in[a], out[a], x);
	}
};

ll n;
SegTree seg(810000);
HLD tree(410000);
vector<P> s;
int main() {
	cin >> n;
	REP(i, n) {
		ll a;
		cin >> a;
		s.push_back(P(a, i));
	}
	sort(ALL(s));
	s.push_back(P(INF, INF));
	tree.input(n);
	vector<P> q;
	REP(i, n) {
		auto Q = [&](ll a, ll b) { return seg.query(a, b); };
		auto F = [&](ll a, ll b) { return max(a, b); };
		ll pos = s[i].second;
		ll dp = tree.query(0, pos, 0, Q, F) + 1;

		q.push_back(P(pos, dp));
		if (s[i].first != s[i + 1].first) {
			while (!q.empty()) {
				seg.update(tree.in[q.back().first], q.back().second);
				q.pop_back();
			}
		}
	}
	REP(i, n) {
		auto Q = [&](ll a, ll b) { return seg.query(a, b); };
		auto F = [&](ll a, ll b) { return max(a, b); };
		ll dp = tree.query(0, i, 0, Q, F);
		cout << dp << endl;
	}
}



Measured memory_bytes: 77656