#include<functional>
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<set>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;}
template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;}

template <typename T>
class SegTree {
private:
    size_t node_size_;
    vector<T> node_;
    T identity_;                     // ex) INF, 0, etc...
    function<T(T, T)> operation_;    // ex) min, max, sum, etc...
    function<T(T, T)> update_;       // ex) add, change, etc..
public:
    SegTree(size_t node_size, T identity, function<T(T, T)> operation, function<T(T, T)> update):
    identity_(identity), operation_(operation), update_(update) {
        node_size_ = 1;
        while (node_size_ < node_size) node_size_ *= 2;
        node_ = vector<T>(2 * node_size_ - 1, identity);
    }
    void change(int idx, T new_value) {
        int node_idx = idx + node_size_ - 1;
        node_[node_idx] = update_(node_[node_idx], new_value);
        while (node_idx > 0) {
            node_idx = (node_idx - 1) / 2;
            node_[node_idx] = operation_(node_[node_idx * 2 + 1], node_[node_idx * 2 + 2]);
        }
    }
    T _query(int left, int right, int node_idx, int node_left, int node_right) {
        if (node_right <= left || right <= node_left) {
            return identity_;
        }
        if (left <= node_left && node_right <= right) {
            return node_[node_idx];
        }
        else {
            T left_value = _query(left, right, node_idx * 2 + 1, node_left, (node_left + node_right) / 2);
            T right_value = _query(left, right, node_idx * 2 + 2, (node_left + node_right) / 2, node_right);
            return operation_(left_value, right_value);
        }
    }
    T query(int left, int right) {
        return _query(left, right, 0, 0, node_size_);
    }
    T operator[](int idx) {
        return node_[idx + node_size_ - 1];
    }
};

void dfs(int M, const vector<vector<int>>& graph, int node, int parent, const vector<int>& A, SegTree<int>& seg, vector<multiset<int>>& value_log, vector<int>& ans) {
    ll now = seg.query(0, A[node]) + 1;
    if (seg[A[node]] < now) {
        seg.change(A[node], now);
    }
    ans[node] = seg.query(0, M);
    ll now_log = seg[A[node]];
    value_log[A[node]].insert(now_log);
    for (auto& next_node : graph[node]) {
        if (next_node == parent) {
            continue;
        }
        dfs(M, graph, next_node, node, A, seg, value_log, ans);
    }
    value_log[A[node]].erase(value_log[A[node]].find(now_log));
    if (value_log[A[node]].size()) {
        ll next_max = *value_log[A[node]].rbegin();
        seg.change(A[node], next_max);
    } else {
        seg.change(A[node], 0);
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<int> A(N);
    for (auto& a : A) {
        cin >> a;
    }
    int u, v;
    vector<vector<int>> graph(N);
    rep(i, 0, N - 1) {
        cin >> u >> v;
        --u, --v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    map<int, int> value_map;
    set<int> S(all(A));
    int M = 0;
    for (auto& s : S) {
        value_map[s] = M++;
    }
    for (auto& a : A) {
        a = value_map[a];
    }
    SegTree<int> seg(M, 0, [](int a, int b) {return max(a, b);}, [](int a, int b) {return b;});
    vector<multiset<int>> value_log(M);
    vector<int> ans(N);
    dfs(M, graph, 0, -1, A, seg, value_log, ans);
    rep(i, 0, N) {
        cout << ans[i] << endl;
    }
    return 0;
}

Measured memory_bytes: 105380