#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7FFFFFFF;
const int MAXN = 8192;
struct Tree {
  int tree[MAXN * 2];
  Tree() { memset(tree, 0, sizeof tree); }
  void reset() { memset(tree, 0, sizeof tree); }
  void set(int i, int v) {
    tree[i + MAXN] = v;
    for (i = (i + MAXN) / 2; i; i /= 2)
      tree[i] = max(tree[i + i], tree[i + i + 1]);
  }
  int get(int l, int r) {
    l += MAXN;
    r += MAXN;
    int res = max(tree[l], tree[r]);
    while (l < r - 1) {
      if (!(l & 1)) res = max(res, tree[l + 1]);
      if (r & 1) res = max(res, tree[r - 1]);
      l /= 2;
      r /= 2;
    }
    return res;
  }
};
Tree tree;
int ans[2][MAXN];
vector<int> ve[MAXN];
int tin[MAXN], tout[MAXN];
int tlist[MAXN];
int pop[MAXN];
int timer = 0;
void dfs(int v, int p = -1) {
  tlist[timer] = v;
  tin[v] = timer++;
  for (int u : ve[v])
    if (u != p) dfs(u, v);
  tout[v] = timer;
}
int order[MAXN];
bool comp(int i, int j) { return pop[i] < pop[j]; }
int dfs_a(int v, int p = -1) {
  int answer = 1;
  for (int u : ve[v]) {
    if (u == p) continue;
    answer = max(answer, dfs_a(u, v));
    for (int ai = (tin[u]); ai < (tout[u]); ai++) {
      int a = tlist[ai];
      if (pop[v] > pop[a]) answer = max(answer, ans[0][a] + 1);
      if (pop[v] < pop[a]) answer = max(answer, ans[1][a] + 1);
      for (int b = (tin[v] + 1); b < (tin[u]); b++)
        if (pop[tlist[b]] > pop[a])
          answer = max(answer, ans[0][a] + ans[1][tlist[b]] +
                                   (pop[v] > pop[a] && pop[v] < pop[tlist[b]]));
      for (int b = (tout[u]); b < (tout[v]); b++)
        if (pop[tlist[b]] > pop[a])
          answer = max(answer, ans[0][a] + ans[1][tlist[b]] +
                                   (pop[v] > pop[a] && pop[v] < pop[tlist[b]]));
    }
  }
  return answer;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = (0); i < (n); i++) cin >> pop[i], order[i] = i;
  for (int i = (0); i < (n - 1); i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    ve[a].push_back(b);
    ve[b].push_back(a);
  }
  sort(order, order + n, comp);
  dfs(0);
  int answer = 0;
  for (int z = (0); z < (2); z++) {
    for (int i = 0, next = 0; i < n; i = next) {
      while (next < n && pop[order[i]] == pop[order[next]]) next++;
      for (int j = (i); j < (next); j++)
        ans[z][order[j]] = tree.get(tin[order[j]], tout[order[j]] - 1) + 1;
      for (int j = (i); j < (next); j++)
        tree.set(tin[order[j]], ans[z][order[j]]);
    }
    tree.reset();
    reverse(order, order + n);
  }
  cout << dfs_a(0);
  return 0;
}
