#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e6 + 7;
const long long LINF = (long long)1e18 + 7;
const int INF = (int)1e9 + 7;
const double eps = 1e-7;
const int MAXN = (int)1e6 + 7;
const double pi = acos(-1);
int a[MAXN];
vector<int> mp[MAXN];
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int mx = -INF, k = 0;
  set<pair<int, int> > se;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    se.insert(make_pair(-a[i], i));
  }
  for (int i = 0; i < n - 1; i++) {
    int b, c;
    cin >> b >> c;
    b--;
    c--;
    mp[b].push_back(c);
    mp[c].push_back(b);
  }
  int ans = INF;
  for (int i = 0; i < n; i++) {
    int ss = -INF;
    for (int j = 0; j < mp[i].size(); j++) {
      se.erase(make_pair(-a[mp[i][j]], mp[i][j]));
      ss = max(ss, a[mp[i][j]] + 1);
    }
    se.erase(make_pair(-a[i], i));
    ss = max(ss, a[i]);
    if (se.size() != 0)
      ans = min(ans, max(-se.begin()->first + 2, ss));
    else
      ans = min(ans, ss);
    for (int j = 0; j < mp[i].size(); j++) {
      se.insert(make_pair(-a[mp[i][j]], mp[i][j]));
    }
    se.insert(make_pair(-a[i], i));
  }
  cout << ans << "\n";
  return 0;
}
