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

#define ll long long
#define FOR(i, st, en) for (int i=st;i<=en;++i)
#define FORD(i, st, en) for (int i=st;i>=en;--i)
#define REP(i, st, en) for (int i=st;i<en;++i)
#define REPD(i, st, en) for (int i=st;i>en;--i)
#define maxn 30005

int n, a[maxn], st[4*maxn];
int ans[maxn], res;

void update(int id, int left, int right, int pos, int val) {
	if (pos < left || pos > right) return ;
	if (left == right) {
		st[id] = val;
		return ;
	}

	int mid = left+(right-left)/2;
	update(2*id, left, mid, pos, val);
	update(2*id+1, mid+1, right, pos, val);
	st[id] = max(st[2*id], st[2*id+1]);
}

int get(int id, int left, int right, int u, int v) {
	if (v < left || u > right) return 0;
	if (u <= left && right <= v) return st[id];

	int mid = left+(right-left)/2;
	return max(
		get(2*id, left, mid, u, v),
		get(2*id+1, mid+1, right, u, v)
	);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);

	cin >> n;
	FOR(i, 1, n) cin >> a[i];
	FOR(i, 1, n) {
		ans[a[i]] = get(1, 1, n, 1, a[i]-1) + 1;
		update(1, 1, n, a[i], ans[a[i]]);
		res = max(res, ans[a[i]]);
	}
	cout << res << endl;
	return 0;
}


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

#define ll long long
#define FOR(i, st, en) for (int i=st;i<=en;++i)
#define FORD(i, st, en) for (int i=st;i>=en;--i)
#define REP(i, st, en) for (int i=st;i<en;++i)
#define REPD(i, st, en) for (int i=st;i>en;--i)
#define maxn 100005

int n, a[maxn], bit[maxn];
int ans[maxn], res;

void update(int pos, int val) {
	for (pos;pos<=n;pos+=pos&-pos) bit[pos] = max(bit[pos], val);
}

int get(int pos) {
	int tmp = 0;
	for (pos;pos>=1;pos-=pos&-pos) tmp = max(tmp, bit[pos]);
	return tmp;
}

int getrange(int left, int right) {
	return get(right) - get(left-1);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);

	cin >> n;
	FOR(i, 1, n) cin >> a[i];
	FOR(i, 1, n) {
		ans[a[i]] = getrange(1, a[i]-1) + 1;
		update(a[i], ans[a[i]]);
		res = max(res, ans[a[i]]);
	}
	cout << res << endl;
	return 0;
}


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

#define F first
#define S second

int main(){
	int n=0;
	cin >> n;
	vector <int > a (n);
	vector <int > used (n);
	for (int i=0;i<n;i++){
		cin >> a[i];
	}
	int curr=0,ans=0;
/*	int df=0,dt=0;
	map <int,bool> m;
	for (int i=0;i<n;i++){
		if (!m.empty() && m.count(i+1)){
			cout << "! ";
			if (m[i+1]) dt--;
			else df--;
		}
		if (a[i]!=i+1+dt-df){
			if (a[i]<i+1+dt-df){
				cout << "< ";
				df++;
				m[a[i]]=false;
			} else {
				cout << "> ";
				dt++;
				m[a[i]]=true;
			}
		}
		if (dt!=0 || df!=0) used[i]=true;
	}  */
	int d=0;
	set <int > s;
	for (int i=0;i<n;i++){
		if (!s.empty() && s.count(i+1)){
			if (a[i]!=i+1-d){
				d+=2;
				s.insert(a[i]);
			}
			s.erase(i+1);
			if (s.empty()) d=0;
			else d--;
			used[i]=true;
//			cout << d << ' ';
			continue;
		}
		if (a[i]!=i+1-d){
			s.insert(a[i]);
			d++;
		}
		if (d>0) used[i]=true;
//		cout << d << ' ';
	}
	cout << endl;
	for (int i=0;i<n;i++){
		if (used[i]==0){
			ans++;
			ans+=curr/2+curr%2;
			curr=0;
		} else {
			curr++;
		}
	}
/*	for (int i=0;i<n;i++){
		if (used[i]) cout << "1 ";
		else cout << "0 ";
	}
	cout << endl;
	*/
	ans+=curr/2+curr%2;
	cout << ans;
}


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

#define F first
#define S second

int main(){
	int n=0;
	cin >> n;
	vector <int > a (n);
	vector <int > used (n);
	for (int i=0;i<n;i++){
		cin >> a[i];
	}
	int curr=0;
	int ans=0;
	for (int i=0;i<n;i++){
		if (a[i]!=i+1) used[i]=1;
	}
	for (int i=0;i<n;i++){
		if (used[i]==0){
			ans++;
			ans+=curr/2+curr%2;
			curr=0;
		} else {
			curr++;
		}
	}
	for (int i=0;i<n;i++) cout << used[i] << ' ';
	ans+=curr/2+curr%2;
	cout << endl;
	cout << ans;
}


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

#define F first
#define S second

int main(){
         int n=0;
         cin >> n;
         vector <int > a (n);
         for (int i=0;i<n;i++) cin >> a[i];
         vector <pair <int,int > > seq;
         for (int i=0;i<n;i++){
                  int len=0;
                  for (int j=0;j<(int)seq.size();j++) if (seq[j].F<a[i]) len=max(len,seq[j].S);
                  seq.push_back({a[i],len+1});
         }
         int ans=0;
         for (int i=0;i<n;i++) ans=max(ans,seq[i].S);
         cout << ans;
}


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

#define F first
#define S second
#define INF 1000009

int main(){
         int n=0;
         cin >> n;
         vector <int > a (n);
         for (int i=0;i<n;i++) cin >> a[i];
//         stable_sort(a.begin(),a.end());
         vector <int > dp (n+1,INF);
         dp[0]=-INF;
         for (int i=0;i<n;i++){
         	int j=(int)(upper_bound(dp.begin(),dp.end(),a[i])-dp.begin());
         	if (dp[j-1]<a[i] && a[i]<dp[j]) dp[j]=a[i];
         }
         int ans=0;
         for (int i=1;i<=n;i++){
                  if (dp[i]!=INF) ans=i;
         }
         cout << ans;
}


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

#define F first
#define S second

int main(){
	int n=0;
	cin >> n;
	vector <int > a (n);
	vector <int > used (n);
	for (int i=0;i<n;i++){
		cin >> a[i];
	}
	int curr=0;
	int ans=0;
	for (int i=0;i<n;i++){
		if (a[i]!=i+1) used[i]=1;
	}
	for (int i=0;i<n;i++){
		if (used[i]==0){
			ans++;
			ans+=curr/2+curr%2;
			curr=0;
		} else {
			curr++;
		}
	}
//	for (int i=0;i<n;i++) cout << used[i] << ' ';
	ans+=curr/2+curr%2;
//	cout << endl;
	cout << ans;
}


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

#define F first
#define S second

int main(){
	int n=0;
	cin >> n;
	vector <int > a (n);
	vector <int > used (n);
	for (int i=0;i<n;i++){
		cin >> a[i];
	}
	int curr=0,ans=0;
/*	int df=0,dt=0;
	map <int,bool> m;
	for (int i=0;i<n;i++){
		if (!m.empty() && m.count(i+1)){
			cout << "! ";
			if (m[i+1]) dt--;
			else df--;
		}
		if (a[i]!=i+1+dt-df){
			if (a[i]<i+1+dt-df){
				cout << "< ";
				df++;
				m[a[i]]=false;
			} else {
				cout << "> ";
				dt++;
				m[a[i]]=true;
			}
		}
		if (dt!=0 || df!=0) used[i]=true;
	}  */
	int d=0;
	set <int > s;
	for (int i=0;i<n;i++){
		if (!s.empty() && s.count(i+1)){
			if (a[i]!=i+1-d){
				d+=2;
				s.insert(a[i]);
			}
			s.erase(i+1);
			if (s.empty()) d=0;
			else d--;
			used[i]=true;
			cout << d << ' ';
			continue;
		}
		if (a[i]!=i+1-d){
			s.insert(a[i]);
			d++;
		}
		if (d>0) used[i]=true;
		cout << d << ' ';
	}
	cout << endl;
	for (int i=0;i<n;i++){
		if (used[i]==0){
			ans++;
			ans+=curr/2+curr%2;
			curr=0;
		} else {
			curr++;
		}
	}
	for (int i=0;i<n;i++){
		if (used[i]) cout << "1 ";
		else cout << "0 ";
	}
	ans+=curr/2+curr%2;
	cout << endl;
	cout << ans;
}


                                  /* This Template is property of Pranav Khiste aka desmond_kaneki*/
#include <bits/stdc++.h>
#include<set>
using namespace std;
#define ll long long int
#define fi(x,n) for(ll i=x;i<n;i++)
#define fj(x,n) for(ll j=x;j<n;j++)
#define vll vector<ll>
#define pb push_back
#define qll queue<ll>
#define p push
#define mp make_pair
#define fst first
#define snd second
#define pll pair<ll,ll>
#define vpll vector<pll>
#define debug(x) cout<<#x<<' '<<x<<endl;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define mod 1000000007
#define SUP LLONG_MAX
#define INF LLONG_MIN
#define test() ll var;  cin>>var; while(var--)
#define fileio freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout);  // use cin cout only!!
main()
{
    fastio
    ll n,x;
    cin>>n;
    ll ar[n],ans=0;
    vll temp;
    fi(0,n)
    {
        cin>>ar[i];
        auto it=upper_bound(temp.begin(),temp.end(),ar[i]);
        if(it==temp.end())
            temp.pb(ar[i]);
        else
            *it=ar[i];
    }
    cout<<temp.size();
}


                                  /* This Template is property of Pranav Khiste aka desmond_kaneki*/
#include <bits/stdc++.h>
#include<set>
using namespace std;
#define ll long long int
#define fi(x,n) for(ll i=x;i<n;i++)
#define fj(x,n) for(ll j=x;j<n;j++)
#define vll vector<ll>
#define pb push_back
#define qll queue<ll>
#define p push
#define mp make_pair
#define fst first
#define snd second
#define pll pair<ll,ll>
#define vpll vector<pll>
#define debug(x) cout<<#x<<' '<<x<<endl;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define mod 1000000007
#define SUP LLONG_MAX
#define INF LLONG_MIN
#define test() ll var;  cin>>var; while(var--)
#define fileio freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout);  // use cin cout only!!
main()
{
    fastio
    ll n;
    cin>>n;
    ll ar[n],m=-1;
    fi(0,n)
    {
        cin>>ar[i];
    }
    set<pll> st;
    for(ll i=n-1;i>=0;i--)
    {
        ll pl=0;
        auto it=lower_bound(st.begin(),st.end(),mp(ar[i],INF));
        if(it==st.end())
            pl--;
        else
        {
            pl=it->snd-1;
        }

        st.insert(mp(ar[i],pl));
        m=max(m,-pl);
    }
    cout<<m<<endl;
}


                                  /* This Template is property of Pranav Khiste aka desmond_kaneki*/
#include <bits/stdc++.h>
#include<set>
using namespace std;
#define ll long long int
#define fi(x,n) for(ll i=x;i<n;i++)
#define fj(x,n) for(ll j=x;j<n;j++)
#define vll vector<ll>
#define pb push_back
#define qll queue<ll>
#define p push
#define mp make_pair
#define fst first
#define snd second
#define pll pair<ll,ll>
#define vpll vector<pll>
#define debug(x) cout<<#x<<' '<<x<<endl;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define mod 1000000007
#define SUP LLONG_MAX
#define INF LLONG_MIN
#define test() ll var;  cin>>var; while(var--)
#define fileio freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout);  // use cin cout only!!
ll CeilIndex(std::vector<ll>& v,ll l,ll r,ll key)
{
    while (r - l > 1) {
        ll m = l + (r - l) / 2;
        if (v[m] >= key)
            r = m;
        else
            l = m;
    }

    return r;
}

ll LongestIncreasingSubsequenceLength(std::vector<ll>& v)
{
    if (v.size() == 0)
        return 0;

    std::vector<ll> tail(v.size(), 0);
    ll length = 1; // always points empty slot in tail

    tail[0] = v[0];
    for (size_t i = 1; i < v.size(); i++) {

        // new smallest value
        if (v[i] < tail[0])
            tail[0] = v[i];

        // v[i] extends largest subsequence
        else if (v[i] > tail[length - 1])
            tail[length++] = v[i];

        // v[i] will become end candidate of an existing
        // subsequence or Throw away larger elements in all
        // LIS, to make room for upcoming grater elements
        // than v[i] (and also, v[i] would have already
        // appeared in one of LIS, identify the location
        // and replace it)
        else
            tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i];
    }

    return length;
}
main()
{
    fastio
    ll n,x;
    cin>>n;
    vll v;
    fi(0,n)
    {
        cin>>x;
        v.pb(x);
    }
    cout<<LongestIncreasingSubsequenceLength(v)<<endl;
}


                                  /* This Template is property of Pranav Khiste aka desmond_kaneki*/
