#include <bits/stdc++.h>
using namespace std;
struct IO {
  char buf[(1 << 20)], *p1, *p2;
  char pbuf[(1 << 20)], *pp;
  IO() : p1(buf), p2(buf), pp(pbuf) {}
  inline char gc() {
    if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, (1 << 20), stdin);
    return p1 == p2 ? ' ' : *p1++;
  }
  inline bool blank(char ch) {
    return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
  }
  template <class T>
  inline void read(T &x) {
    register double tmp = 1;
    register bool sign = 0;
    x = 0;
    register char ch = gc();
    for (; !(ch >= '0' && ch <= '9'); ch = gc())
      if (ch == '-') sign = 1;
    for (; (ch >= '0' && ch <= '9'); ch = gc()) x = x * 10 + (ch - '0');
    if (ch == '.')
      for (ch = gc(); (ch >= '0' && ch <= '9'); ch = gc())
        tmp /= 10.0, x += tmp * (ch - '0');
    if (sign) x = -x;
  }
  inline void read(char *s) {
    register char ch = gc();
    for (; blank(ch); ch = gc())
      ;
    for (; !blank(ch); ch = gc()) *s++ = ch;
    *s = 0;
  }
  inline void read(char &c) {
    for (c = gc(); blank(c); c = gc())
      ;
  }
  template <class t>
  inline void write(t x) {
    if (x < 0)
      putchar('-'), write(-x);
    else {
      if (x > 9) write(x / 10);
      putchar('0' + x % 10);
    }
  }
} io;
const int mod = 1e9 + 7;
const int mo = 998244353;
const int N = 6005;
int n, m, ans, ds, a[N], b[N], Rt[N];
int t[N * 32][2], ls[N * 32], rs[N * 32], lis, lds;
vector<int> G[N];
inline void fix(int &x, int l, int r, int pos, int val, int type) {
  if (!x) x = ++ds;
  if (l == r) {
    t[x][type] = max(t[x][type], val);
    return;
  }
  int mid = l + r >> 1;
  if (pos <= mid)
    fix(ls[x], l, mid, pos, val, type);
  else
    fix(rs[x], mid + 1, r, pos, val, type);
  t[x][type] = max(t[ls[x]][type], t[rs[x]][type]);
}
inline int Ask(int x, int l, int r, int ll, int rr, int type) {
  if (!x) return 0;
  if (ll <= l && r <= rr) return t[x][type];
  int mid = l + r >> 1;
  int cwy = 0;
  if (ll <= mid) cwy = max(cwy, Ask(ls[x], l, mid, ll, rr, type));
  if (rr > mid) cwy = max(cwy, Ask(rs[x], mid + 1, r, ll, rr, type));
  return cwy;
}
inline int merge(int x, int y) {
  if (!x || !y) return x | y;
  t[x][0] = max(t[x][0], t[y][0]);
  t[x][1] = max(t[x][1], t[y][1]);
  ans = max(ans, t[ls[x]][0] + t[rs[y]][1]);
  ans = max(ans, t[rs[x]][1] + t[ls[y]][0]);
  ls[x] = merge(ls[x], ls[y]);
  rs[x] = merge(rs[x], rs[y]);
  return x;
}
inline void dfs(int u, int fa) {
  int lis = 0, lds = 0;
  for (int i = (0); i <= ((int)G[u].size() - 1); i++) {
    int v = G[u][i];
    if (v == fa) continue;
    dfs(v, u);
    int vlis = Ask(Rt[v], 1, m, 1, a[u] - 1, 0);
    int vlds = Ask(Rt[v], 1, m, a[u] + 1, m, 1);
    ans = max(ans, max(lis + vlds, lds + vlis) + 1);
    lis = max(lis, vlis);
    lds = max(lds, vlds);
    Rt[u] = merge(Rt[u], Rt[v]);
  }
  fix(Rt[u], 1, m, a[u], lis + 1, 0);
  fix(Rt[u], 1, m, a[u], lds + 1, 1);
}
int main() {
  io.read(n);
  for (int i = (1); i <= (n); i++) io.read(a[i]), b[i] = a[i];
  sort(b + 1, b + n + 1);
  m = unique(b + 1, b + n + 1) - b - 1;
  for (int i = (1); i <= (n); i++)
    a[i] = lower_bound(b + 1, b + m + 1, a[i]) - b;
  for (int i = (1); i <= (n - 1); i++) {
    int x, y;
    io.read(x), io.read(y);
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(1, 0);
  io.write(ans);
  return 0;
}
