#include <bits/stdc++.h>
using namespace std;
int n;
int a[300005];
vector<int> E[300005];
void Init() {
  for (int i = 1; i <= n; i++) E[i].clear();
}
void solve() {
  int Max, cotM = 0, cotMf = 0, cotMt = 0;
  Max = a[1];
  for (int i = 1; i <= n; i++) Max = max(Max, a[i]);
  for (int i = 1; i <= n; i++) {
    a[i] += 2;
    if (a[i] == Max) cotM++;
    if (a[i] == Max + 1) cotMf++;
    if (a[i] == Max + 2) cotMt++;
  }
  int ans = Max + 2;
  for (int i = 1; i <= n; i++) {
    int M = cotM, Mf = cotMf, Mt = cotMt;
    if (a[i] == Max + 2) Mt--;
    if (a[i] == Max + 1) Mf--;
    if (a[i] == Max) M--;
    if (a[i] - 2 == Max) M++;
    int len = E[i].size();
    for (int j = 0; j < len; j++) {
      int to = E[i][j];
      if (a[to] == Max + 2) Mt--;
      if (a[to] == Max + 1) Mf--;
      if (a[to] == Max) M--;
      if (a[to] - 1 == Max + 1) Mf++;
      if (a[to] - 1 == Max) M++;
    }
    int MAX;
    if (Mt)
      MAX = Max + 2;
    else if (Mf)
      MAX = Max + 1;
    else
      MAX = Max;
    ans = min(ans, MAX);
  }
  cout << ans << endl;
}
int main() {
  while (~scanf("%d", &n)) {
    Init();
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i < n; i++) {
      int x, y;
      scanf("%d %d", &x, &y);
      E[x].push_back(y);
      E[y].push_back(x);
    }
    solve();
  }
  return 0;
}
