#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast", "-funroll-loops", "-fdelete-null-pointer-checks")
#pragma GCC target("ssse3", "sse3", "sse2", "sse", "avx2", "avx")
using std::lower_bound;
using std::max;
using std::min;
using std::vector;
const int MAXN = 6e3 + 50;
vector<int> E[MAXN];
inline void addedge(int u, int v) { E[u].push_back(v), E[v].push_back(u); }
int Ans = 1;
int a[MAXN];
inline void add(vector<int> &v, const int &ai) {
  int len = lower_bound(v.begin(), v.end(), ai) - v.begin() - 1;
  if (len + 1 >= (int)v.size())
    v.push_back(ai);
  else
    v[len + 1] = min(v[len + 1], ai);
}
void updata_ans(const vector<int> &v1, const vector<int> &v2) {
  for (int len_v1 = 0; len_v1 < (int)v1.size(); ++len_v1)
    Ans =
        max(Ans, len_v1 + (int)(lower_bound(v2.begin(), v2.end(), -v1[len_v1]) -
                                v2.begin() - 1));
}
inline void vector_free_memory(vector<int> &v) {
  vector<int> tmp;
  v.swap(tmp);
}
void merge(vector<int> &v1, vector<int> &v2) {
  for (int i = 0; i < (int)v2.size(); ++i) {
    if (i >= (int)v1.size())
      v1.push_back(v2[i]);
    else
      v1[i] = min(v1[i], v2[i]);
  }
}
vector<int> tail_LIS[MAXN];
vector<int> tail_LDS[MAXN];
void dfs(int u, int fa) {
  tail_LIS[u].push_back(-0x7f7f7f7f), tail_LIS[u].push_back(a[u]);
  tail_LDS[u].push_back(-0x7f7f7f7f), tail_LDS[u].push_back(-a[u]);
  for (int v : E[u])
    if (v != fa) {
      dfs(v, u);
      tail_LIS[u].swap(tail_LIS[v]), tail_LIS[u].swap(tail_LIS[v]);
      tail_LDS[u].swap(tail_LDS[v]), tail_LDS[u].swap(tail_LDS[v]);
      add(tail_LIS[v], a[u]), add(tail_LDS[v], -a[u]);
      updata_ans(tail_LIS[u], tail_LDS[v]);
      updata_ans(tail_LIS[v], tail_LDS[u]);
      merge(tail_LIS[u], tail_LIS[v]);
      merge(tail_LDS[u], tail_LDS[v]);
    }
}
inline int read() {
  int x = 0;
  char c = getchar();
  bool f = 0;
  while (c < '0' || c > '9') (c == '-') ? f = 1, c = getchar() : c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (48 ^ c), c = getchar();
  return (f) ? -x : x;
}
int main() {
  int n = read();
  for (int i = 1; i <= n; ++i) a[i] = read();
  for (int i = 0; i < n - 1; ++i) addedge(read(), read());
  dfs(1, 0);
  printf("%d", Ans);
}
