#include <bits/stdc++.h>
using namespace std;
const int delta = (int)1e9 + 7;
int n, a[(int)3e5 + 20], b[(int)3e5 + 20], x, y, z, ans, h[(int)3e5 + 20];
vector<int> G[(int)3e5 + 20];
bool cmp(int u, int v) {
  if (a[u] == a[v]) return u < v;
  return a[u] > a[v];
}
bool ac1() {
  for (int i = 0; i < G[b[1]].size(); ++i) h[G[b[1]][i]] = 1;
  int w = 2;
  while (a[b[w]] == a[b[1]] - 1 && w <= n) {
    if (!h[b[w]]) return 1;
    ++w;
  }
  return 0;
}
bool ac2() {
  for (int i = 1; i <= n; ++i)
    if (a[i] == z) ++y;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == z)
      x = 1;
    else
      x = 0;
    for (int j = 0; j < G[i].size(); ++j)
      if (a[G[i][j]] == z) ++x;
    if (x == y) return 0;
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i], b[i] = i;
  for (int i = 1; i < n; ++i) {
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  sort(b + 1, b + n + 1, cmp);
  z = ans = a[b[1]];
  if (n == 1 || a[b[1]] - a[b[2]] > 1)
    ;
  else if (a[b[1]] == a[b[2]] + 1) {
    if (ac1()) ++ans;
  } else {
    y = 0;
    ans += ac2() + 1;
  }
  cout << ans;
  return cout << endl, 0;
}
