#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; }



// 抽象化したセグメント木
template<class Monoid> struct SegTree {
    using Func = function<Monoid(Monoid, Monoid)>;
    const Func F;
    const Monoid UNITY;
    int SIZE_R;
    vector<Monoid> dat;

    SegTree(int n, const Func f, const Monoid &unity): F(f), UNITY(unity) { init(n); }
    void init(int n) {
        SIZE_R = 1;
        while (SIZE_R < n) SIZE_R *= 2;
        dat.assign(SIZE_R * 2, UNITY);
    }

    /* set, a is 0-indexed */
    void set(int a, const Monoid &v) { dat[a + SIZE_R] = v; }
    void build() {
        for (int k = SIZE_R - 1; k > 0; --k)
            dat[k] = F(dat[k*2], dat[k*2+1]);
    }

    /* update a, a is 0-indexed */
    void update(int a, const Monoid &v) {
        int k = a + SIZE_R;
        dat[k] = v;
        while (k >>= 1) dat[k] = F(dat[k*2], dat[k*2+1]);
    }

    /* get [a, b), a and b are 0-indexed */
    Monoid get(int a, int b) {
        Monoid vleft = UNITY, vright = UNITY;
        for (int left = a + SIZE_R, right = b + SIZE_R; left < right; left >>= 1, right >>= 1) {
            if (left & 1) vleft = F(vleft, dat[left++]);
            if (right & 1) vright = F(dat[--right], vright);
        }                                                                                                              
        return F(vleft, vright);
    }
    inline Monoid operator [] (int a) { return dat[a + SIZE_R]; }

    /* debug */
    void print() {
        for (int i = 0; i < SIZE_R; ++i) {
            cout << (*this)[i];
            if (i != SIZE_R-1) cout << ",";
        }
        cout << endl;
    }
};



using Graph = vector<vector<int>>;
int N;
Graph G;
vector<long long> a, val;
vector<int> cmp;

void dfs(int v, int p, int& ord) {
    cmp[v] = ord;
    for (auto e : G[v]) {
        if (e == p) continue;
        dfs(e, v, ++ord);
    }
}

void rec(int v, int p, SegTree<int> &dp, vector<int> &res, map<int,int> &prev) {
    int g = dp.get(0, val[v]);
    prev[g+1]++;
    res[v] = prev.rbegin()->first;

    /*
    COUT("----");
    COUT(v);
    COUT(val[v]);
    dp.print();
    COUT(prev);
    */

    dp.update(val[v], g+1);
    for (auto e : G[v]) {
        if (e == p) continue;
        rec(e, v, dp, res, prev);
    }
    dp.update(val[v], 0);
    prev[g+1]--;
    if (prev[g+1] == 0) prev.erase(g+1);
}


void solve() {
    cmp.resize(N);
    int ord = 0;
    dfs(0, -1, ord);
    map<int, vector<int> > ma;
    for (int i = 0; i < N; ++i) ma[a[i]].push_back(i);
    val.assign(N, -1);
    int iter = 0;
    for (auto it : ma) {
        auto &v = it.second;
        sort(v.begin(), v.end(), [&](int i, int j) {
            return cmp[i] > cmp[j];
        });
        for (auto v : it.second) {
            val[v] = iter++;
        }
    }

    //COUT(val);

    SegTree<int> dp(N+1,
                    [](int a, int b) { return max(a, b); },
                    0);
    vector<int> res(N, -1);
    map<int,int> prev;
    rec(0, -1, dp, res, prev);

    for (auto v : res) cout << v << endl;
}

int main() {
    while (cin >> N) {
        a.resize(N);
        for (int i = 0; i < N; ++i) cin >> a[i];
        G.assign(N, vector<int>());
        for (int i = 0; i < N-1; ++i) {
            int u, v;
            cin >> u >> v;
            --u, --v;
            G[u].push_back(v);
            G[v].push_back(u);
        }
        solve();
    }

}

















Measured memory_bytes: 66980