#include <bits/stdc++.h>
using namespace std;
const int P = 1e9 + 7;
int add(int a, int b) {
  if ((a += b) >= P) a -= P;
  return a < 0 ? a + P : a;
}
int mul(int a, int b) { return 1ll * a * b % P; }
int kpow(int a, int b) {
  int r = 1;
  for (; b; b >>= 1, a = mul(a, a)) {
    if (b & 1) r = mul(r, a);
  }
  return r;
}
const int N = 6005;
vector<int> V, g[N];
int a[N], n, u, v, ans;
short f[N][N], h[N][N];
void dfs(int u, int fa) {
  f[u][a[u]] = h[u][a[u]] = 1;
  for (int i = (n + 1) - 1; i >= (0); i--) f[u][i] = max(f[u][i + 1], f[u][i]);
  for (int i = (1); i < (n + 1); i++) h[u][i] = max(h[u][i], h[u][i - 1]);
  for (auto v : g[u])
    if (v != fa) {
      dfs(v, u);
      for (int i = (1); i < (n + 1); i++) ans = max(ans, f[v][i] + h[u][i - 1]);
      for (int i = (0); i < (n + 1); i++) ans = max(ans, h[v][i] + f[u][i + 1]);
      f[u][a[u]] = max(f[u][a[u]], (short)(f[v][a[u] + 1] + 1));
      h[u][a[u]] = max(h[u][a[u]], (short)(h[v][a[u] - 1] + 1));
      for (int i = (n + 1) - 1; i >= (0); i--)
        f[u][i] = max(f[u][i], f[v][i]), f[u][i] = max(f[u][i + 1], f[u][i]);
      for (int i = (1); i < (n + 1); i++)
        h[u][i] = max(h[u][i], h[v][i]), h[u][i] = max(h[u][i], h[u][i - 1]);
    }
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cin >> n;
  for (int i = (1); i < (n + 1); i++) cin >> a[i], V.push_back(a[i]);
  sort(V.begin(), V.end());
  V.erase(unique(V.begin(), V.end()), V.end());
  for (int i = (1); i < (n + 1); i++)
    a[i] = lower_bound(V.begin(), V.end(), a[i]) - V.begin(), a[i]++;
  for (int i = (1); i < (n); i++) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  ans = 1;
  dfs(1, 0);
  cout << ans << "\n";
  return 0;
}
