#include <bits/stdc++.h>
using namespace std;
int n, start, u, v;
int a[300005], maxi = -1e9 - 9, answer, mini = 1e9 + 9, i;
vector<int> adj[300005];
multiset<int, greater<int> > MySet;
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] > maxi) maxi = a[i];
  }
  for (i = 1; i < n; i++) {
    scanf("%d%d", &u, &v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (i = 1; i <= n; i++) MySet.insert(a[i]);
  for (i = 1; i <= n; i++) {
    MySet.erase(MySet.find(a[i]));
    for (int j = 0; j < adj[i].size(); j++)
      MySet.erase(MySet.find(a[adj[i][j]]));
    answer = -1e9 - 9;
    if (!MySet.empty()) answer = max(answer, *MySet.begin() + 2);
    answer = max(answer, a[i]);
    for (int j = 0; j < adj[i].size(); j++)
      answer = max(answer, a[adj[i][j]] + 1);
    MySet.insert(a[i]);
    for (int j = 0; j < adj[i].size(); j++) MySet.insert(a[adj[i][j]]);
    mini = min(mini, answer);
  }
  cout << mini;
  return 0;
}
