#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)3e5 + 5;
vector<int> g[MAXN];
int a[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  multiset<int> all;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    all.insert(a[i]);
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int ans = (2e9);
  for (int root = 1; root <= n; root++) {
    all.erase(all.find(a[root]));
    int res = a[root];
    for (int j = 0; j < g[root].size(); j++) {
      int to = g[root][j];
      all.erase(all.find(a[to]));
      res = max(res, a[to] + 1);
    }
    if (!all.empty()) res = max(res, *(--all.end()) + 2);
    ans = min(ans, res);
    all.insert(a[root]);
    for (int j = 0; j < g[root].size(); j++) {
      int to = g[root][j];
      all.insert(a[to]);
    }
  }
  cout << ans << endl;
  return 0;
}
