#include <bits/stdc++.h>
using namespace std;
struct edge {
  int u, v, nt;
} eg[600008];
int lt[300008], sum, a[300008];
void add(int u, int v) {
  eg[++sum] = (edge){u, v, lt[u]};
  lt[u] = sum;
}
int main() {
  cin.sync_with_stdio(false);
  int n;
  cin >> n;
  int mx = -1e9 - 7;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    mx = max(mx, a[i]);
  }
  int num = 0, num0 = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] == mx) num++;
  for (int i = 1; i <= n; i++)
    if (a[i] == mx - 1) num0++;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  int ans = mx + 2;
  for (int u = 1; u <= n; u++) {
    int tmp = a[u] == mx, tmp0 = a[u] == mx - 1, now = mx;
    for (int i = lt[u]; i; i = eg[i].nt) {
      int v = eg[i].v;
      if (a[v] == mx) tmp++;
      if (a[v] == mx - 1) tmp0++;
    }
    if (tmp0 > (a[u] == mx - 1)) now = mx;
    if (tmp > (a[u] == mx)) now = mx + 1;
    if (num0 - tmp0 > 0) now = mx + 1;
    if (num - tmp > 0) now = mx + 2;
    if (now < ans) ans = now;
  }
  cout << ans << endl;
}
