#include <bits/stdc++.h>

#define R "%d"
#define RL "%I64d"
#define RD "%lf"
#define W "%.8lf"

#define Max(x, y) ((x) > (y) ? (x) : (y))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Set(x, y) memset(x, y, sizeof(x))
#define Cpy(x, y) memcpy(x, y, sizeof(y))
#define All(x) x.begin(), x.end()
#define Sqr(x) ((x) * (x))
#define Sz(x) ((LL)x.size())

#define Mk make_pair
#define Pb push_back
#define Pk pop_back
#define Fr first
#define Se second

#define FOR(x, l, r) for (LL x = (l), end = (r); x <= end; ++x)
#define ROF(x, l, r) for (LL x = (l), end = (r); x >= end; --x)
#define Edge(x, y) for (LL x = g[y]; x; x = nt[x])

#define Read(x) ((x) = Get())
#define Lowbit(x) ((x) & (-x))
using namespace std;

typedef int LL;
typedef long long ll;
typedef double LD;
typedef long double real;

typedef map <LL, LL> MI;
typedef pair <LL, LL> PI;
typedef set <LL> SI;
typedef vector <LL> VI;

inline LL Get() {
    LL res = 0, q = 1;
    char ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') q = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar();
    return res * q;
}

const LD eps = 1e-12, pi = M_PI;
const LL oo = (LL)2e9, mod = (LL)1e9 + 7;
const ll INF = (ll)1e17;

const LL N = (LL)6e3;
typedef LL arr[N + 10];
typedef LL arre[2 * N + 10];

LL n, m, tot, j, k, cnt, ans;
arr g, a, c, d, t;
short dp[2][N + 10][N + 10];
arre pt, nt;

void Link(LL x, LL y) {
    pt[++tot] = y, nt[tot] = g[x], g[x] = tot;
    pt[++tot] = x, nt[tot] = g[y], g[y] = tot;
}

LL max(LL x, LL y) { return x > y ? x : y; }

void Dp(LL x, LL fa) {
    Edge (i, x)
        if (pt[i] != fa) Dp(pt[i], x);
    FOR (i, 1, cnt) c[i] = d[i] = t[i] = 0;
    LL F = 0, S = 0, tF = 0;
    Edge (i, x)
        if (pt[i] != fa) {
            LL tmx = 0;
            FOR (j, 1, cnt) {
                dp[0][x][j] = max(dp[0][x][j], dp[0][pt[i]][j]), dp[1][x][j] = max(dp[1][x][j], dp[1][pt[i]][j]);
                if (dp[1][pt[i]][j] > c[j]) d[j] = c[j], c[j] = dp[1][pt[i]][j], t[j] = pt[i];
                else if (dp[1][pt[i]][j] > d[j]) d[j] = dp[1][pt[i]][j];
                if (j > x) tmx = max(tmx, dp[1][pt[i]][j]);
            }
            if (tmx > F) S = F, tmx = F, tF = pt[i];
            else if (tmx > S) S = tmx;
        }

    LL mx = 0;
    FOR (i, 1, a[x] - 1) mx = max(mx, dp[0][x][i]);
    dp[0][x][a[x]] = max(dp[0][x][a[x]], mx + 1);
    mx = 0;
    FOR (i, a[x] + 1, cnt) mx = max(mx, dp[1][x][i]);
    dp[1][x][a[x]] = max(dp[1][x][a[x]], mx + 1);
    Edge (i, x)
        if (pt[i] != fa) {
            LL mx = 0;
            FOR (j, 1, cnt) {
                mx = max(mx, dp[0][pt[i]][j]);
                if (t[j + 1] != pt[i]) ans = max(ans, mx + c[j + 1]); else ans = max(ans, mx + d[j + 1]);
                if (j == a[x] - 1) {
                    if (tF == pt[i]) ans = max(ans, mx + S + 1); else ans = max(ans, mx + F + 1);
                }
            }

        }
    FOR (i, 1, cnt) ans = max(ans, dp[0][x][i]), ans = max(ans, dp[1][x][i]);
}

LL main() {
#ifndef ONLINE_JUDGE
    freopen("f.in", "r", stdin);
    freopen("f.out", "w", stdout);
#endif

    scanf(R "\n", &n);
    FOR (i, 1, n) scanf(R, a + i), c[i] = a[i];
    FOR (i, 1, n - 1) scanf(R R "\n", &j, &k), Link(j, k);
    sort(c + 1, c + n + 1), cnt = 0;
    FOR (i, 1, n) if (c[i] != c[i + 1]) c[++cnt] = c[i];
    FOR (i, 1, n) a[i] = lower_bound(c + 1, c + cnt + 1, a[i]) - c;

    Set(c, 0);
    Dp(1, 0);
    printf(R "\n", ans);
    return 0;
}


#include <bits/stdc++.h>

#define R "%d"
#define RL "%I64d"
#define RD "%lf"
#define W "%.8lf"

#define Max(x, y) ((x) > (y) ? (x) : (y))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Set(x, y) memset(x, y, sizeof(x))
#define Cpy(x, y) memcpy(x, y, sizeof(y))
#define All(x) x.begin(), x.end()
#define Sqr(x) ((x) * (x))
#define Sz(x) ((LL)x.size())

#define Mk make_pair
#define Pb push_back
#define Pk pop_back
#define Fr first
#define Se second

#define FOR(x, l, r) for (LL x = (l), end = (r); x <= end; ++x)
#define ROF(x, l, r) for (LL x = (l), end = (r); x >= end; --x)
#define Edge(x, y) for (LL x = g[y]; x; x = nt[x])

#define Read(x) ((x) = Get())
#define Lowbit(x) ((x) & (-x))
using namespace std;

typedef int LL;
typedef long long ll;
typedef double LD;
typedef long double real;

typedef map <LL, LL> MI;
typedef pair <LL, LL> PI;
typedef set <LL> SI;
typedef vector <LL> VI;

inline LL Get() {
    LL res = 0, q = 1;
    char ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') q = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar();
    return res * q;
}

const LD eps = 1e-12, pi = M_PI;
const LL oo = (LL)2e9, mod = (LL)1e9 + 7;
const ll INF = (ll)1e17;

const LL N = (LL)6e3;
typedef LL arr[N + 10];
typedef LL arre[2 * N + 10];

LL n, m, tot, j, k, cnt, ans;
arr g, a, c, d, t;
short dp[2][N + 10][N + 10];
arre pt, nt;

void Link(LL x, LL y) {
    pt[++tot] = y, nt[tot] = g[x], g[x] = tot;
    pt[++tot] = x, nt[tot] = g[y], g[y] = tot;
}

LL max(LL x, LL y) { return x > y ? x : y; }

void Dp(LL x, LL fa) {
    Edge (i, x)
        if (pt[i] != fa) Dp(pt[i], x);
    FOR (i, 1, cnt) c[i] = d[i] = t[i] = 0;
    LL F = 0, S = 0, tF = 0;
    Edge (i, x)
        if (pt[i] != fa) {
            LL tmx = 0;
            FOR (j, 1, cnt) {
                dp[0][x][j] = max(dp[0][x][j], dp[0][pt[i]][j]), dp[1][x][j] = max(dp[1][x][j], dp[1][pt[i]][j]);
                if (dp[1][pt[i]][j] > c[j]) d[j] = c[j], c[j] = dp[1][pt[i]][j], t[j] = pt[i];
                else if (dp[1][pt[i]][j] > d[j]) d[j] = dp[1][pt[i]][j];
                if (j > a[x]) tmx = max(tmx, dp[1][pt[i]][j]);
            }
            if (tmx > F) S = F, F = tmx, tF = pt[i];
            else if (tmx > S) S = tmx;
        }

    LL mx = 0;
    FOR (i, 1, a[x] - 1) mx = max(mx, dp[0][x][i]);
    dp[0][x][a[x]] = max(dp[0][x][a[x]], mx + 1);
    mx = 0;
    FOR (i, a[x] + 1, cnt) mx = max(mx, dp[1][x][i]);
    dp[1][x][a[x]] = max(dp[1][x][a[x]], mx + 1);
    Edge (i, x)
        if (pt[i] != fa) {
            LL mx = 0;
            FOR (j, 1, cnt) {
                mx = max(mx, dp[0][pt[i]][j]);
                if (t[j + 1] != pt[i]) ans = max(ans, mx + c[j + 1]); else ans = max(ans, mx + d[j + 1]);
                if (j == a[x] - 1) {
                    if (tF == pt[i]) ans = max(ans, mx + S + 1); else ans = max(ans, mx + F + 1);
                }
            }

        }
    FOR (i, 1, cnt) ans = max(ans, dp[0][x][i]), ans = max(ans, dp[1][x][i]);
}

LL main() {
#ifndef ONLINE_JUDGE
    freopen("f.in", "r", stdin);
    freopen("f.out", "w", stdout);
#endif

    scanf(R "\n", &n);
    FOR (i, 1, n) scanf(R, a + i), c[i] = a[i];
    FOR (i, 1, n - 1) scanf(R R "\n", &j, &k), Link(j, k);
    sort(c + 1, c + n + 1), cnt = 0;
    FOR (i, 1, n) if (c[i] != c[i + 1]) c[++cnt] = c[i];
    FOR (i, 1, n) a[i] = lower_bound(c + 1, c + cnt + 1, a[i]) - c;

    Set(c, 0);
    Dp(1, 0);
    printf(R "\n", ans);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define fore(i,n) for(int i = 0; i< n ; i++ )
#define lop(i,n) for(int i = 1 ; i<=n ; i++ )
#define ops(i,n) for(int  i = n-1 ; i>=0 ; i-- )
#define forall( it,g )  for( typeof(g.begin()) it=g.begin();it!=g.end();it++ )
#define PI  3.141592653589793
#define mod  1000000007
#define inf 2000000000
#define INF -2000000000000000
#define modulo 1000000009
#define MH 1234533333333337
#define MH2 7779845079489377
#define enter endl
//ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using namespace std;
typedef vector <int> vi;
typedef vector <vector <int> > vv;
typedef vector <pair <int,int > >vp;
typedef vector <vector <pair <int ,int > > > vvp;
typedef vector <pair <int ,pair <int ,int > > > vpp;
typedef pair<int,int> pp;
typedef long long ll;
typedef unsigned long long ull;
const int M = 6005;
int dp[M][M];
vi g[M];
int p[M];
int dfs(int u, int pr, int last)
{
    int &temp = dp[u][last];
    if(temp != -1)
    {
            return temp;
    }
    int ans = 0;
    fore(i, g[u].size())
    {
            int v = g[u][i];
            if(v != pr)
            {
                    if(last == 0 || p[v] > p[last])
                    {
                            ans = max(ans, 1 + dfs(v, u, v));
                    }
                    ans = max(ans, dfs(v, u, last));
            }
    }
    return temp = ans;
}
int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    scanf("%d", &n);
    lop(i, n)
    {
        scanf("%d", &p[i]);
    }
    fore(i, n - 1)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        g[a].pb(b);
        g[b].pb(a);
    }
    memset(dp, -1, sizeof dp);
    int ans = 0;
    lop(i, n)
    {
            ans = max(ans, 1 + dfs(i, i, 0));
    }
    printf("%d", ans);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define fore(i,n) for(int i = 0; i< n ; i++ )
#define lop(i,n) for(int i = 1 ; i<=n ; i++ )
#define ops(i,n) for(int  i = n-1 ; i>=0 ; i-- )
#define forall( it,g )  for( typeof(g.begin()) it=g.begin();it!=g.end();it++ )
#define PI  3.141592653589793
#define mod  1000000007
#define inf 2000000000
#define INF -2000000000000000
#define modulo 1000000009
#define MH 1234533333333337
#define MH2 7779845079489377
#define enter endl
//ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using namespace std;
typedef vector <int> vi;
typedef vector <vector <int> > vv;
typedef vector <pair <int,int > >vp;
typedef vector <vector <pair <int ,int > > > vvp;
typedef vector <pair <int ,pair <int ,int > > > vpp;
typedef pair<int,int> pp;
typedef long long ll;
typedef unsigned long long ull;
const int M = 6005;
int dp[M][M];
vi g[M];
int p[M];
int dfs(int u, int pr, int last)
{
    int &temp = dp[u][last];
    if(temp != -1)
    {
            return temp;
    }
    int ans = 0;
    fore(i, g[u].size())
    {
            int v = g[u][i];
            if(v != pr)
            {
                    if(last == u || p[u] > p[last])
                    {
                            ans = max(ans, 1 + dfs(v, u, u));
                    }
                    ans = max(ans, dfs(v, u, last));
            }
    }
    if(p[u] > p[last])
    {
            ans = max(ans, 1);
    }
    return temp = ans;
}
int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    scanf("%d", &n);
    vp v;
    lop(i, n)
    {
            scanf("%d", &p[i]);
            v.pb(mp(p[i], i));
    }
    sort(v.begin(), v.end());
    fore(i, n - 1)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        g[a].pb(b);
        g[b].pb(a);
    }
    memset(dp, -1, sizeof dp);
    int ans = 0;
    fore(i, n)
    {
            ans = max(ans, dfs(v[i].s, v[i].s, v[i].s));
    }
    printf("%d", ans);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define fore(i,n) for(int i = 0; i< n ; i++ )
