#include <bits/stdc++.h>
using namespace std;
template <typename T>
void r1(T &x) {
  x = 0;
  char c(getchar());
  int f(1);
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-') f = -1;
  for (; '0' <= c && c <= '9'; c = getchar()) x = (x * 10) + (c ^ 48);
  x *= f;
}
template <typename T, typename... Args>
inline void r1(T &t, Args &...args) {
  r1(t);
  r1(args...);
}
const int maxn = 2e5 + 5;
const int maxm = maxn << 1;
int head[maxn], cnt;
struct Edge {
  int to, next;
} edg[maxn << 1];
void add(int u, int v) { edg[++cnt] = (Edge){v, head[u]}, head[u] = cnt; }
int n, tot(0);
int lss[maxn * 25], rss[maxn * 25];
struct Seg_ment {
  int t[maxn * 25];
  int ask(int p, int l, int r, int ll, int rr) {
    if (!p) return 0;
    if (ll <= l && r <= rr) return t[p];
    int res(0);
    if (ll <= ((l + r) >> 1))
      res = max(res, ask(lss[p], l, ((l + r) >> 1), ll, rr));
    if (((l + r) >> 1) < rr)
      res = max(res, ask(rss[p], ((l + r) >> 1) + 1, r, ll, rr));
    return res;
  }
  void Insert(int &p, int l, int r, int pos, int c) {
    if (!p) p = ++tot;
    t[p] = max(t[p], c);
    if (l == r) return t[p] = max(t[p], c), void();
    if (pos <= ((l + r) >> 1))
      Insert(lss[p], l, ((l + r) >> 1), pos, c);
    else
      Insert(rss[p], ((l + r) >> 1) + 1, r, pos, c);
  }
} lis, lds;
int ans(0);
void merge(int &p, int v) {
  if (!p || !v) return p += v, void();
  lis.t[p] = max(lis.t[p], lis.t[v]);
  lds.t[p] = max(lds.t[p], lds.t[v]);
  ans = max(ans, lis.t[lss[p]] + lds.t[rss[v]]);
  ans = max(ans, lis.t[lss[v]] + lds.t[rss[p]]);
  merge(lss[p], lss[v]), merge(rss[p], rss[v]);
}
int rt[maxn];
int v[maxn], c[maxn];
void dfs(int p, int fa) {
  int mxlis(0), mxlds(0);
  for (int i = head[p]; i; i = edg[i].next) {
    int to = edg[i].to;
    if (to == fa) continue;
    dfs(to, p);
    int vlis = lis.ask(rt[to], 1, n, 1, v[p] - 1);
    int vlds = lds.ask(rt[to], 1, n, v[p] + 1, n);
    ans = max(ans, max(mxlis + vlds + 1, mxlds + vlis + 1));
    merge(rt[p], rt[to]);
    mxlis = max(mxlis, vlis);
    mxlds = max(mxlds, vlds);
  }
  lis.Insert(rt[p], 1, n, v[p], mxlis + 1);
  lds.Insert(rt[p], 1, n, v[p], mxlds + 1);
}
signed main() {
  int i, j;
  r1(n);
  for (i = 1; i <= n; ++i) r1(v[i]), c[i] = v[i];
  sort(c + 1, c + n + 1);
  int len = unique(c + 1, c + n + 1) - c - 1;
  for (i = 1; i <= n; ++i) v[i] = lower_bound(c + 1, c + len + 1, v[i]) - c;
  for (i = 1; i < n; ++i) {
    int u, v;
    r1(u, v), add(u, v), add(v, u);
  }
  dfs(1, 0);
  printf("%d\n", ans);
  return 0;
}
