#include <bits/stdc++.h>
using namespace std;
const int MAX = 3e5 + 5;
map<int, int> mp;
vector<int> adj[MAX];
int arr[MAX];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
    mp[arr[i] + 2]++;
  }
  for (int i = 0; i < (n - 1); i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  int mn = 1e9 + 3;
  for (int i = 1; i <= n; i++) {
    int tmp = arr[i];
    mp[arr[i] + 2]--;
    if (mp[arr[i] + 2] == 0) {
      mp.erase(arr[i] + 2);
    }
    for (int j = 0; j < adj[i].size(); j++) {
      int u = adj[i][j];
      mp[arr[u] + 2]--;
      tmp = max(tmp, arr[u] + 1);
      if (mp[arr[u] + 2] == 0) {
        mp.erase(arr[u] + 2);
      }
    }
    if (!mp.empty()) {
      tmp = max(tmp, mp.rbegin()->first);
    }
    mn = min(mn, tmp);
    mp[arr[i] + 2]++;
    for (int j = 0; j < adj[i].size(); j++) {
      int u = adj[i][j];
      mp[arr[u] + 2]++;
    }
  }
  printf("%d\n", mn);
  return 0;
}