#include <bits/stdc++.h>
#include<set>
using namespace std;
#define ll long long int
#define fi(x,n) for(ll i=x;i<n;i++)
#define fj(x,n) for(ll j=x;j<n;j++)
#define vll vector<ll>
#define pb push_back
#define qll queue<ll>
#define p push
#define mp make_pair
#define fst first
#define snd second
#define pll pair<ll,ll>
#define vpll vector<pll>
#define debug(x) cout<<#x<<' '<<x<<endl;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define mod 1000000007
#define SUP LLONG_MAX
#define INF LLONG_MIN
#define test() ll var;  cin>>var; while(var--)
#define fileio freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout);  // use cin cout only!!
main()
{
    fastio
    ll n;
    cin>>n;
    ll ar[n],ans[n+1],m=-1;
    fi(0,n)
    {
        cin>>ar[i];
        ans[i+1]=1;
    }
    set<ll> st;
    st.insert(ar[n-1]);
    for(ll i=n-2;i>=0;i--)
    {
        auto z=lower_bound(st.begin(),st.end(),ar[i]);
        ll pl=distance(z,st.end());
        ans[ar[i]]+=pl;
        st.insert(ar[i]);
        m=max(m,ans[ar[i]]);
    }
    cout<<m<<endl;
}


/*
Source code by -
 __        ______
 \ \      / /  _ \ _ __ __ _  __ _  ___  _ __
  \ \ /\ / /| | | | '__/ _` |/ _` |/ _ \| '_ \
   \ V  V / | |_| | | | (_| | (_| | (_) | | | |
    \_/\_/  |____/|_|  \__,_|\__, |\___/|_| |_|
                             |___/
*/
#include<bits/stdc++.h>
#define ll long long
#define MAX 1000003
#define pii pair<int,int>
#define VP vector< pii >
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
#define S(x) scanf("%d",&(x))
#define S2(x,y) scanf("%d%d",&(x),&(y))
#define P(x) printf("%d\n",(x))
#define all(v) (v).begin(),(v).end()
#define FF first
#define SS second
using namespace std;
//int dx[]={1,0,0,-1},dy[]={0,1,-1,0};
int a[MAX],last[MAX];
int nextIndex(int h,int val){
	int l=0,mid;
	while(l < h){
		mid = (l+h+1)>>1;
		if(last[mid] <= val)
			l=mid;
		else
			h=mid-1;
	}
		return l;
}
int lis(int n){
	int len=1;
	last[0]=a[0];
	for(int i=1;i<n;++i){
		if(a[i] > last[len - 1]){
			last[len++] = a[i];
		}
		if(a[i] <= last[0])
			last[0] = a[i];
		else{
			int p = nextIndex(len - 1, a[i]);
			last[p] = a[i];
		}
	}
	return len;
}
int main(){
int n;
S(n);
for(int i=0;i<n;++i){
	scanf("%d",&a[i]);
}
int ans = lis(n);
printf("%d\n",ans);
return 0;
}


/*
Source code by -
 __        ______
 \ \      / /  _ \ _ __ __ _  __ _  ___  _ __
  \ \ /\ / /| | | | '__/ _` |/ _` |/ _ \| '_ \
   \ V  V / | |_| | | | (_| | (_| | (_) | | | |
    \_/\_/  |____/|_|  \__,_|\__, |\___/|_| |_|
                             |___/
*/
#include<bits/stdc++.h>
#define ll long long
#define MAX 1000003
#define pii pair<int,int>
#define VP vector< pii >
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
#define S(x) scanf("%d",&(x))
#define S2(x,y) scanf("%d%d",&(x),&(y))
#define P(x) printf("%d\n",(x))
#define all(v) (v).begin(),(v).end()
#define FF first
#define SS second
using namespace std;
//int dx[]={1,0,0,-1},dy[]={0,1,-1,0};
int a[MAX],last[MAX];
int nextIndex(int h,int val){
	int l=0,mid;
	while(l < h){
		mid = (l+h+1)>>1;
		if(last[mid] <= val)
			l=mid;
		else
			h=mid-1;
	}
		return l;
}
int lis(int n){
	int len=1;
	last[0]=a[0];
	for(int i=1;i<n;++i){
		if(a[i] > last[len - 1]){
			last[len++] = a[i];
		}
		else if(a[i] <= last[0])
			last[0] = a[i];
		else{
			int p = nextIndex(len - 1, a[i]);
			last[p] = a[i];
		}
	}
	return len;
}
int main(){
int n;
S(n);
for(int i=0;i<n;++i){
	scanf("%d",&a[i]);
}
int ans = lis(n);
printf("%d\n",ans);
return 0;
}


/*
Source code by -
 __        ______
 \ \      / /  _ \ _ __ __ _  __ _  ___  _ __
  \ \ /\ / /| | | | '__/ _` |/ _` |/ _ \| '_ \
   \ V  V / | |_| | | | (_| | (_| | (_) | | | |
    \_/\_/  |____/|_|  \__,_|\__, |\___/|_| |_|
                             |___/
*/
#include<bits/stdc++.h>
#define ll long long
#define MAX 1000003
#define pii pair<int,int>
#define VP vector< pii >
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
#define S(x) scanf("%d",&(x))
#define S2(x,y) scanf("%d%d",&(x),&(y))
#define P(x) printf("%d\n",(x))
#define all(v) (v).begin(),(v).end()
#define FF first
#define SS second
using namespace std;
//int dx[]={1,0,0,-1},dy[]={0,1,-1,0};
int a[MAX],last[MAX];
int nextIndex(int h,int val){
	int l=0,mid;
	while(l < h){
		mid = (l+h+1)>>1;
		if(last[mid] <= val)
			l=mid;
		else
			h=mid-1;
	}
		return l;
}
int lis(int n){
	int len=1;
	last[0]=a[0];
	for(int i=1;i<n;++i){
		if(a[i] > last[len - 1]){
			last[len++] = a[i];
		}
		if(a[i] <= a[0])
			last[0] = a[i];
		else{
			int p = nextIndex(len - 1, a[i]);
			last[p] = a[i];
		}
	}
	return len;
}
int main(){
int n;
S(n);
for(int i=0;i<n;++i){
	scanf("%d",&a[i]);
}
int ans = lis(n);
printf("%d\n",ans);
return 0;
}


