#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 8;
int head[MAXN], tot;
struct esge {
  int u, v, w, nxt;
} a[2 * MAXN];
inline void add(int u, int v, int w) {
  tot++;
  a[tot].u = u, a[tot].v = v, a[tot].w = w;
  a[tot].nxt = head[u], head[u] = tot;
}
int val[MAXN];
priority_queue<int> pq1, pq2;
queue<int> que;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, i, j, k, u, v, ans = 2e9, sz, t;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> val[i];
    pq2.push(val[i]);
  }
  tot = 1;
  for (i = 1; i < n; i++) {
    cin >> u >> v;
    add(u, v, 1);
    add(v, u, 1);
  }
  if (n == 1) {
    ans = val[1];
  }
  for (i = 1; i <= n; i++) {
    if (n == 1) break;
    pq1.push(val[i]);
    t = val[i];
    for (k = head[i]; k; k = a[k].nxt) {
      v = a[k].v;
      pq1.push(val[v]);
      t = max(t, val[v] + 1);
    }
    while (!pq1.empty() && pq1.top() == pq2.top()) {
      pq1.pop();
      que.push(pq2.top());
      pq2.pop();
    }
    if (!pq2.empty())
      ans = min(ans, max(t, pq2.top() + 2));
    else
      ans = min(ans, t);
    while (!que.empty()) {
      pq2.push(que.front());
      que.pop();
    }
    while (!pq1.empty()) {
      pq1.pop();
    }
  }
  cout << ans << endl;
  return 0;
}
