#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
using llong = long long;


//===
template<typename Monoid, typename OP = function<Monoid(Monoid, Monoid)> >
struct SegmentTree {
    //    using OP = function<Monoid(Monoid, Monoid)>;
    
    vector<Monoid> tree;
    int size;
    const OP merge_monoid; // bin' operation
    const Monoid e; // neutral element
                                           
    SegmentTree(const Monoid &e, const OP &f):
        merge_monoid(f), e(e) {};
    void init(int nmemb) {
        size = nmemb;
        tree.assign(size << 1, e);
    };

    inline void update(int k, Monoid dat) {
        k += size;
        tree[k] = dat;
        
        while(k > 1) {
            k >>= 1;
            tree[k] = merge_monoid(tree[(k << 1)], tree[(k << 1) | 1]);
        }
    };

    // [l, r)
    inline Monoid fold(int l, int r) {
        l += size; //points leaf
        r += size;

        Monoid lv = e;
        Monoid rv = e;
        while (l < r) {
            if (l & 1) lv = merge_monoid(lv, tree[l++]);
            if (r & 1) rv = merge_monoid(tree[--r], rv);

            l >>= 1;
            r >>= 1;
        }

        return merge_monoid(lv, rv);
    };

    inline Monoid operator[] (const int k) const {
        return tree[k + size];
    };
};
//===

class HLD {
private:
    void dfs_sz(int v) {
        for(int &u:G[v])
            if(u==par[v]) swap(u,G[v].back());
        if(~par[v]) G[v].pop_back();

        for(int &u:G[v]){
            par[u]=v;
            dep[u]=dep[v]+1;
            dfs_sz(u);
            sub[v]+=sub[u];
            if(sub[u]>sub[G[v][0]]) swap(u,G[v][0]);
        }
    }

    void dfs_hld(int v,int c,int &pos) {
        vid[v]=pos++;
        inv[vid[v]]=v;
        type[v]=c;
        for(int u:G[v]){
            if(u==par[v]) continue;
            head[u]=(u==G[v][0]?head[v]:u);
            dfs_hld(u,c,pos);
        }
    }

public:
    vector< vector<int> > G;
    vector<int> vid, head, sub, par, dep, inv, type;

    HLD(int n):
        G(n),vid(n,-1),head(n),sub(n,1),
        par(n,-1),dep(n,0),inv(n),type(n){}

    void add_edge(int u,int v) {
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    void build(vector<int> rs={0}) {
        int c=0,pos=0;
        for(int r:rs){
            dfs_sz(r);
            head[r]=r;
            dfs_hld(r,c++,pos);
        }
    }

    int lca(int u,int v){
        while(1){
            if(vid[u]>vid[v]) swap(u,v);
            if(head[u]==head[v]) return u;
            v=par[head[v]];
        }
    }

    int distance(int u,int v){
        return dep[u]+dep[v]-2*dep[lca(u,v)];
    }

    // for_each(vertex)
    // [l, r) <- attention!!
    template<typename F>
    void for_each(int u, int v, const F& f) {
        while(1){
            if(vid[u]>vid[v]) swap(u,v);
            f(max(vid[head[v]],vid[u]),vid[v]+1);
            if(head[u]!=head[v]) v=par[head[v]];
            else break;
        }
    }

    template<typename T,typename Q,typename F>
    T for_each(int u,int v,T ti,const Q &q,const F &f){
        T l=ti,r=ti;
        while(1){
            if(vid[u]>vid[v]){
                swap(u,v);
                swap(l,r);
            }
            l=f(l,q(max(vid[head[v]],vid[u]),vid[v]+1));
            if(head[u]!=head[v]) v=par[head[v]];
            else break;
        }
        return f(l,r);
    }

    // for_each(edge)
    // [l, r) <- attention!!
    template<typename F>
    void for_each_edge(int u, int v,const F& f) {
        while(1){
            if(vid[u]>vid[v]) swap(u,v);
            if(head[u]!=head[v]){
                f(vid[head[v]],vid[v]+1);
                v=par[head[v]];
            }else{
                if(u!=v) f(vid[u]+1,vid[v]+1);
                break;
            }
        }
    }
};

using UnWeightedGraph = vector<vector<int> >;

llong n;
llong a[200005];
vector<tuple<llong, llong>> p;
UnWeightedGraph g;

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];

        p.emplace_back(a[i], i);
    }
    //g.resize(n);
    HLD tree(n);
    for (int i = 0; i < n - 1; i++) {
        llong u, v;
        cin >> u >> v;
        --u, --v;

        tree.add_edge(u, v);
        //g[u].push_back(v);
        //g[v].push_back(u);
    }

    auto ti = make_pair(0ll, (1ll << 60ll));
    SegmentTree<pair<llong, llong>> seg(ti, [](auto a, auto b){ return max(a, b); });
    seg.init(n);
    //HeavyLightDecomposition<UnWeightedGraph> tree(g);
    tree.build();

    auto f = [&](int a, int b) { return seg.fold(a, b); };
    auto g = [](pair<llong, llong> a, pair<llong, llong> b) { return max(a, b); };

    sort(p.begin(), p.end());
    for (int i = 0; i < n; i++) {
        auto [a, u] = p[i];
        auto mv = tree.for_each(0, u, ti, f, g);
        if (-(mv.second) == a) {
            seg.update(tree.vid[u], mv);
        }
        else {
            mv.first += 1;
            mv.second = -a;
            seg.update(tree.vid[u], mv);
        }
    }

    for (int i = 0; i < n; i++) {
        cout << tree.for_each(0, i, ti, f, g).first << endl;
    }

    return 0;
}


Measured memory_bytes: 44656