#include <bits/stdc++.h>
using namespace std;
const int NMAX = 6100, EMAX = 13000, VMAX = 6100;
struct Edge {
  int x;
  int y;
  int next;
};
int N, R[NMAX], ans;
short f[NMAX][NMAX], g[NMAX][NMAX];
int V[VMAX], eid;
Edge E[EMAX];
void insert(int x, int y) {
  E[++eid] = (Edge){x, y, V[x]};
  V[x] = eid;
}
int calc(int x, int y) {
  int i, j, res = max(f[x][0], g[y][0]);
  for (i = 1, j = g[y][0]; i <= f[x][0]; i += 1) {
    while (j && R[f[x][i]] >= R[g[y][j]]) j -= 1;
    res = max(res, i + j);
  }
  return res;
}
void DFS(int pre, int x) {
  int i, j, y, l, r, m;
  f[x][0] = g[x][0] = 1;
  f[x][1] = g[x][1] = x;
  for (i = V[x]; i; i = E[i].next) {
    if ((y = E[i].y) == pre) continue;
    DFS(x, y);
    ans = max(ans, calc(x, y));
    ans = max(ans, calc(y, x));
    if (R[x] <= R[f[y][1]])
      f[y][1] = x;
    else if (R[f[y][f[y][0]]] < R[x])
      f[y][++f[y][0]] = x;
    else {
      for (l = 1, r = f[y][0]; l + 1 < r;) {
        m = (l + r) >> 1;
        if (R[f[y][m]] < R[x])
          l = m;
        else
          r = m;
      }
      f[y][r] = x;
    }
    if (R[x] >= R[g[y][1]])
      g[y][1] = x;
    else if (R[g[y][g[y][0]]] > R[x])
      g[y][++g[y][0]] = x;
    else {
      for (l = 1, r = g[y][0]; l + 1 < r;) {
        m = (l + r) >> 1;
        if (R[g[y][m]] > R[x])
          l = m;
        else
          r = m;
      }
      g[y][r] = x;
    }
    f[x][0] = max(f[x][0], f[y][0]);
    g[x][0] = max(g[x][0], g[y][0]);
    for (j = 1; j <= f[y][0]; j += 1) {
      f[x][j] = f[x][j] && R[f[x][j]] < R[f[y][j]] ? f[x][j] : f[y][j];
    }
    for (j = 1; j <= g[y][0]; j += 1) {
      g[x][j] = g[x][j] && R[g[x][j]] > R[g[y][j]] ? g[x][j] : g[y][j];
    }
  }
}
int main() {
  int i, x, y;
  scanf("%d", &N);
  for (i = 1; i <= N; i += 1) scanf("%d", &R[i]);
  for (i = 1; i < N; i += 1) {
    scanf("%d %d", &x, &y);
    insert(x, y);
    insert(y, x);
  }
  DFS(-1, 1);
  printf("%d\n", ans);
  exit(0);
}
