#include <bits/stdc++.h>
#pragma comment(linker, ”/ STACK : 38777216“
using namespace std;
const int N = 1000005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;
int n;
int a[N];
multiset<int> q;
multiset<int>::iterator it;
vector<int> g[N];
void Del(int x) {
  it = q.find(x);
  q.erase(it);
}
void add(int x) { q.insert(x); }
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    q.insert(a[i]);
  }
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int answ = inf + n;
  for (int i = 1; i <= n; i++) {
    int L = a[i];
    int x = a[i];
    Del(x);
    for (int j = 0; j < (int)g[i].size(); j++) {
      x = a[g[i][j]];
      L = max(L, x + 1);
      Del(x);
    }
    if ((int)q.size()) {
      it = q.end();
      --it;
      L = max(L, *it + 2);
    }
    add(a[i]);
    for (int j = 0; j < (int)g[i].size(); j++) {
      x = a[g[i][j]];
      add(x);
    }
    answ = min(answ, L);
  }
  cout << answ << endl;
  return 0;
}