/*
Source code by -
 __        ______
 \ \      / /  _ \ _ __ __ _  __ _  ___  _ __
  \ \ /\ / /| | | | '__/ _` |/ _` |/ _ \| '_ \
   \ V  V / | |_| | | | (_| | (_| | (_) | | | |
    \_/\_/  |____/|_|  \__,_|\__, |\___/|_| |_|
                             |___/
*/
#include<bits/stdc++.h>
#define ll long long
#define MAX 100003
#define pii pair<int,int>
#define VP vector< pii >
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
#define S(x) scanf("%d",&(x))
#define S2(x,y) scanf("%d%d",&(x),&(y))
#define P(x) printf("%d\n",(x))
#define all(v) (v).begin(),(v).end()
#define FF first
#define SS second
using namespace std;
//int dx[]={1,0,0,-1},dy[]={0,1,-1,0};
int a[MAX],last[MAX];
int nextIndex(int h,int val){
	int l=0,mid;
	while(h - l > 1){
		mid = (l+h)>>1;
		if(last[mid] >= val)
			h = mid;
		else
			l=mid;
	}
		return h;
}
int lis(int n){
	int len=1;
	last[0]=a[0];
	for(int i=1;i<n;++i){
		if(a[i] > last[len - 1]){
			last[len++] = a[i];
		}
		else if(a[i] <= last[0])
			last[0] = a[i];
		else{
			int p = nextIndex(len - 1, a[i]);
			last[p] = a[i];
		}
	}
	return len;
}
int main(){
int n;
S(n);
for(int i=0;i<n;++i){
	scanf("%d",&a[i]);
}
int ans = lis(n);
printf("%d\n",ans);
return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
#define MOD 1000000007
using namespace std;
double PI=4.00*(atan(1));

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin>>n;
    ll val1[n+1];
    for(ll i=1;i<=n;i++)
    {
        cin>>val1[i];
    }
    ll dp[n+1];
    dp[1]=1;
    for(ll i=2;i<=n;i++)
    {
        dp[i]=1;
        for(ll j=i-1;j>=1;j--)
        {
            if(val1[i]>val1[j])
            {
                dp[i]=max(dp[i],dp[j]+1);
            }
        }
    }
    ll ans=-1;
    for(ll i=1;i<=n;i++)
    {
        ans=max(ans,dp[i]);
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
#define MOD 1000000007
using namespace std;
double PI=4.00*(atan(1));

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin>>n;
    ll val1[n+1];
    for(ll i=1;i<=n;i++)
    {
        cin>>val1[i];
    }
    ll dp[n+1];
    dp[1]=1;
    for(ll i=2;i<=n;i++)
    {
        dp[i]=1;
        for(ll j=i-1;j>=1;j--)
        {
            if(val1[i]>val1[j])
            {
                if(dp[i]<dp[j]+1)
                {
                    dp[i]=dp[j]+1;
                }
                else
                {
                    break;
                }
            }
        }
    }
    ll ans=-1;
    for(ll i=1;i<=n;i++)
    {
        ans=max(ans,dp[i]);
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
#define MOD 1000000007
using namespace std;
double PI=4.00*(atan(1));

ll ceilfind(ll endlen[], ll start, ll end, ll key)
{
    ll mid;
    while((end-start)>1)
    {
        mid=(end+start)/2;
        if(endlen[mid]>=key)
        {
            end=mid;
        }
        else
        {
            start=mid;
        }
    }
    return end;
}

ll LIS(ll arr1[], ll n)
{
    ll endpos[n];
    for(ll i=1;i<n;i++)
    {
        endpos[i]=-1;
    }
    endpos[0]=arr1[0];
    ll currend=0;
    for(ll i=1;i<n;i++)
    {
        if(arr1[i]<endpos[0])
        {
            endpos[0]=arr1[i];
            continue;
        }
        if(arr1[i]>endpos[currend])
        {
            currend++;
            endpos[currend]=arr1[i];
            continue;
        }
        ll val1=ceilfind(endpos,-1,currend-1,arr1[i]);
        endpos[val1]=arr1[i];
    }
    return currend+1;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin>>n;
    ll arr1[n];
    for(ll i=0;i<n;i++)
    {
        cin>>arr1[i];
    }
    ll ans=LIS(arr1,n);
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
#define MOD 1000000007
using namespace std;
double PI=4.00*(atan(1));

ll ceilfind(ll endlen[], ll start, ll end, ll key)
{
    ll mid;
    while((end-start)>1)
    {
        mid=(end+start)/2;
        if(endlen[mid]>=key)
        {
            end=mid;
        }
        else
        {
            start=mid;
        }
    }
    return end;
}

ll LIS(ll arr1[], ll n)
{
    ll endpos[n];
    for(ll i=1;i<n;i++)
    {
        endpos[i]=-1;
    }
    endpos[0]=arr1[0];
    ll currend=0;
    for(ll i=1;i<n;i++)
    {
        if(arr1[i]<endpos[0])
        {
            endpos[0]=arr1[i];
            continue;
        }
        if(arr1[i]>endpos[currend])
        {
            currend++;
            endpos[currend]=arr1[i];
            continue;
        }
        ll val1=ceilfind(endpos,-1,currend,arr1[i]);
        endpos[val1]=arr1[i];
    }
    return currend+1;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin>>n;
    ll arr1[n];
    for(ll i=0;i<n;i++)
    {
        cin>>arr1[i];
    }
    ll ans=LIS(arr1,n);
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
#define MOD 1000000007
using namespace std;
double PI=4.00*(atan(1));

ll ceilfind(ll endlen[], ll start, ll end, ll key)
{
    ll mid;
    while((end-start)>1)
    {
        mid=(end+start)/2;
        if(endlen[mid]>=key)
        {
            end=mid;
        }
        else
        {
            start=mid;
        }
    }
    return end;
}

ll LIS(ll arr1[], ll n)
{
    ll endpos[n];
    for(ll i=1;i<n;i++)
    {
        endpos[i]=-1;
    }
    endpos[0]=arr1[0];
    ll currend=0;
    for(ll i=1;i<n;i++)
    {
        if(arr1[i]<endpos[0])
        {
            endpos[0]=arr1[i];
            continue;
        }
        if(arr1[i]>endpos[currend])
        {
            currend++;
            endpos[currend]=arr1[i];
            continue;
        }
        ll val1=ceilfind(endpos,-1,currend-1,arr1[i]);
        endpos[val1]=arr1[i];
    }
    for(ll i=n-1;i>=0;i--)
    {
        if(endpos[i]!=-1)
        {
            return i+1;
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin>>n;
    ll arr1[n];
    for(ll i=0;i<n;i++)
    {
        cin>>arr1[i];
    }
    ll ans=LIS(arr1,n);
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define PB push_back
#define POB pop_back
#define MP make_pair
#define FI first
#define SE second
#define REPP(i,a,b,c) for(int i=a; i<=b; i+=c)
#define REP(i,a,b) REPP(i,a,b,1)
#define REVV(i,a,b,c) for(int i=a; i>=b; i-=c)
#define REV(i,a,b) REVV(i,a,b,1)
#define FOR(i,a) REP(i,0,a-1)
#define FORD(i,a) REV(i,a-1,0)
typedef pair < int , int > ii;
typedef pair < int , ii > iii;
typedef vector < int > vi;
typedef vector < ii > vii;

const int N = 100005;

int main(){
	int n, a;
	cin >> n;
	int ans = 0;
	REP(i,1,n){
		cin >> a;
		ans = max(ans, abs(i-a));
	}
	ans = min(ans,n/2+1);
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define PB push_back
#define POB pop_back
#define MP make_pair
#define FI first
#define SE second
#define REPP(i,a,b,c) for(int i=a; i<=b; i+=c)
#define REP(i,a,b) REPP(i,a,b,1)
#define REVV(i,a,b,c) for(int i=a; i>=b; i-=c)
#define REV(i,a,b) REVV(i,a,b,1)
#define FOR(i,a) REP(i,0,a-1)
#define FORD(i,a) REV(i,a-1,0)
typedef pair < int , int > ii;
typedef pair < int , ii > iii;
typedef vector < int > vi;
typedef vector < ii > vii;

const int N = 1e5 + 5;

int n, a[N];
int arr[N];

int binser(int sz, int v){
	int l = 0, r = sz-1, mid, ret = 0;
	while(l <= r){
		mid = (l+r)/2;
		if(arr[mid] > v){
			r = mid - 1;
			ret = mid;
		}
		else{
			l = mid + 1;

		}
	}
	return ret;
}

int LIS(){
	int sz = 0;
	REP(i,1,n){
		if(sz == 0 || arr[sz-1] < a[i]){
			arr[sz++] = a[i];
			continue;
		}
		arr[binser(sz,a[i])] = a[i];
	}
//	FOR(i,sz) printf("%d ",arr[i]);
//	puts("");
	return sz;
}

int main(){
	cin >> n;
	REP(i,1,n) cin >> a[i];
	cout << LIS() << endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define PB push_back
#define POB pop_back
#define MP make_pair
#define FI first
#define SE second
#define REPP(i,a,b,c) for(int i=a; i<=b; i+=c)
#define REP(i,a,b) REPP(i,a,b,1)
#define REVV(i,a,b,c) for(int i=a; i>=b; i-=c)
#define REV(i,a,b) REVV(i,a,b,1)
#define FOR(i,a) REP(i,0,a-1)
#define FORD(i,a) REV(i,a-1,0)
typedef pair < int , int > ii;
typedef pair < int , ii > iii;
typedef vector < int > vi;
typedef vector < ii > vii;

const int N = 100005;

int main(){
	int n, a;
	cin >> n;
	int ans = 0;
	REP(i,1,n){
		cin >> a;
		ans = max(ans, abs(i-a));
	}
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define PB push_back
#define POB pop_back
#define MP make_pair
#define FI first
#define SE second
#define REPP(i,a,b,c) for(int i=a; i<=b; i+=c)
#define REP(i,a,b) REPP(i,a,b,1)
#define REVV(i,a,b,c) for(int i=a; i>=b; i-=c)
#define REV(i,a,b) REVV(i,a,b,1)
#define FOR(i,a) REP(i,0,a-1)
#define FORD(i,a) REV(i,a-1,0)
typedef pair < int , int > ii;
typedef pair < int , ii > iii;
typedef vector < int > vi;
typedef vector < ii > vii;

const int N = 1e5 + 5;

int n, a[N];
int arr[N];

int binser(int sz, int v){
	int l = 0, r = sz-1, mid, ret = 0;
	while(l <= r){
		mid = (l+r)/2;
		if(arr[mid] > v){
			r = mid - 1;
		}
		else{
			l = mid + 1;
			ret = mid;
		}
	}
	return ret;
}

int LIS(){
	int sz = 0;
	REP(i,1,n){
		if(sz == 0 || arr[sz-1] < a[i]){
			arr[sz++] = a[i];
			continue;
		}
		arr[binser(sz,a[i])] = a[i];
	}
	return sz;
}

int main(){
	cin >> n;
	REP(i,1,n) cin >> a[i];
	cout << LIS() << endl;
	return 0;
}


/* code by AbhiTaker
         ____
        /    \
       /  __  \              _______      __       _   _    _____    _____
      /  /  \  \            |__   __|    /  \     | | / /  |  ___|  |  _  \
     /  /____\  \     ___      | |      / /\ \    | |/ /   | |__    | |_)  )
    /  /______\  \   |___|     | |     / /__\ \   |   /    |  __|   |    _/
   /  /        \  \            | |    / /----\ \  | |\ \   | |___   | |\ \
  /__/          \__\           |_|   /_/      \_\ |_| \_\  |_____|  |_| \_\
*/
#include<bits/stdc++.h>
using namespace std;
typedef  long long ll;
typedef unsigned long long ull;
#define M 1000000007
#define N 1000016
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define f first
#define s second
#define inf 9000000000000000000
ll tree[N],a[N],n,d[N];
set<ll> myset;

void build(ll node, ll start, ll end)
{
    if(start==end)
    {
        tree[node] = a[start];
        return;
    }

    ll mid = (start + end)/2;
    build(2*node, start, mid);
    build(2*node+1, mid+1, end);

    tree[node] = max(tree[2*node], tree[2*node+1]);
}

ll query(ll node, ll start, ll end, ll l, ll r, ll val)
{
    if(start>end || l>end || start>r || tree[node]<val)
        return -1;
    if(start == end)
        return start;

    ll mid = (start + end)/2;
    ll p1=-1;

    if(tree[2*node] >= val)
        p1 = query(2*node, start, mid, l, r, val);
    if(p1==-1)
        p1 = query(2*node+1, mid+1, end, l, r, val);

    return p1;
}

ll dp(ll ind)
{
    myset.erase(ind);
    if(ind==n)
        return 1;
    if(d[ind])
        return d[ind];

    ll l = ind+1;
    d[ind] = 1;
    while(l<=n)
    {
        l = query(1, 1, n, l, n, a[ind]);
        if(l==-1)
            break;
        d[ind] = max(d[ind], 1 + dp(l));
        l++;
    }

    return d[ind];
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll i,ans = 0;
    cin>>n;

    for(i=1; i<=n; i++)
    {
        cin>>a[i];
        myset.insert(i);
    }

    build(1, 1, n);

    while(!myset.empty())
        ans = max(ans, dp(*myset.begin()));

    cout<<ans<<"\n";

    return 0;
}


/* code by AbhiTaker
         ____
        /    \
       /  __  \              _______      __       _   _    _____    _____
      /  /  \  \            |__   __|    /  \     | | / /  |  ___|  |  _  \
     /  /____\  \     ___      | |      / /\ \    | |/ /   | |__    | |_)  )
    /  /______\  \   |___|     | |     / /__\ \   |   /    |  __|   |    _/
   /  /        \  \            | |    / /----\ \  | |\ \   | |___   | |\ \
  /__/          \__\           |_|   /_/      \_\ |_| \_\  |_____|  |_| \_\
*/
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
typedef unsigned long long ull;
#define M 1000000007
#define N 1000016
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define f first
#define s second
#define inf 9000000000000000000
vector<ll> v;
vector<ll>::iterator it;

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll i,x,n;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>x;
        if(v.empty())
            v.push_back(x);
        else if(x >= v[v.size()-1])
            v.push_back(x);
        else if(x < v[v.size()-1])
        {
            it = upper_bound(v.begin(), v.end(), x);
            *it = x;
        }
    }
    cout<<v.size()<<"\n";
    return 0;
}


/* code by AbhiTaker
         ____
        /    \
       /  __  \              _______      __       _   _    _____    _____
      /  /  \  \            |__   __|    /  \     | | / /  |  ___|  |  _  \
     /  /____\  \     ___      | |      / /\ \    | |/ /   | |__    | |_)  )
    /  /______\  \   |___|     | |     / /__\ \   |   /    |  __|   |    _/
   /  /        \  \            | |    / /----\ \  | |\ \   | |___   | |\ \
  /__/          \__\           |_|   /_/      \_\ |_| \_\  |_____|  |_| \_\
*/
#include<bits/stdc++.h>
using namespace std;
typedef  long long ll;
typedef unsigned long long ull;
#define M 1000000007
#define N 1000016
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define f first
#define s second
#define inf 9000000000000000000
ll tree[N],a[N],n,d[N];

void build(ll node, ll start, ll end)
{
    if(start==end)
    {
        tree[node] = a[start];
        return;
    }

    ll mid = (start + end)/2;
    build(2*node, start, mid);
    build(2*node+1, mid+1, end);

    tree[node] = max(tree[2*node], tree[2*node+1]);
}

ll query(ll node, ll start, ll end, ll l, ll r, ll val)
{
    if(start>end || l>end || start>r || tree[node]<val)
        return -1;
    if(start == end)
        return start;

    ll mid = (start + end)/2;
    ll p1=-1;

    if(tree[2*node] >= val)
        p1 = query(2*node, start, mid, l, r, val);
    if(p1==-1)
        p1 = query(2*node+1, mid+1, end, l, r, val);

    return p1;
}

ll dp(ll ind)
{
    if(ind==n)
        return 1;
    if(d[ind])
        return d[ind];

    ll l = ind+1;

    if(ind!=0)
        d[ind] = 1;
    while(l<=n)
    {
        l = query(1, 1, n, l, n, a[ind]);
        if(l==-1)
            break;
        if(ind!=0)
            d[ind] = max(d[ind], 1 + dp(l));
        else
            d[ind] = max(d[ind], dp(l));
        l++;
    }

    return d[ind];
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll i,ans = 0;
    cin>>n;

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

    build(1, 1, n);

    ans = dp(0);

    cout<<ans<<"\n";

    return 0;
}


/* code by AbhiTaker
         ____
        /    \
       /  __  \              _______      __       _   _    _____    _____
      /  /  \  \            |__   __|    /  \     | | / /  |  ___|  |  _  \
     /  /____\  \     ___      | |      / /\ \    | |/ /   | |__    | |_)  )
    /  /______\  \   |___|     | |     / /__\ \   |   /    |  __|   |    _/
   /  /        \  \            | |    / /----\ \  | |\ \   | |___   | |\ \
  /__/          \__\           |_|   /_/      \_\ |_| \_\  |_____|  |_| \_\
*/
#include<bits/stdc++.h>
using namespace std;
typedef  long long ll;
typedef unsigned long long ull;
#define M 1000000007
#define N 1000016
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define f first
#define s second
#define inf 9000000000000000000
ll tree[N],a[N],n,d[N];

void build(ll node, ll start, ll end)
{
    if(start==end)
    {
        tree[node] = a[start];
        return;
    }

    ll mid = (start + end)/2;
    build(2*node, start, mid);
    build(2*node+1, mid+1, end);

    tree[node] = max(tree[2*node], tree[2*node+1]);
}

ll query(ll node, ll start, ll end, ll l, ll r, ll val)
{
    if(start>end || l>end || start>r || tree[node]<val)
        return -1;
    if(start == end)
        return start;

    ll mid = (start + end)/2;
    ll p1=-1;

    if(tree[2*node] >= val)
        p1 = query(2*node, start, mid, l, r, val);
    if(p1==-1)
        p1 = query(2*node+1, mid+1, end, l, r, val);

    return p1;
}

ll dp(ll ind)
{
    if(ind==n)
        return 1;
    if(d[ind])
        return d[ind];

    ll l = ind+1;
    d[ind] = 1;
    while(l<=n)
    {
        l = query(1, 1, n, l, n, a[ind]);
        if(l==-1)
            break;
        d[ind] = max(d[ind], 1 + dp(l));
        l++;
    }

    return d[ind];
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll i,ans = 0;
    cin>>n;

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

    build(1, 1, n);

    for(i=1; i<n; i++)
        ans = max(ans, dp(i));

    cout<<ans<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             998244353
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define FIO             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
int fc[100001], in[100001];
vector<vector<int>> adj;
int pwmd(int a, int n)
{
	if (!n)
		return 1;
	int pt = pwmd(a, n / 2);
	pt *= pt, pt %= mod;
	if (n & 1)
		pt *= a, pt %= mod;
	return pt;
}

int32_t main()
{
	int n;
	cin>>n;
	int a[n];

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

	int dp[n];
	memset(dp,0,sizeof(dp));
	dp[0]=1;
	for(int i=1;i<n;i++)
	{
		dp[i]=1;
		for(int j=0;j<i;j++)
		{
			if(a[i]>a[j] && dp[i]<(dp[j]+1))
				dp[i]=dp[j]+1;
		}
	}

	int ans=*max_element(dp,dp+n);
	cout<<ans<<endl;

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             998244353
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define FIO             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
int fc[100001], in[100001];
vector<vector<int>> adj;
int pwmd(int a, int n)
{
	if (!n)
		return 1;
	int pt = pwmd(a, n / 2);
	pt *= pt, pt %= mod;
	if (n & 1)
		pt *= a, pt %= mod;
	return pt;
}

int a[100005];
int CeilIndex(vector<int>& v, int l, int r, int key)
{
    while (r - l > 1) {
        int m = l + (r - l) / 2;
        if (v[m] >= key)
            r = m;
        else
            l = m;
    }

    return r;
}

int LongestIncreasingSubsequenceLength(vector<int>& v)
{
    if (v.size() == 0)
        return 0;

    std::vector<int> tail(v.size(), 0);
    int length = 1; // always points empty slot in tail

    tail[0] = v[0];
    for (size_t i = 1; i < v.size(); i++) {

        // new smallest value
        if (v[i] < tail[0])
            tail[0] = v[i];

        // v[i] extends largest subsequence
        else if (v[i] > tail[length - 1])
            tail[length++] = v[i];

        // v[i] will become end candidate of an existing
        // subsequence or Throw away larger elements in all
        // LIS, to make room for upcoming grater elements
        // than v[i] (and also, v[i] would have already
        // appeared in one of LIS, identify the location
        // and replace it)
        else
            tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i];
    }

    return length;
}

int32_t main()
{
	int n;
	cin>>n;

	vector<int> v;
	for(int i=0;i<n;i++)
	{
		int x;
		cin>>x;
		v.push_back(x);
	}

	int ans=LongestIncreasingSubsequenceLength(v);
	cout<<ans<<endl;

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             998244353
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define FIO             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
int fc[100001], in[100001];
vector<vector<int>> adj;
int pwmd(int a, int n)
{
	if (!n)
		return 1;
	int pt = pwmd(a, n / 2);
	pt *= pt, pt %= mod;
	if (n & 1)
		pt *= a, pt %= mod;
	return pt;
}

int a[100005];
int32_t main()
{
	int n;
	cin>>n;

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

	int dp[n];
	memset(dp,0,sizeof(dp));
	dp[0]=1;
	for(int i=1;i<n;i++)
	{
		dp[i]=1;
		for(int j=0;j<i;j++)
		{
			if(a[i]>a[j] && dp[i]<(dp[j]+1))
				dp[i]=dp[j]+1;
		}
	}
	int ans=*max_element(dp,dp+n);
	cout<<ans<<endl;

}


/**
*
* Author: MARC
* Lang: GNU C++14
*
**/

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

#define F first
#define S second
#define ii pair < int , int >
#define LS pair < ll , string >

typedef unsigned long long ull;
typedef long double ldbl;
typedef long long ll;
typedef double dbl;

const ldbl pi = 3.1415926535897932385;
const ldbl E = 2.71828182845904523536;
const dbl eps = 1e-9;
const ll mod = 1e9+7;
const ll inf = 1<<30;

/** /////////// ** Code ** /////////// **/

const int N =100100;
int seg[N<<2];

void update(int nd,int L,int R,int idx,int val){
    if( L == R ){
        seg[nd]=val;
        return ;
    }
    int mid = (L+R) >>1;
    if( idx<=mid )
         update(nd<<1  ,  L  ,mid,idx,val);
    else update(nd<<1|1,mid+1, R ,idx,val);
    seg[nd]=max( seg[nd<<1] , seg[nd<<1|1] );
}

int query(int nd,int L,int R,int from,int to){
    if( from<=L && R<=to )return seg[nd];
    if( from>R || to<L )return -inf;

    int mid = (L+R) >>1;

    int leftAns = query(nd<<1  ,  L  ,mid,from,to);
    int rightAns= query(nd<<1|1,mid+1, R ,from,to);
    return max( leftAns ,rightAns );
}

int main(){
    int n; scanf("%d",&n);
    for (int i=0 ; i<n ; i++){
        int x; scanf("%d",&x);
        int mx = query (1,1,n,1,x);
        update (1,1,n,x,mx+1);
    }
    printf("%d", query (1,1,n,1,n));
    return 0;
}


/**
*
* Author: MARC
* Lang: GNU C++14
*
**/

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

#define F first
#define S second
#define ii pair < int , int >
#define LS pair < ll , string >

typedef unsigned long long ull;
typedef long double ldbl;
typedef long long ll;
typedef double dbl;

const ldbl pi = 3.1415926535897932385;
const ldbl E = 2.71828182845904523536;
const dbl eps = 1e-9;
const ll mod = 1e9+7;
const ll inf = 1<<30;

/** /////////// ** Code ** /////////// **/

const int N=100100;
int a[N];

vector<int>adj[N];
vector<int> even , odd;

bool vis[N];

void DFS(int u,int cnt){
    vis[u]=1;
    if(cnt&1) odd.push_back(u);
    else even.push_back(u);

    for(auto v:adj[u]) if(!vis[v]) DFS(v,cnt+1);
}

int main()
{
    int n; scanf("%d",&n);
    for(int i=1 ; i<=n ; i++) scanf("%d",&a[i]);

    for(int j=1 ; j<=n ; j++){
        for(int i=1 ; i <= n-1 ; i++){
            if(a[i] > a[i+1]){
                adj[a[i]].push_back(a[i+1]);
                adj[a[i+1]].push_back(a[i]);

                swap(a[i],a[i+1]);
            }
        }
    }

    ll ans=0;

    for(int i=1 ; i<=n ; i++){
        if(!vis[i] && adj[i].size()){
            DFS(i,0);
            ans=max(even.size() , odd.size() );
            even.clear() ;
            odd.clear() ;
        }
    }

    printf("%lld",ans);

    return 0;
}


/**
*
* Author: MARC
* Lang: GNU C++14
*
**/

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

#define F first
#define S second
#define ii pair < int , int >
#define LS pair < ll , string >

typedef unsigned long long ull;
typedef long double ldbl;
typedef long long ll;
typedef double dbl;

const ldbl pi = 3.1415926535897932385;
const ldbl E = 2.71828182845904523536;
const dbl eps = 1e-9;
const ll mod = 1e9+7;
const ll inf = 1<<30;

/** /////////// ** Code ** /////////// **/

const int N=100100;

int n,LIS;
int T[N<<3]  , a[N] , ans[N];

int query(int v,int tl,int tr,int l,int r){
      if (l>r) return 0;
      if (tl==l && tr==r){
          return T[v];
      }
      int tm=(tl+tr)/2;
      return max(query(2*v,tl,tm,l,min(tm,r)),query(2*v+1,tm+1,tr,max(tm+1,l),r));
}

void update(int v,int tl,int tr,int pos,int val){
   if (tl==tr){
      T[v]=val;
      return;
   }
   int tm=(tl+tr)/2;
   if (tm>=pos) update(2*v,tl,tm,pos,val); else update(2*v+1,tm+1,tr,pos,val);
   T[v]=max(T[2*v],T[2*v+1]);
}

int main(){
    scanf("%d",&n);
    for(int i=1 ; i<=n ; i++) scanf("%d",&a[i]);
    for(int i=1 ; i<=n ; i++){
        ans[a[i]]=query(1,1,n,1,a[i]-1)+1;
        update(1,1,n,a[i],ans[a[i]]);
        LIS=max(LIS,ans[a[i]]);
    }
    printf("%d",LIS);
    return 0;
}


/**
*
* Author: MARC
* Lang: GNU C++14
*
**/

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

#define F first
#define S second
#define ii pair < int , int >
#define LS pair < ll , string >

typedef unsigned long long ull;
typedef long double ldbl;
typedef long long ll;
typedef double dbl;

const ldbl pi = 3.1415926535897932385;
const ldbl E = 2.71828182845904523536;
const dbl eps = 1e-9;
const ll mod = 1e9+7;
const ll inf = 1<<30;

/** /////////// ** Code ** /////////// **/

const int N=100100;

int n,LIS;
int T[N]  , a[N] , ans[N];

int query(int v,int tl,int tr,int l,int r){
      if (l>r) return 0;
      if (tl==l && tr==r){
          return T[v];
      }
      int tm=(tl+tr)/2;
      return max(query(2*v,tl,tm,l,min(tm,r)),query(2*v+1,tm+1,tr,max(tm+1,l),r));
}

void update(int v,int tl,int tr,int pos,int val){
   if (tl==tr){
      T[v]=val;
      return;
   }
   int tm=(tl+tr)/2;
   if (tm>=pos) update(2*v,tl,tm,pos,val); else update(2*v+1,tm+1,tr,pos,val);
   T[v]=max(T[2*v],T[2*v+1]);
}

int main(){
    scanf("%d",&n);
    for(int i=1 ; i<=n ; i++) scanf("%d",&a[i]);
    for(int i=1 ; i<=n ; i++){
        ans[a[i]]=query(1,1,n,1,a[i]-1)+1;
        update(1,1,n,a[i],ans[a[i]]);
        LIS=max(LIS,ans[a[i]]);
    }
    printf("%d",LIS);
    return 0;
}


/**
*
* Author: MARC
* Lang: GNU C++14
*
**/

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

#define F first
#define S second
#define ii pair < int , int >
#define LS pair < ll , string >

typedef unsigned long long ull;
typedef long double ldbl;
typedef long long ll;
typedef double dbl;

const ldbl pi = 3.1415926535897932385;
const ldbl E = 2.71828182845904523536;
const dbl eps = 1e-9;
const ll mod = 1e9+7;
const ll inf = 1<<30;

/** /////////// ** Code ** /////////// **/

const int N=100100;
int a[N];

vector<int>adj[N];
vector<int> even , odd;

bool vis[N];

void DFS(int u,int cnt){
    vis[u]=1;
    if(cnt&1) odd.push_back(u);
    else even.push_back(u);

    for(auto v:adj[u]) if(!vis[v]) DFS(v,cnt+1);
}

int main()
{
    int n; scanf("%d",&n);
    for(int i=1 ; i<=n ; i++) scanf("%d",&a[i]);

    for(int j=1 ; j<=n ; j++){
        for(int i=1 ; i <= n-1 ; i++){
            if(a[i] > a[i+1]){
                adj[i].push_back(i+1);
                adj[i+1].push_back(i);

                swap(a[i],a[i+1]);
            }
        }
    }

    ll ans=0;

    for(int i=1 ; i<=n ; i++){
        if(!vis[i] && adj[i].size()){
            DFS(i,0);
            ans=max(even.size() , odd.size() );
            even.clear() ;
            odd.clear() ;
        }
    }

    printf("%lld",ans);

    return 0;
}


/**
*
* Author: MARC
* Lang: GNU C++14
*
**/

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

#define F first
#define S second
#define ii pair < int , int >
#define LS pair < ll , string >

typedef unsigned long long ull;
typedef long double ldbl;
typedef long long ll;
typedef double dbl;

const ldbl pi = 3.1415926535897932385;
const ldbl E = 2.71828182845904523536;
const dbl eps = 1e-9;
const ll mod = 1e9+7;
const ll inf = 1<<30;

/** /////////// ** Code ** /////////// **/

const int N =100100;
int bit[N];
int n,mx;

int get(int i){
    int ret=0;
    while( i ){
        ret = max ( ret , bit[i]) ;
        i-=i&-i;
    }
    return ret;
}

void add(int i,int v){
    while( i< N  ){
        bit[i]= max ( v , bit[i] );
        i+=i&-i;
    }
}

int main (){
    scanf("%d",&n);
    for (int i=0 ; i<n ; i++){
        int x; scanf("%d",&x);
        add(x,get(x)+1);
        mx=max(mx,x);
    }

    printf("%d",get(mx));
return 0 ;
}


/**
*
* Author: MARC
* Lang: GNU C++14
*
**/

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

#define F first
#define S second
#define ii pair < int , int >
#define LS pair < ll , string >

typedef unsigned long long ull;
typedef long double ldbl;
typedef long long ll;
typedef double dbl;

const ldbl pi = 3.1415926535897932385;
const ldbl E = 2.71828182845904523536;
const dbl eps = 1e-9;
const ll mod = 1e9+7;
const ll inf = 1<<30;

/** /////////// ** Code ** /////////// **/

const int N=100100;

int d[N] , is[N];
vector<int> v;
int n,ans=1;

int findMax(int i) {
    queue<int> q;
    d[i] = 1;
    q.push(i);
    is[i] = 1;
    while (!q.empty()) {
       int vv = q.front();
       q.pop();
       is[vv] = 0;
       for (int j = vv+1; j<n; j++)
        if (v[j] > v[vv] && d[vv]+1 > d[j]) {
            d[j] = d[vv]+1;
            ans = max(ans,d[j]);
            if (is[j] == 0) {
                is[j] = 1;
                q.push(j);
            }
        }
    }

}
int main() {
    scanf("%d",&n);
    for(int i=0 ; i<n ; i++) { int x; scanf("%d",&x); v.push_back(x); }
    for(int i=0 ; i<n ; i++) findMax(i);
    printf("%d",ans);
}


#include <math.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <assert.h>

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

#define nl                std::cout << "\n"
#define in(x)             std::cin  >> (x)
#define out(x)            std::cout << (x) << " "
#define err(x)            std::cerr << "DBG: " << #x << " " << (x) << "\n"

#define inn(x)            int x; in(x)
#define rep(i, n)         for (int i =  0 ; i < (n); ++i)
#define repr(i, l, r)     for (int i = (l); i < (r); ++i)
#define rrep(i, n)        for (int i = (n) - 1; i > -1; --i)

#define no                puts("NO")
#define yes               puts("YES")

using ll  = long long int;
using vi  = std::vector<int>;
using ii  = std::pair<int, int>;
using iii = std::pair<ii, int>;
using vii = std::vector<ii>;

const int N = 1010101;
const int MD = 1e9 + 7;

void solve() {
    inn(n);
    int a[n]; rep(i, n) in(a[i]);

    int b[n];
    int last = -1;
    rep(i, n) {
        if (last == -1 || a[i] > b[last]) {
            b[++last] = a[i];
        } else {
            *std::upper_bound(b, b + last + 1, a[i] - 1) = a[i];
        }
    }

    out(last + 1);
}

int main() {
    std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0);

    //int t; in(t); repr(tc, 1, t + 1)
    solve();

    return 0;
}


#include <math.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <assert.h>

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

#define nl                std::cout << "\n"
#define in(x)             std::cin  >> (x)
#define out(x)            std::cout << (x) << " "
#define err(x)            std::cerr << "DBG: " << #x << " " << (x) << "\n"

#define inn(x)            int x; in(x)
#define rep(i, n)         for (int i =  0 ; i < (n); ++i)
#define repr(i, l, r)     for (int i = (l); i < (r); ++i)
#define rrep(i, n)        for (int i = (n) - 1; i > -1; --i)

#define no                puts("NO")
#define yes               puts("YES")

using ll  = long long int;
using vi  = std::vector<int>;
using ii  = std::pair<int, int>;
using iii = std::pair<ii, int>;
using vii = std::vector<ii>;

const int N = 1010101;
const int MD = 1e9 + 7;

void solve() {
    inn(n);
    int a[n]; rep(i, n) in(a[i]);

    int ans = 0, last = -1;
    int b[n];
    rep(i, n) {
        if (last == -1) {
            b[++last] = a[i];
            ++ans;
        } else {
            if (a[i] > b[last]) {
                b[++last] = a[i];
                ++ans;
            } else {
                int last = std::upper_bound(b, b + ans, a[i] - 1) - b;
                b[last] = a[i];
            }
        }
    }

    out(ans);
}

int main() {
    std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0);

    //int t; in(t); repr(tc, 1, t + 1)
    solve();

    return 0;
}


#include <math.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <assert.h>

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

#define nl                std::cout << "\n"
#define in(x)             std::cin  >> (x)
#define out(x)            std::cout << (x) << " "
#define err(x)            std::cerr << "DBG: " << #x << " " << (x) << "\n"

#define inn(x)            int x; in(x)
#define rep(i, n)         for (int i =  0 ; i < (n); ++i)
#define repr(i, l, r)     for (int i = (l); i < (r); ++i)
#define rrep(i, n)        for (int i = (n) - 1; i > -1; --i)

#define no                puts("NO")
#define yes               puts("YES")

using ll  = long long int;
using vi  = std::vector<int>;
using ii  = std::pair<int, int>;
using iii = std::pair<ii, int>;
using vii = std::vector<ii>;

const int N = 1010101;
const int MD = 1e9 + 7;

void solve() {
    inn(n);
    int a[n]; rep(i, n) in(a[i]);

    int b[n]; rep(i, n) b[a[i] - 1] = i;

    int last = -1, ans = 0;
    rep(i, n) if (b[i] > last) ++ans, last = b[i];

    out(ans);
}

int main() {
    std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0);

    //int t; in(t); repr(tc, 1, t + 1)
    solve();

    return 0;
}


/*  ~*BISMILLAHIR RAHMANIR RAHIM*~  */
#include <bits/stdc++.h>
using namespace std;

#define MX_N 212345
int n, a[MX_N], temp[MX_N];

int main() {
  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  cin>>n;
  for(int i=0; i<n; i++) cin>>a[i];
  for(int i=1; i<=n+5; i++)
    temp[i] = INT_MAX;
  temp[0] = INT_MIN;
  int LIS = 0;
  for(int i=0; i<n; i++) {
    int lo = 0, hi = LIS;
    while( lo<=hi ) {
      int mid = lo+(hi-lo)/2;
      if( temp[mid]<a[i] ) lo = mid+1;
      else hi = mid-1;
    }
    temp[lo] = a[i];
    LIS = max(LIS, lo);
  }
  cout<<LIS<<endl;
  return 0;
}


/*  ~*BISMILLAHIR RAHMANIR RAHIM*~  */
#include <bits/stdc++.h>
using namespace std;

#define MX_N 212345
int n, a[MX_N];

int main() {
  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  cin>>n;
  for(int i=1; i<=n; i++) {
    cin>>a[i];
  }
  int res = 0, cnt = 0;
  for(int i=1; i<=n; i++) {
    if( a[i]==i ) continue;
    if( a[i]>i ) {
      res = max(res, a[i]-i);
      //swap(a[i], a[a[i]]);
    }
  }
  cout<<res<<endl;
  return 0;
}


/*  ~*BISMILLAHIR RAHMANIR RAHIM*~  */
#include <bits/stdc++.h>
using namespace std;

#define MX_N 212345
int n, a[MX_N];

int main() {
  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  cin>>n;
  for(int i=1; i<=n; i++) {
    cin>>a[i];
  }
  int res = 0, cnt = 0;
  for(int i=1; i<=n; i++) {
    if( a[i]==i ) continue;
    if( a[i]>i ) {
      res = max(res, a[i]-i);
      swap(a[i], a[a[i]]);
    }
  }
  cout<<res<<endl;
  return 0;
}


/*  ~*BISMILLAHIR RAHMANIR RAHIM*~  */
#include <bits/stdc++.h>
using namespace std;

#define MX_N 212345
int n, a[MX_N], temp[MX_N];

int main() {
  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  cin>>n;
  for(int i=1; i<=n; i++) cin>>a[i];
  for(int i=2; i<=n+5; i++)
    temp[i] = INT_MAX;
  temp[1] = INT_MIN;
  int LIS = 0;
  for(int i=1; i<=n; i++) {
    int lo = 0, hi = LIS;
    while( lo<=hi ) {
      int mid = lo+(hi-lo)/2;
      if( temp[mid]<a[n-i+1] ) lo = mid+1;
      else hi = mid-1;
    }
    temp[lo] = a[n-i+1];
    LIS = max(LIS, lo);
  }
  cout<<LIS<<endl;
  return 0;
}


/*  ~*BISMILLAHIR RAHMANIR RAHIM*~  */
#include <bits/stdc++.h>
using namespace std;

#define MX_N 212345
int n, a[MX_N], temp[MX_N];

int main() {
  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  cin>>n;
  for(int i=1; i<=n; i++) cin>>a[i];
  for(int i=2; i<=n+5; i++)
    temp[i] = INT_MAX;
  temp[1] = INT_MIN;
  int LIS = 0;
  for(int i=1; i<=n; i++) {
    int lo = 0, hi = LIS;
    while( lo<=hi ) {
      int mid = lo+(hi-lo)/2;
      if( temp[mid]<a[n-i+1] ) lo = mid+1;
      else hi = mid-1;
    }
    temp[lo] = a[n-i+1];
    LIS = max(LIS, lo);
  }
  cout<<LIS-1<<endl;
  return 0;
}


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

const ll nax = 2e5 + 5;

int main(){
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);

    ll n;
    cin >> n;
    vector<ll> v(n + 1);
    for(int i = 1; i <= n; i++){
        cin >> v[i];
    }
//    vector<ll> sorted(v.begin(), v.end());
//    sort(sorted.begin(), sorted.end());
//
//    int dp[n] = {0};
//    dp[0] = 1;
//    for(int i = 1; i < n; i++){
//        dp[i] = dp[i - 1] + (abs((lower_bound(sorted.begin(), sorted.end(), v[i]) - sorted.begin())
//                           -(lower_bound(sorted.begin(), sorted.end(), v[i - 1]) - sorted.begin())) > 1 ? 1 : 0);
//    }
//    cout << dp[n-1] << '\n';
////    for(int i : v) cout << i << " ";
////    cout << '\n';
////    for(int i : v)
////    cout << (lower_bound(sorted.begin(), sorted.end(), i) - sorted.begin()) << " ";
    ll ans = 0;
    for(int i = 1; i <= n; i++){
        if(abs(v[i] - v[i - 1]) > 1) ans++;
    }
    cout << ans << "\n";
}


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

const ll nax = 2e5 + 5;

int main(){
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);

    ll n;
    cin >> n;
    vector<ll> v(n);
    for(int i = 0; i < n; i++){
        cin >> v[i];
    }
    vector<ll> sorted(v.begin(), v.end());
    sort(sorted.begin(), sorted.end());

    int dp[n] = {0};
    dp[0] = 1;
    for(int i = 1; i < n; i++){
        dp[i] = dp[i - 1] + (abs((lower_bound(sorted.begin(), sorted.end(), v[i]) - sorted.begin())
                           -(lower_bound(sorted.begin(), sorted.end(), v[i - 1]) - sorted.begin())) > 1 ? 1 : 0);
    }
    cout << dp[n-1] << '\n';
//    for(int i : v) cout << i << " ";
//    cout << '\n';
//    for(int i : v)
//    cout << (lower_bound(sorted.begin(), sorted.end(), i) - sorted.begin()) << " ";

}


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

const ll nax = 2e5 + 5;

int main(){
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);

    ll n;
    cin >> n;
    vector<ll> v(n);
    for(int i = 0; i < n; i++){
        cin >> v[i];
    }
    vector<ll> dp(n, 1);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < i; j++){
            if(v[j] < v[i]) dp[i] = max(dp[i], dp[j] + 1);
        }
    }
    sort(dp.rbegin(), dp.rend());
    cout << dp[0] << '\n';

}


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

const ll nax = 2e5 + 5;

int main(){
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);

    ll n;
    cin >> n;
    vector<ll> v(n);
    for(int i = 0; i < n; i++){
        cin >> v[i];
    }
    vector<ll> dp;
    dp.push_back(v[0]);
    for(int i = 1; i < n; i++){
        if(dp[dp.size() - 1] < v[i]) dp.push_back(v[i]);
        else{
            int index = lower_bound(dp.begin(), dp.end(), v[i]) - dp.begin();
            dp[index] = v[i];
        }
    }
    cout << dp.size() << "\n";

}


///

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define ii pair < int , int >
#define LS pair < ll , string >

typedef unsigned long long ull;
typedef long double ldbl;
typedef long long ll;
typedef double dbl;

const ldbl E = 2.71828182845904523536;
const ldbl pi = acos(-1);
const dbl eps = 1e-9;
const ll mod = 1e8;
const ll inf = 1LL<<30;

/* ///// ** code ** ///// */

set<int> st;
int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n , a[100005];
    cin>>n;
    for(int i=0; i<n; i++) cin>>a[i];

    set<int>::iterator it;
//    st.clear();

    for(int i=0; i<n; i++)
    {
        st.insert(a[i]);

        auto x=st.begin();
//        while(x!=st.end()) { cout<<*x<< "  " ; x++;} cout<<endl;

        it=st.find(a[i]);
        it++;
//        cout<<*it<<endl;
        if(it!=st.end()) st.erase(it);
    }
    cout<<st.size()<<endl;
/*
8
4 1 2 5 6 3 4 5

8
4 1 2 5 6 3 1 1
*/

//    cout<<ans;
//  // Prinying the LIS :
//    vector <int> T;
//    int require = ans;
//    for (int i=n; i>=1; i--)
//    {
//        if (f[i]==require)
//        {
//            T.push_back(a[i]);
//            require--;
//        }
//    }
//
//    // then print T with reversed order
//    int i=T.size();
//    while (i--) cout<<T[i]<<" ";

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	vector<int> d(n+1, 1000000000);
	for (int i = 0; i < n; i++) {
		*lower_bound(d.begin(), d.end(), a[i]) = a[i];
	}
	for (int i = 0; i <= n; i++) {
		if (d[i] == 1000000000) {
			cout << i << endl;
			return 0;
		}
	}
}


///

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define ii pair < int , int >
#define LS pair < ll , string >

typedef unsigned long long ull;
typedef long double ldbl;
typedef long long ll;
typedef double dbl;

const ldbl E = 2.71828182845904523536;
const ldbl pi = acos(-1);
const dbl eps = 1e-9;
const ll mod = 1e8;
const ll inf = 1LL<<30;

/* ///// ** code ** ///// */

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n , a[100005];
    int b[100005]={0} , f[100005]={0} ,ans=0;

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

    for(int i=1; i<=n; i++)
    {
        f[i] = lower_bound (b+1 , b+ans+1, a[i])-b;
        ans=max(ans,f[i]);

//        for(int z=1; z<=n; z++) cout<<b[z]<<" "; cout<<endl;
//        cout<<i<<" "<<f[i]<<" "<<ans<<endl;

        b[ f[i] ]=a[i];

//        for(int z=1; z<=n; z++) cout<<b[z]<<" "; cout<<endl;

    }

//    cout<<endl;
//    for(int z=1; z<=n; z++) cout<<f[z]<<" "; cout<<endl<<endl;

/*
8
4 1 2 5 6 3 4 5

8
4 1 2 5 6 3 1 1
*/

    cout<<ans;
//    vector <int> T;
//    int require = ans;
//    for (int i=n; i>=1; i--)
//    {
//        if (f[i]==require)
//        {
//            T.push_back(a[i]);
//            require--;
//        }
//    }
//
//    // then print T with reversed order
//    int i=T.size();
//    while (i--) cout<<T[i]<<" ";

    return 0;
}