#define lop(i,n) for(int i = 1 ; i<=n ; i++ )
#define ops(i,n) for(int  i = n-1 ; i>=0 ; i-- )
#define forall( it,g )  for( typeof(g.begin()) it=g.begin();it!=g.end();it++ )
#define PI  3.141592653589793
#define mod  1000000007
#define inf 2000000000
#define INF -2000000000000000
#define modulo 1000000009
#define MH 1234533333333337
#define MH2 7779845079489377
#define enter endl
//ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using namespace std;
typedef vector <int> vi;
typedef vector <vector <int> > vv;
typedef vector <pair <int,int > >vp;
typedef vector <vector <pair <int ,int > > > vvp;
typedef vector <pair <int ,pair <int ,int > > > vpp;
typedef pair<int,int> pp;
typedef long long ll;
typedef unsigned long long ull;
const int M = 6001;
int dp[M][M];
vi g[M];
int p[M];
int dfs(int u, int pr, int last)
{
    int &temp = dp[u][last];
    if(temp != -1)
    {
            return temp;
    }
    int ans = 0;
    fore(i, g[u].size())
    {
            int v = g[u][i];
            if(v != pr)
            {
                    if(last == u || p[u] > p[last])
                    {
                            ans = max(ans, 1 + dfs(v, u, u));
                    }
                    ans = max(ans, dfs(v, u, last));
            }
    }
    if(!ans && p[u] > p[last])
    {
            ans = 1;
    }
    return temp = ans;
}
int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    scanf("%d", &n);
    vp v;
    lop(i, n)
    {
            scanf("%d", &p[i]);
    }
    fore(i, n - 1)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        g[a].pb(b);
        g[b].pb(a);
    }
    memset(dp, -1, sizeof dp);
    int ans = 0;
    lop(i, n)
    {
            ans = max(ans, dfs(i, i, i));
    }
    printf("%d", ans);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define fore(i,n) for(int i = 0; i< n ; i++ )
#define lop(i,n) for(int i = 1 ; i<=n ; i++ )
#define ops(i,n) for(int  i = n-1 ; i>=0 ; i-- )
#define forall( it,g )  for( typeof(g.begin()) it=g.begin();it!=g.end();it++ )
#define PI  3.141592653589793
#define mod  1000000007
#define inf 2000000000
#define INF -2000000000000000
#define modulo 1000000009
#define MH 1234533333333337
#define MH2 7779845079489377
#define enter endl
//ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using namespace std;
typedef vector <int> vi;
typedef vector <vector <int> > vv;
typedef vector <pair <int,int > >vp;
typedef vector <vector <pair <int ,int > > > vvp;
typedef vector <pair <int ,pair <int ,int > > > vpp;
typedef pair<int,int> pp;
typedef long long ll;
typedef unsigned long long ull;
const int M = 6001;
int dp[M][M];
vi g[M];
int p[M];
int dfs(int u, int pr, int last)
{
    int &temp = dp[u][last];
    if(temp != -1)
    {
            return temp;
    }
    int ans = 0;
    fore(i, g[u].size())
    {
            int v = g[u][i];
            if(v != pr)
            {
                    if(last == u || p[u] > p[last])
                    {
                            ans = max(ans, 1 + dfs(v, u, u));
                    }
                    ans = max(ans, dfs(v, u, last));
            }
    }
    if(p[u] > p[last])
    {
            ans = max(ans, 1);
    }
    return temp = ans;
}
int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    scanf("%d", &n);
    vp v;
    lop(i, n)
    {
            scanf("%d", &p[i]);
    }
    fore(i, n - 1)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        g[a].pb(b);
        g[b].pb(a);
    }
    memset(dp, -1, sizeof dp);
    int ans = 0;
    lop(i, n)
    {
            ans = max(ans, dfs(i, i, i));
    }
    printf("%d", ans);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define fore(i,n) for(int i = 0; i< n ; i++ )
#define lop(i,n) for(int i = 1 ; i<=n ; i++ )
#define ops(i,n) for(int  i = n-1 ; i>=0 ; i-- )
#define forall( it,g )  for( typeof(g.begin()) it=g.begin();it!=g.end();it++ )
#define PI  3.141592653589793
#define mod  1000000007
#define inf 2000000000
#define INF -2000000000000000
#define modulo 1000000009
#define MH 1234533333333337
#define MH2 7779845079489377
#define enter endl
//ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using namespace std;
typedef vector <int> vi;
typedef vector <vector <int> > vv;
typedef vector <pair <int,int > >vp;
typedef vector <vector <pair <int ,int > > > vvp;
typedef vector <pair <int ,pair <int ,int > > > vpp;
typedef pair<int,int> pp;
typedef long long ll;
typedef unsigned long long ull;
const int M = 6005;
int dp[M][M];
vi g[M];
int p[M];
int dfs(int u, int pr, int last)
{
    int &temp = dp[u][last];
    if(temp != -1)
    {
            return temp;
    }
    int ans = 0;
    fore(i, g[u].size())
    {
            int v = g[u][i];
            if(v != pr)
            {
                    if(last == 0 || p[u] > p[last])
                    {
                            ans = max(ans, 1 + dfs(v, u, u));
                    }
                    ans = max(ans, dfs(v, u, last));
            }
    }
    if(p[u] > p[last])
    {
            ans = max(ans, 1);
    }
    return temp = ans;
}
int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    scanf("%d", &n);
    vp v;
    lop(i, n)
    {
            scanf("%d", &p[i]);
            v.pb(mp(p[i], i));
    }
    sort(v.begin(), v.end());
    fore(i, n - 1)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        g[a].pb(b);
        g[b].pb(a);
    }
    memset(dp, -1, sizeof dp);
    int ans = 0;
    fore(i, n)
    {
            ans = max(ans, dfs(v[i].s, v[i].s, 0));
    }
    printf("%d", ans);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define fore(i,n) for(int i = 0; i< n ; i++ )
