#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define re return
#define ll long long
#define ull unsigned ll
#define ld long double
#define f first
#define s second
#define pi acos(-1)
#define oo (ll)1e9+8
#define OO 1e18
#define EPS 1e-9
#define Endl '\n'
#define FX(n) fixed<<setprecision(n)
#define mm(o,k) memset(o,k,sizeof o)
#define IO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
const int maxn=2e5+5;
int n,k;
int a[maxn];
int out[maxn];
bool vis[maxn];
int main()
{
    scanf("%d%d",&n,&k);
    priority_queue<pair<int,int> >pq;
    for(int i=0;i<n;++i){
        scanf("%d",a+i);
        pq.push({a[i],i});
    }
    bool fen=1;
    while(pq.size()){
        int val=pq.top().f;
        int idx=pq.top().s;
        pq.pop();
        if(vis[idx])continue;
        fen=!fen;
        vis[idx]=true;
        out[idx]=fen+1;
        for(int i=idx+1,j=0;i<n&&j<k;++i,++j){
            if(vis[i]){--j;continue;}
            vis[i]=true;
            out[i]=fen+1;
        }
        for(int i=idx-1,j=0;i>=0&&j<k;--i,++j){
            if(vis[i]){--j;continue;}
            vis[i]=true;
            out[i]=fen+1;
        }
    }
    for(int i=0;i<n;++i)printf("%d",out[i]);
    re 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define re return
#define ll long long
#define ull unsigned ll
#define ld long double
#define f first
#define s second
#define pi acos(-1)
#define oo (ll)1e9+8
#define OO 1e18
#define EPS 1e-9
#define Endl '\n'
#define FX(n) fixed<<setprecision(n)
#define mm(o,k) memset(o,k,sizeof o)
#define IO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
const int maxn=2e5+5;
int n,k;
int a[maxn];
int out[maxn];
bool vis[maxn];
int mkan[maxn];
int main()
{
    scanf("%d%d",&n,&k);
    priority_queue<pair<int,int> >pq;
    for(int i=0;i<n;++i){
        scanf("%d",a+i);
        pq.push({a[i],i});
    }
    bool fen=1;
    while(pq.size()){
        int val=pq.top().f;
        int idx=pq.top().s;
        pq.pop();
        if(vis[idx])continue;
        fen=!fen;
        vis[idx]=true;
        out[idx]=fen+1;
        for(int i=idx+1,j=0;i<n&&j<k;++i,++j){
            if(vis[i])i=mkan[i];
            vis[i]=true;
            out[i]=fen+1;
        }
        for(int i=idx-1,j=0;i>=0&&j<k;--i,++j){
            if(vis[i])i=mkan[i];
            vis[i]=true;
            out[i]=fen+1;
        }
        mkan[max(0,idx-k)]=min(n-1,idx+k);
        mkan[min(n-1,idx+k)]=max(0,idx-k);
    }
    for(int i=0;i<n;++i)printf("%d",out[i]);
    re 0;
}


#include<stdio.h>
#include<vector>
#include<algorithm>
#include<cmath>
#include<set>
#include<utility>

using namespace std;

vector<int>p(200000);

int find(int el, int l, int r){

    if(l==r)
        return l;
    int mid=(l+r)/2;
    if(p[mid]>=el)
        return find(el,l,mid);
    else
        return find(el,mid+1,r);
}

int main(){
    int n, x, k, kl=1;
    scanf("%d%d",&n,&k);
    vector<int>en(n, 0);
    vector<pair<int,int>>v;
    int lon = n;
    for(int i = 0; i < n; i++){
        scanf("%d",&x);
        p[i]=i;
        v.push_back(make_pair(x, p[i]));
    }
    sort(v.begin(),v.end());

    for(int i = n - 1; i >= 0; i--){
        if(en[v[i].second] == 0){
            int j = find(v[i].second, 0 , lon - 1);
            int st = max(0 , j - k );
            int l = min(lon-1, j + k) - max(0 , j - k) + 1;
            lon = n - l;
            while(l>0){
                en[p[st]]=kl;
                p.erase(p.begin()+st,p.begin()+st+1);
                l--;
            }
         if(kl==1)kl=2;
        else kl=1;
        }
    }
for(int i=0;i<n;i++)printf("%d",en[i]);
}


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

using namespace std;

vector<int>p;

int find(int el, int l, int r){

    if(l==r)
        return l;
    int mid=(l+r)/2;
    if(p[mid]>=el)
        return find(el,l,mid);
    else
        return find(el,mid+1,r);
}

int main(){
    int n, x, k, kl=1;
    cin >> n >> k;
    vector<int>en(n, 0);
    vector<pair<int,int>>v;

    for(int i = 0; i < n; i++){
        cin >> x;
        p.push_back(i);
        v.push_back(make_pair(x, p[i]));
    }
    sort(v.begin(),v.end());

    for(int i = n - 1; i >= 0; i--){
        if(en[v[i].second] == 0){
            int lon = p.size();
            int j = find(v[i].second, 0 , lon - 1);
            int st = max(0 , j - k );
            int l = min(lon, j + k) - max(0 , j - k);
            while(l>0){
                en[p[st]]=kl;
                p.erase(p.begin()+st,p.begin()+st+1);
                l--;
            }
        }
        if(kl==1)kl=2;
        else kl=1;
    }
for(int i=0;i<n;i++)cout<<en[i];
}


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

using namespace std;

vector<int>p;

int find(int el, int l, int r){

    if(l==r)
        return l;
    int mid=(l+r)/2;
    if(p[mid]>=el)
        return find(el,l,mid);
    else
        return find(el,mid+1,r);
}

int main(){
    int n, x, k, kl=1;
    cin >> n >> k;
    vector<int>en(n, 0);
    vector<pair<int,int>>v;

    for(int i = 0; i < n; i++){
        cin >> x;
        p.push_back(i);
        v.push_back(make_pair(x, p[i]));
    }
    sort(v.begin(),v.end());

    for(int i = n - 1; i >= 0; i--){
        if(en[v[i].second] == 0){
            int lon = p.size();
            int j = find(v[i].second, 0 , lon - 1);
            int st = max(0 , j - k );
            int l = min(lon-1, j + k) - max(0 , j - k) + 1;
            while(l>0){
                en[p[st]]=kl;
                p.erase(p.begin()+st,p.begin()+st+1);
                l--;
            }
         if(kl==1)kl=2;
        else kl=1;
        }
    }
for(int i=0;i<n;i++)cout<<en[i];
}


#include<stdio.h>
#include<vector>
#include<algorithm>
#include<cmath>
#include<set>
#include<utility>

using namespace std;

vector<int>p(200000);

int find(int el, int l, int r){

    if(l==r)
        return l;
    int mid=(l+r)/2;
    if(p[mid]>=el)
        return find(el,l,mid);
    else
        return find(el,mid+1,r);
}

int main(){
    int n, x, k, kl=1;
    scanf("%d%d",&n,&k);
    vector<int>en(n, 0);
    vector<pair<int,int>>v;
    int lon = n;
    for(int i = 0; i < n; i++){
        scanf("%d",&x);
        p[i]=i;
        v.push_back(make_pair(x, p[i]));
    }
    sort(v.begin(),v.end());

    for(int i = n - 1; i >= 0; i--){
        if(en[v[i].second] == 0){
            int j = find(v[i].second, 0 , lon - 1);
            int st = max(0 , j - k );
            int l = min(lon-1, j + k) - max(0 , j - k) + 1;
            lon -= l;
            while(l>0){
                en[p[st]]=kl;
                p.erase(p.begin()+st,p.begin()+st+1);
                l--;
            }
         if(kl==1)kl=2;
        else kl=1;
        }
    }
for(int i=0;i<n;i++)printf("%d",en[i]);
}


#include<stdio.h>
#include<vector>
#include<algorithm>
#include<cmath>
#include<set>
#include<utility>

using namespace std;

vector<int>p(200000);

int find(int el, int l, int r){

    if(l==r)
        return l;
    int mid=(l+r)/2;
    if(p[mid]>=el)
        return find(el,l,mid);
    else
        return find(el,mid+1,r);
}

int main(){
    int n, x, k, kl=1;
    scanf("%d%d",&n,&k);
    vector<int>en(n, 0);
    vector<pair<int,int>>v;
    int lon = n;
    for(int i = 0; i < n; i++){
        scanf("%d",&x);
        p[i]=i;
        v.push_back(make_pair(x, p[i]));
    }
    sort(v.begin(),v.end());

    for(int i = n - 1; i >= 0; i--){
        if(en[v[i].second] == 0){
            int j = find(v[i].second, 0 , lon - 1);
            int st = max(0 , j - k );
            int l = min(lon-1, j + k) - max(0 , j - k) + 1;
            lon -= l;
            for(int o = st;o<st+l;o++)en[p[o]]=kl;
        p.erase(p.begin()+st,p.begin()+l+st);
         if(kl==1)kl=2;
        else kl=1;
        }
    }
for(int i=0;i<n;i++)printf("%d",en[i]);
}


#include<stdio.h>
#include<vector>
#include<algorithm>
#include<cmath>
#include<set>
#include<utility>

using namespace std;

vector<int>p(200000);

int find(int el, int l, int r){

    if(l==r)
        return l;
    int mid=(l+r)/2;
    if(p[mid]>=el)
        return find(el,l,mid);
    else
        return find(el,mid+1,r);
}

int main(){
    int n, x, k, kl=1;
    scanf("%d%d",&n,&k);
    vector<int>en(n, 0);
    vector<pair<int,int>>v;
    int lon = n;
    for(int i = 0; i < n; i++){
        scanf("%d",&x);
        p[i]=i;
        v.push_back(make_pair(x, p[i]));
    }
    sort(v.begin(),v.end());

    for(int i = n - 1; i >= 0; i--){
        if(en[v[i].second] == 0){
            int j = find(v[i].second, 0 , lon - 1);
            int st = max(0 , j - k );
            int l = min(lon-1, j + k) - max(0 , j - k) + 1;
            lon -= l;
            for(int o = st;o<st+l;o++)en[p[o]]=kl;
        p.erase(p.begin()+st,p.begin()+l+1);
         if(kl==1)kl=2;
        else kl=1;
        }
    }
for(int i=0;i<n;i++)printf("%d",en[i]);
}


#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define IO std::ios::sync_with_stdio(0);
#include <bits/stdc++.h>
#define iter ::iterator
using namespace  std;
typedef long long ll;
typedef pair<ll,ll>P;
#define pb push_back
#define se second
#define fi first
#define rs o<<1|1
#define ls o<<1
const ll inf=0x7fffffff;
const int N=2e5+10;
int n,x,y;
int a[N],b[N],c[N];
int maxv[N*4],lazy[N*4];
void push(int o){
    maxv[o]=max(maxv[ls],maxv[rs]);
}
void down(int o){
    if(lazy[o]){
        maxv[o]=0;
        lazy[ls]=lazy[rs]=1;
        maxv[ls]=maxv[rs]=0;
    }
}
void build(int o,int l,int r){
    if(l==r){
        cin>>maxv[o];
        return;
    }
    int m=(l+r)/2;
    build(ls,l,m);
    build(rs,m+1,r);
    push(o);
}
void up(int o,int l,int r,int ql,int qr){
    if(l>=ql&&r<=qr){
        maxv[o]=0;
        lazy[o]=1;
        return;
    }
    down(o);
    int m=(l+r)/2;
    if(ql<=m)up(ls,l,m,ql,qr);
    if(qr>m)up(rs,m+1,r,ql,qr);
    push(o);
}
int qu(int o,int l,int r){
    if(l==r){
        return l;
    }
    down(o);
    int m=(l+r)/2;
    if(maxv[ls]==maxv[1]){
        return qu(ls,l,m);
    }
    else return qu(rs,m+1,r);
}
int k;
int main(){
    IO;
    cin>>n>>k;
    build(1,1,n);
    int g=0;
    for(int i=1;i<=n;i++){
        b[i]=c[i]=i;
    }
    while(1){
        if(maxv[1]==0)break;
        g++;
        int h=g%2?1:2;
        int p=qu(1,1,n);
        //cout<<p<<endl;
        int ql=0,qr=n+10;
        for(int i=p,cnt=0;i>=1&&cnt<=k;i--){
            ql=i;
            //if(p==7)printf("i=%d: %d\n",i,a[i]);
            i=c[i];
            if(a[i]!=0)continue;
            a[i]=h;
            cnt++;

        }
        for(int i=p+1,cnt=1;i<=n&&cnt<=k;i++){
            qr=i;
            i=b[i];
            if(a[i]!=0)continue;
            a[i]=h;
            cnt++;

        }
        for(int i=ql;i<=min(ql+10,qr);i++){
            b[i]=qr;
        }
        for(int i=qr;i>=max(qr-10,ql);i--){
            c[i]=ql;
        }
        /*for(int i=1;i<=7;i++)cout<<a[i];
        cout<<endl;*/
        //cout<<ql<<" "<<qr<<endl;
        up(1,1,n,max(1,ql),min(qr,n));
        //cout<<maxv[1]<<endl;
    }
    for(int i=1;i<=n;i++)cout<<a[i];
}


#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define IO std::ios::sync_with_stdio(0);
#include <bits/stdc++.h>
#define iter ::iterator
using namespace  std;
typedef long long ll;
typedef pair<ll,ll>P;
#define pb push_back
#define se second
#define fi first
#define rs o<<1|1
#define ls o<<1
const ll inf=0x7fffffff;
const int N=2e5+10;
int n,x,y;
int a[N],b[N];
int maxv[N*4],lazy[N*4];
void push(int o){
    maxv[o]=max(maxv[ls],maxv[rs]);
}
void down(int o){
    if(lazy[o]){
        maxv[o]=0;
        lazy[ls]=lazy[rs]=1;
        maxv[ls]=maxv[rs]=0;
    }
}
void build(int o,int l,int r){
    if(l==r){
        cin>>maxv[o];
        return;
    }
    int m=(l+r)/2;
    build(ls,l,m);
    build(rs,m+1,r);
    push(o);
}
void up(int o,int l,int r,int ql,int qr){
    if(l>=ql&&r<=qr){
        maxv[o]=0;
        lazy[o]=1;
        return;
    }
    down(o);
    int m=(l+r)/2;
    if(ql<=m)up(ls,l,m,ql,qr);
    if(qr>m)up(rs,m+1,r,ql,qr);
    push(o);
}
int qu(int o,int l,int r){
    if(l==r){
        return l;
    }
    down(o);
    int m=(l+r)/2;
    if(maxv[ls]==maxv[1]){
        return qu(ls,l,m);
    }
    else return qu(rs,m+1,r);
}
int k;
int main(){
    //IO;
    cin>>n>>k;
    build(1,1,n);
    int g=0;
    while(1){
        if(maxv[1]==0)break;
        g++;
        int h=g%2?1:2;
        int p=qu(1,1,n);
        //cout<<p<<endl;
        int ql=0,qr=n+10;
        for(int i=p,cnt=0;i>=1&&cnt<=k;i--){
            ql=i;
            //if(p==7)printf("i=%d: %d\n",i,a[i]);
            if(a[i]!=0)continue;
            a[i]=h;
            cnt++;

        }
        for(int i=p+1,cnt=1;i<=n&&cnt<=k;i++){
            qr=i;
            if(a[i]!=0)continue;
            a[i]=h;
            cnt++;

        }
        /*for(int i=1;i<=7;i++)cout<<a[i];
        cout<<endl;*/
        //cout<<ql<<" "<<qr<<endl;
        up(1,1,n,max(1,ql),min(qr,n));
        //cout<<maxv[1]<<endl;
    }
    for(int i=1;i<=n;i++)cout<<a[i];
}


#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define IO std::ios::sync_with_stdio(0);
#include <bits/stdc++.h>
#define iter ::iterator
using namespace  std;
typedef long long ll;
typedef pair<ll,ll>P;
#define pb push_back
#define se second
#define fi first
#define rs o<<1|1
#define ls o<<1
const ll inf=0x7fffffff;
const int N=2e5+10;
int n,x,y;
int a[N],b[N];
int maxv[N*4],lazy[N*4];
void push(int o){
    maxv[o]=max(maxv[ls],maxv[rs]);
}
void down(int o){
    if(lazy[o]){
        maxv[o]=0;
        lazy[ls]=lazy[rs]=1;
        maxv[ls]=maxv[rs]=0;
    }
}
void build(int o,int l,int r){
    if(l==r){
        cin>>maxv[o];
        return;
    }
    int m=(l+r)/2;
    build(ls,l,m);
    build(rs,m+1,r);
    push(o);
}
void up(int o,int l,int r,int ql,int qr){
    if(l>=ql&&r<=qr){
        maxv[o]=0;
    }
    down(o);
    int m=(l+r)/2;
    if(ql<=m)up(ls,l,m,ql,qr);
    if(qr>m)up(rs,m+1,r,ql,qr);
    push(o);
}
int qu(int o,int l,int r){
    if(l==r){
        return l;
    }
    int m=(l+r)/2;
    if(maxv[ls]==maxv[1]){
        return qu(ls,l,m);
    }
    else return qu(rs,m+1,r);
}
int k;
int main(){
    IO;
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    build(1,1,n);
    int g=0;
    while(1){
        if(maxv[1]==0)break;
        g=g+1;
        int h=g%2?1:2;
        int p=qu(1,1,n);
        int ql=0,qr=0;
        for(int i=p,cnt=1;i>=1&&cnt<=k;i--){
            if(a[i]!=0)continue;
            a[i]=h;
            cnt++;
            ql=i;
        }
        for(int i=p,cnt=1;i<=n&&cnt<=k;i++){
            if(a[i]!=0)continue;
            a[i]=h;
            cnt++;
            qr=i;
        }
        up(1,1,n,ql,qr);
    }
    for(int i=1;i<=n;i++)cout<<a[i];
}


#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define IO std::ios::sync_with_stdio(0);
#include <bits/stdc++.h>
#define iter ::iterator
using namespace  std;
typedef long long ll;
typedef pair<ll,ll>P;
#define pb push_back
#define se second
#define fi first
#define rs o<<1|1
#define ls o<<1
const ll inf=0x7fffffff;
const int N=2e5+10;
int n,x,y;
int a[N],b[N];
int maxv[N*4],lazy[N*4];
void push(int o){
    maxv[o]=max(maxv[ls],maxv[rs]);
}
void down(int o){
    if(lazy[o]){
        maxv[o]=0;
        lazy[ls]=lazy[rs]=1;
        maxv[ls]=maxv[rs]=0;
    }
}
void build(int o,int l,int r){
    if(l==r){
        cin>>maxv[o];
        return;
    }
    int m=(l+r)/2;
    build(ls,l,m);
    build(rs,m+1,r);
    push(o);
}
void up(int o,int l,int r,int ql,int qr){
    if(l>=ql&&r<=qr){
        maxv[o]=0;
        lazy[o]=1;
        return;
    }
    down(o);
    int m=(l+r)/2;
    if(ql<=m)up(ls,l,m,ql,qr);
    if(qr>m)up(rs,m+1,r,ql,qr);
    push(o);
}
int qu(int o,int l,int r){
    if(l==r){
        return l;
    }
    down(o);
    int m=(l+r)/2;
    if(maxv[ls]==maxv[1]){
        return qu(ls,l,m);
    }
    else return qu(rs,m+1,r);
}
int k;
int main(){
    IO;
    cin>>n>>k;
    build(1,1,n);
    int g=0;
    while(1){
        if(maxv[1]==0)break;
        g++;
        int h=g%2?1:2;
        int p=qu(1,1,n);
        //cout<<p<<endl;
        int ql=0,qr=n+10;
        for(int i=p,cnt=0;i>=1&&cnt<=k;i--){
            ql=i;
            //if(p==7)printf("i=%d: %d\n",i,a[i]);
            if(a[i]!=0)continue;
            a[i]=h;
            cnt++;

        }
        for(int i=p+1,cnt=1;i<=n&&cnt<=k;i++){
            qr=i;
            if(a[i]!=0)continue;
            a[i]=h;
            cnt++;

        }
        /*for(int i=1;i<=7;i++)cout<<a[i];
        cout<<endl;*/
        //cout<<ql<<" "<<qr<<endl;
        up(1,1,n,max(1,ql),min(qr,n));
        //cout<<maxv[1]<<endl;
    }
    for(int i=1;i<=n;i++)cout<<a[i];
}


#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define IO std::ios::sync_with_stdio(0);
#include <bits/stdc++.h>
#define iter ::iterator
using namespace  std;
typedef long long ll;
typedef pair<ll,ll>P;
#define pb push_back
#define se second
#define fi first
#define rs o<<1|1
#define ls o<<1
const ll inf=0x7fffffff;
const int N=2e5+10;
int n,x,y;
int a[N],b[N],c[N];
int maxv[N*4],lazy[N*4];
void push(int o){
    maxv[o]=max(maxv[ls],maxv[rs]);
}
void down(int o){
    if(lazy[o]){
        maxv[o]=0;
        lazy[ls]=lazy[rs]=1;
        maxv[ls]=maxv[rs]=0;
    }
}
void build(int o,int l,int r){
    if(l==r){
        cin>>maxv[o];
        return;
    }
    int m=(l+r)/2;
    build(ls,l,m);
    build(rs,m+1,r);
    push(o);
}
void up(int o,int l,int r,int ql,int qr){
    if(l>=ql&&r<=qr){
        maxv[o]=0;
        lazy[o]=1;
        return;
    }
    down(o);
    int m=(l+r)/2;
    if(ql<=m)up(ls,l,m,ql,qr);
    if(qr>m)up(rs,m+1,r,ql,qr);
    push(o);
}
int qu(int o,int l,int r){
    if(l==r){
        return l;
    }
    down(o);
    int m=(l+r)/2;
    if(maxv[ls]==maxv[1]){
        return qu(ls,l,m);
    }
    else return qu(rs,m+1,r);
}
int k;
int main(){
    IO;
    cin>>n>>k;
    build(1,1,n);
    int g=0;
    for(int i=1;i<=n;i++){
        b[i]=c[i]=i;
    }
    while(1){
        if(maxv[1]==0)break;
        g++;
        int h=g%2?1:2;
        int p=qu(1,1,n);
        //cout<<p<<endl;
        int ql=0,qr=n+10;
        for(int i=p,cnt=0;i>=1&&cnt<=k;i--){
            ql=i;
            //if(p==7)printf("i=%d: %d\n",i,a[i]);
            i=c[i];
            if(a[i]!=0)continue;
            a[i]=h;
            cnt++;

        }
        for(int i=p+1,cnt=1;i<=n&&cnt<=k;i++){
            qr=i;
            i=b[i];
            if(a[i]!=0)continue;
            a[i]=h;
            cnt++;

        }
        for(int i=ql;i<=qr;i++){
            b[i]=qr;
        }
        for(int i=qr;i>=ql;i--){
            c[i]=ql;
        }
        /*for(int i=1;i<=7;i++)cout<<a[i];
        cout<<endl;*/
        //cout<<ql<<" "<<qr<<endl;
        up(1,1,n,max(1,ql),min(qr,n));
        //cout<<maxv[1]<<endl;
    }
    for(int i=1;i<=n;i++)cout<<a[i];
}


#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define IO std::ios::sync_with_stdio(0);
#include <bits/stdc++.h>
#define iter ::iterator
using namespace  std;
typedef long long ll;
typedef pair<ll,ll>P;
#define pb push_back
#define se second
#define fi first
#define rs o<<1|1
#define ls o<<1
const ll inf=0x7fffffff;
const int N=2e5+10;
int n,x,y;
int a[N],b[N];
int maxv[N*4],lazy[N*4];
void push(int o){
    maxv[o]=max(maxv[ls],maxv[rs]);
}
void down(int o){
    if(lazy[o]){
        maxv[o]=0;
        lazy[ls]=lazy[rs]=1;
        maxv[ls]=maxv[rs]=0;
    }
}
void build(int o,int l,int r){
    if(l==r){
        cin>>maxv[o];
        return;
    }
    int m=(l+r)/2;
    build(ls,l,m);
    build(rs,m+1,r);
    push(o);
}
void up(int o,int l,int r,int ql,int qr){
    if(l>=ql&&r<=qr){
        maxv[o]=0;
        lazy[o]=1;
        return;
    }
    down(o);
    int m=(l+r)/2;
    if(ql<=m)up(ls,l,m,ql,qr);
    if(qr>m)up(rs,m+1,r,ql,qr);
    push(o);
}
int qu(int o,int l,int r){
    if(l==r){
        return l;
    }
    down(o);
    int m=(l+r)/2;
    if(maxv[ls]==maxv[1]){
        return qu(ls,l,m);
    }
    else return qu(rs,m+1,r);
}
int k;
int main(){
    IO;
    cin>>n>>k;
    build(1,1,n);
    int g=0;
    while(1){
        if(maxv[1]==0)break;
        g++;
        int h=g%2?1:2;
        int p=qu(1,1,n);
        //cout<<p<<endl;
        int ql=0,qr=n+10;
        for(int i=p,cnt=0;i>=1&&cnt<=k;i--){
            ql=i;
            //if(p==7)printf("i=%d: %d\n",i,a[i]);
            if(a[i]!=0)continue;
            a[i]=h;
            cnt++;
            i=b[i];

        }
        for(int i=p+1,cnt=1;i<=n&&cnt<=k;i++){
            qr=i;
            if(a[i]!=0)continue;
            a[i]=h;
            cnt++;
            i=b[i];
        }
        for(int i=ql;i<=qr;i++){
            b[i]=qr;
        }
        /*for(int i=1;i<=7;i++)cout<<a[i];
        cout<<endl;*/
        //cout<<ql<<" "<<qr<<endl;
        up(1,1,n,max(1,ql),min(qr,n));
        //cout<<maxv[1]<<endl;
    }
    for(int i=1;i<=n;i++)cout<<a[i];
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define MAX 1e18
#define N 200005
int mod = 998244353;
ll n,k;
int a[N];

void solve()
{
  cin>>n>>k;
  priority_queue<pair<int,int>> q;
  for(int i =0;i<n;++i)
  {
    int x;
    cin>>x;
    q.push(mp(x,i));
  }
  int i = 1;
  while(q.size())
  {
    while(q.size()&&a[q.top().ss] != 0)
    {
      q.pop();
    }
    if(q.size()==0)
    break;
    pair<int,int> temp = q.top();q.pop();
    int p = k + 1  ;
    //cout<<temp.ss<<" ";
    for(int j = temp.ss;j>=0&&p;--p,--j)
    { if(a[j]>0)
      ++p;
      else
      a[j] = i;
    }
    p = k ;
    for(int j = temp.ss + 1;j<n&&p;--p,++j)
     if(a[j]>0)
      ++p;
      else
    a[j] = i;

    if(i==1)
    i=2;
    else
    i = 1;

  }

  for(int i = 0;i<n;++i)
  printf("%d",a[i]);

}

int main()
{

ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

int t=1;
//cin>>t;

while(t--)
{

  solve();
  //cout<<endl;

}
return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define MAX 1e18
#define N 200005
int mod = 998244353;
ll n,k;

void solve()
{
  cin>>n>>k;
  priority_queue<pair<int,int>> q;
  for(int i =0;i<n;++i)
  {
    int x;
    cin>>x;
    q.push(mp(x,i));
  }

  set<int> s;
  string ans(n,'0');
  for(int i =0;i<n;++i)
  s.insert(i);

  int r = 0;

  while(s.size())
  {
    while(s.count(q.top().ss)==0)
    {
      q.pop();
    }

    int pos = q.top().ss;
    q.pop();

    vector<int> add;

    auto it = s.find(pos);
  //cout<<1<<" ";
    for(int i = 0;i<=k;++i)
    {
      add.pb(*it);

      if(it == s.begin()) break;

      --it;
    }

    it = next(s.find(pos));

    for(int i = 0;i<k;++i)
    {
      if(it == s.end()) break;
      add.pb(*it);
      ++it;
    }

    for(auto it: add)
    {
      s.erase(it);
      ans[it] =  r + '1' ;
    }

    r ^=1;

  }

  cout<<ans;

}

int main()
{

ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

int t=1;
//cin>>t;

while(t--)
{

  solve();
  //cout<<endl;

}
return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define MAX 1e18
#define N 200005
int mod = 998244353;
ll n,k;
int a [N];

void solve()
{
  cin>>n>>k;
  priority_queue<pair<int,int>> q;
  for(int i =0;i<n;++i)
  {
    int x;
    cin>>x;
    q.push(mp(x,i));
  }
  int i = 1;
  while(q.size())
  {
    while(q.size()&&a[q.top().ss] != 0)
    {
      q.pop();
    }
    if(q.size()==0)
    break;
    pair<int,int> temp = q.top();q.pop();
    int p = k + 1  ;
    //cout<<temp.ss<<" ";
    for(int j = temp.ss;j>=0&&p&&a[j]==0;--p,--j)
    a[j] = i;
    p = k ;
    for(int j = temp.ss + 1;j<n&&p&&a[j]==0;--p,++j)
    a[j] = i;

    if(i==1)
    i=2;
    else
    i = 1;

  }

  for(int i = 0;i<n;++i)
  cout<<a[i];

}

int main()
{

ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

int t=1;
//cin>>t;

while(t--)
{

  solve();
  //cout<<endl;

}
return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define MAX 1e18
#define N 200005
int mod = 998244353;
ll n,k;
int a[N];

void solve()
{
  cin>>n>>k;
  priority_queue<pair<int,int>> q;
  for(int i =0;i<n;++i)
  {
    int x;
    cin>>x;
    q.push(mp(x,i));
  }
  int i = 1;
  while(q.size())
  {
    while(q.size()&&a[q.top().ss] != 0)
    {
      q.pop();
    }
    if(q.size()==0)
    break;
    pair<int,int> temp = q.top();q.pop();
    int p = k + 1  ;
    //cout<<temp.ss<<" ";
    for(int j = temp.ss;j>=0&&p;--p,--j)
    { if(a[j]>0)
      {++p;
        j -=2*k;
      }
      else
      a[j] = i;
    }
    p = k ;
    for(int j = temp.ss + 1;j<n&&p;--p,++j)
     if(a[j]>0)
      {
        ++p;
        j += 2*k;

      }
      else
    a[j] = i;

    if(i==1)
    i=2;
    else
    i = 1;

  }

  for(int i = 0;i<n;++i)
  printf("%d",a[i]);

}

int main()
{

ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

int t=1;
//cin>>t;

while(t--)
{

  solve();
  //cout<<endl;

}
return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define MAX 1e18
#define N 200005
int mod = 998244353;
ll n,k;
int a [N];

void solve()
{
  cin>>n>>k;
  priority_queue<pair<int,int>> q;
  for(int i =0;i<n;++i)
  {
    int x;
    cin>>x;
    q.push(mp(x,i));
  }
  int i = 1;
  while(q.size())
  {
    while(q.size()&&a[q.top().ss] != 0)
    {
      q.pop();
    }
    if(q.size()==0)
    break;
    pair<int,int> temp = q.top();q.pop();
    int p = k +1  ;
    //cout<<temp.ss<<" ";
    for(int j = temp.ss;j>=0&&p;--p,--j)
    a[j] = i;
    p = k + 1;
    for(int j = temp.ss;j<n&&p;--p,++j)
    a[j] = i;

    if(i==1)
    i=2;
    else
    i = 1;

  }

  for(int i = 0;i<n;++i)
  cout<<a[i];

}

int main()
{

ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

int t=1;
//cin>>t;

while(t--)
{

  solve();
  //cout<<endl;

}
return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define MAX 1e18
#define N 200005
int mod = 998244353;
ll n,k;
int a[N];

void solve()
{
  cin>>n>>k;
  priority_queue<pair<int,int>> q;
  for(int i =0;i<n;++i)
  {
    int x;
    cin>>x;
    q.push(mp(x,i));
  }

  set<int> s;
  string ans(n,'0');
  for(int i =0;i<n;++i)
  s.insert(i);

  int r = 0;

  while(q.size())
  {
    while(s.count(q.top().ss)==0)
    {
      q.pop();
    }
    if(q.size()==0)
    break;
    int pos = q.top().ss;
    q.pop();

    vector<int> add;

    auto it = s.find(pos);

    for(int i = 0;i<=k;++i)
    {
      add.pb(*it);

      if(it == s.begin()) break;

      --it;
    }

    it = next(s.find(pos));

    for(int i = 0;i<k;++i)
    {
      if(it == s.end()) break;
      add.pb(*it);
      ++it;
    }

    for(int i =0;i<add.size();++i)
    {
      ans[add[i]] =  r + '1' ;

      s.erase(add[i]);

    }

    r ^=1;

  }

  cout<<ans;

}

int main()
{

ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

int t=1;
//cin>>t;

while(t--)
{

  solve();
  //cout<<endl;

}
return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define MAX 1e18
#define N 200005
int mod = 998244353;
ll n,k;
int a [N];

void solve()
{
  cin>>n>>k;
  priority_queue<pair<int,int>> q;
  for(int i =0;i<n;++i)
  {
    int x;
    cin>>x;
    q.push(mp(x,i));
  }
  int i = 1;
  while(q.size())
  {
    while(q.size()&&a[q.top().ss] != 0)
    {
      q.pop();
    }
    if(q.size()==0)
    break;
    pair<int,int> temp = q.top();q.pop();
    int p = k + 1  ;
    //cout<<temp.ss<<" ";
    for(int j = temp.ss;j>=0&&p;--p,--j)
    { if(a[j]>0)
      ++p;
      else
      a[j] = i;
    }
    p = k ;
    for(int j = temp.ss + 1;j<n&&p;--p,++j)
     if(a[j]>0)
      ++p;
      else
    a[j] = i;

    if(i==1)
    i=2;
    else
    i = 1;

  }

  for(int i = 0;i<n;++i)
  cout<<a[i];

}

int main()
{

ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

int t=1;
//cin>>t;

while(t--)
{

  solve();
  //cout<<endl;

}
return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define MAX 1e18
#define N 200005
int mod = 998244353;
ll n,k;
int a[N];

void solve()
{
  cin>>n>>k;
  priority_queue<pair<int,int>> q;
  for(int i =0;i<n;++i)
  {
    int x;
    scanf("%d",&x);
    q.push(mp(x,i));
  }
  int i = 1;
  while(q.size())
  {
    while(q.size()&&a[q.top().ss] != 0)
    {
      q.pop();
    }
    if(q.size()==0)
    break;
    pair<int,int> temp = q.top();q.pop();
    int p = k + 1  ;
    //cout<<temp.ss<<" ";
    for(int j = temp.ss;j>=0&&p;--p,--j)
    { if(a[j]>0)
      {++p;
        j -=2*k;
      }
      else
      a[j] = i;
    }
    p = k ;
    for(int j = temp.ss + 1;j<n&&p;--p,++j)
     if(a[j]>0)
      {
        ++p;
        j += 2*k;

      }
      else
    a[j] = i;

    if(i==1)
    i=2;
    else
    i = 1;

  }

  for(int i = 0;i<n;++i)
  printf("%d",a[i]);

}

int main()
{

ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

int t=1;
//cin>>t;

while(t--)
{

  solve();
  //cout<<endl;

}
return 0;

}


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

long long A[100005];
long long n, k;
set<pair<long long, long long> > s;
long long ans[100005];
long long hsh[100005];
int main(){
	std::ios_base::sync_with_stdio(0);
	cin >> n >> k;
	for(long long int i=0; i<n; i++){
		cin >> A[i];
		hsh[A[i]] = i;
		s.insert({A[i], i});
		ans[i] = 0;
	}

	long long move = 1;
	while(!s.empty()){
//		for(auto i:s)cout<<i.first<<"-";
//		cout<<"\n\n";
		move^=1;
//		cout<<"Move: "<<move<<"\n";
		pair<long long, long long> top = *s.rbegin();
		ans[top.second] = move;
//		cout<<"Removing: "<<top.first<<"\n";
//		cout << top.first <<" " <<top.second<<"\n";
		long long removes = 0;
		for(long long cur=hsh[top.first]-1; cur>=0; cur--){
			pair<long long, long long> rmv = {A[cur], cur};
//			cout<<"Removing: "<<rmv.first<<"\n";
			if(s.find(rmv)!=s.end()) {
				s.erase(s.find(rmv));
				ans[cur] = move;
				removes++;
			}
			if(removes==k) break;
		}
		removes = 0;
		for(long long cur = hsh[top.first]+1; cur<n; cur++){
			pair<long long, long long> rmv = {A[cur], cur};
//			cout<<"Removing: "<<rmv.first<<"\n";
			if(s.find(rmv)!=s.end()) {
				removes++;
				s.erase(s.find(rmv));
				ans[cur] = move;
			}
			if(removes==k)break;
		}
		removes = 0;
		s.erase(top);
	}
	for(long long i =0; i<n; i++)cout<<ans[i]+1;

}


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

long long A[100005];
long long n, k;
set<pair<long long, long long> > s;
long long ans[100005];
long long hsh[100005];
int main(){
	std::ios_base::sync_with_stdio(0);
	cin >> n >> k;
	for(long long int i=0; i<n; i++){
		cin >> A[i];
		hsh[A[i]] = i;
		s.insert({A[i], i});
		ans[i] = 0;
	}

	long long move = 1;
	while(!s.empty()){
		move^=1;
		pair<long long, long long> top = *s.rbegin();
		ans[top.second] = move;

		s.erase(top);

		long long removes = 0;

		if(s.empty())break;

		for(long long cur=hsh[top.first]-1; cur>=0; cur--){
			pair<long long, long long> rmv = {A[cur], cur};
			if(s.find(rmv)!=s.end()) {
				s.erase(s.find(rmv));
				ans[cur] = move;
				removes++;
			}
			if(removes==k) break;
		}
		removes = 0;
		for(long long cur = hsh[top.first]+1; cur<n; cur++){
			pair<long long, long long> rmv = {A[cur], cur};
			if(s.find(rmv)!=s.end()) {
				removes++;
				s.erase(s.find(rmv));
				ans[cur] = move;
			}
			if(removes==k)break;
		}
		removes = 0;
//		s.erase(top);
	}
	for(long long i =0; i<n; i++)cout<<ans[i]+1;

}


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

long long A[200005];
long long n, k;
set<pair<long long, long long> > s;
long long ans[200005];
long long hsh[200005];
int main(){
	std::ios_base::sync_with_stdio(0);
	cin >> n >> k;
	for(long long int i=0; i<n; i++){
		cin >> A[i];
		hsh[A[i]] = i;
		s.insert({A[i], i});
		ans[i] = 0;
	}

	long long move = 1;
	while(!s.empty()){
		move^=1;
		pair<long long, long long> top = *s.rbegin();
		ans[top.second] = move;

		long long removes = 0;

		if(s.empty())break;

		for(long long cur=hsh[top.first]-1; cur>=0; cur--){
			if(removes==k) break;
			pair<long long, long long> rmv = {A[cur], cur};
			if(s.find(rmv)!=s.end()) {
				s.erase(s.find(rmv));
				ans[cur] = move;
				removes++;
			}
		}

		if(s.empty())break;

		removes = 0;

		for(long long cur = hsh[top.first]+1; cur<n; cur++){
			if(removes==k)break;
			pair<long long, long long> rmv = {A[cur], cur};
			if(s.find(rmv)!=s.end()) {
				removes++;
				s.erase(s.find(rmv));
				ans[cur] = move;
			}
		}
		removes = 0;
		s.erase(top);
	}
	for(long long i=0; i<n; i++)cout<<ans[i]+1;

}


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

long long A[100005];
long long n, k;
set<pair<long long, long long> > s;
long long ans[100005];
long long hsh[100005];
int main(){
	std::ios_base::sync_with_stdio(0);
	cin >> n >> k;
	for(long long int i=0; i<n; i++){
		cin >> A[i];
		hsh[A[i]] = i;
		s.insert({A[i], i});
		ans[i] = 0;
	}

	long long move = 1;
	while(!s.empty()){
		move^=1;
		pair<long long, long long> top = *s.rbegin();
		ans[top.second] = move;

//		s.erase(top);

		long long removes = 0;

		if(s.empty())break;

		for(long long cur=hsh[top.first]-1; cur>=0; cur--){
			if(removes==k) break;
			pair<long long, long long> rmv = {A[cur], cur};
			if(s.find(rmv)!=s.end()) {
				s.erase(s.find(rmv));
				ans[cur] = move;
				removes++;
			}
		}

		if(s.empty())break;

		removes = 0;

		for(long long cur = hsh[top.first]+1; cur<n; cur++){
			if(removes==k)break;
			pair<long long, long long> rmv = {A[cur], cur};
			if(s.find(rmv)!=s.end()) {
				removes++;
				s.erase(s.find(rmv));
				ans[cur] = move;
			}
		}
		removes = 0;
		s.erase(top);
	}
	for(long long i=0; i<n; i++)cout<<ans[i]+1;

}


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

long long A[100005];
long long n, k;
set<pair<long long, long long> > s;
long long ans[100005];
long long hsh[100005];
int main(){
	std::ios_base::sync_with_stdio(0);
	cin >> n >> k;
	for(long long int i=0; i<n; i++){
		cin >> A[i];
		hsh[A[i]] = i;
		s.insert({A[i], i});
		ans[i] = 0;
	}

	long long move = 1;
	while(!s.empty()){
		move^=1;
		pair<long long, long long> top = *s.rbegin();
		ans[top.second] = move;

		s.erase(top);

		long long removes = 0;

		if(s.empty())break;

		for(long long cur=hsh[top.first]-1; cur>=0; cur--){
			pair<long long, long long> rmv = {A[cur], cur};
			if(s.find(rmv)!=s.end()) {
				s.erase(s.find(rmv));
				ans[cur] = move;
				removes++;
			}
			if(removes==k) break;
		}
		removes = 0;
		for(long long cur = hsh[top.first]+1; cur<n; cur++){
			pair<long long, long long> rmv = {A[cur], cur};
			if(s.find(rmv)!=s.end()) {
				removes++;
				s.erase(s.find(rmv));
				ans[cur] = move;
			}
			if(removes==k)break;
		}
		removes = 0;
		s.erase(top);
	}
	for(long long i =0; i<n; i++)cout<<ans[i]+1;

}


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

long long A[200005];
long long n, k;
set<pair<long long, long long> > s;
set<pair<long long, long long> > idxs;
long long ans[200005];
long long hsh[200005];
int main(){
	std::ios_base::sync_with_stdio(0);
	cin >> n >> k;
	idxs.insert({-1, -1});
	for(long long int i=0; i<n; i++){
		cin >> A[i];
		hsh[A[i]] = i;
		s.insert({A[i], i});
		idxs.insert({i, A[i]});
		ans[i] = 0;
	}
	idxs.insert({n+1, n+1});
	long long move = 1;
	while(!s.empty()){
		move^=1;
		pair<long long, long long> top = *s.rbegin();
		ans[top.second] = move;

		long long removes = 0;

		if(s.empty())break;
		auto itt = idxs.find({top.second, top.first});
		--itt;
		while((*itt).first != -1){
			if(removes==k) break;
			pair<long long, long long> xxx = {(*itt).second, (*itt).first};
			ans[xxx.second] = move;
			s.erase(xxx);
			idxs.erase(itt--);
			removes++;
		}
		removes = 0;
		itt = idxs.find({top.second, top.first});
		++itt;
		while((*itt).first!=(n+1)){
			if(removes==k)break;
			pair<long long, long long> xxx = {(*itt).second, (*itt).first};
			ans[xxx.second] = move;
			s.erase(xxx);
			idxs.erase(itt++);
			removes++;
		}
		idxs.erase(idxs.find({top.second, top.first}));
		removes = 0;
		s.erase(top);
//		cout<<"ENDING:\n";
//		for(auto i:s)cout<<i.first<<" "<<i.second<<"\n";

	}
	for(long long i=0; i<n; i++)cout<<ans[i]+1;

}


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

long long A[100005];
long long n, k;
set<pair<long long, long long> > s;
long long ans[100005];
long long hsh[100005];
int main(){
	std::ios_base::sync_with_stdio(0);
	cin >> n >> k;
	for(long long int i=0; i<n; i++){
		cin >> A[i];
		hsh[A[i]] = i;
		s.insert({A[i], i});
		ans[i] = 0;
	}

	long long move = 1;
	while(!s.empty()){
		move^=1;
		pair<long long, long long> top = *s.rbegin();
		ans[top.second] = move;

		s.erase(top);

		long long removes = 0;

		if(s.empty())break;

		for(long long cur=hsh[top.first]-1; cur>=0; cur--){
			if(removes==k) break;
			pair<long long, long long> rmv = {A[cur], cur};
			if(s.find(rmv)!=s.end()) {
				s.erase(s.find(rmv));
				ans[cur] = move;
				removes++;
			}
		}
		removes = 0;
		for(long long cur = hsh[top.first]+1; cur<n; cur++){
			if(removes==k)break;
			pair<long long, long long> rmv = {A[cur], cur};
			if(s.find(rmv)!=s.end()) {
				removes++;
				s.erase(s.find(rmv));
				ans[cur] = move;
			}
		}
		removes = 0;
//		s.erase(top);
	}
	for(long long i =0; i<n; i++)cout<<ans[i]+1;

}


//Program.cpp
//Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii,int>
#define rep(i,n) for(int (i) = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define fill(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 2*1e5 +5 ;
const int mod = 1e9 + 7 ;
const int inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace std;

#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout if(false) cout
#define mycerr if(false) cerr
#define AaryanS 0
#endif

void solve()
{
    int n , k;
    cin >> n >> k ;
    set<int> num, ind ;
    map<int,int> iden ;
    int ans[n] = {} , arr[n] ;
    rep(i,n){
        cin >> arr[i] ;
        num.insert(arr[i]) ;
        ind.insert(i) ;
        iden[arr[i]] = i ;
    }
    int cur = 0 ;
    while(!num.empty()){
        int pos = iden[*num.rbegin()] ;
        auto it = ind.find(pos) ;
        int cnt = 0 ;
        while(cnt < k){
            if(it != ind.beg()){
                it-- ;
                ans[*it] = cur ;
                num.erase(arr[*it]) ;
                ind.erase(*it) ;
                cnt++ ;
            }
            else
                 break ;
        }
        it = ind.find(pos) ;
        it++ ;
        cnt = 0 ;
        while(cnt < k){
            if(it != ind.end()){
                ans[*it] = cur ;
                num.erase(arr[*it]) ;
                ind.erase(*it) ;
                cnt++ ;
                it++ ;
            }
            else
                break ;

        }
        ans[pos] = cur ;
        num.erase(arr[pos]);
        ind.erase(pos) ;
        cur ^= 1;
    }
    rep(i,n)
        cout << ans[i] + 1 ;

}

int32_t main(int32_t argc, char *argv[])
{
    double t1 = clock() ;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case " << t << ":\n" ;
        solve();
    }
    mycerr << "Time : " << 1000 *(clock() - t1)/CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}


//Program.cpp
//Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii,int>
#define rep(i,n) for(int (i) = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define fill(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 2*1e5 +5 ;
const int mod = 1e9 + 7 ;
const int inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace std;

#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout if(false) cout
#define mycerr if(false) cerr
#define AaryanS 0
#endif

void solve()
{
    int n , k;
    cin >> n >> k ;
    set<int> num, ind ;
    map<int,int> iden ;
    int ans[n] = {} , arr[n] ;
    rep(i,n){
        cin >> arr[i] ;
        num.insert(arr[i]) ;
        ind.insert(i) ;
        iden[arr[i]] = i ;
    }
    int cur = 0 ;
    while(!num.empty()){
        int pos = iden[*num.rbegin()] ;
        auto it = ind.find(pos) ;
        int cnt = 0 ;
        while(cnt < k){
            if(it != ind.beg()){
                it = ind.find(pos) ;
                it-- ;
                ans[*it] = cur ;
                mycout << *it << " W " << cur << "\n" ;
                num.erase(arr[*it]) ;
                ind.erase(*it) ;
                cnt++ ;
            }
            else
                 break ;
        }
        it = ind.find(pos) ;
        it++ ;
        cnt = 0 ;
        while(cnt < k){
            if(it != ind.end()){
                ans[*it] = cur ;
                num.erase(arr[*it]) ;
                ind.erase(*it) ;
                mycout << *it << " w " << cur << "\n" ;
                cnt++ ;
                 it = ind.find(pos) ;
                it++ ;
            }
            else
                break ;

        }
        mycout << pos <<" " << cur << "\n" ;
        ans[pos] = cur ;
        num.erase(arr[pos]);
        ind.erase(pos) ;
        cur ^= 1;
    }
    rep(i,n)
        cout << ans[i] + 1 ;

}

int32_t main(int32_t argc, char *argv[])
{
    double t1 = clock() ;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case " << t << ":\n" ;
        solve();
    }
    mycerr << "Time : " << 1000 *(clock() - t1)/CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}


//Program.cpp
//Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii,int>
#define rep(i,n) for(int (i) = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define fill(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 2*1e5 +5 ;
const int mod = 1e9 + 7 ;
const int inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace std;

#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout if(false) cout
#define mycerr if(false) cerr
#define AaryanS 0
#endif

void solve()
{
    int n , k;
    cin >> n >> k ;
    set<int> num, ind ;
    map<int,int> iden ;
    int ans[n] = {} , arr[n] ;
    rep(i,n){
        cin >> arr[i] ;
        num.insert(arr[i]) ;
        ind.insert(i) ;
        iden[arr[i]] = i ;
    }
    int cur = 0 ;
    while(!num.empty()){
        int pos = iden[*num.rbegin()] ;
        auto it = ind.find(pos) ;
        int cnt = 0 ;
        while(cnt < k){
            if(it != ind.beg()){
                it-- ;
                ans[*it] = cur ;
                mycout << *it << " W " << cur << "\n" ;
                num.erase(arr[*it]) ;
                ind.erase(*it) ;
                cnt++ ;
            }
            else
                 break ;
        }
        it = ind.find(pos) ;
        it++ ;
        cnt = 0 ;
        while(cnt < k){
            if(it != ind.end()){
                ans[*it] = cur ;
                num.erase(arr[*it]) ;
                ind.erase(*it) ;
                mycout << *it << " w " << cur << "\n" ;
                cnt++ ;
                it = ind.find(pos) ;
                it++ ;
            }
            else
                break ;

        }
        mycout << pos <<" " << cur << "\n" ;
        ans[pos] = cur ;
        num.erase(arr[pos]);
        ind.erase(pos) ;
        cur ^= 1;
    }
    rep(i,n)
        cout << ans[i] + 1 ;

}

int32_t main(int32_t argc, char *argv[])
{
    double t1 = clock() ;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case " << t << ":\n" ;
        solve();
    }
    mycerr << "Time : " << 1000 *(clock() - t1)/CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 6e3 + 9;

int n, p[N], u, v, ans;

vector < int > gr[N], path;

void dfs(int node, int from) {
	path.push_back(node);
	for (int i = 0; i < gr[node].size(); i++)
		if (gr[node][i] != from)
			dfs(gr[node][i], node);
	if (gr[node].size() == 1) {
		int cur = 1;
		int dp[N];
		set < pii > st;
		st.clear();
		st.insert(make_pair(-p[path[0]], 1));
		memset(dp, false, sizeof dp);
		for (int i = 1; i < path.size(); i++) {
//			if (p[path[i]] > p[path[i-1]]) cur++;
//			else {
				if (st.upper_bound(make_pair(-p[path[i]], -inf)) == st.end()) cur = 1;
				else cur = st.upper_bound(make_pair(-p[path[i]], -inf)) -> second + 1;
//			}
			dp[i] = max(cur, dp[i-1]);
			st.insert(make_pair(-p[path[i]], cur));
		}
		ans = max(ans, dp[path.size() - 1]);
	}
	path.pop_back();
}

int main() {

	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> p[i];

	for (int i = 1; i < n; i++) {
		cin >> u >> v;
		gr[u].push_back(v);
		gr[v].push_back(u);
	}

	dfs(1, -1);

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 6e3 + 9;

int n, p[N], u, v, ans;

vector < int > gr[N], path;

vector < int > st(N, inf);

void dfs(int node, int from) {
	*lower_bound(st.begin(), st.end(), p[node]) = p[node];
	for (int i = 0; i < gr[node].size(); i++)
		if (gr[node][i] != from)
			dfs(gr[node][i], node);
}

int main() {

	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> p[i];

	for (int i = 1; i < n; i++) {
		cin >> u >> v;
		gr[u].push_back(v);
		gr[v].push_back(u);
	}

	for (int i = 1; i <= n; i++) {
		dfs(i, -1);
		for (int j = 0; j <= N; j++) {
			if (st[j] == inf) {
				ans = max(j, ans);
				break;
			}
		}
		st.clear();
		for (int i = 1; i <= N; i++)
			st.push_back(inf);
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 7e3 + 9;

int n, p[N], u, v, ans, cnt;

vector < int > gr[N], path, way[(int)(1e7+9)];

void dfs(int node, int from) {
	path.push_back(node);
	for (int i = 0; i < gr[node].size(); i++)
		if (gr[node][i] != from)
			dfs(gr[node][i], node);
	if (gr[node].size() == 1) {
		for (int i = 0; i < path.size(); i++) {
			way[cnt].push_back(p[path[i]]);
		}
		cnt++;
	}
	path.pop_back();
}

int LIS(int cur) {
	vector < int > st(N, inf);
	for (int i = 0; i < way[cur].size(); i++)
		*lower_bound(st.begin(), st.end(), way[cur][i]) = way[cur][i];
	for (int i = 0; i < N; i++)
		if (st[i] == inf)
			return i;
}

int main() {

	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> p[i];

	for (int i = 1; i < n; i++) {
		cin >> u >> v;
		gr[u].push_back(v);
		gr[v].push_back(u);
	}

	for (int i = 1; i <= n; i++) {
		dfs(i, -1);
	}

	for (int i = 0; i < cnt; i++) {
		ans = max(ans, LIS(i));
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 6e3 + 9;

int n, p[N], u, v, ans, cnt;

vector < int > gr[N], path, way[N];

void dfs(int node, int from) {
	path.push_back(node);
	for (int i = 0; i < gr[node].size(); i++)
		if (gr[node][i] != from)
			dfs(gr[node][i], node);
	if (gr[node].size() == 1) {
		for (int i = 0; i < path.size(); i++) {
			way[cnt].push_back(p[path[i]]);
		}
		cnt++;
	}
	path.pop_back();
}

int LIS(int cur) {
	vector < int > st(N, inf);
	for (int i = 0; i < way[cur].size(); i++)
		*lower_bound(st.begin(), st.end(), way[cur][i]) = way[cur][i];
	for (int i = 0; i <= N; i++)
		if (st[i] == inf)
			return i;
}

int main() {

	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> p[i];

	for (int i = 1; i < n; i++) {
		cin >> u >> v;
		gr[u].push_back(v);
		gr[v].push_back(u);
	}

	for (int i = 1; i <= n; i++) {
		dfs(i, -1);
		path.clear();
	}

	for (int i = 0; i < cnt; i++) {
		ans = max(ans, LIS(i));
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 6e3 + 9;

int n, p[N], u, v, ans, cnt;

vector < int > gr[N], path, way[10000007];

void dfs(int node, int from) {
	way[cnt].push_back(p[node]);
	for (int i = 0; i < gr[node].size(); i++)
		if (gr[node][i] != from)
			dfs(gr[node][i], node);
	if (gr[node].size() == 1) cnt++;
}

int LIS(int cur) {
	vector < int > st(N, inf);
	for (int i = 0; i < way[cur].size(); i++)
		*lower_bound(st.begin(), st.end(), way[cur][i]) = way[cur][i];
	for (int i = 0; i < N; i++)
		if (st[i] == inf)
			return i;
}

int main() {

	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> p[i];

	for (int i = 1; i < n; i++) {
		cin >> u >> v;
		gr[u].push_back(v);
		gr[v].push_back(u);
	}

	for (int i = 1; i <= n; i++) {
		dfs(i, -1);
	}

	for (int i = 0; i < cnt; i++) {
		ans = max(ans, LIS(i));
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
#define L(x) (x<<1+1)
#define R(x) (x<<1+2)
#define REP(a,b) for(int a=0;a<b;a++)
#define REP1(a,b) for(int a=1;a<=b;a++)
#define RREP(a,b) for(int a=b-1;a>=0;a--)
#define RREP1(a,b) for(int a=b;a>0;a--)
#define ITR(a,b) for(;a!=b;a++)
using namespace std;
#define P 1000000007LL
#define MAXN 6010
#define E7 10000000LL
typedef long long ll;

#include "windows.h"
#define PAU system("pause");
ifstream fin("data.in");
int n;
int v[MAXN];
vector<int> e[MAXN];

int dfs(int x, int f, int last, bool up){
	int ans=0;
	REP(i,e[x].size()){
		int t=e[x][i];
		if(t==f)continue;
		if(v[t]==last)ans=max(ans,dfs(t,x,last,up));
		bool flag=v[t]>last^up;
		if(v[t]>last^up)
			ans=max(ans,dfs(t,x,v[t],up)+1);
		else
			ans=max(ans,dfs(t,x,last,up));
	}
	return ans;
}
int main(){
	cin>>n;
	REP1(i,n)cin>>v[i];
	REP(i,n-1){
		int a,b;
		cin>>a>>b;
		e[a].push_back(b);
		e[b].push_back(a);
	}
	int maxx=0;
	REP1(i,n)maxx=max(maxx,dfs(i,-1,v[i],1)+dfs(i,-1,v[i],0));
	cout<<maxx;
	PAU
	return 0;
}


#include <bits/stdc++.h>
#define L(x) (x<<1+1)
#define R(x) (x<<1+2)
#define REP(a,b) for(int a=0;a<b;a++)
#define REP1(a,b) for(int a=1;a<=b;a++)
#define RREP(a,b) for(int a=b-1;a>=0;a--)
#define RREP1(a,b) for(int a=b;a>0;a--)
#define ITR(a,b) for(;a!=b;a++)
using namespace std;
#define P 1000000007LL
#define MAXN 6010
#define E7 10000000LL
typedef long long ll;

#include "windows.h"
#define PAU system("pause");
ifstream fin("data.in");
int n;
int v[MAXN];
vector<int> e[MAXN];

int dfs(int x, int f, int last, bool up){
	int ans=0;
	REP(i,e[x].size()){
		int t=e[x][i];
		if(t==f)continue;
		if(v[t]==last)ans=max(ans,dfs(t,x,last,up));
		bool flag=v[t]>last^up;
		if(v[t]>last^up)
			ans=max(ans,dfs(t,x,v[t],up)+1);
		else
			ans=max(ans,dfs(t,x,last,up));
	}
	return ans;
}
int main(){
	cin>>n;
	REP1(i,n)cin>>v[i];
	REP(i,n-1){
		int a,b;
		cin>>a>>b;
		e[a].push_back(b);
		e[b].push_back(a);
	}
	int maxx=0;
	REP1(i,n)
		cout<<dfs(i,-1,v[i],1)<<' '<<dfs(i,-1,v[i],0)<<endl;
		//maxx=max(maxx,dfs(i,-1,v[i],1)+dfs(i,-1,v[i],0));
	cout<<maxx;
	PAU
	return 0;
}


#include <bits/stdc++.h>
#define L(x) (x<<1+1)
#define R(x) (x<<1+2)
#define REP(a,b) for(int a=0;a<b;a++)
#define REP1(a,b) for(int a=1;a<=b;a++)
#define RREP(a,b) for(int a=b-1;a>=0;a--)
#define RREP1(a,b) for(int a=b;a>0;a--)
#define ITR(a,b) for(;a!=b;a++)
using namespace std;
#define P 1000000007LL
#define MAXN 6010
#define E7 10000000LL
typedef long long ll;

#include "windows.h"
#define PAU system("pause");
ifstream fin("data.in");
int n;
int v[MAXN];
vector<int> e[MAXN];

int dfs(int x, int f, int last, bool up){
	int ans=0;
	REP(i,e[x].size()){
		int t=e[x][i];
		if(t==f)continue;
		if(v[t]==last)ans=max(ans,dfs(t,x,last,up));
		bool flag=v[t]>last^up;
		if(v[t]>last^up)
			ans=max(ans,dfs(t,x,v[t],up)+1);
		else
			ans=max(ans,dfs(t,x,last,up));
	}
	return ans;
}
int main(){
	cin>>n;
	REP1(i,n)cin>>v[i];
	REP(i,n-1){
		int a,b;
		cin>>a>>b;
		e[a].push_back(b);
		e[b].push_back(a);
	}
	int maxx=0;
	REP1(i,n)
		cout<<dfs(i,-1,v[i],1)<<' '<<dfs(i,-1,v[i],0)<<endl;
		//maxx=max(maxx,dfs(i,-1,v[i],1)+dfs(i,-1,v[i],0));
	cout<<maxx;
	return 0;
}


#include <bits/stdc++.h>
#define L(x) (x<<1+1)
#define R(x) (x<<1+2)
#define REP(a,b) for(int a=0;a<b;a++)
#define REP1(a,b) for(int a=1;a<=b;a++)
#define RREP(a,b) for(int a=b-1;a>=0;a--)
#define RREP1(a,b) for(int a=b;a>0;a--)
#define ITR(a,b) for(;a!=b;a++)
using namespace std;
#define P 1000000007LL
#define MAXN 6010
#define E7 10000000LL
typedef long long ll;

#include "windows.h"
#define PAU system("pause");
ifstream fin("data.in");
int n;
int v[MAXN];
vector<int> e[MAXN];

int dfs(int x, int f, int last){
	// cout<<x<<' '<<f<<' '<<last<<' '<<up<<endl;
	int ans=0;
	REP(i,e[x].size()){
		int t=e[x][i];
		if(t==f)continue;
		// bool flag=v[t]>last^up;
		if(v[t]<=last)
			ans=max(ans,dfs(t,x,last));
		else// if(v[t]>last^up)
			ans=max(ans,dfs(t,x,v[t])+1);
		// else
		// 	ans=max(ans,dfs(t,x,last,up));
	}
	// cout<<x<<' '<<f<<' '<<last<<' '<<up<<' '<<ans<<endl;
	return ans;
}
int main(){
	cin>>n;
	REP1(i,n)cin>>v[i];
	REP(i,n-1){
		int a,b;
		cin>>a>>b;
		e[a].push_back(b);
		e[b].push_back(a);
	}
	int maxx=0;
	REP1(i,n)
		maxx=max(maxx,dfs(i,-1,v[i])+1);
		// maxx=max(maxx,dfs(i,-1,v[i],1)+dfs(i,-1,v[i],0)+1);
	cout<<maxx;
	return 0;
}


#include "iostream"
#include "cstdio"
#include "algorithm"
#include "math.h"
#include "queue"
#include "map"
#include "set"
#include "stack"
#include "fstream"
#include "cstring"
#include "string"
#define L(x) (x<<1+1)
#define R(x) (x<<1+2)
#define REP(a,b) for(int a=0;a<b;a++)
#define REP1(a,b) for(int a=1;a<=b;a++)
#define RREP(a,b) for(int a=b-1;a>=0;a--)
#define RREP1(a,b) for(int a=b;a>0;a--)
#define ITR(a,b) for(;a!=b;a++)
using namespace std;
#define P 1000000007LL
#define MAXN 100
#define E7 10000000LL
typedef long long ll;

#include "windows.h"
#define PAU system("pause");
ifstream fin("data.in");
int n;
int v[10000];
int inc[10000];
int decv[10000];

vector<vector<int> >e;
void add(int a,int b){
	e[a].push_back(b);
	e[b].push_back(a);
}

int up(int x, int from){
	inc[x]=1;
	REP(i,e[x].size()){
		if(e[x][i]==from)continue;
		if(v[x]>=v[e[x][i]])continue;
		inc[x]=max(inc[x],up(e[x][i],x)+1);
	}
	return inc[x];
}
int down(int x, int from){
	decv[x]=1;
	REP(i,e[x].size()){
		if(e[x][i]==from)continue;
		if(v[x]<=v[e[x][i]])continue;
		decv[x]=max(decv[x],down(e[x][i],x)+1);
	}
	return decv[x];
}

int main(){
	cin>>n;
	e.clear();
	e.resize(n);
	REP(i,n)cin>>v[i];
	REP(i,n-1){
		int a,b;
		cin>>a>>b;
		a--;b--;
		add(a,b);
	}
	up(0,-1);
	down(0,-1);
	int ans=0;
	REP(i,n){ans=max(ans,inc[i]+decv[i]-1);}
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
#define L(x) (x<<1+1)
#define R(x) (x<<1+2)
#define REP(a,b) for(int a=0;a<b;a++)
#define REP1(a,b) for(int a=1;a<=b;a++)
#define RREP(a,b) for(int a=b-1;a>=0;a--)
#define RREP1(a,b) for(int a=b;a>0;a--)
#define ITR(a,b) for(;a!=b;a++)
using namespace std;
#define P 1000000007LL
#define MAXN 6010
#define E7 10000000LL
typedef long long ll;

#include "windows.h"
#define PAU system("pause");
ifstream fin("data.in");
int n;
int v[MAXN];
vector<int> e[MAXN];
vector<int> q;
int maxx=0;
int dfs(int x, int f){
	if(q.empty()||q[q.size()-1]<v[x]){
		q.push_back(v[x]);
		maxx=max(maxx,int(q.size()));
		REP(i,e[x].size())
			if(e[x][i]!=f)dfs(e[x][i],x);
		q.pop_back();
	}
	else{
		int low=lower_bound(q.begin(), q.end(),v[x])-q.begin();
		int rem=q[low];
		q[low]=v[x];
		REP(i,e[x].size())
			if(e[x][i]!=f)dfs(e[x][i],x);
		q[low]=rem;
	}
}
int main(){
	cin>>n;
	REP1(i,n)cin>>v[i];
	REP(i,n-1){
		int a,b;
		cin>>a>>b;
		e[a].push_back(b);
		e[b].push_back(a);
	}
	REP1(i,n)dfs(i,-1);
	cout<<maxx;
	return 0;
}


#include <bits/stdc++.h>
#define L(x) (x<<1+1)
#define R(x) (x<<1+2)
#define REP(a,b) for(int a=0;a<b;a++)
#define REP1(a,b) for(int a=1;a<=b;a++)
#define RREP(a,b) for(int a=b-1;a>=0;a--)
#define RREP1(a,b) for(int a=b;a>0;a--)
#define ITR(a,b) for(;a!=b;a++)
using namespace std;
#define P 1000000007LL
#define MAXN 6010
#define E7 10000000LL
typedef long long ll;

#include "windows.h"
#define PAU system("pause");
ifstream fin("data.in");
int n;
int v[MAXN];
vector<int> e[MAXN];

int dfs(int x, int f, int last, bool up){
	int ans=0;
	REP(i,e[x].size()){
		int t=e[x][i];
		if(t==f)continue;
		if(v[t]==last)ans=max(ans,dfs(t,x,last,up));
		bool flag=v[t]>last^up;
		if(v[t]>last^up)
			ans=max(ans,dfs(t,x,v[t],up)+1);
		else
			ans=max(ans,dfs(t,x,last,up));
	}
	return ans;
}
int main(){
	cin>>n;
	REP1(i,n)cin>>v[i];
	REP(i,n-1){
		int a,b;
		cin>>a>>b;
		e[a].push_back(b);
		e[b].push_back(a);
	}
	int maxx=0;
	REP1(i,n)
		// cout<<dfs(i,-1,v[i],1)<<' '<<dfs(i,-1,v[i],0)<<endl;
		maxx=max(maxx,dfs(i,-1,v[i],1)+dfs(i,-1,v[i],0));
	cout<<maxx;
	return 0;
}


#include "iostream"
#include "cstdio"
#include "algorithm"
#include "math.h"
#include "queue"
#include "map"
#include "set"
#include "stack"
#include "fstream"
#include "cstring"
#include "string"
#define L(x) (x<<1+1)
#define R(x) (x<<1+2)
#define REP(a,b) for(int a=0;a<b;a++)
#define REP1(a,b) for(int a=1;a<=b;a++)
#define RREP(a,b) for(int a=b-1;a>=0;a--)
#define RREP1(a,b) for(int a=b;a>0;a--)
#define ITR(a,b) for(;a!=b;a++)
using namespace std;
#define P 1000000007LL
#define MAXN 100
#define E7 10000000LL
typedef long long ll;

#include "windows.h"
#define PAU system("pause");
ifstream fin("data.in");
int n;
int v[10000];
int inc[10000];
int decv[10000];

vector<vector<int> >e;
void add(int a,int b){
	e[a].push_back(b);
	e[b].push_back(a);
}

int up(int x, int from){
	inc[x]=1;
	REP(i,e[x].size()){
		if(e[x][i]==from)continue;
		if(v[x]>=v[e[x][i]])continue;
		inc[x]=max(inc[x],up(e[x][i],x)+1);
	}
	return inc[x];
}
int down(int x, int from){
	decv[x]=1;
	REP(i,e[x].size()){
		if(e[x][i]==from)continue;
		if(v[x]<=v[e[x][i]])continue;
		decv[x]=max(decv[x],down(e[x][i],x)+1);
	}
	return decv[x];
}

int main(){
	cin>>n;
	e.clear();
	e.resize(n);
	REP(i,n)cin>>v[i];
	REP(i,n-1){
		int a,b;
		cin>>a>>b;
		a--;b--;
		add(a,b);
	}
	up(0,-1);
	down(0,-1);
	int ans=1;
	REP(i,n){ans=max(ans,inc[i]+decv[i]-1);}
	cout<<ans;
	return 0;
}


#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
vector<int> ve[11111];
int p[111111];
int dp[111111],dp1[111111];
int res = 1;
int num1[111111],num2[111111], index;
int dfs1(int x,int f,int root)
{
    int i;
    if(p[x]<p[root]) {
        num1[index] = max(num1[index],dp[x]);
    }
    if(p[x]>p[root])
    {
        num2[index] = max(num2[index],dp1[x]);
    }
    for(i=0;i<ve[x].size();i++)
    {
        if(ve[x][i]!=f) dfs1(ve[x][i],x,root);
    }
}
void dfs(int x,int f)
{
    int i;
    for(i=0;i<ve[x].size();i++)
    {
        if(ve[x][i]!=f) dfs(ve[x][i],x);
    }
    index = 0;
    int max_a = 0, max_b = 0,index_a = -1,index_b = -2 ;
    memset(num1,0,sizeof(num1));
    memset(num2,0,sizeof(num2));
    for(i=0;i<ve[x].size();i++)
    {
        if(ve[x][i]!=f)
        {
            dfs1(ve[x][i],x,x);
            index++;
        }
    }
    for(i=0;i<index;i++)
    {
        if(num1[i]>max_a) max_a = num1[i],index_a = i;
        if(num2[i]>max_b) max_b = num2[i],index_b = i;
    }
    if(index_a!=index_b)
    {
        res = max(res,1+max_a+max_b);
        dp[x] = 1+max_a;
        dp1[x] = 1+max_b;
    }
    else
    {
        if(index==1)
        {
            res = max(res,1+max(max_a,max_b));
            dp[x] = 1+max_a;
            dp1[x] = 1+max_b;
            return;
        }
        int aa = 0, bb  = 0;
        for(i=0;i<index;i++)
        {
            if(i==index_a) continue;
            aa = max(aa,num1[i]);
            bb = max(bb,num2[i]);
        }
        res = max(res,1+max(max_a+bb,max_b+aa));
        dp[x] = 1+max_a;
        dp1[x] = 1+max_b;

    }
    //if(res==7) printf("%d %d %d %d %d\n",x,max_a,max_b,index_a,index_b);
}
int main()
{
    int n, i, x, y;
    scanf("%d",&n);
    for(i=1;i<=n;i++) scanf("%d",&p[i]);
    for(i=1;i<n;i++)
    {
        scanf("%d %d",&x,&y);
        ve[x].push_back(y);
        ve[y].push_back(x);
    }
    dfs(1,1);
    printf("%d\n",res);
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

int n, r[6001], a[6001], b[12000], e[12000];
int c[6000], ci;
int ans;
bool vs[6001];

void dfs(int v) {
    if (vs[v])
        return;
    vs[v] = true;
    int l = lower_bound(c, c + ci, r[v]) - c;
    int tmp2 = ci;
    if (l == ci)
        ci++;
    int tmp = c[l];
    c[l] = r[v];
    ans = max(ci, ans);
    for (int i = a[v]; i; i = b[i])
        dfs(e[i]);
    c[l] = tmp;
    ci = tmp2;
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", r + i);
    for (int i = 1; i < n; i++) {
        scanf("%d%d", e + i, e + i + n);
        b[i + n] = a[e[i]];
        a[e[i]] = i + n;
        b[i] = a[e[i + n]];
        a[e[i + n]] = i;
    }
    for (int i = 1; i <= n; i++) {
        dfs(i);
        fill(vs, vs + n + 1, false);
    }
    printf("%d\n", ans);
}


#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
vector<int> ve[11111];
int p[111111];
int dp[111111],dp1[111111];
int res = 1;
int num1[111111],num2[111111], index;
int dfs1(int x,int f,int root)
{
    int i;
    if(p[x]<p[root]) {
        num1[index] = max(num1[index],dp[x]);
    }
    if(p[x]>p[root])
    {
        num2[index] = max(num2[index],dp1[x]);
    }
    for(i=0;i<ve[x].size();i++)
    {
        if(ve[x][i]!=f) dfs1(ve[x][i],x,root);
    }
}
void dfs(int x,int f)
{
    int i;
    for(i=0;i<ve[x].size();i++)
    {
        if(ve[x][i]!=f) dfs(ve[x][i],x);
    }
    index = 0;
    int max_a = 0, max_b = 0,index_a = -1,index_b = -2 ;
    memset(num1,0,sizeof(num1));
    memset(num2,0,sizeof(num2));
    for(i=0;i<ve[x].size();i++)
    {
        if(ve[x][i]!=f)
        {
            dfs1(ve[x][i],x,x);
            index++;
        }
    }
    for(i=0;i<index;i++)
    {
        if(num1[i]>max_a) max_a = num1[i],index_a = i;
        if(num2[i]>max_b) max_b = num2[i],index_b = i;
    }
    if(index_a!=index_b)
    {
        res = max(res,1+max_a+max_b);
        dp[x] = 1+max_a;
        dp1[x] = 1+max_b;
    }
    else
    {
        if(index==1)
        {
            res = max(res,1+max(max_a,max_b));
            dp[x] = 1+max_a;
            dp1[x] = 1+max_b;
            return;
        }
        int aa = 0, bb  = 0;
        for(i=0;i<index;i++)
        {
            aa = max(aa,num1[i]);
            bb = max(bb,num2[i]);
        }
        res = max(res,1+max(max_a+bb,max_b+aa));
        dp[x] = 1+max_a;
        dp1[x] = 1+max_b;

    }
//  if(res==6) printf("%d %d %d %d %d\n",x,max_a,max_b,index_a,index_b);
}
int main()
{
    int n, i, x, y;
    scanf("%d",&n);
    for(i=1;i<=n;i++) scanf("%d",&p[i]);
    for(i=1;i<n;i++)
    {
        scanf("%d %d",&x,&y);
        ve[x].push_back(y);
        ve[y].push_back(x);
    }
    dfs(1,1);
    printf("%d\n",res);
    return 0;
}


#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
vector<int> ve[11111];
int p[111111];
int dp[111111],dp1[111111];
int res = 1;
int num1[111111],num2[111111], index;
int dfs1(int x,int f,int root)
{
    int i;
    if(p[x]<p[root]) {
        num1[index] = max(num1[index],dp[x]);
    }
    if(p[x]>p[root])
    {
        num2[index] = max(num2[index],dp1[x]);
    }
    for(i=0;i<ve[x].size();i++)
    {
        if(ve[x][i]!=f) dfs1(ve[x][i],x,root);
    }
}
void dfs(int x,int f)
{
    int i;
    for(i=0;i<ve[x].size();i++)
    {
        if(ve[x][i]!=f) dfs(ve[x][i],x);
    }
    index = 0;
    int max_a = 0, max_b = 0,index_a = -1,index_b = -2 ;
    for(i=0;i<ve[x].size();i++)
    {
        if(ve[x][i]!=f)
        {
            dfs1(ve[x][i],x,x);
            index++;
        }
    }
    for(i=0;i<index;i++)
    {
        if(num1[i]>max_a) max_a = num1[i],index_a = i;
        if(num2[i]>max_b) max_b = num2[i],index_b = i;
    }
    if(index_a!=index_b)
    {
        res = max(res,1+max_a+max_b);
        dp[x] = 1+max_a;
        dp1[x] = 1+max_b;
    }
    else
    {
        if(index==1)
        {
            res = max(res,1+max(max_a,max_b));
            dp[x] = 1+max_a;
            dp1[x] = 1+max_b;
            return;
        }
        int aa = 0, bb  = 0;
        for(i=0;i<index;i++)
        {
            aa = max(aa,num1[i]);
            bb = max(bb,num2[i]);
        }
        res = max(res,1+max(max_a+bb,max_b+aa));
        dp[x] = 1+max_a;
        dp1[x] = 1+max_b;

    }
}
int main()
{
    int n, i, x, y;
    scanf("%d",&n);
    for(i=1;i<=n;i++) scanf("%d",&p[i]);
    for(i=1;i<n;i++)
    {
        scanf("%d %d",&x,&y);
        ve[x].push_back(y);
        ve[y].push_back(x);
    }
    dfs(1,1);
    printf("%d\n",res);
    return 0;
}


#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
vector<int> ve[11111];
int p[111111];
int dp[111111],dp1[111111];
int res = 1;
void dfs(int x,int f)
{
//  printf("%d %d\n",x,f);
    int i;
    for(i=0;i<ve[x].size();i++)
    {
        if(ve[x][i]!=f) dfs(ve[x][i],x);
    }
    int a = 0, b = 0;
    for(i=0;i<ve[x].size();i++)
    {
        if(ve[x][i]!=f)
        {
            if(p[ve[x][i]]<p[x]) a = max(a,dp[ve[x][i]]);
            if(p[ve[x][i]]>p[x]) b = max(b,dp1[ve[x][i]]);
        }

    }
    res = max(res,a+b+1);
    dp[x] = 1+a;
    dp1[x] = 1+b;
//  printf("%d %d\n",x,dp[x]);
}
int main()
{
    int n, i, x, y;
    scanf("%d",&n);
    for(i=1;i<=n;i++) scanf("%d",&p[i]);
    for(i=1;i<n;i++)
    {
        scanf("%d %d",&x,&y);
        ve[x].push_back(y);
        ve[y].push_back(x);
    }
    dfs(1,1);
    printf("%d\n",res);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <bitset>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <ctime>
#include <cmath>
#include <list>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int,int> pr;
const int infi=1000000;
const int mod=1000000007;
const D eps=1e-6;
const int N=100010;
const int M=1000100;
struct node{int l,r,ma;}t[M<<4];
int top,n,v[N],head[N],tmp,len;
int g[N],to[N],nxt[N],tot,ans;
int build(int l,int r) {
    int i=++len,mid=(l+r)>>1;
    if (l==r) return i;
    t[i].l=build(l,mid);
    t[i].r=build(mid+1,r);
    return i;
}
void addedge(int x,int y) {
    to[++tot]=y; nxt[tot]=g[x]; g[x]=tot;
    to[++tot]=x; nxt[tot]=g[y]; g[y]=tot;
}
int query(int i,int l,int r,int x,int y) {
    if (x<=l&&y>=r) return t[i].ma;
    int mid=(l+r)>>1,ans=0;
    if (x<=mid) ans=max(ans,query(t[i].l,l,mid,x,y));
    if (y>mid) ans=max(ans,query(t[i].r,mid+1,r,x,y));
    return ans;
}
int add(int i,int l,int r,int x,int y) {
    int k=++len,mid=(l+r)>>1;
    if (l==r) {
        t[k].ma=max(t[i].ma,y);
        return k;
    }
    if (x<=mid) t[k].r=t[i].r,t[k].l=add(t[i].l,l,mid,x,y);
    else t[k].l=t[i].l,t[k].r=add(t[i].r,mid+1,r,x,y);
    t[k].ma=max(t[t[k].l].ma,t[t[k].r].ma);
    return k;
}
void dfs(int x,int fat) {
    int k,tmp=query(head[fat],1,top,v[x]+1,top)+1;
    head[x]=add(head[fat],1,top,v[x],tmp);
    if (tmp>ans) ans=tmp;
    for (k=g[x];k;k=nxt[k]) if (to[k]!=fat) dfs(to[k],x);
}
int main() {
    int i,x,y;
    scanf("%d",&n);
    for (i=1;i<=n;i++) {
        scanf("%d",&v[i]);
        top=max(v[i],top);
    }
    top++;
    for (i=1;i<n;i++) {
        scanf("%d%d",&x,&y);
        addedge(x,y);
    }
    head[0]=build(1,top); tmp=len;
    for (i=1;i<=n;i++) len=tmp,dfs(i,0);
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <bitset>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <ctime>
#include <cmath>
#include <list>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int,int> pr;
const int infi=1000000;
const int mod=1000000007;
const D eps=1e-6;
const int N=100010;
const int M=1000100;
int top,n,v[N],ma[M],w[N];
int g[N],to[N],nxt[N],tot,ans,b[N],cnt;
inline void addedge(int x,int y) {
    to[++tot]=y; nxt[tot]=g[x]; g[x]=tot;
    to[++tot]=x; nxt[tot]=g[y]; g[y]=tot;
}
void build(int i,int l,int r) {
    if (l==r) {
        w[l]=i;
        return;
    }
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
}
void Max(int &x,int y) {if (x<y) x=y;}
int ask(int i,int l,int r,int x,int y) {
    if (x<=l&&y>=r) return ma[i];
    int mid=(l+r)>>1,t=0;
    if (x<=mid) Max(t,ask(i<<1,l,mid,x,y));
    if (y>mid) Max(t,ask(i<<1|1,mid+1,r,x,y));
    return t;
}
void modify(int x,int y) {
    ma[x]=y;
    for (x>>=1;x;x>>=1) ma[x]=max(ma[x<<1],ma[x<<1|1]);
}
void dfs(int x,int fat) {
    int k,t=ask(1,1,top,v[x]+1,top)+1,tmp=ma[w[v[x]]];
    if (ma[w[v[x]]]<t) modify(w[v[x]],t);
    Max(ans,t);
    for (k=g[x];k;k=nxt[k]) if (to[k]!=fat) dfs(to[k],x);
    if (ma[w[v[x]]]!=tmp) modify(w[v[x]],tmp);
}
int check(int l,int r,int x) {
    int mid;
    while (l<r) {
        mid=(l+r)>>1;
        if (b[mid]<x) l=mid+1;
        else r=mid;
    }
    return r;
}
int main() {
    int i,x,y;
    scanf("%d",&n);
    for (i=1;i<=n;i++) {
        scanf("%d",&v[i]);
        b[++cnt]=v[i];
    }
    sort(b+1,b+1+cnt);
    for (i=1;i<=n;i++) v[i]=check(1,n,v[i]);
    top=n+1;
    for (i=1;i<n;i++) {
        scanf("%d%d",&x,&y);
        addedge(x,y);
    }
    build(1,1,top);
    for (i=1;i<=n;i++) dfs(i,0);
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <bitset>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <ctime>
#include <cmath>
#include <list>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int,int> pr;
const int infi=1000000;
const int mod=1000000007;
const D eps=1e-6;
const int N=100010;
const int M=1000100;
int top,n,v[N],ma[M],w[N];
int g[N],to[N],nxt[N],tot,ans,b[N],cnt;
inline void addedge(int x,int y) {
    to[++tot]=y; nxt[tot]=g[x]; g[x]=tot;
    to[++tot]=x; nxt[tot]=g[y]; g[y]=tot;
}
void build(int i,int l,int r) {
    if (l==r) {
        w[l]=i;
        return;
    }
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
}
void Max(int &x,int y) {if (x<y) x=y;}
int ask(int i,int l,int r,int x,int y) {
    if (x<=l&&y>=r) return ma[i];
    if (!ma[i]) return 0;
    int mid=(l+r)>>1,t=0;
    if (x<=mid) Max(t,ask(i<<1,l,mid,x,y));
    if (y>mid) Max(t,ask(i<<1|1,mid+1,r,x,y));
    return t;
}
void modify(int x,int y) {
    ma[x]=y; for (x>>=1;x;x>>=1) ma[x]=max(ma[x<<1],ma[x<<1|1]);
}
void dfs(int x,int fat) {
    int k,t=ask(1,1,top,1,v[x]-1)+1,tmp=ma[w[v[x]]];
    if (ma[w[v[x]]]<t) modify(w[v[x]],t);
    Max(ans,t);
    for (k=g[x];k;k=nxt[k]) if (to[k]!=fat) dfs(to[k],x);
    if (ma[w[v[x]]]!=tmp) modify(w[v[x]],tmp);
}
int check(int l,int r,int x) {
    int mid;
    while (l<r) {
        mid=(l+r)>>1;
        if (b[mid]<x) l=mid+1;
        else r=mid;
    }
    return r;
}
int main() {
    int i,x,y;
    scanf("%d",&n);
    for (i=1;i<=n;i++) {
        scanf("%d",&v[i]);
        b[++cnt]=v[i];
    }
    sort(b+1,b+1+cnt);
    for (i=1;i<=n;i++) v[i]=check(1,n,v[i]);
    top=n+1;
    for (i=1;i<n;i++) {
        scanf("%d%d",&x,&y);
        addedge(x,y);
    }
    build(1,1,top);
    for (i=1;i<=n;i++) dfs(i,0);
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <bitset>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <ctime>
#include <cmath>
#include <list>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int,int> pr;
const int infi=1000000;
const int mod=1000000007;
const D eps=1e-6;
const int N=100010;
const int M=1000100;
struct node{int l,r,ma;}t[M<<4];
int top,n,v[N],head[N],tmp,len;
int g[N],to[N],nxt[N],tot,ans;
inline int build(int l,int r) {
    int i=++len,mid=(l+r)>>1;
    if (l==r) return i;
    t[i].l=build(l,mid);
    t[i].r=build(mid+1,r);
    return i;
}
inline void addedge(int x,int y) {
    to[++tot]=y; nxt[tot]=g[x]; g[x]=tot;
    to[++tot]=x; nxt[tot]=g[y]; g[y]=tot;
}
inline int query(int i,int l,int r,int x,int y) {
    if (x<=l&&y>=r) return t[i].ma;
    if (!t[i].ma) return 0;
    int mid=(l+r)>>1,ans=0;
    if (x<=mid) ans=max(ans,query(t[i].l,l,mid,x,y));
    if (y>mid) ans=max(ans,query(t[i].r,mid+1,r,x,y));
    return ans;
}
inline int add(int i,int l,int r,int x,int y) {
    int k=++len,mid=(l+r)>>1;
    if (l==r) {
        t[k].ma=max(t[i].ma,y);
        return k;
    }
    if (x<=mid) t[k].r=t[i].r,t[k].l=add(t[i].l,l,mid,x,y);
    else t[k].l=t[i].l,t[k].r=add(t[i].r,mid+1,r,x,y);
    t[k].ma=max(t[t[k].l].ma,t[t[k].r].ma);
    return k;
}
inline void dfs(int x,int fat) {
    int k,tmp=query(head[fat],1,top,v[x]+1,top)+1;
    head[x]=add(head[fat],1,top,v[x],tmp);
    if (tmp>ans) ans=tmp;
    for (k=g[x];k;k=nxt[k]) if (to[k]!=fat) dfs(to[k],x);
}
int main() {
    int i,x,y;
    scanf("%d",&n);
    for (i=1;i<=n;i++) {
        scanf("%d",&v[i]);
        top=max(v[i],top);
    }
    top++;
    for (i=1;i<n;i++) {
        scanf("%d%d",&x,&y);
        addedge(x,y);
    }
    head[0]=build(1,top); tmp=len;
    for (i=1;i<=n;i++) len=tmp,dfs(i,0);
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <bitset>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <ctime>
#include <cmath>
#include <list>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int,int> pr;
const int infi=1000000;
const int mod=1000000007;
const D eps=1e-6;
const int N=100010;
const int M=1000100;
struct node{int l,r,ma;}t[M<<4];
int top,n,v[N],head[N],tmp,len;
int g[N],to[N],nxt[N],tot,ans,b[N],cnt;
inline int build(int l,int r) {
    int i=++len,mid=(l+r)>>1;
    if (l==r) return i;
    t[i].l=build(l,mid);
    t[i].r=build(mid+1,r);
    return i;
}
inline void addedge(int x,int y) {
    to[++tot]=y; nxt[tot]=g[x]; g[x]=tot;
    to[++tot]=x; nxt[tot]=g[y]; g[y]=tot;
}
inline int query(int i,int l,int r,int x,int y) {
    if (x<=l&&y>=r) return t[i].ma;
    if (!t[i].ma) return 0;
    int mid=(l+r)>>1,ans=0;
    if (x<=mid) ans=max(ans,query(t[i].l,l,mid,x,y));
    if (y>mid) ans=max(ans,query(t[i].r,mid+1,r,x,y));
    return ans;
}
inline int add(int i,int l,int r,int x,int y) {
    int k=++len,mid=(l+r)>>1;
    if (l==r) {
        t[k].ma=max(t[i].ma,y);
        return k;
    }
    if (x<=mid) t[k].r=t[i].r,t[k].l=add(t[i].l,l,mid,x,y);
    else t[k].l=t[i].l,t[k].r=add(t[i].r,mid+1,r,x,y);
    t[k].ma=max(t[t[k].l].ma,t[t[k].r].ma);
    return k;
}
inline void dfs(int x,int fat) {
    int k,tmp=query(head[fat],1,top,v[x]+1,top)+1;
    head[x]=add(head[fat],1,top,v[x],tmp);
    if (tmp>ans) ans=tmp;
    for (k=g[x];k;k=nxt[k]) if (to[k]!=fat) dfs(to[k],x);
}
int check(int l,int r,int x) {
    int mid;
    while (l<r) {
        mid=(l+r)>>1;
        if (b[mid]<=x) l=mid+1;
        else r=mid;
    }
    return r;
}
int main() {
    int i,x,y;
    scanf("%d",&n);
    for (i=1;i<=n;i++) {
        scanf("%d",&v[i]);
        b[++cnt]=v[i];
    }
    sort(b+1,b+1+cnt);
    for (i=1;i<=n;i++) v[i]=check(1,n,v[i]);
    top=n+1;
    for (i=1;i<n;i++) {
        scanf("%d%d",&x,&y);
        addedge(x,y);
    }
    head[0]=build(1,top); tmp=len;
    for (i=1;i<=n;i++) len=tmp,dfs(i,0);
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <bitset>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <ctime>
#include <cmath>
#include <list>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int,int> pr;
const int infi=1000000;
const int mod=1000000007;
const D eps=1e-6;
const int N=100010;
const int M=1000100;
struct node{int l,r,ma;}t[M<<4];
int top,n,v[N],head[N],tmp,len;
int g[N],to[N],nxt[N],tot,ans,b[N],cnt;
inline int build(int l,int r) {
    int i=++len,mid=(l+r)>>1;
    if (l==r) return i;
    t[i].l=build(l,mid);
    t[i].r=build(mid+1,r);
    return i;
}
inline void addedge(int x,int y) {
    to[++tot]=y; nxt[tot]=g[x]; g[x]=tot;
    to[++tot]=x; nxt[tot]=g[y]; g[y]=tot;
}
inline int query(int i,int l,int r,int x,int y) {
    if (x<=l&&y>=r) return t[i].ma;
    if (!t[i].ma) return 0;
    int mid=(l+r)>>1,ans=0;
    if (x<=mid) ans=max(ans,query(t[i].l,l,mid,x,y));
    if (y>mid) ans=max(ans,query(t[i].r,mid+1,r,x,y));
    return ans;
}
inline int add(int i,int l,int r,int x,int y) {
    int k=++len,mid=(l+r)>>1;
    if (l==r) {
        t[k].ma=max(t[i].ma,y);
        return k;
    }
    if (x<=mid) t[k].r=t[i].r,t[k].l=add(t[i].l,l,mid,x,y);
    else t[k].l=t[i].l,t[k].r=add(t[i].r,mid+1,r,x,y);
    t[k].ma=max(t[t[k].l].ma,t[t[k].r].ma);
    return k;
}
inline void dfs(int x,int fat) {
    int k,tmp=query(head[fat],1,top,v[x]+1,top)+1;
    head[x]=add(head[fat],1,top,v[x],tmp);
    if (tmp>ans) ans=tmp;
    for (k=g[x];k;k=nxt[k]) if (to[k]!=fat) dfs(to[k],x);
}
int check(int l,int r,int x) {
    int mid;
    while (l<r) {
        mid=(l+r)>>1;
        if (b[mid]<x) l=mid+1;
        else r=mid;
    }
    return r;
}
int main() {
    int i,x,y;
    scanf("%d",&n);
    for (i=1;i<=n;i++) {
        scanf("%d",&v[i]);
        b[++cnt]=v[i];
    }
    sort(b+1,b+1+cnt);
    for (i=1;i<=n;i++) v[i]=check(1,n,v[i]);
    top=n+1;
    for (i=1;i<n;i++) {
        scanf("%d%d",&x,&y);
        addedge(x,y);
    }
    head[0]=build(1,top); tmp=len;
    for (i=1;i<=n;i++) len=tmp,dfs(i,0);
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <bitset>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <ctime>
#include <cmath>
#include <list>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int,int> pr;
const int infi=1000000;
const int mod=1000000007;
const D eps=1e-6;
const int N=100010;
const int M=1000100;
int top,n,v[N],ma[M],w[N];
int g[N],to[N],nxt[N],tot,ans,b[N],cnt;
inline void addedge(int x,int y) {
    to[++tot]=y; nxt[tot]=g[x]; g[x]=tot;
    to[++tot]=x; nxt[tot]=g[y]; g[y]=tot;
}
void build(int i,int l,int r) {
    if (l==r) {
        w[l]=i;
        return;
    }
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
}
void Max(int &x,int y) {if (x<y) x=y;}
int ask(int i,int l,int r,int x,int y) {
    if (x<=l&&y>=r) return ma[i];
    if (!ma[i]) return 0;
    int mid=(l+r)>>1,t=0;
    if (x<=mid) Max(t,ask(i<<1,l,mid,x,y));
    if (y>mid) Max(t,ask(i<<1|1,mid+1,r,x,y));
    return t;
}
void modify(int x,int y) {
    ma[x]=y; for (x>>=1;x;x>>=1) ma[x]=max(ma[x<<1],ma[x<<1|1]);
}
void dfs(int x,int fat) {
    int k,t=ask(1,1,top,v[x]+1,top)+1,tmp=ma[w[v[x]]];
    if (ma[w[v[x]]]<t) modify(w[v[x]],t);
    Max(ans,t);
    for (k=g[x];k;k=nxt[k]) if (to[k]!=fat) dfs(to[k],x);
    if (ma[w[v[x]]]!=tmp) modify(w[v[x]],tmp);
}
int check(int l,int r,int x) {
    int mid;
    while (l<r) {
        mid=(l+r)>>1;
        if (b[mid]<x) l=mid+1;
        else r=mid;
    }
    return r;
}
int main() {
    int i,x,y;
    scanf("%d",&n);
    for (i=1;i<=n;i++) {
        scanf("%d",&v[i]);
        b[++cnt]=v[i];
    }
    sort(b+1,b+1+cnt);
    for (i=1;i<=n;i++) v[i]=check(1,n,v[i]);
    top=n+1;
    for (i=1;i<n;i++) {
        scanf("%d%d",&x,&y);
        addedge(x,y);
    }
    build(1,1,top);
    for (i=1;i<=n;i++) dfs(i,0);
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <bitset>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <ctime>
#include <cmath>
#include <list>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int,int> pr;
const int infi=1000000000;
const int mod=1000000007;
const D eps=1e-6;
const int N=100010;
const int M=1000100;
int top,n,v[N],dp[N];
int g[N],to[N],nxt[N],tot,ans;
inline void addedge(int x,int y) {
    to[++tot]=y; nxt[tot]=g[x]; g[x]=tot;
    to[++tot]=x; nxt[tot]=g[y]; g[y]=tot;
}
void Max(int &x,int y) {if (y>x) x=y;}
void dfs(int x,int fat) {
    int k,t=lower_bound(dp,dp+1+n,v[x])-dp,tmp=dp[t];
    Max(ans,t+1); dp[t]=v[x];
    for (k=g[x];k;k=nxt[k]) if (to[k]!=fat) dfs(to[k],x);
    dp[t]=tmp;
}
int main() {
    int i,x,y;
    scanf("%d",&n);
    dp[0]=infi;
    for (i=1;i<=n;i++) {
        scanf("%d",&v[i]);
        dp[i]=infi;
    }
    for (i=1;i<n;i++) {
        scanf("%d%d",&x,&y);
        addedge(x,y);
    }
    for (i=1;i<=n;i++) dfs(i,0);
    printf("%d\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, dp[2][N + 10], a, c;
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;
}

void Dp(LL x, LL fa) {
    Edge (i, x)
        if (pt[i] != fa) Dp(pt[i], x);
    LL F = 0, S = 0, tF = 0;
    Edge (i, x)
        if (pt[i] != fa) {
            LL tmx = 0;
            FOR (j, 1, n) {
                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 (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);
    ans = max(ans, F + 1);
    Edge (i, x)
        if (pt[i] != fa) {
            mx = 0;
            FOR (j, 1, a[x] - 1) mx = max(mx, dp[0][pt[i]][j]);
            if (pt[i] == tF) ans = max(ans, mx + S + 1); else ans = max(ans, mx + F + 1);
        }
}

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;

    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;
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);
    LL F = 0, S = 0, tF = 0;
    Edge (i, x)
        if (pt[i] != fa) {
            LL tmx = 0;
            FOR (j, 1, n) {
                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 (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);
    ans = max(ans, F + 1);
    Edge (i, x)
        if (pt[i] != fa) {
            mx = 0;
            FOR (j, 1, a[x] - 1) mx = max(mx, dp[0][pt[i]][j]);
            if (pt[i] == tF) ans = max(ans, mx + S + 1); else ans = max(ans, mx + F + 1);
        }
}

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;

    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;
    Edge (i, x)
        if (pt[i] != fa) {
            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];
            }
        }
    LL F = 0, S = 0, tF = 0;
    FOR (i, a[x] + 1, cnt) if (c[i] > F) S = F, F = c[i], tF = t[i];
    else if (c[i] > S) S = c[i];
    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;
    Edge (i, x)
        if (pt[i] != fa) {
            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];
            }
        }
    LL F = 0, S = 0, tF = 0;
    FOR (i, a[x] + 1, cnt) if (c[i] > F) S = F, F = c[i], tF = t[i];
    else if (c[i] > S && t[i] != tF) S = c[i];
    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;
    Edge (i, x)
        if (pt[i] != fa) {
            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];
            }
        }
    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]);
            }
        }
    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;
}