#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9 + 7;
int n, a[300000], an = -INF;
vector<int> adjlist[300000];
bool b1[300000] = {0}, b2[300000] = {0};
void dfs(int u, int p, int step) {
  an = max(an, a[u] + min(step, 2));
  for (auto v : adjlist[u]) {
    if (v == p) continue;
    dfs(v, u, step + 1);
  }
}
int main() {
  scanf("%d", &n);
  int mx = -INF, from, from2, cnt2 = 0;
  bool duplicate = 0;
  for (int(i) = 0; (i) < (n); (i)++) {
    scanf("%d", &a[i]);
    if (a[i] > mx) {
      mx = a[i];
      from = i;
      duplicate = 0;
    } else if (a[i] == mx) {
      duplicate = 1;
      from2 = i;
    }
  }
  for (int(i) = 0; (i) < (n); (i)++) {
    if (a[i] == mx) cnt2++;
  }
  for (int(i) = 0; (i) < (n - 1); (i)++) {
    int u, v;
    scanf("%d %d", &u, &v);
    u--;
    v--;
    adjlist[u].push_back(v);
    adjlist[v].push_back(u);
  }
  if (!duplicate) {
    dfs(from, -1, 0);
    printf("%d\n", an);
  } else {
    for (int(u) = 0; (u) < (n); (u)++) {
      int cnt = 0;
      if (a[u] == mx) cnt++;
      for (auto v : adjlist[u]) {
        if (a[v] == mx) cnt++;
      }
      if (cnt == cnt2) {
        from = u;
        dfs(from, -1, 0);
        return 0 * printf("%d\n", an);
      }
    }
    dfs(from, -1, 0);
    printf("%d\n", an);
  }
  return 0;
}
