#include <bits/stdc++.h>
using namespace std;
int A[300002];
vector<int> G[300002];
int main() {
  map<int, int> cnt;
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
    cnt[A[i]]++;
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  int ans = INT_MAX;
  for (int i = 1; i <= n; i++) {
    int ans1 = -INT_MAX;
    map<int, int> x;
    ans1 = max(ans1, A[i]);
    x[A[i]]++;
    for (int j = 0; j < G[i].size(); j++) {
      int u = G[i][j];
      x[A[u]]++;
      ans1 = max(ans1, A[u] + 1);
    }
    for (auto j = cnt.rbegin(); j != cnt.rend(); j++) {
      if (j->second > x[j->first]) {
        ans1 = max(ans1, j->first + 2);
        break;
      }
    }
    ans = min(ans, ans1);
  }
  cout << ans << endl;
  return 0;
}
