#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 45;
int n, a[N];
vector<int> g[N];
map<int, int> mapa;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    mapa[a[i]]++;
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int maxi = -1e9;
  for (int i = 1; i <= n; i++) {
    maxi = max(maxi, a[i]);
  }
  int puta = 0, sta = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == maxi) {
      puta++;
      sta = i;
    }
  }
  if (puta == 1) {
    int ima = 0;
    for (auto u : g[sta]) {
      ima += (a[u] == maxi - 1);
    }
    if (ima == mapa[maxi - 1]) {
      cout << maxi << endl;
    } else {
      cout << maxi + 1 << endl;
    }
    return 0;
  }
  bool postoji = 0;
  for (int i = 1; i <= n; i++) {
    int cnt = 0;
    for (auto u : g[i]) {
      if (a[u] == maxi) cnt++;
    }
    if (cnt == puta - (a[i] == maxi)) {
      postoji = 1;
    }
  }
  if (postoji) {
    cout << maxi + 1 << endl;
  } else {
    cout << maxi + 2 << endl;
  }
}
