#include <bits/stdc++.h>
using namespace std;
int a[300001];
int main() {
  int n;
  cin >> n;
  multiset<int> s;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), s.insert(a[i]);
  vector<int> v[300001];
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int ans = 2e9;
  for (int i = 1; i <= n; i++) {
    int mx = a[i];
    s.erase(s.find(a[i]));
    for (int j : v[i]) {
      mx = max(mx, a[j] + 1);
      s.erase(s.find(a[j]));
    }
    if (!s.empty()) mx = max(mx, *s.rbegin() + 2);
    s.insert(a[i]);
    for (int j : v[i]) s.insert(a[j]);
    ans = min(mx, ans);
  }
  cout << ans << endl;
}
