#include <bits/stdc++.h>
using namespace std;
vector<int> mp[300005];
int num[300005];
int main() {
  int n;
  cin >> n;
  int Max = -1e10;
  int mc = 0, my = 0;
  int u;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &num[i]);
    Max = max(Max, num[i]);
  }
  for (int i = 1; i <= n; i++) {
    if (num[i] == Max - 1)
      my++;
    else if (num[i] == Max)
      mc++, u = i;
  }
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    mp[x].push_back(y);
    mp[y].push_back(x);
  }
  if (mc == 1) {
    int tot = 0;
    int ans = Max;
    for (int i = 0; i < mp[u].size(); i++) {
      if (num[mp[u][i]] == Max - 1) tot++;
    }
    if (tot != my) ans++;
    cout << ans << endl;
  } else {
    int f = 0;
    int ans = Max;
    for (int i = 1; i <= n; i++) {
      int tot = 0;
      if (num[i] == Max) tot++;
      for (int j = 0; j < mp[i].size(); j++)
        if (num[mp[i][j]] == Max) tot++;
      if (tot == mc) {
        f = 1;
      }
    }
    if (f)
      ans++;
    else
      ans += 2;
    cout << ans << endl;
  }
  return 0;
}
