#include <bits/stdc++.h>
using namespace std;
constexpr const int NMAX = 3 * 100 * 1000 + 513;
vector<int> g[NMAX];
int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  multiset<int, greater<int>> m;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    m.insert(a[i] + 2);
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int ans = *m.begin();
  for (int i = 0; i < n; ++i) {
    int cur = a[i];
    m.erase(m.find(a[i] + 2));
    for (int j = 0; j < g[i].size(); ++j) {
      int idx = g[i][j];
      m.erase(m.find(a[idx] + 2));
      cur = max(cur, a[idx] + 1);
    }
    if (m.size() > 0) {
      ans = min(ans, max(cur, *m.begin()));
    } else {
      ans = min(ans, cur);
    }
    m.insert(a[i] + 2);
    for (int j = 0; j < g[i].size(); ++j) {
      int idx = g[i][j];
      m.insert(a[idx] + 2);
    }
  }
  printf("%d\n", ans);
  return 0;
}
