#include <bits/stdc++.h>
using namespace std;
int n, res = 0;
int a[6004], ft[6004], fg[6004], p[6004];
int t[6004][5004], g[6004][5004];
vector<vector<int> > v(6004);
void dfs(int u) {
  int i, j, k, x;
  p[u] = 1;
  fg[u] = 1;
  g[u][1] = a[u];
  for (i = 0; i < v[u].size(); i++)
    if (p[v[u][i]] == 0) {
      x = v[u][i];
      for (j = 0; j <= ft[u]; j++) t[x][j] = min(t[x][j], t[u][j]);
      ft[x] = max(ft[u], ft[x]);
      for (j = ft[x]; j >= 0; j--)
        if (a[x] > t[x][j]) break;
      if (j == ft[x]) {
        ft[x]++;
        t[x][ft[x]] = a[x];
      } else if (t[x][j + 1] > a[x])
        t[x][j + 1] = a[x];
      res = max(res, ft[x]);
      dfs(x);
      for (j = 0; j <= fg[x]; j++) g[u][j] = min(g[u][j], g[x][j]);
      fg[u] = max(fg[u], fg[x]);
      for (j = fg[u]; j >= 0; j--)
        if (a[u] > g[u][j]) break;
      if (j == fg[u]) {
        fg[u]++;
        g[u][fg[u]] = a[u];
      } else if (g[u][j + 1] > a[u])
        g[u][j + 1] = a[u];
      for (j = 0; j <= fg[u]; j++) t[u][j] = min(t[u][j], g[u][j]);
      ft[u] = max(ft[u], fg[u]);
      res = max(res, ft[u]);
    }
}
void dfs2(int u) {
  int i, j, k, x;
  p[u] = 2;
  fg[u] = 1;
  g[u][1] = a[u];
  for (i = v[u].size() - 1; i >= 0; i--)
    if (p[v[u][i]] == 1) {
      x = v[u][i];
      for (j = 0; j <= ft[u]; j++) t[x][j] = min(t[x][j], t[u][j]);
      ft[x] = max(ft[u], ft[x]);
      for (j = ft[x]; j >= 0; j--)
        if (a[x] > t[x][j]) break;
      if (j == ft[x]) {
        ft[x]++;
        t[x][ft[x]] = a[x];
      } else if (t[x][j + 1] > a[x])
        t[x][j + 1] = a[x];
      res = max(res, ft[x]);
      dfs2(x);
      for (j = 0; j <= fg[x]; j++) g[u][j] = min(g[u][j], g[x][j]);
      fg[u] = max(fg[u], fg[x]);
      for (j = fg[u]; j >= 0; j--)
        if (a[u] > g[u][j]) break;
      if (j == fg[u]) {
        fg[u]++;
        g[u][fg[u]] = a[u];
      } else if (g[u][j + 1] > a[u])
        g[u][j + 1] = a[u];
      for (j = 0; j <= fg[u]; j++) t[u][j] = min(t[u][j], g[u][j]);
      ft[u] = max(ft[u], fg[u]);
      res = max(res, ft[u]);
    }
}
void init() {
  int i, j;
  for (i = 1; i <= n; i++) fg[i] = ft[i] = 0;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) g[i][j] = t[i][j] = (int)1e9;
  ft[1] = 1;
  t[1][1] = a[1];
}
int main() {
  int i, x, y;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  init();
  dfs(1);
  init();
  dfs2(1);
  printf("%d", res);
}
