#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  map<int, int> M;
  vector<int> V(n);
  vector<int> sorted;
  vector<vector<int> > adj(n, vector<int>());
  for (int i = 0; i < n; ++i) {
    cin >> V[i];
    if (!M.count(V[i])) {
      M[V[i]] = 0;
      sorted.push_back(V[i]);
    }
    M[V[i]]++;
  }
  sort(sorted.begin(), sorted.end());
  reverse(sorted.begin(), sorted.end());
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  int ans = 1000000003;
  for (int i = 0; i < n; ++i) {
    int mx = V[i];
    M[V[i]]--;
    for (auto v : adj[i]) {
      M[V[v]]--;
      mx = max(mx, V[v] + 1);
    }
    for (int i = 0; i < min(3, (int)sorted.size()); ++i) {
      int x = sorted[i];
      if (M[x]) {
        mx = max(mx, x + 2);
        break;
      }
    }
    M[V[i]]++;
    for (auto v : adj[i]) {
      M[V[v]]++;
    }
    ans = min(ans, mx);
  }
  cout << ans << '\n';
  return 0;
}
