#include <bits/stdc++.h>
using namespace std;
bool vis[300010];
vector<int> G[300010];
int num[300010];
int main() {
  int maxx = -1e9;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &num[i]);
    maxx = max(num[i], maxx);
  }
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int num1 = 0;
  int num2 = 0;
  for (int i = 1; i <= n; i++) {
    if (num[i] == maxx) num1++;
    if (num[i] == maxx - 1) num2++;
  }
  int mann = maxx + 2;
  for (int i = 1; i <= n; i++) {
    int t = 0, tt = 0;
    if (num[i] == maxx) t++;
    for (int j = 0; j < G[i].size(); j++) {
      if (num[G[i][j]] == maxx)
        t++;
      else if (num[G[i][j]] == maxx - 1)
        tt++;
    }
    if (t == num1) {
      if (num1 == 1)
        if (num2 == tt)
          mann = min(mann, maxx);
        else
          mann = min(mann, maxx + 1);
      else
        mann = min(mann, maxx + 1);
    }
  }
  printf("%d\n", mann);
  return 0;
}
