#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 100, inf = 2e9;
int koszt[maxn];
vector<int> graph[maxn];
struct cmp {
  bool operator()(const int &a, const int &b) {
    if (koszt[a] != koszt[b]) return koszt[a] > koszt[b];
    return a > b;
  }
};
set<int, cmp> s;
int main() {
  int n, a, b;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &koszt[i]);
    koszt[i] += 2;
    s.insert(i);
  }
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &a, &b);
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  int wyn = inf;
  for (int i = 1; i <= n; i++) {
    int kan = koszt[i] - 2;
    s.erase(i);
    for (int j = 0; j < graph[i].size(); j++) {
      s.erase(graph[i][j]);
      kan = max(kan, koszt[graph[i][j]] - 1);
    }
    if (s.size()) kan = max(kan, koszt[*s.begin()]);
    for (int j = 0; j < graph[i].size(); j++) s.insert(graph[i][j]);
    s.insert(i);
    wyn = min(wyn, kan);
  }
  printf("%d", wyn);
}
