#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long data[n + 1];
  long long mx = LLONG_MIN;
  for (int i = 1; i <= n; i++) {
    cin >> data[i];
    mx = max(mx, data[i]);
  }
  vector<int> graph[n + 1];
  for (int i = 0; i < n - 1; i++) {
    int s, t;
    cin >> s >> t;
    graph[s].push_back(t);
    graph[t].push_back(s);
  }
  int mxi = 0;
  long long mxcnt = 0;
  for (int i = 1; i <= n; i++) {
    if (data[i] == mx) {
      mxi = i;
      mxcnt++;
    }
  }
  long long res = mx;
  if (mxcnt == 1) {
    int level[n + 1];
    fill_n(level, n + 1, -1);
    deque<int> queue;
    queue.push_back(mxi);
    level[mxi] = 0;
    while (!queue.empty()) {
      int cur = queue.front();
      res = max(res, data[cur] + level[cur]);
      queue.pop_front();
      for (int next : graph[cur]) {
        if (level[next] < 0) {
          level[next] = min(2, level[cur] + 1);
          queue.push_back(next);
        }
      }
    }
  } else {
    bool level1 = false;
    for (int i = 1; !level1 && i <= n; i++) {
      long long cnt = data[i] == mx ? 1 : 0;
      for (int next : graph[i]) {
        if (data[next] == mx) cnt++;
      }
      level1 |= cnt == mxcnt;
    }
    res = level1 ? mx + 1 : mx + 2;
  }
  cout << res << '\n';
  return 0;
}
