#include <bits/stdc++.h>
const int Max = 1e6 + 100;
using namespace std;
vector<int> vec[Max];
int dist[Max];
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> dist[i];
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    vec[u].push_back(v);
    vec[v].push_back(u);
  }
  int mx = -2e9, cnt = 0, tmp = 0;
  for (int i = 1; i <= n; i++) mx = max(mx, dist[i]);
  for (int i = 1; i <= n; i++) {
    if (mx == dist[i]) cnt++;
    if (mx - 1 == dist[i]) tmp++;
  }
  int ans;
  if (cnt == 1 && tmp == 0) {
    cout << mx << endl;
    return 0;
  }
  ans = 2e9;
  for (int i = 1; i <= n; i++) {
    int cnt1 = 0, tmp1 = 0;
    if (dist[i] == mx) cnt1++;
    for (int j = 0; j < vec[i].size(); j++) {
      int v = vec[i][j];
      if (dist[v] == mx) cnt1++;
      if (dist[v] == mx - 1) tmp1++;
    }
    if (cnt1 != cnt) ans = min(ans, mx + 2);
    if (cnt1 == cnt) {
      if (tmp1 == tmp) {
        if (cnt1 == 1)
          ans = min(ans, mx);
        else
          ans = min(ans, mx + 1);
      } else {
        ans = min(mx + 1, ans);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
