#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"

using namespace std;
using namespace __gnu_pbds;

template <class T> bool MIN(T &a, T b)                 { return a > b ? (a = b, true) : false; }
template <class T> bool MAX(T &a, T b)                 { return a < b ? (a = b, true) : false; }

template <class Node_CItr, class Node_Itr, class Cmp_Fn, class _Alloc>
struct my_update_policy {
      virtual Node_CItr node_begin() const = 0;
      virtual Node_CItr node_end() const = 0;
      typedef int metadata_type;

      int prefix_max(int x) { // sum of indices <= x
            int ans = 0;
            auto it = node_begin();
            while (it != node_end()) {
                  auto l = it.get_l_child();
                  auto r = it.get_r_child();
                  if (Cmp_Fn()(x, (*it)->first)) {
                        it = l;
                  }
                  else {
                        MAX(ans, (*it)->second);
                        if (l != node_end()) {
                              MAX(ans, l.get_metadata());
                        }
                        it = r;
                  }
            }
            return ans;
      }
      void operator()(Node_Itr it, Node_CItr end_it) {
            auto l = it.get_l_child();
            auto r = it.get_r_child();
            int lft = 0;
            int rgt = 0;
            if (l != node_end()) lft = l.get_metadata();
            if (r != node_end()) rgt = r.get_metadata();
            const_cast <int&> (it.get_metadata()) = max({lft, rgt, (*it)->second});
      }
};

template <class T> using ordermap = tree <T, T, less <T>, rb_tree_tag, my_update_policy>;

const int maxn = 2e5 + 5;

int n;
vector < vector <int> > graph;
int arr[maxn];
int bad[maxn];
int dp[maxn];

ordermap <int> om;

void dfs(int u = 1, int p = 0) {
      int val = arr[u];
      int maxi = om.prefix_max(val - 1);
      int val_lis = maxi + 1;
      auto it = om.find(val);
      if (it == om.end()) {
            bad[u] = -1;
            om.insert(make_pair(val, val_lis));
      }
      else {
            bad[u] = om[val];
            om.erase(val);
            om.insert(make_pair(val, val_lis));
      }
      int lis = om.prefix_max(n);
      dp[u] = lis;
      for (int v : graph[u]) {
            if (v == p) {
                  continue;
            }
            dfs(v, u);
      }
      om.erase(val);
      if (bad[u] != -1) {
            om.insert(make_pair(val, bad[u]));
      }
}

signed main()
{
      ios_base::sync_with_stdio(false);
      cin.tie(nullptr); cout.tie(nullptr);
      cout.precision(12);

      bool FILEIO = 0;
      if (FILEIO and fopen("in.txt", "r")) {
            freopen("in.txt", "r", stdin);
      }

      cin >> n;
      map <int, int> mp;
      for (int i = 1; i <= n; i++) {
            cin >> arr[i];
            mp[ arr[i] ];
      }
      int ptr = 0;
      for (auto &it : mp) {
            it.second = ++ptr;
      }
      for (int i = 1; i <= n; i++) {
            arr[i] = mp[ arr[i] ];
      }
      graph.resize(n + 1);
      for (int e = 1; e < n; e++) {
            int u, v;
            cin >> u >> v;
            graph[u].push_back(v);
            graph[v].push_back(u);
      }
      dfs();
      for (int i = 1; i <= n; i++) {
            cout << dp[i] << endl;
      }
}


Measured memory_bytes: 42940