///

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define ii pair < int , int >
#define LS pair < ll , string >

typedef unsigned long long ull;
typedef long double ldbl;
typedef long long ll;
typedef double dbl;

const ldbl E = 2.71828182845904523536;
const ldbl pi = acos(-1);
const dbl eps = 1e-9;
const ll mod = 1e8;
const ll inf = 1LL<<30;

/* ///// ** code ** ///// */

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n , a[100005];
    int b[100005]={0} , f[100005]={0} ,ans=0;
    cin>>n;
    for(int i=0; i<n; i++) cin>>a[i];

    set<int> st;
    set<int>::iterator it;
    st.clear();

    for(int i=0; i<n; i++)
    {
        st.insert(a[i]);
        it=st.find(a[i]);
        it++;
        if(it!=st.end()) st.erase(it);
    }
    cout<<st.size()<<endl;
/*
8
4 1 2 5 6 3 4 5

8
4 1 2 5 6 3 1 1
*/

//    cout<<ans;
//  // Prinying the LIS :
//    vector <int> T;
//    int require = ans;
//    for (int i=n; i>=1; i--)
//    {
//        if (f[i]==require)
//        {
//            T.push_back(a[i]);
//            require--;
//        }
//    }
//
//    // then print T with reversed order
//    int i=T.size();
//    while (i--) cout<<T[i]<<" ";

    return 0;
}


