#include <bits/stdc++.h>
using namespace std;
long long int A[300005];
vector<vector<int> > adj;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, a, b;
  multiset<long long int> s;
  long long int x;
  cin >> n;
  adj = vector<vector<int> >(n + 1, vector<int>());
  for (int i = 1; i <= n; i++) {
    cin >> x;
    A[i] = x;
    s.insert(x);
  }
  for (int i = 1; i < n; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  long long int ans = 1 << 30;
  multiset<long long int>::iterator it;
  for (int i = 1; i <= n; i++) {
    long long int aux = A[i];
    it = s.find(aux);
    s.erase(it);
    for (int j = 0; j < adj[i].size(); j++) {
      int v = adj[i][j];
      aux = max(aux, A[v] + 1);
      it = s.find(A[v]);
      s.erase(it);
    }
    if (!s.empty()) {
      it = s.end();
      it--;
      aux = max(aux, *it + 2);
    }
    s.insert(A[i]);
    for (int j = 0; j < adj[i].size(); j++) {
      int v = adj[i][j];
      s.insert(A[v]);
    }
    ans = min(ans, aux);
  }
  cout << ans << '\n';
  return 0;
}