#define lop(i,n) for(int i = 1 ; i<=n ; i++ )
#define ops(i,n) for(int  i = n-1 ; i>=0 ; i-- )
#define forall( it,g )  for( typeof(g.begin()) it=g.begin();it!=g.end();it++ )
#define PI  3.141592653589793
#define mod  1000000007
#define inf 2000000000
#define INF -2000000000000000
#define modulo 1000000009
#define MH 1234533333333337
#define MH2 7779845079489377
#define enter endl
//ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using namespace std;
typedef vector <int> vi;
typedef vector <vector <int> > vv;
typedef vector <pair <int,int > >vp;
typedef vector <vector <pair <int ,int > > > vvp;
typedef vector <pair <int ,pair <int ,int > > > vpp;
typedef pair<int,int> pp;
typedef long long ll;
typedef unsigned long long ull;
const int M = 6005;
int dp[M][M];
vi g[M];
int p[M];
int dfs(int u, int pr, int last)
{
    int &temp = dp[u][last];
    if(temp != -1)
    {
            return temp;
    }
    int ans = 0;
    fore(i, g[u].size())
    {
            int v = g[u][i];
            if(v != pr)
            {
                    if(last == 0 || p[u] > p[last])
                    {
                            ans = max(ans, 1 + dfs(v, u, u));
                    }
                    ans = max(ans, dfs(v, u, last));
            }
    }
    if(p[u] > p[last])
    {
            ans = max(ans, 1);
    }
    return temp = ans;
}
int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    scanf("%d", &n);
    lop(i, n)
    {
        scanf("%d", &p[i]);
    }
    fore(i, n - 1)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        g[a].pb(b);
        g[b].pb(a);
    }
    memset(dp, -1, sizeof dp);
    int ans = 0;
    lop(i, n)
    {
            ans = max(ans, dfs(i, i, 0));
    }
    printf("%d", ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=6005;
const int inf=0x7fffffff;
int dp[maxn],n,r[maxn];
int G[maxn][maxn],len[maxn];
int ans;
struct edge{
    int f,t;
}e[maxn];
int elen;
int heap[maxn];
void getnum(int dep){
    fill(dp,dp+n,inf);
    for(int i=0;i<=dep;i++){
      //  printf("heap[%d]:%d\n",i,r[heap[i]]);
        int l=lower_bound(dp,dp+n,r[heap[i]])-dp;
        dp[l]=min(dp[l],r[heap[i]]);
        ans=max(l+1,ans);
    }
    //for(int i=0;i<n;i++)printf("dp:%d%c",dp[i],i==n-1?'\n':' ');
}
void dfs(int s,int f,int dep){
    heap[dep]=s;
    for(int i=0;i<len[s];i++){
        edge te=e[G[s][i]];
        if(te.t==f)continue;
        dfs(te.t,s,dep+1);
    }
    if(len[s]==1&&f!=-1){
     //   printf("ok %d\n",s);
        getnum(dep);
    }
}
void addedge(int f,int t){
    e[elen].f=f;e[elen].t=t;
    G[f][len[f]++]=elen++;
    e[elen].f=t;e[elen].t=f;
    G[t][len[t]++]=elen++;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",r+i);
    for(int i=1;i<n;i++){int f,t;scanf("%d%d",&f,&t);addedge(f,t);}
    for(int i=1;i<n;i++){dfs(i,-1,0);}
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=6005;
const int inf=0x7fffffff;
int dp[maxn],n,r[maxn];
int G[maxn][maxn],len[maxn];
int ans;
struct edge{
    int f,t;
}e[maxn];
int elen;
int heap[maxn];
void getnum(int dep){
    fill(dp,dp+n,inf);
    for(int i=0;i<=dep;i++){
      //  printf("heap[%d]:%d\n",i,r[heap[i]]);
        int l=lower_bound(dp,dp+n,r[heap[i]])-dp;
        dp[l]=min(dp[l],r[heap[i]]);
        ans=max(l+1,ans);
    }
    //for(int i=0;i<n;i++)printf("dp:%d%c",dp[i],i==n-1?'\n':' ');
}
void dfs(int s,int f,int dep){
    heap[dep]=s;
    for(int i=0;i<len[s];i++){
        edge te=e[G[s][i]];
        if(te.t==f)continue;
        dfs(te.t,s,dep+1);
    }
    if(len[s]==1&&f!=-1){
     //   printf("ok %d\n",s);
        getnum(dep);
    }
}
void addedge(int f,int t){
    e[elen].f=f;e[elen].t=t;
    G[f][len[f]++]=elen++;
    e[elen].f=t;e[elen].t=f;
    G[t][len[t]++]=elen++;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",r+i);
    for(int i=1;i<n;i++){int f,t;scanf("%d%d",&f,&t);addedge(f,t);}
    for(int i=1;i<=n;i++){dfs(i,-1,0);}
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=6005;
const int inf=0x7fffffff;
int dp[maxn],n,r[maxn];
int first[maxn],nxt[2*maxn],to[2*maxn],len[maxn];
int ans;;
int elen;
int heap[maxn];
void getnum(int dep){
    fill(dp,dp+n,inf);
    for(int i=0;i<=dep;i++){
      //  printf("heap[%d]:%d\n",i,r[heap[i]]);
        int l=lower_bound(dp,dp+n,r[heap[i]])-dp;
        dp[l]=min(dp[l],r[heap[i]]);
        ans=max(l+1,ans);
    }
    //for(int i=0;i<n;i++)printf("dp:%d%c",dp[i],i==n-1?'\n':' ');
}
void dfs(int s,int f,int dep){
    heap[dep]=s;
    bool fl=false;
    for(int i=first[s];i!=-1;i=nxt[i]){
        int t=to[i];
        if(t==f)continue;
        dfs(t,s,dep+1);
        fl=true;
    }
    if(!fl){
        //printf("ok %d\n",s);
        getnum(dep);
    }
}
void addedge(int f,int t,int ind){
    nxt[ind*2]=first[f];to[ind*2]=t;first[f]=2*ind;
    nxt[ind*2+1]=first[t];to[ind*2+1]=f;first[t]=2*ind+1;
}
int main(){
    scanf("%d",&n);
    memset(first,-1,sizeof(first));
    for(int i=1;i<=n;i++)scanf("%d",r+i);
    for(int i=1;i<n;i++){int f,t;scanf("%d%d",&f,&t);addedge(f,t,i);}
    for(int i=1;i<=n;i++){dfs(i,-1,0);}
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=6005;
const int inf=0x7fffffff;
int dp[maxn],n,r[maxn];
int first[maxn],nxt[2*maxn],to[2*maxn],len[maxn];
int ans;;
int elen;
int heap[maxn];
void getnum(int dep){
    fill(dp,dp+dep+1,inf);
    for(int i=0;i<=dep;i++){
      //  printf("heap[%d]:%d\n",i,r[heap[i]]);
        int l=lower_bound(dp,dp+n,r[heap[i]])-dp;
        dp[l]=min(dp[l],r[heap[i]]);
        ans=max(l+1,ans);
    }
    //for(int i=0;i<n;i++)printf("dp:%d%c",dp[i],i==n-1?'\n':' ');
}
void dfs(int s,int f,int dep){
    heap[dep]=s;
    bool fl=false;
    for(int i=first[s];i!=-1;i=nxt[i]){
        int t=to[i];
        if(t==f)continue;
        dfs(t,s,dep+1);
        fl=true;
    }
    if(!fl){
        //printf("ok %d\n",s);
        getnum(dep);
    }
}
void addedge(int f,int t,int ind){
    nxt[ind*2]=first[f];to[ind*2]=t;first[f]=2*ind;
    nxt[ind*2+1]=first[t];to[ind*2+1]=f;first[t]=2*ind+1;
}
int main(){
    scanf("%d",&n);
    memset(first,-1,sizeof(first));
    for(int i=1;i<=n;i++)scanf("%d",r+i);
    for(int i=1;i<n;i++){int f,t;scanf("%d%d",&f,&t);addedge(f,t,i);}
    for(int i=1;i<=n;i++){dfs(i,-1,0);}
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=6005;
const int inf=0x7fffffff;
int dp[maxn],n,r[maxn];
int first[maxn],nxt[2*maxn],to[2*maxn],len[maxn];
int ans;;
int elen;
int heap[maxn];
void getnum(int dep){
    fill(dp,dp+dep+1,inf);
    for(int i=0;i<=dep;i++){
      //  printf("heap[%d]:%d\n",i,r[heap[i]]);
        int l=lower_bound(dp,dp+dep+1,r[heap[i]])-dp;
        dp[l]=min(dp[l],r[heap[i]]);
        ans=max(l+1,ans);
    }
    //for(int i=0;i<n;i++)printf("dp:%d%c",dp[i],i==n-1?'\n':' ');
}
void dfs(int s,int f,int dep){
    heap[dep]=s;
    bool fl=false;
    for(int i=first[s];i!=-1;i=nxt[i]){
        int t=to[i];
        if(t==f)continue;
        dfs(t,s,dep+1);
        fl=true;
    }
    if(!fl){
        //printf("ok %d\n",s);
        getnum(dep);
    }
}
void addedge(int f,int t,int ind){
    nxt[ind*2]=first[f];to[ind*2]=t;first[f]=2*ind;
    nxt[ind*2+1]=first[t];to[ind*2+1]=f;first[t]=2*ind+1;
}
int main(){
    scanf("%d",&n);
    memset(first,-1,sizeof(first));
    for(int i=1;i<=n;i++)scanf("%d",r+i);
    for(int i=1;i<n;i++){int f,t;scanf("%d%d",&f,&t);addedge(f,t,i);}
    for(int i=1;i<=n;i++){dfs(i,-1,0);}
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=6005;
const int inf=0x7fffffff;
int dp[maxn],n,r[maxn];
int first[maxn],nxt[2*maxn],to[2*maxn],len[maxn];
int ans;
void dfs(int s,int f){
    int pos=lower_bound(dp,dp+n,r[s])-dp;
    int tmp=dp[pos];
    ans=max(pos+1,ans);
    dp[pos]=min(dp[pos],r[s]);
    for(int i=first[s];i!=-1;i=nxt[i]){
        int t=to[i];
        if(t==f)continue;
        dfs(t,s);
    }
    dp[pos]=tmp;
}
void addedge(int f,int t,int ind){
    nxt[ind*2]=first[f];to[ind*2]=t;first[f]=2*ind;
    nxt[ind*2+1]=first[t];to[ind*2+1]=f;first[t]=2*ind+1;
}
int main(){
    scanf("%d",&n);
    memset(first,-1,sizeof(first));
    fill(dp,dp+n,inf);
    for(int i=1;i<=n;i++)scanf("%d",r+i);
    for(int i=1;i<n;i++){int f,t;scanf("%d%d",&f,&t);addedge(f,t,i-1);}
    for(int i=1;i<=n;i++){dfs(i,-1);}
    printf("%d\n",ans);
    return 0;
}


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<string>
using namespace std;

#define LL long long
#define ULL unsigned long long
const int maxn = 6005;
struct sw{
    int v,next;
}E[maxn*2];
int n,head[maxn],idx=1;
int dp1[maxn][maxn];
int w[maxn];
void add(int u,int v){
    E[idx].v=v,E[idx].next=head[u],head[u]=idx++;
    E[idx].v=u,E[idx].next=head[v],head[v]=idx++;
}
void dfs1(int u,int p){
    dp1[u][1]=w[u];
    for(int i=head[u];i;i=E[i].next){
        int v=E[i].v;
        if(v!=p){
            dfs1(v,u);
            for(int j=1;dp1[v][j]!=dp1[v][0];j++){
                dp1[u][j]=max(dp1[u][j],dp1[v][j]);
                if(w[u]<dp1[v][j])
                    dp1[u][j+1]=max(dp1[u][j+1],w[u]);
            }
        }
    }
}
int main(void){
    int u,v;
    int ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%d",&w[i]);
    }
    for(int i=1;i<n;i++){
        scanf("%d%d",&u,&v);
        add(u,v);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++) memset(dp1[j]+1,0,sizeof(int)*n);
        dfs1(i,-1);
        int j;
        for(j=1;dp1[i][j];j++);
        ans=max(ans,j-1);
    }
    printf("%d\n",ans);

    return 0;
}


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<string>
using namespace std;

#define LL long long
#define ULL unsigned long long
const int maxn = 6005;
struct sw{
	int v,next;
}E[maxn*2];
int n,head[maxn],idx=1;
int dp[maxn];
int w[maxn];
int ans=0;
void add(int u,int v){
	E[idx].v=v,E[idx].next=head[u],head[u]=idx++;
	E[idx].v=u,E[idx].next=head[v],head[v]=idx++;
}
int find(int l,int r,int x){
	while(l<r){
		int mid=(l+r+1)>>1;
		if(x>dp[mid])
			l=mid;
		else r=mid-1;
	}
	return l;
}
void dfs(int u,int p){
	for(int i=head[u];i;i=E[i].next){
		int v=E[i].v;
		if(v!=p){
			int d=find(0,n,w[v]);
			ans=max(ans,d+1);
			int t=dp[d+1];
			if(w[v]<dp[d+1]){
				dp[d+1]=w[v];
			}
			dfs(v,u);
			dp[d+1]=t;
		}
	}
}
int main(void){
	int u,v;

	scanf("%d",&n);
	for(int i=1;i<=n;i++) {
		scanf("%d",&w[i]);
	}
	for(int i=1;i<n;i++){
		scanf("%d%d",&u,&v);
		add(u,v);
	}
	for(int i=1;i<=n;i++){
		memset(dp,3,sizeof(dp));
		dp[0]=0;
		dp[1]=w[i];
		dfs(i,-1);
		int j;
		j=find(0,n,dp[6001]);
		ans=max(ans,j-1);
	}
	printf("%d\n",ans);

	return 0;
}


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<string>
using namespace std;

#define LL long long
#define ULL unsigned long long
const int maxn = 6005;
struct sw{
    int v,next;
}E[maxn*2];
int n,head[maxn],idx=1;
int dp1[maxn][maxn];
int w[maxn];
void add(int u,int v){
    E[idx].v=v,E[idx].next=head[u],head[u]=idx++;
    E[idx].v=u,E[idx].next=head[v],head[v]=idx++;
}
void dfs1(int u,int p){
    dp1[u][1]=w[u];
    for(int i=head[u];i;i=E[i].next){
        int v=E[i].v;
        if(v!=p){
            dfs1(v,u);
            for(int j=1;dp1[v][j]!=dp1[v][0];j++){
                dp1[u][j]=max(dp1[u][j],dp1[v][j]);
                if(w[u]<dp1[v][j])
                    dp1[u][j+1]=max(dp1[u][j+1],w[u]);
            }
        }
    }
}
int main(void){
    int u,v;
    int ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%d",&w[i]);
    }
    for(int i=1;i<n;i++){
        scanf("%d%d",&u,&v);
        add(u,v);
    }
    for(int i=1;i<=n;i++){
        memset(dp1,0,sizeof(dp1));
        dfs1(i,-1);
        int j;
        for(j=1;dp1[i][j];j++);
        ans=max(ans,j-1);
    }
    printf("%d\n",ans);

    return 0;
}


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<string>
using namespace std;

#define LL long long
#define ULL unsigned long long
struct sw{
	int v,next;
}E[12010];
int n,dp[6005],head[6005],idx=1;
int w[6005];
void add(int u,int v){
	E[idx].v=v,E[idx].next=head[u],head[u]=idx++;
	E[idx].v=u,E[idx].next=head[v],head[v]=idx++;
}
void dfs(int u,int p){
	dp[u]=1;
	for(int i=head[u];i;i=E[i].next){
		int v=E[i].v;
		if(v!=p){
			if(w[u]<w[v]){
				dfs(v,u);
				dp[u]=max(dp[u],1+dp[v]);
			}
			else dfs(v,u);
		}
	}
}
void dfs2(int u,int p){
	for(int i=head[u];i;i=E[i].next){
		int v=E[i].v;
		if(v!=p){
			if(w[u]>w[v]){
				dp[v]=max(dp[v],1+dp[u]);
				dfs2(v,u);
			}
		}
	}
}
int main(void){
	int u,v;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&w[i]);
	for(int i=1;i<n;i++){
		scanf("%d%d",&u,&v);
		add(u,v);
	}
	dfs(1,-1);
	dfs2(1,-1);
	int ans=0;
	for(int i=1;i<=n;i++)
		ans=max(ans,dp[i]);
	printf("%d\n",ans);
	return 0;
}


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<string>
using namespace std;

#define LL long long
#define ULL unsigned long long
const int maxn = 6005;
struct sw{
    int v,next;
}E[maxn*2];
int n,head[maxn],idx=1;
int dp1[maxn][maxn];
int dp2[maxn][maxn];
int w[maxn];
void add(int u,int v){
    E[idx].v=v,E[idx].next=head[u],head[u]=idx++;
    E[idx].v=u,E[idx].next=head[v],head[v]=idx++;
}
void dfs1(int u,int p){
    dp1[u][1]=w[u];
    for(int i=head[u];i;i=E[i].next){
        int v=E[i].v;
        if(v!=p){
            dfs1(v,u);
            for(int j=1;dp1[v][j]!=dp1[v][0];j++){
                dp1[u][j]=max(dp1[u][j],dp1[v][j]);
                if(w[u]<dp1[v][j])
                    dp1[u][j+1]=max(dp1[u][j+1],w[u]);
            }
        }
    }
}
int main(void){
    int u,v;
    int ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%d",&w[i]);
    }
    for(int i=1;i<n;i++){
        scanf("%d%d",&u,&v);
        add(u,v);
    }
    for(int i=1;i<=n;i++){
        memset(dp1,0,sizeof(dp1));
        dfs1(i,-1);
        int j;
        for(j=1;dp1[i][j];j++);
        ans=max(ans,j-1);
    }
    printf("%d\n",ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int inc[6009][6009];
int arr[9009];
vector<vector<int> > v;
int n;

int dfsi(int u,int last,int p)
{
	if(inc[u][last])
		return inc[u][last];

	int &ret=inc[u][last];
	ret=1;
	for(int i=0;i<v[u].size();i++)
	{
		int nu=v[u][i];
		if(nu==p)
			continue;

		if(arr[nu]>arr[last])
			ret=max(ret,1+dfsi(nu,nu,u));
		ret=max(ret,dfsi(nu,last,u));
	}
	return ret;
}
int dfsd(int u)
{
	if(decr[u])
		return decr[u];

	int &ret=decr[u];
	ret=0;
	for(int i=0;i<v[u].size();i++)
	{
		int nu=v[u][i];
		if(arr[nu]<arr[u])
			ret=max(ret,1+dfsd(nu));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	for(int i=1;i<=n;i++)
	{
		ret=max(ret,dfsi(i,i,0));

	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int inc[9009];
int decr[9009];
int arr[9009];
vector<vector<int> > v;
int n;

int dfsi(int u,int last,int p)
{
	if(inc[u])
		return inc[u];

	int &ret=inc[u];
	ret=1;
	for(int i=0;i<v[u].size();i++)
	{
		int nu=v[u][i];
		if(nu==p)
			continue;

		if(arr[nu]>arr[last])
			ret=max(ret,1+dfsi(nu,nu,u));
		ret=max(ret,dfsi(nu,last,u));
	}
	return ret;
}
int dfsd(int u)
{
	if(decr[u])
		return decr[u];

	int &ret=decr[u];
	ret=0;
	for(int i=0;i<v[u].size();i++)
	{
		int nu=v[u][i];
		if(arr[nu]<arr[u])
			ret=max(ret,1+dfsd(nu));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	for(int i=1;i<=n;i++)
	{
		ret=max(ret,dfsi(i,i,0));

	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int inc[6009][6009];
int arr[9009];
vector<vector<int> > v;
int n;

int dfsi(int u,int last,int p)
{
	if(inc[u][last])
		return inc[u][last];

	int &ret=inc[u][last];
	ret=0;
	for(int i=0;i<(int)v[u].size();i++)
	{
		int nu=v[u][i];
		if(nu==p)
			continue;

		if(arr[nu]>arr[last])
			ret=max(ret,1+dfsi(nu,nu,u));
		ret=max(ret,dfsi(nu,last,u));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	for(int i=1;i<=n;i++)
	{
		ret=max(ret,1+max(dfsi(i,i,0),dfsi(i,0,0)));

	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int inc[6009][6009];
int arr[9009];
vector<vector<int> > v;
int n;

int dfsi(int u,int last,int p)
{
	int &ret=inc[u][last];
	if(ret!=-1)
		return ret;
	ret=arr[u]>arr[last];
	for(int i=0;i<(int)v[u].size();i++)
	{
		int nu=v[u][i];
		if(nu==p)
			continue;

		if(arr[u]>arr[last])
			ret=max(ret,1+dfsi(nu,u,u));

		ret=max(ret,dfsi(nu,last,u));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	memset(inc,-1,sizeof inc);
	for(int i=1;i<=n;i++)
	{
		ret=max(ret,dfsi(i,0,0));

	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int inc[6003][6003];
int arr[6002];
vector<vector<int> > v;
int n;

int dfsi(int u, int last, int p) {
	int &ret = inc[u][last];
	if (ret != -1)
		return ret;

	ret = (arr[u] > arr[last]);
	for (int i = 0; i < (int) v[u].size(); i++) {
		int nu = v[u][i];
		if (nu == p)
			continue;

		if(arr[u]>arr[last])
			ret = max(ret, 1 + dfsi(nu, u, u));

		ret = max(ret, dfsi(nu, last, u));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n;
	int a;
	int b;
	v.resize(n + 2);
	arr[0] = -2;

	for (int i = 0; i < n; i++)
		cin >> arr[i + 1];
	for (int i = 0; i < n - 1; i++) {
		cin >> a >> b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret = 1;
	memset(inc, -1, sizeof inc);
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < v[i].size(); j++) {
			ret = max(ret, 1 + dfsi(v[i][j], i, i));
		}

	}
	cout << ret << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int inc[6002][6002][2];
int arr[9009];
vector<vector<int> > v;
int n;

int dfsi(int u,int last,int k,int p)
{
	int &ret=inc[u][last][k];
	if(ret!=-1)
		return ret;

	ret=(arr[u]>arr[last]);
	for(int i=0;i<(int)v[u].size();i++)
	{
		int nu=v[u][i];
		if(nu==p)
			continue;

		if(arr[u]>arr[last])
			ret=max(ret,1+dfsi(nu,u,u<nu,u));

		ret=max(ret,dfsi(nu,last,u<nu,u));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	arr[0]=-2;

	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	memset(inc,-1,sizeof inc);
	for(int i=1;i<=n;i++)
	{
		ret=max(ret,dfsi(i,0,0,0));

	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int inc[6009][6009];
int arr[9009];
vector<vector<int> > v;
int n;

int dfsi(int u,int last,int p)
{
	if(inc[u][last])
		return inc[u][last];

	int &ret=inc[u][last];
	ret=0;
	for(int i=0;i<(int)v[u].size();i++)
	{
		int nu=v[u][i];
		if(nu==p)
			continue;

		if(arr[nu]>arr[last])
			ret=max(ret,1+dfsi(nu,nu,u));

		ret=max(ret,dfsi(nu,last,u));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	for(int i=1;i<=n;i++)
	{
		ret=max(ret,max(1+dfsi(i,i,0),dfsi(i,0,0)));

	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int inc[9009];
int decr[9009];
int arr[9009];
vector<vector<int> > v;
int n;

int dfsi(int u)
{
	if(inc[u])
		return inc[u];

	int &ret=inc[u];
	ret=0;
	for(int i=0;i<v[u].size();i++)
	{
		int nu=v[u][i];
		if(arr[nu]>arr[u])
			ret=max(ret,1+dfsi(nu));
	}
	return ret;
}
int dfsd(int u)
{
	if(decr[u])
		return decr[u];

	int &ret=decr[u];
	ret=0;
	for(int i=0;i<v[u].size();i++)
	{
		int nu=v[u][i];
		if(arr[nu]<arr[u])
			ret=max(ret,1+dfsd(nu));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	for(int i=1;i<=n;i++)
	{
		dfsi(i);
		dfsd(i);
		//cout<<i<<" "<<decr[i]<<" "<<inc[i]<<endl;
		ret=max(ret,decr[i]+inc[i]+1);
	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int inc[9009];
int decr[9009];
int arr[9009];
vector<vector<int> > v;
int n;

int dfsi(int u)
{
	if(inc[u])
		return inc[u];

	int &ret=inc[u];
	ret=1;
	for(int i=0;i<v[u].size();i++)
	{
		int nu=v[u][i];
		if(arr[nu]>arr[u])
			ret=max(ret,1+dfsi(nu));
	}
	return ret;
}
int dfsd(int u)
{
	if(decr[u])
		return decr[u];

	int &ret=decr[u];
	ret=1;
	for(int i=0;i<v[u].size();i++)
	{
		int nu=v[u][i];
		if(arr[nu]<arr[u])
			ret=max(ret,1+dfsd(nu));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	for(int i=1;i<=n;i++)
	{
		dfsi(i);
		dfsd(i);
		//cout<<i<<" "<<decr[i]<<" "<<inc[i]<<endl;
		ret=max(ret,decr[i]+inc[i]-1);
	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int inc[6002][6002];
int arr[6002];
vector<vector<int> > v;
int n;

int dfsi(int u,int last,int p)
{
	int &ret=inc[u][last];
	if(ret!=-1)
		return ret;

	ret=(arr[u]>arr[last]);
	for(int i=0;i<(int)v[u].size();i++)
	{
		int nu=v[u][i];
		if(nu==p || u>nu)
			continue;

		if(arr[u]>arr[last])
			ret=max(ret,1+dfsi(nu,u,u));

		ret=max(ret,dfsi(nu,last,u));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	arr[0]=-2;

	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	memset(inc,-1,sizeof inc);
	for(int i=1;i<=n;i++)
	{
		ret=max(ret,dfsi(i,0,0));

	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int inc[9009][9009];
int decr[9009];
int arr[9009];
vector<vector<int> > v;
int n;

int dfsi(int u,int last,int p)
{
	if(inc[u][last])
		return inc[u][last];

	int &ret=inc[u][last];
	ret=1;
	for(int i=0;i<v[u].size();i++)
	{
		int nu=v[u][i];
		if(nu==p)
			continue;

		if(arr[nu]>arr[last])
			ret=max(ret,1+dfsi(nu,nu,u));
		ret=max(ret,dfsi(nu,last,u));
	}
	return ret;
}
int dfsd(int u)
{
	if(decr[u])
		return decr[u];

	int &ret=decr[u];
	ret=0;
	for(int i=0;i<v[u].size();i++)
	{
		int nu=v[u][i];
		if(arr[nu]<arr[u])
			ret=max(ret,1+dfsd(nu));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	for(int i=1;i<=n;i++)
	{
		ret=max(ret,dfsi(i,i,0));

	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

 int  inc[6001][6001][2];
int arr[6002];
vector<vector<int> > v;
int n;

 int dfsi(int u,int last,bool k,int p)
{
	 int &ret=inc[u][last][k];
	if(ret!=-1)
		return ret;

	ret=(arr[u]>arr[last]);
	for(int i=0;i<(int)v[u].size();i++)
	{
		int nu=v[u][i];
		if(nu==p)
			continue;

		if(arr[u]>arr[last])
			ret=max(ret,( int)(1+dfsi(nu,u,u<nu,u)));

		ret=max(ret,dfsi(nu,last,u<nu,u));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	arr[0]=-2;

	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	memset(inc,-1,sizeof inc);
	for(int i=1;i<=n;i++)
	{
		ret=max(ret,(int)dfsi(i,0,0,0));

	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int inc[6009][6009];
int arr[9009];
vector<vector<int> > v;
int n;

int dfsi(int u,int last,int p)
{
	if(inc[u][last])
		return inc[u][last];

	int &ret=inc[u][last];
	ret=1;
	for(int i=0;i<v[u].size();i++)
	{
		int nu=v[u][i];
		if(nu==p)
			continue;

		if(arr[nu]>arr[last])
			ret=max(ret,1+dfsi(nu,nu,u));
		ret=max(ret,dfsi(nu,last,u));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	for(int i=1;i<=n;i++)
	{
		ret=max(ret,dfsi(i,i,0));

	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int inc[6009][6009][3];
int arr[9009];
vector<vector<int> > v;
int n;

int dfsi(int u,int last,int k,int p)
{
	int &ret=inc[u][last][k];
	if(ret!=-1)
		return ret;

	ret=(arr[u]>arr[last]);
	for(int i=0;i<(int)v[u].size();i++)
	{
		int nu=v[u][i];
		if(nu==p)
			continue;

		if(arr[u]>arr[last])
			ret=max(ret,1+dfsi(nu,u,u<nu,u));

		ret=max(ret,dfsi(nu,last,u<nu,u));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	arr[0]=-2;

	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	memset(inc,-1,sizeof inc);
	for(int i=1;i<=n;i++)
	{
		ret=max(ret,dfsi(i,0,0,0));

	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int inc[6009][6009];
int arr[9009];
vector<vector<int> > v;
int n;

int dfsi(int u,int last,int p)
{
	if(inc[u][last]!=-1)
		return inc[u][last];

	int &ret=inc[u][last];
	ret=0;
	for(int i=0;i<(int)v[u].size();i++)
	{
		int nu=v[u][i];
		if(nu==p)
			continue;

		if(arr[nu]>arr[last])
			ret=max(ret,1+dfsi(nu,nu,u));

		ret=max(ret,dfsi(nu,last,u));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	memset(inc,-1,sizeof inc);
	for(int i=1;i<=n;i++)
	{
		ret=max(ret,max(1+dfsi(i,i,0),dfsi(i,0,0)));

	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

int inc[6009][6009];
int arr[9009];
vector<vector<int> > v;
int n;

int dfsi(int u,int last,int p)
{
	if(inc[u][last])
		return inc[u][last];

	int &ret=inc[u][last];
	ret=0;
	for(int i=0;i<(int)v[u].size();i++)
	{
		int nu=v[u][i];
		if(nu==p)
			continue;

		if(arr[nu]>arr[last])
			ret=max(ret,1+dfsi(nu,u,u));

		ret=max(ret,dfsi(nu,last,u));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	for(int i=1;i<=n;i++)
	{
		ret=max(ret,1+max(dfsi(i,i,0),dfsi(i,0,0)));

	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

short int  inc[6002][6002][2];
int arr[6002];
vector<vector<int> > v;
int n;

short int dfsi(int u,int last,bool k,int p)
{
	short int &ret=inc[u][last][k];
	if(ret!=-1)
		return ret;

	ret=(arr[u]>arr[last]);
	for(int i=0;i<(int)v[u].size();i++)
	{
		int nu=v[u][i];
		if(nu==p)
			continue;

		if(arr[u]>arr[last])
			ret=max(ret,(short int)(1+dfsi(nu,u,u<nu,u)));

		ret=max(ret,dfsi(nu,last,u<nu,u));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	arr[0]=-2;

	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	memset(inc,-1,sizeof inc);
	for(int i=1;i<=n;i++)
	{
		ret=max(ret,dfsi(i,0,0,0));

	}
	cout<<ret<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

short int  inc[6002][6002][2];
int arr[6002];
vector<vector<int> > v;
int n;

short int dfsi(int u,int last,bool k,int p)
{
	short int &ret=inc[u][last][k];
	if(ret!=-1)
		return ret;

	ret=(arr[u]>arr[last]);
	for(int i=0;i<(int)v[u].size();i++)
	{
		int nu=v[u][i];
		if(nu==p)
			continue;

		if(arr[u]>arr[last])
			ret=max(ret,(short int)(1+dfsi(nu,u,u<nu,u)));

		ret=max(ret,dfsi(nu,last,u<nu,u));
	}
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("2.in", "r", stdin);

#endif // ONLINE_JUDGE
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int a;
	int b;
	v.resize(n+2);
	arr[0]=-2;

	for(int i=0;i<n;i++)
		cin>>arr[i+1];
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int ret=1;
	memset(inc,-1,sizeof inc);
	for(int i=1;i<=n;i++)
	{
		ret=max(ret,(int)dfsi(i,0,0,0));

	}
	cout<<ret<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> AdjOf[NMAX];

int C[NMAX];
bool Visited[NMAX];

void readInput(){
    cin >>n;
    for(int i = 1; i<=n;i++){
        cin >>Pop[i];
    };

    for(int i = 0; i<n-1; i++){
        int x, y;
        cin >>x>>y;
        AdjOf[x].push_back(y);
        AdjOf[y].push_back(x);
    };
};

int LIS(int len){

    //for(int i = 0; i<=len; i++) cout <<C[i]<<" ";
    //cout <<" that is seq.\n";
    int Cnt[NMAX];
    int best = 1;
    for(int i = 0; i<len+2; i++) Cnt[i] = 1;

    for(int i = 1; i<=len; i++)
    for(int j = 0; j<i; j++)
    if (C[j]<C[i])  // population
    if (Cnt[j]+1 > Cnt[i]) {    // better value for Cnt
        Cnt[i] = Cnt[j] +1;
        if (Cnt[i]>best) best = Cnt[i];
    };

    return best;
}

int myBest = 1;

void bestFrom(int u, int len){

    C[len] = Pop[u];

    bool leave = true;
    for(int iv = 0; iv < AdjOf[u].size(); iv++){
        int v = AdjOf[u][iv];
        if (!Visited[v]){
            leave = false;
            Visited[v] = true;
            bestFrom(v, len+1);
            Visited[v] = false;
        };// if
    };//for

    if (leave){
        int lis = LIS(len);
        myBest = myBest < lis ? lis: myBest;
    };
}

void solveIt(){

    myBest = 1;
    for(int i = 1; i<=n; i++){

        for(int j = 0; j<=n; j++){
            C[j] = 0;
            Visited[j] = false;
        };

        Visited[i] = true;
        bestFrom(i,0);
    }

    cout <<myBest<<endl;

};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> AdjOf[NMAX];

int C[NMAX];
bool Visited[NMAX];

void readInput(){
    cin >>n;
    for(int i = 1; i<=n;i++){
        cin >>Pop[i];
    };

    for(int i = 0; i<n-1; i++){
        int x, y;
        cin >>x>>y;
        AdjOf[x].push_back(y);
        AdjOf[y].push_back(x);
    };
};

int LIS(int len){

    //for(int i = 0; i<=len; i++) cout <<C[i]<<" ";
    //cout <<" that is seq.\n";

    vector<int> BB;

    int u,v,i,j;

    int Cnt[NMAX];
    int best = 1;
    for(i = 0; i<len+2; i++) Cnt[i] = 1;

    BB.push_back(0);

    for(i = 1; i<=len; i++)
    for(j = 0; j<i; j++){
        if (C[BB.back()]<C[i]){  // population
            BB.push_back(i);
        }

        for (u = 0, v = BB.size()-1; u < v;){
            int c = (u + v) / 2;
            if (C[BB[c]] < C[i]) u=c+1; else v=c;
        };

        if (C[i] < C[BB[u]]){
            BB[u] = i;
        }
    }// double for

    return best;
}

int LIS2(int len){
    set<int> st;
    set<int>::iterator it;
    st.clear();
    int i;
    for(i=0; i<=len; i++) {
        st.insert(C[i]); it=st.find(C[i]);
        it++;
        if(it!=st.end()) st.erase(it);
    }

    //cout<<st.size()<<endl;
    //return st.size();
    int s1 = st.size();
    st.clear();
    i;
    for(i=len; i>=0; i--) {
        st.insert(C[i]); it=st.find(C[i]);
        it++;
        if(it!=st.end()) st.erase(it);
    }
    int s2 = st.size();

    if (s1<s2) return s2;
    return s1;
}

int myBest = 1;
set<int> MySet;
set<int> ::iterator it;

void bestFrom(int u, int len){

    C[len] = Pop[u];
    //MySet.insert(Pop[u]);
    //it = MySet.find(Pop[u]);
    //it++;
    //if (it!= MySet.end()) MySet.erase(it);

    //Visited[u] = true;

    bool leaf = true;
    for(int iv = 0; iv < AdjOf[u].size(); iv++){
        int v = AdjOf[u][iv];
        if (!Visited[v]){
            leaf = false;
            Visited[v] = true;
            //MySet.insert(Pop[v]);
            bestFrom(v, len+1);
            //MySet.erase(Pop[v]);
            Visited[v] = false;
        };// if
    };//for

    //Visited[u] = false;

    if (leaf){
        //Visited[u] = true;
        if (len >= myBest){
            //int lis = LIS(len);
            int lis = LIS2(len);
            //int lis = MySet.size();
            myBest = myBest < lis ? lis: myBest;
        };
    };
}

void solveIt(){

    myBest = 1;
    for(int i = 1; i<=n; i++){
        if (AdjOf[i].size() >1) continue;
        //if (Visited[i]) continue;

        for(int j = 0; j<=n; j++){
            C[j] = 0;
            Visited[j] = false;
        };

        MySet.clear();
        MySet.insert(Pop[i]);

        Visited[i] = true;
        bestFrom(i,0);
        break;
        //Visited[i] = true;
    }

    cout <<myBest<<endl;

};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <sstream>
#include <string.h>
#include <math.h>
#include <stdlib.h>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> NextOf[NMAX];

void readInput(){
    cin >>n;
    for(int i = 1; i<=n; i++)
        cin>>Pop[i];

    for(int i = 0; i<n-1; i++){
        int u, v;
        cin >>u>>v;
        if (Pop[v] > Pop[u]){
            NextOf[u].push_back(v);
        };
        if (Pop[u] > Pop[v]){
            NextOf[v].push_back(u);
        }
    };
};

int BestFrom(int x){
    int Q[NMAX];
    memset(Q, 0, sizeof(Q));

    int dq = 0;
    int cq = 0;
    Q[0] = x;

    bool Visit[NMAX];
    memset(Visit, 0, sizeof(Visit)); // false;

    int Dist[NMAX];
    memset(Dist,0,sizeof(Dist));

    Visit[x] = true;
    Dist[x] = 1;
    int mD = 1;

    while(dq <= cq){
        int u = Q[dq];
        dq ++;
        for(int iv = 0; iv <NextOf[u].size(); iv++){
            int v = NextOf[u][iv];
            if (Visit[v]) continue;
            Visit[v] = true;
            Dist[v] = Dist[u] +1;
            mD = max(mD, Dist[v]);
            cq ++;
            Q[cq] = v;
        };
    };
    return mD;
};

int mLen = 1;
void bestDFS(int u, int len ){
    mLen = max(mLen,len);
    for(int iv = 0; iv <NextOf[u].size(); iv ++){
        int v = NextOf[u][iv];
        bestDFS(v, len+1);
    }
};

void solveIt(){
    int best = 1;
    mLen = 1;
    for(int i = 1; i<=n; i++){
        bestDFS(i,1);
        //best = max(best, c);
    }

   cout <<mLen;
};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <sstream>
#include <string.h>
#include <math.h>
#include <stdlib.h>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> NextOf[NMAX];

void readInput(){
    cin >>n;
    for(int i = 1; i<=n; i++)
        cin>>Pop[i];

    for(int i = 0; i<n-1; i++){
        int u, v;
        cin >>u>>v;
        //if (Pop[v] > Pop[u]){
            NextOf[u].push_back(v);
        //};
        //if (Pop[u] > Pop[v]){
            NextOf[v].push_back(u);
        //}
    };
};

int BestFrom(int x){
    int Q[NMAX];
    memset(Q, 0, sizeof(Q));

    int dq = 0;
    int cq = 0;
    Q[0] = x;

    bool Visit[NMAX];
    memset(Visit, 0, sizeof(Visit)); // false;

    int Dist[NMAX];
    memset(Dist,0,sizeof(Dist));

    Visit[x] = true;
    Dist[x] = 1;
    int mD = 1;

    while(dq <= cq){
        int u = Q[dq];
        dq ++;
        for(int iv = 0; iv <NextOf[u].size(); iv++){
            int v = NextOf[u][iv];
            if (Visit[v]) continue;
            Visit[v] = true;
            Dist[v] = Dist[u] +1;
            mD = max(mD, Dist[v]);
            cq ++;
            Q[cq] = v;
        };
    };
    return mD;
};

int mLen = 1;
int C[NMAX];

int LIS(int A[NMAX], int L){
    int Cnt[NMAX];

//for (int i = 0; i<L; i++) cout <<A[i]<<endl;

    for(int i = 0; i<=L; i++) Cnt[i] = 1;

    for(int i = 1; i<L; i++)
    for(int j = 0; j<i; j++)
        if (A[j] < A[i]) Cnt[i] = max(Cnt[i], Cnt[j]+1);

    int b = 1;
    for(int i = 0; i<L; i++)
        if (b<Cnt[i]) b = Cnt[i];

    return b;
};

bool check[NMAX];

void bestDFS(int u, int len ){
    //mLen = max(mLen,len);
    C[len] = Pop[u];
    mLen = max(mLen, LIS(C, len+1));
    for(int iv = 0; iv < NextOf[u].size(); iv ++){
        int v = NextOf[u][iv];
        if (!check[v]) continue;
        check[v] = false;
        bestDFS(v, len+1);
        check[v] = true;
    };
};

void solveIt(){
    int best = 1;
    mLen = 1;
    for(int i = 1; i<=n; i++){
        //cout <<"chec for "<<i<<endl;
        for(int j = 0; j<=n; j++) check[j] = true;
        check[i] = false;
        bestDFS(i,0);
        //best = max(best, c);
        //cout <<mLen<<endl;
    }
   cout <<mLen;
};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> AdjOf[NMAX];

int C[NMAX];
bool Visited[NMAX];

void readInput(){
    cin >>n;
    for(int i = 1; i<=n;i++){
        cin >>Pop[i];
    };

    for(int i = 0; i<n-1; i++){
        int x, y;
        cin >>x>>y;
        AdjOf[x].push_back(y);
        AdjOf[y].push_back(x);
    };
};

int LIS(int len){

    //for(int i = 0; i<=len; i++) cout <<C[i]<<" ";
    //cout <<" that is seq.\n";

    vector<int> BB;

    int u,v,i,j;

    int Cnt[NMAX];
    int best = 1;
    for(i = 0; i<len+2; i++) Cnt[i] = 1;

    BB.push_back(0);

    for(i = 1; i<=len; i++)
    for(j = 0; j<i; j++){
        if (C[BB.back()]<C[i]){  // population
            BB.push_back(i);
        }

        for (u = 0, v = BB.size()-1; u < v;){
            int c = (u + v) / 2;
            if (C[BB[c]] < C[i]) u=c+1; else v=c;
        };

        if (C[i] < C[BB[u]]){
            BB[u] = i;
        }
    }// double for

    return best;
}

int LIS2(int len){
    set<int> st;
    set<int>::iterator it;
    st.clear();
    int i;
    for(i=0; i<=len; i++) {
        st.insert(C[i]); it=st.find(C[i]);
        it++;
        if(it!=st.end()) st.erase(it);
    }

    //cout<<st.size()<<endl;
    return st.size();
}

int myBest = 1;

void bestFrom(int u, int len){

    C[len] = Pop[u];

    bool leaf = true;
    for(int iv = 0; iv < AdjOf[u].size(); iv++){
        int v = AdjOf[u][iv];
        if (!Visited[v]){
            leaf = false;
            Visited[v] = true;
            bestFrom(v, len+1);
            Visited[v] = false;
        };// if
    };//for

    if (leaf){
        if (len >= myBest){
            //int lis = LIS(len);
            int lis = LIS2(len);
            myBest = myBest < lis ? lis: myBest;
        };
    };
}

void solveIt(){

    myBest = 1;
    for(int i = 1; i<=n; i++){
        if (AdjOf[i].size() >1) continue;
        for(int j = 0; j<=n; j++){
            C[j] = 0;
            Visited[j] = false;
        };

        Visited[i] = true;
        bestFrom(i,0);
    }

    cout <<myBest<<endl;

};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> AdjOf[NMAX];

int C[NMAX];
int Opt[NMAX];
bool Visited[NMAX];
int myBest = 1;

void readInput(){
    cin >>n;
    for(int i = 1; i<=n;i++){
        cin >>Pop[i];
    };
    for(int i = 0; i<n-1; i++){
        int x, y;
        cin >>x>>y;
        AdjOf[x].push_back(y);
        AdjOf[y].push_back(x);
    };
};

int LIS2(int len, int from){
    set<int> st;
    set<int>::iterator it;
    st.clear();
    int i;
    for(i=0; i<=len; i++) {
        st.insert(C[i]); it=st.find(C[i]);
        it++;
        if(it!=st.end()) st.erase(it);
    }

    //cout<<st.size()<<endl;
    return st.size();
};

int LIS(int len){

    vector<int> BB; BB.clear();
    int u,v,i;
    int best = 1;
    //for(i = 0; i<=len; i++) cout <<C[i] <<" "; cout <<endl;

    BB.push_back(0);

    for(i = 1; i<=len; i++){
        if (C[BB.back()]<C[i]){  // population
            BB.push_back(i);
            best = max(best, (int)BB.size());
            continue;
        }

        for (u = 0, v = BB.size()-1; u < v;){
            int c = (u + v) / 2;
            if (C[BB[c]] < C[i]) u=c+1; else v=c;
        };

        if (C[i] < C[BB[u]]){
            BB[u] = i;

        }

    }// for

    return best;
}

//int Opt[NMAX];

void bestFrom(int u, int len){

    C[len] = Pop[u];
    bool leaf = true;
    for(int iv = 0; iv < AdjOf[u].size(); iv++){
        int v = AdjOf[u][iv];
        if (!Visited[v]){
            leaf = false;
            Visited[v] = true;
            bestFrom(v, len+1);
            Visited[v] = false;
        };// if
    };//for

    if (leaf){
        if (len >= myBest){
            int lis;
            //lis = LIS2(len, myBest);
            lis = LIS(len);
            myBest = myBest < lis ? lis: myBest;
            //cout <<" best = "<<myBest<<endl;
        };// if len
    };//if leaf
}

void solveIt(){

    myBest = 1;
    for(int i = 1; i<=n; i++){
        if (AdjOf[i].size() >1) continue;
        for(int j = 0; j<=n; j++){
            C[j] = 0;
            Visited[j] = false;
            Opt[i] = 1;
        };
        //cout <<"From "<<i<<" : "<<endl;
        Visited[i] = true;
        bestFrom(i,0);
    }

    cout <<myBest<<endl;

};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> AdjOf[NMAX];

int C[NMAX];
bool Visited[NMAX];

void readInput(){
    cin >>n;
    for(int i = 1; i<=n;i++){
        cin >>Pop[i];
    };

    for(int i = 0; i<n-1; i++){
        int x, y;
        cin >>x>>y;
        AdjOf[x].push_back(y);
        AdjOf[y].push_back(x);
    };
};

int LIS(int len){

    //for(int i = 0; i<=len; i++) cout <<C[i]<<" ";
    //cout <<" that is seq.\n";

    vector<int> BB;

    int u,v,i,j;

    int Cnt[NMAX];
    int best = 1;
    for(i = 0; i<len+2; i++) Cnt[i] = 1;

    BB.push_back(0);

    for(i = 1; i<=len; i++)
    for(j = 0; j<i; j++){
        if (C[BB.back()]<C[i]){  // population
            BB.push_back(i);
        }

        for (u = 0, v = BB.size()-1; u < v;){
            int c = (u + v) / 2;
            if (C[BB[c]] < C[i]) u=c+1; else v=c;
        };

        if (C[i] < C[BB[u]]){
            BB[u] = i;
        }
    }// double for

    return best;
}

int LIS2(int len){
    set<int> st;
    set<int>::iterator it;
    st.clear();
    int i;
    for(i=0; i<=len; i++) {
        st.insert(C[i]); it=st.find(C[i]);
        it++;
        if(it!=st.end()) st.erase(it);
    }

    //cout<<st.size()<<endl;
    return st.size();
}

int myBest = 1;
set<int> MySet;
set<int> ::iterator it;

void bestFrom(int u, int len){

    C[len] = Pop[u];
    MySet.insert(Pop[u]);
    it = MySet.find(Pop[u]);
    it++;
    if (it!= MySet.end()) MySet.erase(it);

    bool leaf = true;
    for(int iv = 0; iv < AdjOf[u].size(); iv++){
        int v = AdjOf[u][iv];
        if (!Visited[v]){
            leaf = false;
            Visited[v] = true;
            bestFrom(v, len+1);
            Visited[v] = false;
        };// if
    };//for

    if (leaf){
        if (len >= myBest){
            //int lis = LIS(len);
            //int lis = LIS2(len);

            int lis = MySet.size();
            myBest = myBest < lis ? lis: myBest;
        };
    };
}

void solveIt(){

    myBest = 1;
    for(int i = 1; i<=n; i++){
        if (AdjOf[i].size() >1) continue;
        for(int j = 0; j<=n; j++){
            C[j] = 0;
            Visited[j] = false;
        };
        MySet.clear();
        Visited[i] = true;
        bestFrom(i,0);
    }

    cout <<myBest<<endl;

};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> AdjOf[NMAX];

int C[NMAX];
bool Visited[NMAX];

void readInput(){
    cin >>n;
    for(int i = 1; i<=n;i++){
        cin >>Pop[i];
    };

    for(int i = 0; i<n-1; i++){
        int x, y;
        cin >>x>>y;
        AdjOf[x].push_back(y);
        AdjOf[y].push_back(x);
    };
};

int LIS(int len){

    //for(int i = 0; i<=len; i++) cout <<C[i]<<" ";
    //cout <<" that is seq.\n";

    vector<int> BB;

    int u,v,i,j;

    int Cnt[NMAX];
    int best = 1;
    for(i = 0; i<len+2; i++) Cnt[i] = 1;

    BB.push_back(0);

    for(i = 1; i<=len; i++)
    for(j = 0; j<i; j++){
        if (C[BB.back()]<C[i]){  // population
            BB.push_back(i);
        }

        for (u = 0, v = BB.size()-1; u < v;){
            int c = (u + v) / 2;
            if (C[BB[c]] < C[i]) u=c+1; else v=c;
        };

        if (C[i] < C[BB[u]]){
            BB[u] = i;
        }
    }// double for

    return best;
}

int LIS2(int len){
    set<int> st;
    set<int>::iterator it;
    st.clear();
    int i;
    for(i=0; i<=len; i++) {
        st.insert(C[i]); it=st.find(C[i]);
        it++;
        if(it!=st.end()) st.erase(it);
    }

    //cout<<st.size()<<endl;
    return st.size();
}

int myBest = 1;

void bestFrom(int u, int len){

    C[len] = Pop[u];

    bool leave = true;
    for(int iv = 0; iv < AdjOf[u].size(); iv++){
        int v = AdjOf[u][iv];
        if (!Visited[v]){
            leave = false;
            Visited[v] = true;
            bestFrom(v, len+1);
            Visited[v] = false;
        };// if
    };//for

    if (leave){
        if (len > myBest){
            //int lis = LIS(len);
            int lis = LIS2(len);
            myBest = myBest < lis ? lis: myBest;
        };
    };
}

void solveIt(){

    myBest = 1;
    for(int i = 1; i<=n; i++){

        for(int j = 0; j<=n; j++){
            C[j] = 0;
            Visited[j] = false;
        };

        Visited[i] = true;
        bestFrom(i,0);
    }

    cout <<myBest<<endl;

};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> AdjOf[NMAX];

int C[NMAX];
bool Visited[NMAX];

void readInput(){
    cin >>n;
    for(int i = 1; i<=n;i++){
        cin >>Pop[i];
    };

    for(int i = 0; i<n-1; i++){
        int x, y;
        cin >>x>>y;
        AdjOf[x].push_back(y);
        AdjOf[y].push_back(x);
    };
};

int LIS(int len){

    //for(int i = 0; i<=len; i++) cout <<C[i]<<" ";
    //cout <<" that is seq.\n";

    vector<int> BB;

    int u,v,i,j;

    int Cnt[NMAX];
    int best = 1;
    for(i = 0; i<len+2; i++) Cnt[i] = 1;

    BB.push_back(0);

    for(i = 1; i<=len; i++)
    for(j = 0; j<i; j++){
        if (C[BB.back()]<C[i]){  // population
            BB.push_back(i);
        }

        for (u = 0, v = BB.size()-1; u < v;){
            int c = (u + v) / 2;
            if (C[BB[c]] < C[i]) u=c+1; else v=c;
        };

        if (C[i] < C[BB[u]]){
            BB[u] = i;
        }
    }// double for

    return best;
}

int LIS2(int len){
    set<int> st;
    set<int>::iterator it;
    st.clear();
    int i;
    for(i=0; i<=len; i++) {
        st.insert(C[i]); it=st.find(C[i]);
        it++;
        if(it!=st.end()) st.erase(it);
    }

    //cout<<st.size()<<endl;
    return st.size();
}

int myBest = 1;

void bestFrom(int u, int len){

    C[len] = Pop[u];

    bool leave = true;
    for(int iv = 0; iv < AdjOf[u].size(); iv++){
        int v = AdjOf[u][iv];
        if (!Visited[v]){
            leave = false;
            Visited[v] = true;
            bestFrom(v, len+1);
            Visited[v] = false;
        };// if
    };//for

    if (leave){
        //int lis = LIS(len);
        int lis = LIS2(len);
        myBest = myBest < lis ? lis: myBest;
    };
}

void solveIt(){

    myBest = 1;
    for(int i = 1; i<=n; i++){

        for(int j = 0; j<=n; j++){
            C[j] = 0;
            Visited[j] = false;
        };

        Visited[i] = true;
        bestFrom(i,0);
    }

    cout <<myBest<<endl;

};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> AdjOf[NMAX];

int C[NMAX];
bool Visited[NMAX];

void readInput(){
    cin >>n;
    for(int i = 1; i<=n;i++){
        cin >>Pop[i];
    };

    for(int i = 0; i<n-1; i++){
        int x, y;
        cin >>x>>y;
        AdjOf[x].push_back(y);
        AdjOf[y].push_back(x);
    };
};

int LIS(int len){

    //for(int i = 0; i<=len; i++) cout <<C[i]<<" ";
    //cout <<" that is seq.\n";

    vector<int> BB;

    int u,v,i,j;

    int Cnt[NMAX];
    int best = 1;
    for(i = 0; i<len+2; i++) Cnt[i] = 1;

    BB.push_back(0);

    for(i = 1; i<=len; i++)
    for(j = 0; j<i; j++){
        if (C[BB.back()]<C[i]){  // population
            BB.push_back(i);
        }

        for (u = 0, v = BB.size()-1; u < v;){
            int c = (u + v) / 2;
            if (C[BB[c]] < C[i]) u=c+1; else v=c;
        };

        if (C[i] < C[BB[u]]){
            BB[u] = i;
        }
    }// double for

    return best;
}

int LIS2(int len){
    set<int> st;
    set<int>::iterator it;
    st.clear();
    int i;
    for(i=0; i<=len; i++) {
        st.insert(C[i]); it=st.find(C[i]);
        it++;
        if(it!=st.end()) st.erase(it);
    }

    //cout<<st.size()<<endl;
    //return st.size();
    int s1 = st.size();
    st.clear();
    i;
    for(i=len; i>=0; i--) {
        st.insert(C[i]); it=st.find(C[i]);
        it++;
        if(it!=st.end()) st.erase(it);
    }
    int s2 = st.size();

    if (s1<s2) return s2;
    return s1;
}

int myBest = 1;
set<int> MySet;
set<int> ::iterator it;

void bestFrom(int u, int len){

    C[len] = Pop[u];
    //MySet.insert(Pop[u]);
    //it = MySet.find(Pop[u]);
    //it++;
    //if (it!= MySet.end()) MySet.erase(it);

    Visited[u] = true;

    bool leaf = true;
    for(int iv = 0; iv < AdjOf[u].size(); iv++){
        int v = AdjOf[u][iv];
        if (!Visited[v]){
            leaf = false;
            //Visited[v] = true;
            bestFrom(v, len+1);
            //Visited[v] = false;
        };// if
    };//for

    Visited[u] = false;

    if (leaf){
        Visited[u] = true;
        if (len >= myBest){
            //int lis = LIS(len);
            int lis = LIS2(len);
            //int lis = MySet.size();
            myBest = myBest < lis ? lis: myBest;
        };
    };
}

void solveIt(){

    myBest = 1;
    for(int i = 1; i<=n; i++){
        if (AdjOf[i].size() >1) continue;
        if (Visited[i]) continue;

        for(int j = 0; j<=n; j++){
            C[j] = 0;
            //Visited[j] = false;
        };

        MySet.clear();

        Visited[i] = true;
        bestFrom(i,0);
    }

    cout <<myBest<<endl;

};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> AdjOf[NMAX];

int C[NMAX];
bool Visited[NMAX];

void readInput(){
    cin >>n;
    for(int i = 1; i<=n;i++){
        cin >>Pop[i];
    };

    for(int i = 0; i<n-1; i++){
        int x, y;
        cin >>x>>y;
        AdjOf[x].push_back(y);
        AdjOf[y].push_back(x);
    };
};

int LIS(int len){

    //for(int i = 0; i<=len; i++) cout <<C[i]<<" ";
    //cout <<" that is seq.\n";

    vector<int> BB;

    int u,v,i,j;

    int Cnt[NMAX];
    int best = 1;
    for(i = 0; i<len+2; i++) Cnt[i] = 1;

    BB.push_back(0);

    for(i = 1; i<=len; i++)
    for(j = 0; j<i; j++){
        if (C[BB.back()]<C[i]){  // population
            BB.push_back(i);
        }

        for (u = 0, v = BB.size()-1; u < v;){
            int c = (u + v) / 2;
            if (C[BB[c]] < C[i]) u=c+1; else v=c;
        };

        if (C[i] < C[BB[u]]){
            BB[u] = i;
        }
    }// double for

    return best;
}

int LIS2(int len){
    set<int> st;
    set<int>::iterator it;
    st.clear();
    int i;
    for(i=0; i<=len; i++) {
        st.insert(C[i]); it=st.find(C[i]);
        it++;
        if(it!=st.end()) st.erase(it);
    }

    //cout<<st.size()<<endl;
    return st.size();
}

int myBest = 1;

void bestFrom(int u, int len){

    C[len] = Pop[u];

    bool leave = true;
    for(int iv = 0; iv < AdjOf[u].size(); iv++){
        int v = AdjOf[u][iv];
        if (!Visited[v]){
            leave = false;
            Visited[v] = true;
            bestFrom(v, len+1);
            Visited[v] = false;
        };// if
    };//for

    if (leave){
        if (len >= myBest){
            //int lis = LIS(len);
            int lis = LIS2(len);
            myBest = myBest < lis ? lis: myBest;
        };
    };
}

void solveIt(){

    myBest = 1;
    for(int i = 1; i<=n; i++){

        for(int j = 0; j<=n; j++){
            C[j] = 0;
            Visited[j] = false;
        };

        Visited[i] = true;
        bestFrom(i,0);
    }

    cout <<myBest<<endl;

};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> AdjOf[NMAX];

int C[NMAX];
bool Visited[NMAX];

void readInput(){
    cin >>n;
    for(int i = 1; i<=n;i++){
        cin >>Pop[i];
    };

    for(int i = 0; i<n-1; i++){
        int x, y;
        cin >>x>>y;
        AdjOf[x].push_back(y);
        AdjOf[y].push_back(x);
    };
};

int LIS(int len){

    //for(int i = 0; i<=len; i++) cout <<C[i]<<" ";
    //cout <<" that is seq.\n";

    vector<int> BB;

    int u,v,i,j;

    int Cnt[NMAX];
    int best = 1;
    for(i = 0; i<len+2; i++) Cnt[i] = 1;

    BB.push_back(0);

    for(i = 1; i<=len; i++)
    for(j = 0; j<i; j++){
        if (C[BB.back()]<C[i]){  // population
            BB.push_back(i);
        }

        for (u = 0, v = BB.size()-1; u < v;){
            int c = (u + v) / 2;
            if (C[BB[c]] < C[i]) u=c+1; else v=c;
        };

        if (C[i] < C[BB[u]]){
            BB[u] = i;
        }
    }// double for

    return best;
}

int LIS2(int len){
    set<int> st;
    set<int>::iterator it;
    st.clear();
    int i;
    for(i=0; i<=len; i++) {
        st.insert(C[i]); it=st.find(C[i]);
        it++;
        if(it!=st.end()) st.erase(it);
    }

    //cout<<st.size()<<endl;
    //return st.size();
    int s1 = st.size();
    st.clear();
    i;
    for(i=len; i>=0; i--) {
        st.insert(C[i]); it=st.find(C[i]);
        it++;
        if(it!=st.end()) st.erase(it);
    }
    int s2 = st.size();

    if (s1<s2) return s2;
    return s1;
}

int myBest = 1;
set<int> MySet;
set<int> ::iterator it;

void bestFrom(int u, int len){

    C[len] = Pop[u];
    //MySet.insert(Pop[u]);
    it = MySet.find(Pop[u]);
    it++;
    if (it!= MySet.end()) MySet.erase(it);

    //Visited[u] = true;

    bool leaf = true;
    for(int iv = 0; iv < AdjOf[u].size(); iv++){
        int v = AdjOf[u][iv];
        if (!Visited[v]){
            leaf = false;
            Visited[v] = true;
            MySet.insert(Pop[v]);
            bestFrom(v, len+1);
            MySet.erase(Pop[v]);
            Visited[v] = false;
        };// if
    };//for

    //Visited[u] = false;

    if (leaf){
        //Visited[u] = true;
        if (len >= myBest){
            //int lis = LIS(len);
            int lis = LIS2(len);
            //int lis = MySet.size();
            myBest = myBest < lis ? lis: myBest;
        };
    };
}

void solveIt(){

    myBest = 1;
    for(int i = 1; i<=n; i++){
        if (AdjOf[i].size() >1) continue;
        if (Visited[i]) continue;

        for(int j = 0; j<=n; j++){
            C[j] = 0;
            //Visited[j] = false;
        };

        MySet.clear();
        MySet.insert(Pop[i]);

        Visited[i] = true;
        bestFrom(i,0);
        //Visited[i] = true;
    }

    cout <<myBest<<endl;

};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> AdjOf[NMAX];

int C[NMAX];
bool Visited[NMAX];

void readInput(){
    cin >>n;
    for(int i = 1; i<=n;i++){
        cin >>Pop[i];
    };

    for(int i = 0; i<n-1; i++){
        int x, y;
        cin >>x>>y;
        AdjOf[x].push_back(y);
        AdjOf[y].push_back(x);
    };
};

int LIS(int len){

    //for(int i = 0; i<=len; i++) cout <<C[i]<<" ";
    //cout <<" that is seq.\n";

    vector<int> BB;

    int u,v,i,j;

    int Cnt[NMAX];
    int best = 1;
    for(i = 0; i<len+2; i++) Cnt[i] = 1;

    BB.push_back(0);

    for(i = 1; i<=len; i++)
    for(j = 0; j<i; j++){
        if (C[BB.back()]<C[i]){  // population
            BB.push_back(i);
        }

        for (u = 0, v = BB.size()-1; u < v;){
            int c = (u + v) / 2;
            if (C[BB[c]] < C[i]) u=c+1; else v=c;
        };

        if (C[i] < C[BB[u]]){
            BB[u] = i;
        }
    }// double for

    return best;
}

int LIS2(int len){
    set<int> st;
    set<int>::iterator it;
    st.clear();
    int i;
    for(i=0; i<=len; i++) {
        st.insert(C[i]); it=st.find(C[i]);
        it++;
        if(it!=st.end()) st.erase(it);
    }

    //cout<<st.size()<<endl;
    //return st.size();
    int s1 = st.size();
    st.clear();
    i;
    for(i=len; i>=0; i--) {
        st.insert(C[i]); it=st.find(C[i]);
        it++;
        if(it!=st.end()) st.erase(it);
    }
    int s2 = st.size();

    if (s1<s2) return s2;
    return s1;
}

int myBest = 1;
set<int> MySet;
set<int> ::iterator it;

void bestFrom(int u, int len){

    C[len] = Pop[u];
    //MySet.insert(Pop[u]);
    //it = MySet.find(Pop[u]);
    //it++;
    //if (it!= MySet.end()) MySet.erase(it);

    //Visited[u] = true;

    bool leaf = true;
    for(int iv = 0; iv < AdjOf[u].size(); iv++){
        int v = AdjOf[u][iv];
        if (!Visited[v]){
            leaf = false;
            Visited[v] = true;
            //MySet.insert(Pop[v]);
            bestFrom(v, len+1);
            //MySet.erase(Pop[v]);
            Visited[v] = false;
        };// if
    };//for

    //Visited[u] = false;

    if (leaf){
        //Visited[u] = true;
        if (len >= myBest){
            //int lis = LIS(len);
            int lis = LIS2(len);
            //int lis = MySet.size();
            myBest = myBest < lis ? lis: myBest;
        };
    };
}

void solveIt(){

    myBest = 1;
    for(int i = 1; i<=n; i++){
        if (AdjOf[i].size() >1) continue;
        //if (Visited[i]) continue;

        for(int j = 0; j<=n; j++){
            C[j] = 0;
            //Visited[j] = false;
        };

        MySet.clear();
        MySet.insert(Pop[i]);

        Visited[i] = true;
        bestFrom(i,0);
        break;
        //Visited[i] = true;
    }

    cout <<myBest<<endl;

};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> AdjOf[NMAX];

int C[NMAX];
bool Visited[NMAX];

void readInput(){
    cin >>n;
    for(int i = 1; i<=n;i++){
        cin >>Pop[i];
    };
    for(int i = 0; i<n-1; i++){
        int x, y;
        cin >>x>>y;
        AdjOf[x].push_back(y);
        AdjOf[y].push_back(x);
    };
};

int myBest = 1;
int Opt[NMAX];

void bestFrom(int u, int len){

    C[len] = Pop[u];

    Opt[len] = 1;
    for(int i = 0; i<=len; i++){
        if (C[i]<C[len]){
            Opt[len] = Opt[len] < Opt[i] +1 ? Opt[i] +1 : Opt[len];
        };
        //cout <<C[i]<<" ";
    };

    myBest = myBest < Opt[len] ? Opt[len]: myBest;
    //cout <<" best = "<<myBest<<endl;

    bool leaf = true;
    for(int iv = 0; iv < AdjOf[u].size(); iv++){
        leaf = false;
        int v = AdjOf[u][iv];
        if (!Visited[v]){
            Visited[v] = true;
            bestFrom(v, len+1);
            Visited[v] = false;
        };// if
    };//for

    if (leaf){
        if (len >= myBest){
            //int lis;
            // = LIS2(len);

            myBest = myBest < Opt[len] ? Opt[len]: myBest;
            //cout <<" best = "<<myBest<<endl;

        };// if len

        //Opt[len] = 1;

    };//if leaf
}

void solveIt(){

    myBest = 1;
    for(int i = 1; i<=n; i++){
        if (AdjOf[i].size() >1) continue;
        for(int j = 0; j<=n; j++){
            C[j] = 0;
            Visited[j] = false;
            Opt[i] = 1;
        };
        //cout <<"From "<<i<<" : "<<endl;
        Visited[i] = true;
        bestFrom(i,0);
    }

    cout <<myBest<<endl;

};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> AdjOf[NMAX];

int C[NMAX];
bool Visited[NMAX];

void readInput(){
    cin >>n;
    for(int i = 1; i<=n;i++){
        cin >>Pop[i];
    };
    for(int i = 0; i<n-1; i++){
        int x, y;
        cin >>x>>y;
        AdjOf[x].push_back(y);
        AdjOf[y].push_back(x);
    };
};

int myBest = 1;
int Opt[NMAX];

void bestFrom(int u, int len){

    C[len] = Pop[u];

    Opt[len] = 1;
    for(int i = 0; i<=len; i++){
        if (C[i]<C[len]){
            Opt[len] = Opt[len] < Opt[i] +1 ? Opt[i] +1 : Opt[len];
        };
        //cout <<C[i]<<" ";
    };

    myBest = myBest < Opt[len] ? Opt[len]: myBest;
    //cout <<" best = "<<myBest<<endl;

    bool leaf = true;
    for(int iv = 0; iv < AdjOf[u].size(); iv++){
        int v = AdjOf[u][iv];
        if (!Visited[v]){
            leaf = false;
            Visited[v] = true;
            bestFrom(v, len+1);
            Visited[v] = false;
        };// if
    };//for

    if (leaf){
        if (len >= myBest){
            //int lis;
            // = LIS2(len);
            Opt[len] = 1;
            for(int i = 0; i<=len; i++){
                if (C[i]<C[len]){
                    Opt[len] = Opt[len] < Opt[i] +1 ? Opt[i] +1 : Opt[len];
                };
                //cout <<C[i]<<" ";
            };

            myBest = myBest < Opt[len] ? Opt[len]: myBest;
            //cout <<" best = "<<myBest<<endl;

        };// if len

        //Opt[len] = 1;

    };//if leaf
}

void solveIt(){

    myBest = 1;
    for(int i = 1; i<=n; i++){
        if (AdjOf[i].size() >1) continue;
        for(int j = 0; j<=n; j++){
            C[j] = 0;
            Visited[j] = false;
            Opt[i] = 1;
        };
        //cout <<"From "<<i<<" : "<<endl;
        Visited[i] = true;
        bestFrom(i,0);
    }

    cout <<myBest<<endl;

};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <sstream>
#include <string.h>
#include <math.h>
#include <stdlib.h>

using namespace std;

const int NMAX = 6600;

int n;
int Pop[NMAX];
vector<int> NextOf[NMAX];

void readInput(){
    cin >>n;
    for(int i = 1; i<=n; i++)
        cin>>Pop[i];

    for(int i = 0; i<n-1; i++){
        int u, v;
        cin >>u>>v;
        if (Pop[v] > Pop[u]){
            NextOf[u].push_back(v);
        };
        if (Pop[u] > Pop[v]){
            NextOf[v].push_back(u);
        }
    };
};

int BestFrom(int x){
    int Q[NMAX];
    memset(Q, 0, sizeof(Q));

    int dq = 0;
    int cq = 0;
    Q[0] = x;

    bool Visit[NMAX];
    memset(Visit, 0, sizeof(Visit)); // false;

    int Dist[NMAX];
    memset(Dist,0,sizeof(Dist));

    Visit[x] = true;
    Dist[x] = 1;
    int mD = 1;

    while(dq <= cq){
        int u = Q[dq];
        dq ++;
        for(int iv = 0; iv <NextOf[u].size(); iv++){
            int v = NextOf[u][iv];
            if (Visit[v]) continue;
            Dist[v] = Dist[u] +1;
            mD = max(mD, Dist[v]);
            cq ++;
            Q[cq] = v;
        };
    };
    return mD;
};

void solveIt(){
    int best = 1;

    for(int i = 1; i<=n; i++){
        int c = BestFrom(i);
        best = max(best, c);
    }

   cout <<best;
};

int main()
{
    readInput();
    solveIt();
    return 0;
}


#include <bits/stdc++.h>

using namespace std ;

const int MaxN = 6000 + 20 ;
int n , w[MaxN] ;
vector <int> adj[MaxN] ;
int mark[MaxN] ;
int cnt[MaxN] , b[MaxN][MaxN] ;
int par[MaxN] , maxcnt[MaxN] ;

void readInput ()
{
    cin >> n ;
    for (int i = 0; i < n ; i ++)
        cin >> w[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) ;

    }
    return ;
}

void DFS(int v)
{
    mark[v] = 1 ;

    int ind = lower_bound(b[par[v]] , b[par[v]] + cnt[par[v]] , w[v]) - b[par[v]] ;
    cnt[v] = max(cnt[par[v]] , ind + 1) ;
    for (int i = 0; i < cnt[par[v]]; i ++)
    {
        b[v][i] = b[par[v]][i] ;
    }
    b[v][ind] = w[v] ;

    for (int i = 0; i < adj[v].size(); i ++)
    {
        if (mark[adj[v][i]] != 1)
        {
            par[adj[v][i]] = v ;
            DFS(adj[v][i]) ;
        }
    }
    return ;
}

int main()
{
    readInput() ;
    for (int j = 0; j < n; j ++)
    {
        for (int i = 0; i < n; i ++)
        {
            mark[i] = 0 ;
            cnt[i] = 0 ;
        }

        par[j] = 0 ;
        DFS(j) ;
        for (int i = 0; i < n; i ++)
        {
            maxcnt[j] = max(maxcnt[j] , cnt[i]) ;
        }

    }

    int maxxcnt = 0 ;
    for (int i = 0; i < n; i ++)
    {
        maxxcnt = max(maxxcnt , maxcnt[i]) ;
    }

    cout << maxxcnt ;
    return 0 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int MaxN = 6000 + 20 ;
int n , w[MaxN] ;
vector <int> adj[MaxN] ;
int mark[MaxN] ;
int cnt[MaxN] , b[MaxN][MaxN] ;
int par[MaxN] , maxcnt[MaxN] ;
int r ;

void readInput ()
{
    cin >> n ;
    for (int i = 0; i < n ; i ++)
        cin >> w[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) ;

    }
    return ;
}

void DFS(int v)
{
    mark[v] = 1 ;

    int ind = lower_bound(b[par[v]] , b[par[v]] + cnt[par[v]] , w[v]) - b[par[v]] ;
    cnt[v] = max(cnt[par[v]] , ind + 1) ;
    for (int i = 0; i < cnt[par[v]]; i ++)
    {
        b[v][i] = b[par[v]][i] ;
    }
    b[v][ind] = w[v] ;
    maxcnt[r] = max(maxcnt[r] , cnt[v]) ;

    for (int i = 0; i < adj[v].size(); i ++)
    {
        if (mark[adj[v][i]] != 1)
        {
            par[adj[v][i]] = v ;
            DFS(adj[v][i]) ;
        }
    }
    return ;
}

int main()
{
    readInput() ;
    for (int j = 0; j < n; j ++)
    {
        fill(mark , mark + n , 0) ;
        fill(cnt , cnt + n , 0) ;
        par[j] = 0 ;
        r = j ;
        DFS(j) ;
    }

    int maxxcnt = 0 ;
    for (int i = 0; i < n; i ++)
    {
        maxxcnt = max(maxxcnt , maxcnt[i]) ;
    }

    cout << maxxcnt ;
    return 0 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int MaxN = 6020 ;

int n ;
int val[MaxN] ;
vector <int> adj[MaxN] ;
bool mark[MaxN] ;
int h[MaxN] , H[MaxN];

void readInput ()
{
    cin >> n ;
    for (int i = 0; i < n; i++)
    {
        cin >> val[i] ;
    }

    for (int i = 0; i < n - 1; i++)
    {
        int a , b ;
        cin >> a >> b ;
        a -- ;  b -- ;
        adj[a].push_back (b) ;
        adj[b].push_back (a) ;
    }

    return ;
}

void DFS(int r)
{
    mark[r] = 1 ;
    for (int i = 0; i < adj[r].size(); i++)
    {
        if (mark[adj[r][i]] != 1 && val[r] < val[adj[r][i]])
        {
            h[adj[r][i]] = h[r] + 1 ;
            DFS(adj[r][i]) ;
        }
    }

    return ;
}

int main()
{
    readInput() ;

    for (int i = 0; i < n; i++)
    {
        h[i] = 0 ;
        DFS(i) ;
        int maxH = 0 ;
        for (int j = 0; j < n; j ++)
        {
            maxH = max(maxH , h[j]) ;
        }
        H[i] = maxH ;
        for (int i = 0; i < n; i++)
        {
            h[i] = 0 ;
            mark[i] = 0 ;
        }
    }

    int maxH = 0 ;
    for (int i = 0; i < n; i++)
    {
        maxH = max(maxH , H[i]) ;
    }
    cout << ++ maxH ;

//    cout << endl ;

//    for (int i = 0; i < n; i++)
//        cout << i << ':' << H[i] << endl ;

    return 0 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int MaxN = 6000 + 20 ;
int n , w[MaxN] ;
vector <int> adj[MaxN] ;
int mark[MaxN] ;
int cnt , b[MaxN] ;
int maxcnt ;

void readInput ()
{
    scanf("%d" , &n) ;
    for (int i = 0; i < n ; i ++)
         scanf("%d" , &w[i]);
    for (int i = 0; i < n - 1; i ++)
    {
        int u , v ;
        scanf("%d %d" , &u , &v) ;
        u -- ;      v -- ;
        adj[u].push_back(v) ;
        adj[v].push_back(u) ;

    }
    return ;
}

void DFS(int v)
{
    mark[v] = 1 ;
    int ind = lower_bound (b , b + cnt , w[v]) - b ;

    if(ind >= cnt)
        cnt ++ ;

    b[ind] = w[v] ;
    int t = b[ind] ;

    for (int i = 0; i < adj[v].size(); i ++)
    {
        if (!mark[adj[v][i]])
        {
            DFS(adj[v][i]) ;
        }
    }
    maxcnt = max(maxcnt , cnt) ;
    b[ind] = t ;
    if (ind + 1 == cnt)
        cnt -- ;

    return ;
}

int main()
{
    readInput() ;
    for (int j = 0; j < n; j ++)
    {
        fill(mark , mark + n , 0) ;
        fill(b , b + n , 0) ;
        cnt = 0 ;
        DFS(j) ;
    }
    printf("%d" , maxcnt);
    return 0 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int MaxN = 6000 + 20 ;
int n , w[MaxN] ;
vector <int> adj[MaxN] ;
int mark[MaxN] ;
int cnt[MaxN] , b[MaxN][MaxN] ;
int par[MaxN] , maxcnt ;

void readInput ()
{
    scanf("%d" , &n) ;
    for (int i = 0; i < n ; i ++)
         scanf("%d" , &w[i]);
    for (int i = 0; i < n - 1; i ++)
    {
        int u , v ;
        scanf("%d" , &u) ;
        scanf("%d" , &v) ;
        u -- ;      v -- ;
        adj[u].push_back(v) ;
        adj[v].push_back(u) ;

    }
    return ;
}

void DFS(int v)
{
    mark[v] = 1 ;

    int ind = lower_bound(b[par[v]] , b[par[v]] + cnt[par[v]] , w[v]) - b[par[v]] ;
    cnt[v] = max(cnt[par[v]] , ind + 1) ;
    for (int i = 0; i < cnt[par[v]]; i ++)
    {
        b[v][i] = b[par[v]][i] ;
    }
    b[v][ind] = w[v] ;
    maxcnt = max(maxcnt , cnt[v]) ;

    for (int i = 0; i < adj[v].size(); i ++)
    {
        if (mark[adj[v][i]] != 1)
        {
            par[adj[v][i]] = v ;
            DFS(adj[v][i]) ;
        }
    }
    return ;
}

int main()
{
    readInput() ;
    for (int j = 0; j < n; j ++)
    {
        fill(mark , mark + n , 0) ;
        fill(cnt , cnt + n , 0) ;
        par[j] = 0 ;
        DFS(j) ;
    }
    printf("%d" , maxcnt);
    return 0 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int MaxN = 6000 + 20 ;
int n , w[MaxN] ;
vector <int> adj[MaxN] ;
int mark[MaxN] ;
int cnt , b[MaxN] ;
int maxcnt ;

void readInput ()
{
    scanf("%d" , &n) ;
    for (int i = 0; i < n ; i ++)
         scanf("%d" , &w[i]);
    for (int i = 0; i < n - 1; i ++)
    {
        int u , v ;
        scanf("%d %d" , &u , &v) ;
        u -- ;      v -- ;
        adj[u].push_back(v) ;
        adj[v].push_back(u) ;

    }
    return ;
}

void DFS(int v)
{
    mark[v] = 1 ;
    int ind = lower_bound (b , b + cnt , w[v]) - b ;

    if(ind >= cnt)
        cnt ++ ;

    int t = b[ind] ;
    b[ind] = w[v] ;

    for (int i = 0; i < adj[v].size(); i ++)
    {
        if (!mark[adj[v][i]])
        {
            DFS(adj[v][i]) ;
        }
    }
    maxcnt = max(maxcnt , cnt) ;
    b[ind] = t ;
    if (ind + 1 == cnt && b[ind] == 0)
        cnt -- ;

    return ;
}

int main()
{
    readInput() ;
    for (int j = 0; j < n; j ++)
    {
        fill(mark , mark + n , 0) ;
        fill(b , b + n , 0) ;
        cnt = 0 ;
        DFS(j) ;
    }
    printf("%d" , maxcnt);
    return 0 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int MaxN = 6000 + 20 ;
int n , w[MaxN] ;
vector <int> adj[MaxN] ;
int mark[MaxN] ;
int cnt , b[MaxN] ;
int maxcnt ;

void readInput ()
{
    scanf("%d" , &n) ;
    for (int i = 0; i < n ; i ++)
         scanf("%d" , &w[i]);
    for (int i = 0; i < n - 1; i ++)
    {
        int u , v ;
        scanf("%d %d" , &u , &v) ;
        u -- ;      v -- ;
        adj[u].push_back(v) ;
        adj[v].push_back(u) ;

    }
    return ;
}

void DFS(int v)
{
    mark[v] = 1 ;
    int ind = lower_bound (b , b + cnt , w[v]) - b ;

    if(ind >= cnt)
        cnt ++ ;

    b[ind] = w[v] ;
    int t = b[ind] ;

    for (int i = 0; i < adj[v].size(); i ++)
    {
        if (!mark[adj[v][i]])
        {
            DFS(adj[v][i]) ;
        }
    }
    maxcnt = max(maxcnt , cnt) ;
    b[ind] = t ;

    return ;
}

int main()
{
    readInput() ;
    for (int j = 0; j < n; j ++)
    {
        fill(mark , mark + n , 0) ;
        fill(b , b + n , 0) ;
        cnt = 0 ;
        DFS(j) ;
    }
    maxcnt -- ;
    printf("%d" , maxcnt);
    return 0 ;
}


#include <iostream>
#include <algorithm>
#include <vector>
#define repeat(ii,a,b) for(int ii=(a);ii<=(b);ii++)
#define maxN 6003
using namespace std;
vector<int> a[maxN];
int n,ans=0,r[maxN],mark[maxN],dp[maxN];
void dfs(int x){
    int t,k;
    mark[x]=1;
    k=lower_bound(dp,dp+n,r[x])-dp;
    if (k>ans) ans=k;
    t=dp[k];
    dp[k]=r[x];
    for (vector<int>::iterator it=a[x].begin();it<a[x].end();it++){
        int y=*it;
        if (mark[y]==0){
            dfs(y);
        }
    }
    dp[k]=t;
}
int main(){
    cin>>n;
    repeat(i,1,n) cin>>r[i];
    repeat(i,1,n-1){
         int x,y;
         cin>>x>>y;
         a[x].push_back(y);
         a[y].push_back(x);
    }
    repeat(j,0,n-1){dp[j]=1<<30;}
    repeat(i,1,n){
         repeat(j,1,n) mark[j]=0;
         dfs(i);
    }
    cout<<ans+1;
    //cin.get();
}


#include <iostream>
#include <vector>
#define repeat(ii,a,b) for(int ii=(a);ii<=(b);ii++)
#define maxN 6003
using namespace std;
vector<int> a[maxN];
vector<int>::iterator it;
int n,ans=0,r[maxN],mark[maxN],dp[maxN];
void dfs(int x){
    mark[x]=1;
    for (it=a[x].begin();it<a[x].end();it++){
        int y=*it;
        if ((!mark[x])&&(r[y]>r[x])){
            dp[y]=dp[x]+1;
            dfs(y);
        }
    }
}
int main(){
    cin>>n;
    repeat(i,1,n) cin>>r[i];
    repeat(i,1,n-1){
         int x,y;
         cin>>x>>y;
         a[x].push_back(y);
         a[y].push_back(x);
    }
    repeat(i,1,n){
         repeat(j,1,n){mark[j]=0;dp[j]=0;}
         dfs(i);
         repeat(j,1,n) ans=max(ans,dp[j]);
    }
    cout<<ans;
    cin.get();
}


#include <iostream>
#include <algorithm>
#include <vector>
#define repeat(ii,a,b) for(int ii=(a);ii<=(b);ii++)
#define maxN 6003
using namespace std;
vector<int> a[maxN];
int n,ans,r[maxN],mark[maxN],dp[maxN];
void dfs(int x){
    mark[x]=1;
    for (vector<int>::iterator it=a[x].begin();it<a[x].end();it++){
        int y=*it;
        if ((mark[y]==0)&&(r[y]>r[x])){
            dp[y]=dp[x]+1;
            dfs(y);
        }
    }
}
int main(){
    cin>>n;
    repeat(i,1,n) cin>>r[i];

    repeat(i,1,n-1){
         int x,y;
         cin>>x>>y;
         a[x].push_back(y);
         a[y].push_back(x);
    }

    repeat(i,1,n){
         repeat(j,1,n){mark[j]=0;dp[j]=1;}
         dfs(i);
         repeat(j,1,n) ans=max(ans,dp[j]);
    }
    cout<<ans;

}


#include <iostream>
#include <algorithm>
#include <vector>
#define repeat(ii,a,b) for(int ii=(a);ii<=(b);ii++)
#define maxN 6003
using namespace std;
vector<int> a[maxN];
int n,ans,r[maxN],mark[maxN],dp[maxN];
void dfs(int x){
    mark[x]=1;
    for (vector<int>::iterator it=a[x].begin();it<a[x].end();it++){
        int y=*it;
        if ((mark[y]==0)&&(r[y]>r[x])){
            dp[y]=dp[x]+1;
            dfs(y);
        }
    }
}
int main(){
    cin>>n;
    repeat(i,1,n) cin>>r[i];

    repeat(i,1,n-1){
         int x,y;
         cin>>x>>y;
         a[x].push_back(y);
         a[y].push_back(x);
    }

    repeat(i,1,n){
         repeat(j,1,n){mark[j]=0;dp[j]=1;}
         dfs(i);
         repeat(j,1,n) ans=max(ans,dp[j]);
    }
    cout<<ans;
}


#ifndef ONLINE_JUDGE
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#else
#include <bits/stdc++.h>
#endif
using namespace std;

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define FORd(i, a, b) for(int i = a - 1; i >= b; i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<pi> vii;
typedef vector<pl> vll;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

const int maxn = 6010;
int n;
int r[maxn];
vi adj[maxn];
int p[maxn];
int cnt[maxn];
int ans;
set<int> st;

void dfs(int u) {
	FOR(i, 0, sz(adj[u])) {
		int v = adj[u][i];
		if (p[u] != v) {
			set<int>::iterator it = st.lower_bound(r[v]);
			int tmp = -1;
			if (it != st.end()) {tmp = *it; st.erase(tmp);}
			st.insert(r[v]);
			ans = max(ans, sz(st));
			p[v] = u; dfs(v);
			st.erase(r[v]);
			if (tmp >= 0) st.insert(tmp);
		}
	}
}

void solve() {
	ms(cnt, 0);
	scanf("%d", &n);
	FOR(i, 0, n) scanf("%d", r + i);
	FOR(i, 0, n - 1) {int a, b; scanf("%d%d", &a, &b); a--; b--; adj[a].push_back(b); adj[b].push_back(a); cnt[a]++; cnt[b]++;}
	ans = 0;
	FOR(i, 0, n) if (cnt[i] == 1) {p[i] = -1; st.clear(); st.insert(r[i]); dfs(i);}
	printf("%d", ans);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
	solve();
#ifndef ONLINE_JUDGE
	printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}