///

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define ii pair < int , int >
#define LS pair < ll , string >

typedef unsigned long long ull;
typedef long double ldbl;
typedef long long ll;
typedef double dbl;

const ldbl E = 2.71828182845904523536;
const ldbl pi = acos(-1);
const dbl eps = 1e-9;
const ll mod = 1e8;
const ll inf = 1LL<<30;

/* ///// ** code ** ///// */

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n , a[100];
    int b[100]={0} , f[100] ,ans=0;

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

    for(int i=1; i<=n; i++)
    {
        f[i] = lower_bound (b+1 , b+ans+1, a[i])-b;
        ans=max(ans,f[i]);

//        for(int z=1; z<=n; z++) cout<<b[z]<<" "; cout<<endl;
//        cout<<i<<" "<<f[i]<<" "<<ans<<endl;

        b[ f[i] ]=a[i];

//        for(int z=1; z<=n; z++) cout<<b[z]<<" "; cout<<endl;

    }

//    cout<<endl;
//    for(int z=1; z<=n; z++) cout<<f[z]<<" "; cout<<endl<<endl;

/*
8
4 1 2 5 6 3 4 5

8
4 1 2 5 6 3 1 1
*/

    cout<<ans;
//    vector <int> T;
//    int require = ans;
//    for (int i=n; i>=1; i--)
//    {
//        if (f[i]==require)
//        {
//            T.push_back(a[i]);
//            require--;
//        }
//    }
//
//    // then print T with reversed order
//    int i=T.size();
//    while (i--) cout<<T[i]<<" ";

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	vector<int> d(n+1, 1000000000);
	for (int i = 0; i < n; i++) {
		*lower_bound(d.begin(), d.end(), a[i]) = a[i];
	}
	for (int i = 0; i <= n; i++) {
		if (d[i] == 1000000000) {
			cout << i << endl;
			return 0;
		}
	}
}


