#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  t = 1;
  while (t--) {
    long long int n;
    cin >> n;
    long long int arr[n + 1];
    long long int mx = INT_MIN, s = 0;
    set<pair<long long int, long long int>> st;
    for (long long int i = 1; i <= n; i++) {
      cin >> arr[i];
      st.insert({arr[i], i});
    }
    vector<vector<long long int>> graph;
    graph.assign(n + 1, vector<long long int>());
    for (long long int i = 1; i < n; i++) {
      long long int a, b;
      cin >> a >> b;
      graph[a].push_back(b);
      graph[b].push_back(a);
    }
    long long int ans = LONG_MAX;
    for (long long int i = 1; i <= n; i++) {
      long long int temp = arr[i];
      st.erase({arr[i], i});
      for (auto it : graph[i]) {
        temp = max(temp, arr[it] + 1);
        st.erase({arr[it], it});
      }
      if (st.size()) {
        auto it = *st.rbegin();
        temp = max(temp, it.first + 2);
      }
      st.insert({arr[i], i});
      for (auto it : graph[i]) {
        st.insert({arr[it], it});
      }
      ans = min(ans, temp);
    }
    cout << ans;
  }
  return 0;
}
