#include <bits/stdc++.h>
using namespace std;
const int N = 6000 + 10, M = (N << 1);
template <typename Tp>
void read(Tp &x) {
  x = 0;
  int op = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') op = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= op;
}
template <typename Tp>
void CMax(Tp &x, Tp y) {
  if (y > x) x = y;
}
template <typename Tp>
void CMin(Tp &x, Tp y) {
  if (y < x) x = y;
}
int n, a[N], num[N], ind;
int head[N], nxt[M], to[M], tot;
void add_edge(int x, int y) {
  to[++tot] = y;
  nxt[tot] = head[x];
  head[x] = tot;
}
void add(int x, int y) {
  add_edge(x, y);
  add_edge(y, x);
}
short f[N][N], g[N][N], pre[N], suf[N], ans;
void DP(int x, int fa) {
  f[x][a[x]] = 1;
  g[x][a[x]] = 1;
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    if (y == fa) continue;
    DP(y, x);
    for (int j = 1; j <= ind; ++j) pre[j] = max(pre[j - 1], f[x][j]);
    for (int j = ind; j >= 1; --j) suf[j] = max(suf[j + 1], g[x][j]);
    for (int j = 1; j <= ind; ++j) {
      CMax(ans, (short)(g[y][j] + pre[j - 1]));
      CMax(ans, (short)(f[y][j] + suf[j + 1]));
      CMax(f[x][j], f[y][j]);
      CMax(g[x][j], g[y][j]);
      if (j < a[x]) CMax(f[x][a[x]], (short)(f[y][j] + 1));
      if (j > a[x]) CMax(g[x][a[x]], (short)(g[y][j] + 1));
    }
  }
}
int main() {
  read(n);
  for (int i = (1); i <= (n); ++i) read(a[i]), num[++ind] = a[i];
  sort(num + 1, num + ind + 1);
  ind = unique(num + 1, num + ind + 1) - num - 1;
  for (int i = (1); i <= (n); ++i)
    a[i] = lower_bound(num + 1, num + ind + 1, a[i]) - num;
  int u, v;
  for (int i = (2); i <= (n); ++i) read(u), read(v), add(u, v);
  DP(1, 0);
  printf("%d", ans);
  return 0;
}
