#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
vector<int> def;
vector<vector<int> > graph;
vector<int> dist;
vector<int> color;
void bfs(int v) {
  queue<int> q;
  q.push(v);
  dist[v] = 0;
  while (!q.empty()) {
    int vv = q.front();
    q.pop();
    for (int i = 0; i < graph[vv].size(); i++) {
      int to = graph[vv][i];
      if (dist[to] > dist[vv] + 1) {
        dist[to] = dist[vv] + 1;
        q.push(to);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  def.resize(n);
  graph.resize(n);
  dist.resize(n, INF);
  for (auto& i : def) {
    cin >> i;
  }
  vector<int> cp = def;
  sort(cp.begin(), cp.end());
  int maxx = cp.back();
  int amount = 0;
  vector<int> best;
  for (int i = 0; i < n; i++) {
    if (def[i] == maxx) {
      amount++;
      best.push_back(i);
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  if (amount == 1) {
    bfs(best[0]);
    for (int i = 0; i < n; i++) {
      if (def[i] == maxx - 1 && dist[i] >= 2) {
        cout << maxx + 1;
        return 0;
      }
    }
    cout << maxx;
    return 0;
  } else {
    if (amount == 2) {
      bfs(best[0]);
      if (dist[best[1]] <= 2) {
        cout << maxx + 1;
      } else {
        cout << maxx + 2;
      }
    } else {
      bfs(best[0]);
      int s = best[0];
      int far = 0;
      for (int i = 0; i < best.size(); i++) {
        if (dist[best[i]] > far) {
          far = dist[best[i]];
          s = best[i];
        }
      }
      for (int i = 0; i < n; i++) {
        dist[i] = INF;
      }
      bfs(s);
      far = 0;
      for (int i = 0; i < best.size(); i++) {
        far = max(far, dist[best[i]]);
      }
      if (far <= 2) {
        cout << maxx + 1;
      } else {
        cout << maxx + 2;
      }
    }
  }
}
