#include<bits/stdc++.h>
#define ll long long
#define pp push_back
#define endl '\n'
#define all(x) x.begin(),x.end()
#define ld long double
#define PI acos(-1)
#define sin(a) sin((a)*PI/180)
#define cos(a) cos((a)*PI/180)
#define ones(x) __builtin_popcountll(x)
//#define int ll
using namespace std;
void Drakon() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef Clion
freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
}
unsigned long long inf = 1e10;
const double EPS = 1e-6;
const int MOD = 1000000007, N = 200005, LOG = 25;
ll mul(const ll &a, const ll &b) {
return (a % MOD + MOD) * (b % MOD + MOD) % MOD;
}
ll add(const ll &a, const ll &b) {
return (a + b + 2 * MOD) % MOD;
}
ll pw(ll x, ll y) {
ll ret = 1;
while (y > 0) {
if (y % 2 == 0) {
x = mul(x, x);
y = y / 2;
} else {
ret = mul(ret, x);
y = y - 1;
}
}
return ret;
}
struct seg {
int len = 0;
seg() {
}
seg(int len) {
this->len = len;
}
};
struct segtree {
vector<seg> values;
int size = 1, n;
void init(int nn) {
n = nn;
while (size < nn)size *= 2;
values.resize(2 * size, seg());
}
seg mrg(seg &a, seg &b) {
seg c;
c.len = max(a.len, b.len);
return c;
}
void update(int i, int v, int x, int lx, int rx) {
if (lx == rx) {
if(v == -1) values[x].len = 0;
else values[x].len = max(values[x].len, v);
return;
}
int mid = (lx + rx) / 2;
if (i <= mid)
update(i, v, 2 * x + 1, lx, mid);
else
update(i, v, 2 * x + 2, mid + 1, rx);
values[x] = mrg(values[2 * x + 1], values[2 * x + 2]);
}
void update(int i, int v) {
update(i, v, 0, 0, n - 1);
}
seg query(int l, int r, int x, int lx, int rx) {
if (l > rx || lx > r) {
return seg();
}
else if (lx >= l && rx <= r) {
return values[x];
}
int mid = (lx + rx) / 2;
seg s1 = query(l, r, 2 * x + 1, lx, mid);
seg s2 = query(l, r, 2 * x + 2, mid + 1, rx);
return mrg(s1, s2);
}
seg query(int l, int r) {
return query(l, r, 0, 0, n - 1);
}
};
vector<int> adj[N];
int n, sz[N], big[N];
void dfsSz(int u, int par) {
sz[u] = 1;
for (auto &v: adj[u]) {
if (v == par)continue;
dfsSz(v, u);
sz[u] += sz[v];
if (big[u] == -1 || sz[v] > sz[big[u]])
big[u] = v;
}
}
segtree nhaya, bdaya;
int ans, startNode[N], endNode[N], s[N];
vector<pair<int, pair<int, int>>> vec;
void collect(int u, int par, int val, int bestNhaya, int bestBdaya) {
// start
if(s[u] > val) {
ans = max(ans, bestNhaya + 1 + startNode[u]);
}
// end
if(s[u] < val) {
ans = max(ans, endNode[u] + 1 + bestBdaya);
}
ans = max(ans, endNode[u] + bdaya.query(s[u] + 1, N - 1).len);
ans = max(ans, nhaya.query(0, s[u] - 1).len + startNode[u]);
vec.push_back({s[u], {startNode[u], endNode[u]}});
for (auto v: adj[u]) {
if (v == par)continue;
collect(v, u, val, bestNhaya, bestBdaya);
}
}
void reset(int u, int par) {
bdaya.update(s[u], -1);
nhaya.update(s[u], -1);
for (auto v: adj[u]) {
if (v == par)continue;
reset(v, u);
}
}
void dfs(int u, int par, bool keep) {
for (auto v: adj[u]) {
if (v == par || v == big[u])continue;
dfs(v, u, false);
}
if (~big[u]) {
dfs(big[u], u, true);
}
for (auto v: adj[u]) {
if (v == par || v == big[u])continue;
vec.clear();
collect(v, u, s[u], nhaya.query(0, s[u] - 1).len, bdaya.query(s[u] + 1, N - 1).len);
for(auto val : vec) {
bdaya.update(val.first, val.second.first);
nhaya.update(val.first, val.second.second);
}
}
startNode[u] = bdaya.query(s[u] + 1, N - 1).len + 1;
endNode[u] = nhaya.query(0, s[u] - 1).len + 1;
ans = max(ans, startNode[u]);
ans = max(ans, endNode[u]);
bdaya.update(s[u], startNode[u]);
nhaya.update(s[u], endNode[u]);
// reset
if (!keep) {
reset(u, par);
}
}
void solve() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
memset(big, -1, sizeof big);
dfsSz(0, 0);
bdaya.init(N);
nhaya.init(N);
dfs(0, 0, true);
cout << ans;
}
signed main() {
Drakon();
int t = 1;
//cin >> t;
while (t--) {
solve();
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgcHAgcHVzaF9iYWNrCiNkZWZpbmUgZW5kbCAnXG4nCiNkZWZpbmUgYWxsKHgpIHguYmVnaW4oKSx4LmVuZCgpCiNkZWZpbmUgbGQgbG9uZyBkb3VibGUKI2RlZmluZSBQSSBhY29zKC0xKQojZGVmaW5lIHNpbihhKSBzaW4oKGEpKlBJLzE4MCkKI2RlZmluZSBjb3MoYSkgY29zKChhKSpQSS8xODApCiNkZWZpbmUgb25lcyh4KSBfX2J1aWx0aW5fcG9wY291bnRsbCh4KQovLyNkZWZpbmUgaW50IGxsCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdm9pZCBEcmFrb24oKSB7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUobnVsbHB0cik7CiAgICBjb3V0LnRpZShudWxscHRyKTsKI2lmZGVmIENsaW9uCiAgICBmcmVvcGVuKCJpbnB1dC50eHQiLCAiciIsIHN0ZGluKSwgZnJlb3Blbigib3V0cHV0LnR4dCIsICJ3Iiwgc3Rkb3V0KTsKI2VuZGlmCn0KCnVuc2lnbmVkIGxvbmcgbG9uZyBpbmYgPSAxZTEwOwpjb25zdCBkb3VibGUgRVBTID0gMWUtNjsKY29uc3QgaW50IE1PRCA9IDEwMDAwMDAwMDcsIE4gPSAyMDAwMDUsIExPRyA9IDI1OwoKbGwgbXVsKGNvbnN0IGxsICZhLCBjb25zdCBsbCAmYikgewogICAgcmV0dXJuIChhICUgTU9EICsgTU9EKSAqIChiICUgTU9EICsgTU9EKSAlIE1PRDsKfQoKbGwgYWRkKGNvbnN0IGxsICZhLCBjb25zdCBsbCAmYikgewogICAgcmV0dXJuIChhICsgYiArIDIgKiBNT0QpICUgTU9EOwp9CgpsbCBwdyhsbCB4LCBsbCB5KSB7CiAgICBsbCByZXQgPSAxOwogICAgd2hpbGUgKHkgPiAwKSB7CiAgICAgICAgaWYgKHkgJSAyID09IDApIHsKICAgICAgICAgICAgeCA9IG11bCh4LCB4KTsKICAgICAgICAgICAgeSA9IHkgLyAyOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHJldCA9IG11bChyZXQsIHgpOwogICAgICAgICAgICB5ID0geSAtIDE7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIHJldDsKfQoKc3RydWN0IHNlZyB7CiAgICBpbnQgbGVuID0gMDsKCiAgICBzZWcoKSB7CgogICAgfQoKICAgIHNlZyhpbnQgbGVuKSB7CiAgICAgICAgdGhpcy0+bGVuID0gbGVuOwogICAgfQp9OwoKc3RydWN0IHNlZ3RyZWUgewoKICAgIHZlY3RvcjxzZWc+IHZhbHVlczsKICAgIGludCBzaXplID0gMSwgbjsKCiAgICB2b2lkIGluaXQoaW50IG5uKSB7CiAgICAgICAgbiA9IG5uOwogICAgICAgIHdoaWxlIChzaXplIDwgbm4pc2l6ZSAqPSAyOwogICAgICAgIHZhbHVlcy5yZXNpemUoMiAqIHNpemUsIHNlZygpKTsKICAgIH0KCiAgICBzZWcgbXJnKHNlZyAmYSwgc2VnICZiKSB7CiAgICAgICAgc2VnIGM7CiAgICAgICAgYy5sZW4gPSBtYXgoYS5sZW4sIGIubGVuKTsKICAgICAgICByZXR1cm4gYzsKICAgIH0KCiAgICB2b2lkIHVwZGF0ZShpbnQgaSwgaW50IHYsIGludCB4LCBpbnQgbHgsIGludCByeCkgewoKICAgICAgICBpZiAobHggPT0gcngpIHsKICAgICAgICAgICAgaWYodiA9PSAtMSkgdmFsdWVzW3hdLmxlbiA9IDA7CiAgICAgICAgICAgIGVsc2UgdmFsdWVzW3hdLmxlbiA9IG1heCh2YWx1ZXNbeF0ubGVuLCB2KTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBpbnQgbWlkID0gKGx4ICsgcngpIC8gMjsKICAgICAgICBpZiAoaSA8PSBtaWQpCiAgICAgICAgICAgIHVwZGF0ZShpLCB2LCAyICogeCArIDEsIGx4LCBtaWQpOwogICAgICAgIGVsc2UKICAgICAgICAgICAgdXBkYXRlKGksIHYsIDIgKiB4ICsgMiwgbWlkICsgMSwgcngpOwogICAgICAgIHZhbHVlc1t4XSA9IG1yZyh2YWx1ZXNbMiAqIHggKyAxXSwgdmFsdWVzWzIgKiB4ICsgMl0pOwogICAgfQoKICAgIHZvaWQgdXBkYXRlKGludCBpLCBpbnQgdikgewogICAgICAgIHVwZGF0ZShpLCB2LCAwLCAwLCBuIC0gMSk7CiAgICB9CgogICAgc2VnIHF1ZXJ5KGludCBsLCBpbnQgciwgaW50IHgsIGludCBseCwgaW50IHJ4KSB7CgogICAgICAgIGlmIChsID4gcnggfHwgbHggPiByKSB7CiAgICAgICAgICAgIHJldHVybiBzZWcoKTsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZiAobHggPj0gbCAmJiByeCA8PSByKSB7CiAgICAgICAgICAgIHJldHVybiB2YWx1ZXNbeF07CiAgICAgICAgfQoKICAgICAgICBpbnQgbWlkID0gKGx4ICsgcngpIC8gMjsKICAgICAgICBzZWcgczEgPSBxdWVyeShsLCByLCAyICogeCArIDEsIGx4LCBtaWQpOwogICAgICAgIHNlZyBzMiA9IHF1ZXJ5KGwsIHIsIDIgKiB4ICsgMiwgbWlkICsgMSwgcngpOwogICAgICAgIHJldHVybiBtcmcoczEsIHMyKTsKICAgIH0KCiAgICBzZWcgcXVlcnkoaW50IGwsIGludCByKSB7CiAgICAgICAgcmV0dXJuIHF1ZXJ5KGwsIHIsIDAsIDAsIG4gLSAxKTsKICAgIH0KCn07Cgp2ZWN0b3I8aW50PiBhZGpbTl07CmludCBuLCBzeltOXSwgYmlnW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