#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300000;
int strength[MAXN];
vector<vector<int>> graph;
int main() {
  int n;
  scanf("%d", &n);
  multiset<int> s;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &strength[i]);
    s.insert(strength[i]);
  }
  graph.resize(n);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    graph[--u].push_back(--v);
    graph[v].push_back(u);
  }
  int result = INT_MAX;
  for (int i = 0; i < n; ++i) {
    int maxNeighbor = INT_MIN;
    s.erase(s.find(strength[i]));
    for (int u : graph[i]) {
      maxNeighbor = max(maxNeighbor, strength[u]);
      s.erase(s.find(strength[u]));
    }
    int maxFar = INT_MIN;
    if (s.size() > 0) maxFar = *s.rbegin();
    s.insert(strength[i]);
    for (int u : graph[i]) s.insert(strength[u]);
    int newResult = strength[i];
    newResult = max(newResult, maxNeighbor + 1);
    newResult = max(newResult, maxFar + 2);
    result = min(result, newResult);
  }
  printf("%d\n", result);
  return 0;
}
