#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<int> A(N);
  set<pair<int, int>> S;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    S.insert({A[i], i});
  }
  vector<vector<int>> gr(N);
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    gr[a].push_back(b);
    gr[b].push_back(a);
  }
  int result = numeric_limits<int>::max();
  for (int i = 0; i < N; ++i) {
    int cand = A[i];
    S.erase({A[i], i});
    for (int j : gr[i]) {
      if (j != i) {
        S.erase({A[j], j});
        cand = max(cand, A[j] + 1);
      }
    }
    if (!S.empty()) {
      cand = max(cand, S.rbegin()->first + 2);
    }
    result = min(result, cand);
    S.insert({A[i], i});
    for (int j : gr[i]) {
      if (j != i) {
        S.insert({A[j], j});
      }
    }
  }
  cout << result << '\n';
  return 0;
}