///

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define ii pair < int , int >
#define LS pair < ll , string >

typedef unsigned long long ull;
typedef long double ldbl;
typedef long long ll;
typedef double dbl;

const ldbl E = 2.71828182845904523536;
const ldbl pi = acos(-1);
const dbl eps = 1e-9;
const ll mod = 1e8;
const ll inf = 1LL<<30;

/* ///// ** code ** ///// */

set<int> st;
int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n , a[100005];
    cin>>n;
    for(int i=0; i<n; i++) cin>>a[i];

    set<int>::iterator it;
//    st.clear();

    for(int i=0; i<n; i++)
    {
        st.insert(a[i]);

        auto x=st.begin();
        while(x!=st.end()) { cout<<*x<< "  " ; x++;} cout<<endl;

        it=st.find(a[i]);
        it++;
        cout<<*it<<endl;
        if(it!=st.end()) st.erase(it);
    }
    cout<<st.size()<<endl;
/*
8
4 1 2 5 6 3 4 5

8
4 1 2 5 6 3 1 1
*/

//    cout<<ans;
//  // Prinying the LIS :
//    vector <int> T;
//    int require = ans;
//    for (int i=n; i>=1; i--)
//    {
//        if (f[i]==require)
//        {
//            T.push_back(a[i]);
//            require--;
//        }
//    }
//
//    // then print T with reversed order
//    int i=T.size();
//    while (i--) cout<<T[i]<<" ";

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define lld long long double
#define ull unsigned long long
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define nl puts("")
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vpii vector<pii>
#define vpll vector<pll>
#define vs vector<string>
#define vll vector<ll>
#define si set<int>
#define sll set<ll>
#define ss set<string>
#define sc set<char>
#define mii map<int,int>
#define mll map<ll,ll>
#define mpiii map<pii,int>
#define msi map<string,int>
#define mci map<char,int>
#define mk make_pair
using namespace std;
int main()
{
	int n;
	cin >> n;
	vi v(n),res;
	for (int i = 0; i < n; i++)
		cin >> v[i];

	for (int i = 0; i < n; i++)
	{
		auto it = upper_bound(all(res), v[i]);
		if (it == res.end())
			res.push_back(v[i]);
		else
			*it = min(*it, v[i]);
	}
	cout << res.size() << endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define lld long long double
#define ull unsigned long long
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define nl puts("")
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vpii vector<pii>
#define vpll vector<pll>
#define vs vector<string>
#define vll vector<ll>
#define si set<int>
#define sll set<ll>
#define ss set<string>
#define sc set<char>
#define mii map<int,int>
#define mll map<ll,ll>
#define mpiii map<pii,int>
#define msi map<string,int>
#define mci map<char,int>
#define mk make_pair
using namespace std;
int main()
{
	int n;
	cin >> n;
	vi v(n),res;
	for (int i = 0; i < n; i++)
		cin >> v[i];

	for (int i = 0; i < n; i++)
	{
		auto it = upper_bound(all(res), v[i]);
		it == res.end() ? res.push_back(v[i]) : *it = min(*it, v[i]);
	}
	cout << res.size() << endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define lld long long double
#define ull unsigned long long
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define nl puts("")
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vpii vector<pii>
#define vpll vector<pll>
#define vs vector<string>
#define vll vector<ll>
#define si set<int>
#define sll set<ll>
#define ss set<string>
#define sc set<char>
#define mii map<int,int>
#define mll map<ll,ll>
#define mpiii map<pii,int>
#define msi map<string,int>
#define mci map<char,int>
#define mk make_pair
using namespace std;
int main()
{
	int n;
	cin >> n;
	vi v(n),res;
	for (int i = 0; i < n; i++)
		cin >> v[i];

	for (int i = 0; i < n; i++)
	{
		auto it = upper_bound(all(res), v[i]);
		it != res.end() ? *it = min(*it, v[i]) : res.push_back(v[i]);
	}
	cout << res.size() << endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define lld long long double
#define ull unsigned long long
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define nl puts("")
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vpii vector<pii>
#define vpll vector<pll>
#define vs vector<string>
#define vll vector<ll>
#define si set<int>
#define sll set<ll>
#define ss set<string>
#define sc set<char>
#define mii map<int,int>
#define mll map<ll,ll>
#define mpiii map<pii,int>
#define msi map<string,int>
#define mci map<char,int>
#define mk make_pair
using namespace std;
int main()
{
	int n;
	cin >> n;
	vi v;
	for (int i = 0; i < n; i++)
	{
		int a;
		cin >> a;
		auto it = upper_bound(all(v), a);
		if (it != v.end())
			*it = min(*it, a);
		else
			v.push_back(a);
	}
	cout << v.size() << endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define lld long long double
#define ull unsigned long long
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define nl puts("")
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vpii vector<pii>
#define vpll vector<pll>
#define vs vector<string>
#define vll vector<ll>
#define si set<int>
#define sll set<ll>
#define ss set<string>
#define sc set<char>
#define mii map<int,int>
#define mll map<ll,ll>
#define mpiii map<pii,int>
#define msi map<string,int>
#define mci map<char,int>
#define mk make_pair
using namespace std;
int main()
{
	int n;
	cin >> n;
	vi v;
	for (int i = 0; i < n; i++)
	{
		int a;
		cin >> a;
		auto it = upper_bound(all(v), a);
		it != v.end() ? *it = min(*it, a) :v.push_back(a);
	}
	cout << v.size() << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
//const double pi=acos(-1.0);
#define mod 1000000007
#define forn(i,k,n) for(ll i=k;i<n;i++)
#define fornr(i,k,k1) for(ll i=k;i>=k1;i--)
#define v(ll) vector<ll>
#define vl(a,n,ll,k) vector<ll> a(n,k)
#define vli(a,n) vector<ll> a(n)
#define init(a,n) vector<ll> a(n);forn(i,0,n)cin>>a[i]
#define vll(a,n,m,ll,k) vector<vector<ll>> a(n,vector<ll>(m,k))
#define mp(a,ll,ll1) map<ll,ll1> a
#define st(a,ll) set<ll> a
#define mpa make_pair
#define pr pair<ll,ll>
#define pb push_back
#define MAX 1000000
#define ed '\n'
#define fileTake ifstream cin("input.txt");ofstream cout("output.txt");
#define sortv(a) sort(a.begin(),a.end())
#define sorta(a,n) sort(a,a+n)
#define yeah "YES"
#define nil "NO"
#define All(x) x.begin(),x.end()
#define remDup(a) a.erase(unique(All(a)),a.end())

void swap(ll &x,ll &y)
{
	x=x^y;
	y=x^y;
	x=x^y;
}

template<typename T>
void print(T a){for(auto i=a.begin();i!=a.end();i++)cout<<*i<<" ";cout<<endl;}
bool isVowel(char c)
{
	if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')return true;
	return false;
}

void sync(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
ll ceilIndex(v(ll) a,v(ll) temp,ll len,ll x)
{
	ll start=0;
	ll mid;
	ll end=len;
	while(start<=end)
	{
		mid=start+(end-start)/2;
		if(a[temp[mid]]<x&&x<=a[temp[mid+1]])
		return mid+1;
		else if(a[temp[mid]]<x)
		start=mid+1;
		else end=mid-1;
	}
	return -1;
}
ll longestIncreasingSubsequence(v(ll) a)
{
	ll n=a.size();
	vl(temporaryArray,n,ll,0);
	vl(result,n,ll,-1);
	ll len=0;
	for(ll i=1;i<n;i++)
	{
		if(a[temporaryArray[0]]>a[i])
		temporaryArray[0]=i;
		else if(a[temporaryArray[len]]<a[i])
		{
			len++;
			temporaryArray[len]=i;
			result[temporaryArray[len]]=temporaryArray[len-1];
		}
		else
		{
			ll index=ceilIndex(a,temporaryArray,len,a[i]);
			temporaryArray[index]=i;
			result[temporaryArray[index]]=temporaryArray[index-1];
		}
	}
	return len+1;
}
int main(){
	sync();
	ll n;
	cin>>n;
	init(a,n);
	cout<<longestIncreasingSubsequence(a)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
//const double pi=acos(-1.0);
#define mod 1000000007
#define forn(i,k,n) for(ll i=k;i<n;i++)
#define fornr(i,k,k1) for(ll i=k;i>=k1;i--)
#define v(ll) vector<ll>
#define vl(a,n,ll,k) vector<ll> a(n,k)
#define vli(a,n) vector<ll> a(n)
#define init(a,n) vector<ll> a(n);forn(i,0,n)cin>>a[i]
#define vll(a,n,m,ll,k) vector<vector<ll>> a(n,vector<ll>(m,k))
#define mp(a,ll,ll1) map<ll,ll1> a
#define st(a,ll) set<ll> a
#define mpa make_pair
#define pr pair<ll,ll>
#define pb push_back
#define MAX 1000000
#define ed '\n'
#define fileTake ifstream cin("input.txt");ofstream cout("output.txt");
#define sortv(a) sort(a.begin(),a.end())
#define sorta(a,n) sort(a,a+n)
#define yeah "YES"
#define nil "NO"
#define All(x) x.begin(),x.end()
#define remDup(a) a.erase(unique(All(a)),a.end())

void swap(ll &x,ll &y)
{
	x=x^y;
	y=x^y;
	x=x^y;
}

template<typename T>
void print(T a){for(auto i=a.begin();i!=a.end();i++)cout<<*i<<" ";cout<<endl;}
bool isVowel(char c)
{
	if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')return true;
	return false;
}

void sync(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
ll ceilIndex(v(ll) a,v(ll) temp,ll len,ll x)
{
	ll start=0;
	ll mid;
	ll end=len;
	while(start<=end)
	{
		mid=start+(end-start)/2;
		if(mid<len&&a[temp[mid]]<x&&x<=a[temp[mid+1]])
		return mid+1;
		else if(a[temp[mid]]<x)
		start=mid+1;
		else end=mid-1;
	}
	return -1;
}
ll longestIncreasingSubsequence(v(ll) a)
{
	ll n=a.size();
	vl(temporaryArray,n,ll,0);
	vl(result,n,ll,-1);
	ll len=0;
	for(ll i=1;i<n;i++)
	{
		if(a[temporaryArray[0]]>a[i])
		temporaryArray[0]=i;
		else if(a[temporaryArray[len]]<a[i])
		{
			len++;
			temporaryArray[len]=i;
			result[temporaryArray[len]]=temporaryArray[len-1];
		}
		else
		{
			ll index=ceilIndex(a,temporaryArray,len,a[i]);
			temporaryArray[index]=i;
			result[temporaryArray[index]]=temporaryArray[index-1];
		}
	}
	return len+1;
}
int main(){
	sync();
	ll n;
	cin>>n;
	init(a,n);
	cout<<longestIncreasingSubsequence(a)<<endl;
}


#include <iostream>
using namespace std;
#include<vector>

int CeilIndex(vector<int>& v, int l, int r, int key)
{
	while (r - l > 1) {
		int m = l + (r - l) / 2;
		if (v[m] >= key)
			r = m;
		else
			l = m;
	}

	return r;
}

int LongestIncreasingSubsequenceLength(vector<int>& v)
{
	if (v.size() == 0)
		return 0;

	vector<int> tail(v.size(), 0);
	int length = 1;

	tail[0] = v[0];
	for (int i = 1; i < v.size(); i++) {

		if (v[i] < tail[0])
			tail[0] = v[i];

		else if (v[i] > tail[length - 1])
			tail[length++] = v[i];

		else
			tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i];
	}

	return length;
}

int main()
{
	int n;
	cin>>n;
	vector<int> v(n);
	for(int i=0;i<n;i++)cin>>v[i];
	cout<< LongestIncreasingSubsequenceLength(v) << '\n';
	return 0;
}


#include <iostream>
using namespace std;
#include<vector>

int CeilIndex(vector<int>& v, int l, int r, int key)
{
	int ans=-1;
	while(l<=r)
	{
	int mid=(l+r)/2;
	if(v[mid]>=key)
	{
		ans=mid;
		r=mid-1;
		}
	else l=mid+1;
	}
	return ans;
}

int LongestIncreasingSubsequenceLength(vector<int>& v)
{
	if (v.size() == 0)
		return 0;

	vector<int> tail(v.size(), 0);
	int length = 1;

	tail[0] = v[0];
	for (int i = 1; i < v.size(); i++) {

		if (v[i] < tail[0])
			tail[0] = v[i];

		else if (v[i] > tail[length - 1])
			tail[length++] = v[i];

		else
			tail[CeilIndex(tail, 0, length - 1, v[i])] = v[i];
	}

	return length;
}

int main()
{
	int n;
	cin>>n;
	vector<int> v(n);
	for(int i=0;i<n;i++)cin>>v[i];
	cout<< LongestIncreasingSubsequenceLength(v) << '\n';
	return 0;
}


#include <iostream>
using namespace std;
#include<vector>

int CeilIndex(vector<int>& v, int l, int r, int key)
{
	while (r - l > 1) {
		int m = l + (r - l) / 2;
		if (v[m] >= key)
			r = m;
		else
			l = m;
	}

	return r;
}

int LongestIncreasingSubsequenceLength(vector<int>& v)
{
	if (v.size() == 0)
		return 0;

	vector<int> tail(v.size(), 0);
	int length = 1;

	tail[0] = v[0];
	for (size_t i = 1; i < v.size(); i++) {

		if (v[i] < tail[0])
			tail[0] = v[i];

		else if (v[i] > tail[length - 1])
			tail[length++] = v[i];

		else
			tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i];
	}

	return length;
}

int main()
{
	int n;
	cin>>n;
	vector<int> v(n);
	for(int i=0;i<n;i++)cin>>v[i];
	cout<< LongestIncreasingSubsequenceLength(v) << '\n';
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define ll long long
#define mp make_pair
#define ss second
#define pb push_back
#define ff first
#define um unordered_map
#define us unordered_set

int bit[100001];

int query(int index){

  int ans = 0;

  for(int i=1;i<=index;i+=(i&(-1)))
     ans = max(ans,bit[i]);

  return ans;

}

void update(int index,int value){

  for(int i=index;i>0;i-=i&(-i))
    bit[i] = max(bit[i],value);

}

int main() {

    int n;
    cin>>n;

    int *a = new int[n];

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

    int ans = 0;

    for(int i=0;i<n;i++){
      int temp = query(a[i])+1;
      ans = max(temp,ans);
      update(a[i],temp);
    }

    cout<<ans;

    delete []a;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define ll long long
#define mp make_pair
#define ss second
#define pb push_back
#define ff first
#define um unordered_map
#define us unordered_set

int bit[100001];

int query(int index){

  int ans = 0;

  for(int i=1;i<=index;i+=(i&(-1)))
     ans = max(ans,bit[i]);

  return ans;

}

void update(int index,int value){

  for(int i=index;i>0;i-=i&(-i))
    bit[i] = max(bit[i],value);

}

int main() {

    int n;
    cin>>n;

    int *a = new int[n];

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

    int ans = 0;

    for(int i=0;i<n;i++){
      int temp = query(a[i]-1)+1;
      ans = max(temp,ans);
      update(a[i],temp);
    }

    cout<<ans;

    delete []a;

	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
bool sign(ll a,ll b)
{
    return (a>=0)^(b<0);
}
int main()
{
 ios_base::sync_with_stdio(false);
 cin.tie(NULL);
 ll x,i,j,k,n,t,f,c,c1;
 vector<ll> v;
 string s;
 cin>>t;
 i=0;
 while(i<t)
 {
     cin>>n>>x;
     cin>>s;
     f=0;
     j=0;
     c=0;
     while(j<n)
     {
         if(s[j]=='0')
         {
             c++;
         }
         else
         {
             c--;
         }
         if(c==x)
         {
             f=1;
         }
         v.pb(c);
         j++;
     }
     //v->-10-10-1
     k=v[v.size()-1];
     if(k==0 && f==1)
     {
         cout<<-1<<endl;
     }
     else if(k==x)
     {
         cout<<-1<<endl;
     }
     else if(k!=0)
     {
         c=0;
         c1=0;
         j=0;
         while(j<v.size())
         {
             if(v[j]==x%k || v[j]==(x%k)-k)
             {
             if(sign(x,k)==1)
             {
                 c++;
             }
             }
             else if(v[j]==x)
             {
                 c++;
             }
             else if(v[j]==k)
             {
                 c1++;
             }
             j++;
         }
         if(x%k==0 && sign(x,k)==1)
         {
             c=c+c1;
         }
         cout<<c<<endl;
     }
     else
     {
         j=0;
         c=0;
         while(j<v.size())
         {
             if(v[j]==x)
             {
                c++;
             }
             j++;
         }
         cout<<c<<endl;
     }
     i++;
     s.clear();
     v.clear();
 }
return(0);
}


#include<iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
int main()
{
 ios_base::sync_with_stdio(false);
 cin.tie(NULL);
 ll x,i,j,k,n,t,f,c,c1;
 vector<ll> v;
 string s;
 cin>>t;
 i=0;
 while(i<t)
 {
     cin>>n>>x;
     cin>>s;
     f=0;
     j=0;
     c=0;
     while(j<n)
     {
         if(s[j]=='0')
         {
             c++;
         }
         else
         {
             c--;
         }
         if(c==x)
         {
             f=1;
         }
         v.pb(c);
         j++;
     }
     //v->-10-10-1
     k=v[v.size()-1];
     if(k==0 && f==1)
     {
         cout<<-1<<endl;
     }
     else if(k==x)
     {
         cout<<-1<<endl;
     }
     else
     {
         c=0;
         c1=0;
         j=0;
         while(j<v.size())
         {
             if(v[j]==x%k && x^k>=0)
             {
                 c++;
             }
             else if(v[j]==x)
             {
                 c++;
             }
             else if(v[j]==k)
             {
                 c1++;
             }
             j++;
         }
         if(x%k==0 && x^k>=0)
         {
             c=c+c1;
         }
         cout<<c<<endl;
     }
     //010010->1,0,1,2,1,2   10
     i++;
     s.clear();
     v.clear();
 }
return(0);
}


#include<iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
bool sign(ll a,ll b)
{
    return (a>=0)^(b<0);
}
int main()
{
 ios_base::sync_with_stdio(false);
 cin.tie(NULL);
 ll x,i,j,k,n,t,f,c,c1;
 vector<ll> v;
 string s;
 cin>>t;
 i=0;
 while(i<t)
 {
     cin>>n>>x;
     cin>>s;
     f=0;
     j=0;
     c=0;
     while(j<n)
     {
         if(s[j]=='0')
         {
             c++;
         }
         else
         {
             c--;
         }
         if(c==x)
         {
             f=1;
         }
         v.pb(c);
         j++;
     }
     //v->-10-10-1
     k=v[v.size()-1];
     if(k==0 && f==1)
     {
         cout<<-1<<endl;
     }
     else if(k==x)
     {
         cout<<-1<<endl;
     }
     else if(k!=0)
     {
         c=0;
         c1=0;
         j=0;
         while(j<v.size())
         {
             if(v[j]==x%k && sign(x,k)==1)
             {
                 c++;
             }
             else if(v[j]==x)
             {
                 c++;
             }
             else if(v[j]==k)
             {
                 c1++;
             }
             j++;
         }
         if(x%k==0 && sign(x,k)==1)
         {
             c=c+c1;
         }
         cout<<c<<endl;
     }
     else
     {
         j=0;
         c=0;
         while(j<v.size())
         {
             if(v[j]==x)
             {
                c++;
             }
             j++;
         }
         cout<<c<<endl;
     }
     //010010->1,0,1,2,1,2   10
     i++;
     s.clear();
     v.clear();
 }
return(0);
}


#include<iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
bool sign(ll a,ll b)
{
    return (a>=0)^(b<0);
}
int main()
{
 ios_base::sync_with_stdio(false);
 cin.tie(NULL);
 ll x,i,j,k,n,t,f,c,c1;
 vector<ll> v;
 string s;
 cin>>t;
 i=0;
 while(i<t)
 {
     cin>>n>>x;
     cin>>s;
     f=0;
     j=0;
     c=0;
     while(j<n)
     {
         if(s[j]=='0')
         {
             c++;
         }
         else
         {
             c--;
         }
         if(c==x)
         {
             f=1;
         }
         v.pb(c);
         j++;
     }
     //v->-10-10-1
     k=v[v.size()-1];
     if(k==0 && f==1)
     {
         cout<<-1<<endl;
     }
     else if(k!=0)
     {
         c=0;
         c1=0;
         j=0;
         while(j<v.size())
         {
             if((x-v[j])%k==0)
             {
             if(sign((x-v[j]),k)==1)
             {
                 c++;
             }
             }
             else if(v[j]==x)
             {
                 c++;
             }
             else if(v[j]==k)
             {
                 c1++;
             }
             j++;
         }
         if(x%k==0 && sign(x,k)==1)
         {
             c=c+c1;
         }
         if(x==0)
         {
             c++;
         }
         cout<<c<<endl;
     }
     else
     {
         j=0;
         c=0;
         while(j<v.size())
         {
             if(v[j]==x)
             {
                c++;
             }
             j++;
         }
         if(x==0)
         {
             c++;
         }
         cout<<c<<endl;
     }
     i++;
     s.clear();
     v.clear();
 }
return(0);
}


#include<iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
bool sign(ll a,ll b)
{
    return (a>=0)^(b<0);
}
int main()
{
 ios_base::sync_with_stdio(false);
 cin.tie(NULL);
 ll x,i,j,k,n,t,f,c,c1;
 vector<ll> v;
 string s;
 cin>>t;
 i=0;
 while(i<t)
 {
     cin>>n>>x;
     cin>>s;
     f=0;
     j=0;
     c=0;
     while(j<n)
     {
         if(s[j]=='0')
         {
             c++;
         }
         else
         {
             c--;
         }
         if(c==x)
         {
             f=1;
         }
         v.pb(c);
         j++;
     }
     //v->-10-10-1
     k=v[v.size()-1];
     if(k==0 && f==1)
     {
         cout<<-1<<endl;
     }
     else if(k!=0)
     {
         c=0;
         c1=0;
         j=0;
         while(j<v.size())
         {
             if((x-v[j])%k==0)
             {
             if(sign((x-v[j]),k)==1)
             {
                 c++;
             }
             }
             if(v[j]==x && v[j]!=k && v[j]!=x)
             {
                 c++;
             }
             j++;
         }
         //101212
         if(x==0)
         {
             c++;
         }
         cout<<c<<endl;
     }
     else
     {
         j=0;
         c=0;
         while(j<v.size())
         {
             if(v[j]==x)
             {
                c++;
             }
             j++;
         }
         if(x==0)
         {
             c++;
         }
         cout<<c<<endl;
     }
     i++;
     s.clear();
     v.clear();
 }
return(0);
}


#include<iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
bool sign(ll x, ll y)
{
    return (x >= 0) ^ (y < 0);
}
int main()
{
 ios_base::sync_with_stdio(false);
 cin.tie(NULL);
 ll x,i,j,k,n,t,f,c,c1;
 vector<ll> v;
 string s;
 cin>>t;
 i=0;
 while(i<t)
 {
     cin>>n>>x;
     cin>>s;
     f=0;
     j=0;
     c=0;
     while(j<n)
     {
         if(s[j]=='0')
         {
             c++;
         }
         else
         {
             c--;
         }
         if(c==x)
         {
             f=1;
         }
         v.pb(c);
         j++;
     }
     //v->-10-10-1
     k=v[v.size()-1];
     if(k==0 && f==1)
     {
         cout<<-1<<endl;
     }
     else if(k==x)
     {
         cout<<-1<<endl;
     }
     else
     {
         c=0;
         c1=0;
         j=0;
         while(j<v.size())
         {
             if(v[j]==x%k && sign(x,k)==1)
             {
                 c++;
             }
             else if(v[j]==x)
             {
                 c++;
             }
             else if(v[j]==k)
             {
                 c1++;
             }
             j++;
         }
         if(x%k==0 && sign(x,k)==1)
         {
             c=c+c1;
         }
         cout<<c<<endl;
     }
     //010010->1,0,1,2,1,2   10
     i++;
     s.clear();
     v.clear();
 }
return(0);
}


#include<iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
bool sign(ll a,ll b)
{
    return (a>=0)^(b<0);
}
int main()
{
 ios_base::sync_with_stdio(false);
 cin.tie(NULL);
 ll x,i,j,k,n,t,f,c,c1;
 vector<ll> v;
 string s;
 cin>>t;
 i=0;
 while(i<t)
 {
     cin>>n>>x;
     cin>>s;
     f=0;
     j=0;
     c=0;
     while(j<n)
     {
         if(s[j]=='0')
         {
             c++;
         }
         else
         {
             c--;
         }
         if(c==x)
         {
             f=1;
         }
         v.pb(c);
         j++;
     }
     //v->-10-10-1
     k=v[v.size()-1];
     if(k==0 && f==1)
     {
         cout<<-1<<endl;
     }
     else if(k!=0)
     {
         c=0;
         c1=0;
         j=0;
         while(j<v.size())
         {
             if((x-v[j])%k==0)
             {
             if(sign((x-v[j]),k)==1)
             {
                 c++;
             }
             }
             if(v[j]==x && v[j]!=k)
             {
                 c++;
             }
             j++;
         }
         //101212
         if(x==0)
         {
             c++;
         }
         cout<<c<<endl;
     }
     else
     {
         j=0;
         c=0;
         while(j<v.size())
         {
             if(v[j]==x)
             {
                c++;
             }
             j++;
         }
         if(x==0)
         {
             c++;
         }
         cout<<c<<endl;
     }
     i++;
     s.clear();
     v.clear();
 }
return(0);
}


#include<iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
bool sign(ll a,ll b)
{
    return (a>=0)^(b<0);
}
int main()
{
 ios_base::sync_with_stdio(false);
 cin.tie(NULL);
 ll x,i,j,k,n,t,f,c,c1;
 vector<ll> v;
 string s;
 cin>>t;
 i=0;
 while(i<t)
 {
     cin>>n>>x;
     cin>>s;
     f=0;
     j=0;
     c=0;
     while(j<n)
     {
         if(s[j]=='0')
         {
             c++;
         }
         else
         {
             c--;
         }
         if(c==x)
         {
             f=1;
         }
         v.pb(c);
         j++;
     }
     //v->-10-10-1
     k=v[v.size()-1];
     if(k==0 && f==1)
     {
         cout<<-1<<endl;
     }
     else if(k==x)
     {
         cout<<-1<<endl;
     }
     else if(k!=0)
     {
         c=0;
         c1=0;
         j=0;
         while(j<v.size())
         {
             if(v[j]==x%k && sign(x,k)==1)
             {
                 c++;
             }
             else if(v[j]==x)
             {
                 c++;
             }
             else if(v[j]==k)
             {
                 c1++;
             }
             j++;
         }
         if(x%k==0 && sign(x,k)==1)
         {
             c=c+c1;
         }
         cout<<c<<endl;
     }
     else
     {
         cout<<0<<endl;
     }
     //010010->1,0,1,2,1,2   10
     i++;
     s.clear();
     v.clear();
 }
return(0);
}


#include<iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
bool sign(ll a,ll b)
{
    return (a>=0)^(b<0);
}
int main()
{
 ios_base::sync_with_stdio(false);
 cin.tie(NULL);
 ll x,i,j,k,n,t,f,c,c1;
 vector<ll> v;
 string s;
 cin>>t;
 i=0;
 while(i<t)
 {
     cin>>n>>x;
     cin>>s;
     f=0;
     j=0;
     c=0;
     while(j<n)
     {
         if(s[j]=='0')
         {
             c++;
         }
         else
         {
             c--;
         }
         if(c==x)
         {
             f=1;
         }
         v.pb(c);
         j++;
     }
     //v->-10-10-1
     k=v[v.size()-1];
     if(k==0 && f==1)
     {
         cout<<-1<<endl;
     }
     else if(k!=0)
     {
         c=0;
         c1=0;
         j=0;
         f=0;
         while(j<v.size())
         {
             f=0;
             if((x-v[j])%k==0)
             {
             if(sign((x-v[j]),k)==1)
             {
                 f=1;
                 c++;
             }
             }
             if(f==0 && x==v[j])
             {
                 c++;
             }
             j++;
         }
         //101212
         if(x==0)
         {
             c++;
         }
         cout<<c<<endl;
     }
     else
     {
         j=0;
         c=0;
         while(j<v.size())
         {
             if(v[j]==x)
             {
                c++;
             }
             j++;
         }
         if(x==0)
         {
             c++;
         }
         cout<<c<<endl;
     }
     i++;
     s.clear();
     v.clear();
 }
return(0);
}


#include<iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
bool sign(ll a,ll b)
{
    return (a>=0)^(b<0);
}
int main()
{
 ios_base::sync_with_stdio(false);
 cin.tie(NULL);
 ll x,i,j,k,n,t,f,c,c1;
 vector<ll> v;
 string s;
 cin>>t;
 i=0;
 while(i<t)
 {
     cin>>n>>x;
     cin>>s;
     f=0;
     j=0;
     c=0;
     while(j<n)
     {
         if(s[j]=='0')
         {
             c++;
         }
         else
         {
             c--;
         }
         if(c==x)
         {
             f=1;
         }
         v.pb(c);
         j++;
     }
     //v->-10-10-1
     k=v[v.size()-1];
     if(k==0 && f==1)
     {
         cout<<-1<<endl;
     }
     else if(k==x)
     {
         cout<<-1<<endl;
     }
     else if(k!=0)
     {
         c=0;
         c1=0;
         j=0;
         while(j<v.size())
         {
             if((x-v[j])%k==0)
             {
             if(sign((x-v[j]),k)==1)
             {
                 c++;
             }
             }
             else if(v[j]==x)
             {
                 c++;
             }
             else if(v[j]==k)
             {
                 c1++;
             }
             j++;
         }
         if(x%k==0 && sign(x,k)==1)
         {
             c=c+c1;
         }
         if(x==0)
         {
             c++;
         }
         cout<<c<<endl;
     }
     else
     {
         j=0;
         c=0;
         while(j<v.size())
         {
             if(v[j]==x)
             {
                c++;
             }
             j++;
         }
         if(x==0)
         {
             c++;
         }
         cout<<c<<endl;
     }
     i++;
     s.clear();
     v.clear();
 }
return(0);
}


/*********************
*  Author: xuziyuan  *
*********************/

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define LL long long
#define pii pair <int,int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD=1e9+7;

LL t,n,x,ans,num0,num1,num[200020],mx,mn;
string s;

int main()
{
	cin>>t;
	rep(tn,t)
	{
		scanf("%lld%lld",&n,&x);
		cin>>s;
		num0=num1=ans=0;
		mx=-1e18;
		mn=1e18;
		memset(num,0,sizeof(num));
		if(x==0) ans++;
		rep(i,n)
		{
			if(s[i]=='0') num0++;
			else num1++;
			num[num0-num1+100010]++;
			if(x==num0-num1) ans++;
			mx=max(mx,num0-num1);
			mn=min(mn,num0-num1);
		}
		//cout<<ans<<endl;
		LL val=num0-num1;
		if(val!=0)
		{
			LL lb=x-mx,ub=x-mn,bas;
			if(lb>=0&&val>0)
			{
				bas=(lb+val-1)/val*val;
				for(LL i=bas;lb<=i&&i<=ub;i+=val) ans+=num[x-i+100010];//,cout<<i<<' '<<ans<<endl;
			}
			if(ub<=0&&val<0)
			{
				bas=(-ub-val-1)/(-val)*(-val);
				for(LL i=bas;lb<=i&&i<=ub;i+=val) ans+=num[x-i+100010];
			}
			cout<<ans<<endl;
		}
		else puts("-1");
	}
	return 0;
}


/*********************
*  Author: xuziyuan  *
*********************/

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define LL long long
#define pii pair <int,int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD=1e9+7;

LL t,n,x,ans,num0,num1,num[200020],mx,mn;
string s;

int main()
{
	cin>>t;
	rep(tn,t)
	{
		scanf("%lld%lld",&n,&x);
		cin>>s;
		num0=num1=ans=0;
		mx=-1e18;
		mn=1e18;
		memset(num,0,sizeof(num));
		if(x==0) ans++;
		rep(i,n)
		{
			if(s[i]=='0') num0++;
			else num1++;
			num[num0-num1+100010]++;
			if(x==num0-num1) ans++;
			mx=max(mx,num0-num1);
			mn=min(mn,num0-num1);
		}
		//cout<<ans<<endl;
		LL val=num0-num1;
		if(val!=0)
		{
			LL lb=x-mx,ub=x-mn,bas;
			if(lb>=0&&val>0)
			{
				bas=(lb+val-1)/val*val;
				for(LL i=bas;lb<=i&&i<=ub;i+=val) ans+=num[x-i+100010];//,cout<<i<<' '<<ans<<endl;
			}
			if(ub<=0&&val<0)
			{
				bas=(-ub-val-1)/(-val)*(-val);
				for(LL i=bas;lb<=i&&i<=ub;i+=val) ans+=num[x-i+100010];
			}
			if(lb<=0&&0<=ub) for(LL i=bas;lb<=i&&i<=ub;i+=val) ans+=num[x-i+100010];
			cout<<ans<<endl;
		}
		else
		{
			if(ans==0) cout<<0<<endl;
			else puts("-1");
		}
	}
	return 0;
}


/*********************
*  Author: xuziyuan  *
*********************/

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define LL long long
#define pii pair <int,int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD=1e9+7;

LL t,n,x,ans,num0,num1,num[200020],mx,mn;
string s;

int main()
{
	cin>>t;
	rep(tn,t)
	{
		scanf("%lld%lld",&n,&x);
		cin>>s;
		num0=num1=ans=0;
		mx=-1e18;
		mn=1e18;
		memset(num,0,sizeof(num));
		if(x==0) ans++;
		rep(i,n)
		{
			if(s[i]=='0') num0++;
			else num1++;
			num[num0-num1+100010]++;
			if(x==num0-num1) ans++;
			mx=max(mx,num0-num1);
			mn=min(mn,num0-num1);
		}
		//cout<<ans<<endl;
		LL val=num0-num1;
		if(val!=0)
		{
			LL lb=x-mx,ub=x-mn,bas;
			if(lb>=0&&val>0)
			{
				bas=(lb+val-1)/val*val;
				for(LL i=bas;lb<=i&&i<=ub;i+=val) ans+=num[x-i+100010];//,cout<<i<<' '<<ans<<endl;
			}
			if(ub<=0&&val<0)
			{
				bas=(-ub-val-1)/(-val)*(-val);
				for(LL i=bas;lb<=i&&i<=ub;i+=val) ans+=num[x-i+100010];
			}
			cout<<ans<<endl;
		}
		else
		{
			if(ans==0) cout<<0<<endl;
			else puts("-1");
		}
	}
	return 0;
}


/*********************
*  Author: xuziyuan  *
*********************/

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define LL long long
#define pii pair <int,int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD=1e9+7;

LL t,n,x,ans,num0,num1,num[400040],mx,mn;
string s;

int main()
{
	cin>>t;
	rep(tn,t)
	{
		scanf("%lld%lld",&n,&x);
		cin>>s;
		num0=num1=ans=0;
		mx=-1e18;
		mn=1e18;
		memset(num,0,sizeof(num));
		if(x==0) ans++;
		rep(i,n)
		{
			if(s[i]=='0') num0++;
			else num1++;
			num[num0-num1+200020]++;
			if(x==num0-num1) ans++;
			mx=max(mx,num0-num1);
			mn=min(mn,num0-num1);
		}
		//cout<<ans<<endl;
		LL val=num0-num1;
		if(val!=0)
		{
			LL lb=x-mx,ub=x-mn,bas;
			if(lb>=0&&val>0)
			{
				bas=(lb+val-1)/val*val;
				if(bas==0) bas=val;
				for(LL i=bas;lb<=i&&i<=ub;i+=val) ans+=num[x-i+200020];//,cout<<i<<' '<<ans<<endl;
			}
			if(ub<=0&&val<0)
			{
				bas=(-ub-val-1)/(-val)*val;
				if(bas==0) bas=val;
				//cout<<bas<<endl;
				for(LL i=bas;lb<=i&&i<=ub;i+=val) ans+=num[x-i+200020];
			}
			if(lb<=0&&0<=ub) for(LL i=1;lb<=i&&i<=ub;i+=val) ans+=num[x-i+200020];
			cout<<ans<<endl;
		}
		else
		{
			if(ans==0) cout<<0<<endl;
			else puts("-1");
		}
	}
	return 0;
}


/*********************
*  Author: xuziyuan  *
*********************/

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define LL long long
#define pii pair <int,int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD=1e9+7;

LL t,n,x,ans,num0,num1,num[400040],mx,mn;
string s;

int main()
{
	cin>>t;
	rep(tn,t)
	{
		scanf("%lld%lld",&n,&x);
		cin>>s;
		num0=num1=ans=0;
		mx=-1e18;
		mn=1e18;
		memset(num,0,sizeof(num));
		if(x==0) ans++;
		rep(i,n)
		{
			if(s[i]=='0') num0++;
			else num1++;
			num[num0-num1+200020]++;
			if(x==num0-num1) ans++;
			mx=max(mx,num0-num1);
			mn=min(mn,num0-num1);
		}
		LL val=num0-num1;
		if(val!=0)
		{
			LL lb=x-mx,ub=x-mn,bas;
			if(lb>=0&&val>0)
			{
				bas=(lb+val-1)/val*val;
				if(bas==0) bas=val;
				for(LL i=bas;lb<=i&&i<=ub;i+=val) ans+=num[x-i+200020];
			}
			if(ub<=0&&val<0)
			{
				bas=(-ub-val-1)/(-val)*val;
				if(bas==0) bas=val;
				//cout<<bas<<endl;
				for(LL i=bas;lb<=i&&i<=ub;i+=val) ans+=num[x-i+200020];
			}
			if(lb<=0&&0<=ub) for(LL i=val;lb<=i&&i<=ub;i+=val) ans+=num[x-i+200020];
			cout<<ans<<endl;
		}
		else
		{
			if(ans==0) cout<<0<<endl;
			else puts("-1");
		}
	}
	return 0;
}