#include <bits/stdc++.h>
using namespace std;
multiset<int> ms;
int strength[300003];
const int inf = (int)(1e9);
vector<int> g[300003];
int n, x, y;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> strength[i];
    ms.insert(strength[i]);
  }
  for (int i = 0; i < n - 1; i++) {
    cin >> x >> y;
    g[x].emplace_back(y);
    g[y].emplace_back(x);
  }
  int ans = (int)(1e9) + 7;
  for (int i = 1; i <= n; i++) {
    int maxime = strength[i];
    ms.erase(ms.find(strength[i]));
    for (int ch : g[i]) {
      maxime = max(maxime, strength[ch] + 1);
      ms.erase(ms.find(strength[ch]));
    }
    if (ms.size() != 0) {
      maxime = max(maxime, *prev(ms.end()) + 2);
    }
    for (int ch : g[i]) {
      ms.insert(strength[ch]);
    }
    ms.insert(strength[i]);
    ans = min(ans, maxime);
  }
  cout << ans << '\n';
  return 0;
}
