#include <bits/stdc++.h>
using namespace std;
void io() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
}
const int MAXN = 3e5 + 50;
long long INF = 1e16;
int A[MAXN];
map<int, int> cnt[MAXN];
set<int> g[MAXN];
int maxval;
int vis[MAXN];
int deg[MAXN];
int compsz;
void dfs(int u) {
  vis[u] = 1;
  compsz++;
  for (auto v : g[u]) {
    if (!vis[v] && (A[v] == maxval)) {
      deg[u]++;
      deg[v]++;
      dfs(v);
    }
  }
}
int main(int argc, char* argv[]) {
  io();
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  maxval = *max_element(A + 1, A + n + 1);
  int u, v;
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    g[u].insert(v);
    g[v].insert(u);
  }
  int maxid = -1;
  int maxcnt = 0;
  for (int i = 1; i <= n; i++) {
    if (A[i] == maxval) {
      maxid = i;
      maxcnt++;
    }
  }
  if (maxcnt == 1) {
    bool ok = 1;
    for (int i = 1; i <= n; i++) {
      if (A[i] == (maxval - 1)) {
        if (g[i].find(maxid) == g[i].end()) {
          ok = 0;
        }
      }
    }
    if (ok) {
      cout << (maxval) << '\n';
      return 0;
    } else {
      cout << (maxval + 1) << '\n';
      return 0;
    }
  }
  compsz = 0;
  dfs(maxid);
  bool flag = 1;
  for (int i = 1; i <= n; i++) {
    if (A[i] == maxval && !vis[i]) {
      flag = 0;
    }
  }
  if (compsz == 2 && flag) {
    cout << (maxval + 1) << '\n';
    return 0;
  }
  int cnt1 = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] && deg[i] == (compsz - 1)) {
      cnt1++;
    }
  }
  if ((cnt1 == 1 || compsz == 2) && flag) {
    cout << (maxval + 1);
  } else {
    int maxcnt2 = 0;
    for (int i = 1; i <= n; i++) {
      if (A[i] != maxval) {
        maxcnt2 = 0;
        for (auto v : g[i]) {
          if (A[v] == maxval) {
            maxcnt2++;
          }
        }
      }
      if (maxcnt2 == maxcnt) {
        cout << (maxval + 1) << '\n';
        return 0;
      }
    }
    cout << (maxval + 2) << '\n';
  }
  return 0;
}
