#include<bits/stdc++.h>
#include<bits/stdc++.h>
#include <vector>
#define ll long long int
#define ve vector<int>
#define vell vector<ll>
#define ve_pa vector<pair<ll,ll> >
#define pb push_back
#define mod 1000000007
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
using namespace std;
int n,m;
map<int,bool>u[200005];
bitset<2000005>bit;
void dfs(int v){
	bit[v]=0;
	for(int i=bit._Find_first();i<bit.size();i=bit._Find_next(i)) if(!u[v][i])dfs(i);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
for(int i=0;i<m;i++)
{
	int x,y;cin>>x>>y;
	u[x][y]=u[y][x]=1;
}
int ans=0;
for(int i=1;i<=n;i++)
bit[i]=1;

for(int i=1;i<=n;i++)
{
	if(bit[i])
	{
		dfs(i);
		ans++;
	}
}
cout<<ans-1;

}


#include<bits/stdc++.h>
#include<bits/stdc++.h>
#include <vector>
#define ll long long int
#define ve vector<int>
#define vell vector<ll>
#define ve_pa vector<pair<ll,ll> >
#define pb push_back
#define mod 1000000007
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
using namespace std;
int n,m;
map<int, bool>u[200005];
bitset<200005> bit;
void dfs(int v){
	bit[v]=0;
	for(int i=bit._Find_first();i<bit.size();i=bit._Find_next(i)) if(!u[v][i])dfs(i);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
for(int i=0;i<m;i++)
{
	int x,y;cin>>x>>y;
	u[x][y]=u[y][x] =1;
}
int ans=0;
for(int i=1;i<=n;i++)
bit[i]=1;

for(int i=1;i<=n;i++)
{
	if(bit[i])
	{
		dfs(i);
		ans++;
	}
}
cout<<ans-1;

}


#include<bits/stdc++.h>
#include<bits/stdc++.h>
#include <vector>
#define ll long long int
#define ve vector<int>
#define vell vector<ll>
#define ve_pa vector<pair<ll,ll> >
#define pb push_back
#define mod 1000000007
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
using namespace std;
map<int,bool>g[200005];
bitset<2000005> v;
void dfs(int s)
{
	v[s]=0;
	for(int i=v._Find_first();i<v.size();i=v._Find_next(i))
	{
		if(g[s][i]==0)
		{
			dfs(i);
		}
	}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;cin>>n>>m;
for(int i=0;i<m;i++)
{
	int u,v;cin>>u>>v;
	g[u][v]=1;
	g[v][u]=1;
}
int ans=0;
for(int i=1;i<=n;i++)
v[i]=1;

for(int i=1;i<=n;i++)
{
	if(v[i])
	{
		dfs(i);
		ans++;
	}
}
cout<<ans-1;

}


#include<bits/stdc++.h>
#include<bits/stdc++.h>
#include <vector>
#define ll long long int
#define ve vector<int>
#define vell vector<ll>
#define ve_pa vector<pair<ll,ll> >
#define pb push_back
#define mod 1000000007
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
using namespace std;
unordered_map<int,int>g[200001];
bitset<2000000> v;
void dfs(int s)
{
	v[s]=0;
	for(int i=v._Find_first();i<v.size();i=v._Find_next(i))
	{
		if(g[s][i]==0)
		{
			dfs(i);
		}
	}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;cin>>n>>m;
for(int i=0;i<m;i++)
{
	int u,v;cin>>u>>v;
	g[u][v]=1;
	g[v][u]=1;
}
int ans=0;
for(int i=1;i<=n;i++)
v[i]=1;

for(int i=1;i<=n;i++)
{
	if(v[i])
	{
		dfs(i);
		ans++;
	}
}
cout<<ans-1;

}


#include<bits/stdc++.h>
#include<bits/stdc++.h>
#include <vector>
#define ll long long int
#define ve vector<int>
#define vell vector<ll>
#define ve_pa vector<pair<ll,ll> >
#define pb push_back
#define mod 1000000007
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
using namespace std;
int n,m;
map<int,bool>u[200005];
bitset<2000005>bit;

void dfs(int v)
{
	bit[v]=0;
	for(int i=bit._Find_first();i<bit.size();i=bit._Find_next(i))
		if(!u[v][i])
		{
			dfs(i);
		}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
for(int i=0;i<m;i++)
{
	int x,y;cin>>x>>y;
	u[x][y]=u[y][x]=1;
}
int ans=0;
for(int i=1;i<=n;i++)
bit[i]=1;

for(int i=1;i<=n;i++)
{
	if(bit[i])
	{
		dfs(i);
		ans++;
	}
}
cout<<ans-1;

}


#include<bits/stdc++.h>
#include<bits/stdc++.h>
#include <vector>
#define ll long long int
#define ve vector<int>
#define vell vector<ll>
#define ve_pa vector<pair<ll,ll> >
#define pb push_back
#define mod 1000000007
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
using namespace std;
map<int,int>g[200001];
bitset<2000000> v;
void dfs(int s)
{
	v[s]=0;
	for(int i=v._Find_first();i<v.size();i=v._Find_next(i))
	{
		if(g[s][i]==0)
		{
			dfs(i);
		}
	}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;cin>>n>>m;
for(int i=0;i<m;i++)
{
	int u,v;cin>>u>>v;
	g[u][v]=1;
	g[v][u]=1;
}
int ans=0;
for(int i=1;i<=n;i++)
v[i]=1;

for(int i=1;i<=n;i++)
{
	if(v[i])
	{
		dfs(i);
		ans++;
	}
}
cout<<ans-1;

}


#include<bits/stdc++.h>
#include<bits/stdc++.h>
#include <vector>
#define ll long long int
#define ve vector<int>
#define vell vector<ll>
#define ve_pa vector<pair<ll,ll> >
#define pb push_back
#define mod 1000000007
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
using namespace std;
int n,m;
map<int,bool>u[200005];
bitset<200005>bit;
void dfs(int v){
	bit[v]=0;
	for(int i=bit._Find_first();i<bit.size();i=bit._Find_next(i)) if(!u[v][i])dfs(i);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
for(int i=0;i<m;i++)
{
	int x,y;cin>>x>>y;
	u[x][y]=u[y][x] =1;
}
int ans=0;
for(int i=1;i<=n;i++)
bit[i]=1;

for(int i=1;i<=n;i++)
{
	if(bit[i])
	{
		dfs(i);
		ans++;
	}
}
cout<<ans-1;

}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
// #define int short int
using namespace std;
vector <int> parents;

int myFind(int i) {
    if (parents[i] == i) return i;
    return parents[i] = myFind(parents[i]);
}
void merge(int a, int b) {
    int from = myFind(a);
    int to = myFind(b);
    parents[from] = parents[to];
}
signed main () {
    int n, m, a, b;
    cin >> n >> m;
    vector <pair <int, int> > paths;
    set <pair <int, int> > numbers;
    for (int i = 0; i < m; i++) {
        cin >> a >> b;
        a--;
        b--;
        if (a > b) swap(a, b);
        numbers.insert(mp(a, b));
    }

    vector <vector <bool> > points(n, vector<bool>(n, true));
    for (auto it = numbers.begin(); it != numbers.end(); it++) {
        pair <int, int> tmp = *it;
        points[tmp.first][tmp.second] = false;
    }
    numbers.clear();

    for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
        if (points[i][j]) paths.pb(mp(i, j));

    points.clear();
    for (int i = 0; i < n; i++)
        parents.pb(i);

    for (int i = 0; i < paths.size(); i++) {
        if (myFind(paths[i].first) == myFind(paths[i].second)) continue;
        merge(paths[i].first, paths[i].second);
    }
    paths.clear();
    vector <bool> seen(n, false);
    int ans = 0;
    for (int i = 0; i < n; i++) {
        if (seen[myFind(i)]) continue;
        seen[myFind(i)] = true;
        ans++;
    }
    cout << ans - 1 << '\n';

    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define int unsigned int
using namespace std;
vector <int> parents;

int myFind(int i) {
    if (parents[i] == i) return i;
    return parents[i] = myFind(parents[i]);
}
void merge(int a, int b) {
    int from = myFind(a);
    int to = myFind(b);
    parents[from] = parents[to];
}
signed main () {
    int n, m, a, b;
    cin >> n >> m;
    vector <pair <int, int> > paths;
    vector <vector <int> > points(n);
    for (int i = 0; i < m; i++) {
        cin >> a >> b;
        a--;
        b--;
        if (a > b) swap(a, b);
        points[a].pb(b);
    }
    for (int i = 0; i < n; i++) {
        vector <bool> seen(n, true);
        for (int j = 0; j < points[i].size(); j++)
            seen[points[i][j]] = false;

        points[i].clear();
        for (int j = 0; j < n; j++)
            if (seen[j] && i < j) paths.pb(mp(i, j));
    }
    // for (auto it = numbers.begin(); it != numbers.end(); it++) {
    //     pair <int, int> tmp = *it;
    //     points[tmp.first][tmp.second] = false;
    // }
    // numbers.clear();

    // for (int i = 0; i < n; i++)
    // for (int j = i + 1; j < n; j++)
    //     if (points[i][j]) paths.pb(mp(i, j));

    // points.clear();

    for (int i = 0; i < n; i++)
        parents.pb(i);

    for (int i = 0; i < paths.size(); i++) {
        if (myFind(paths[i].first) == myFind(paths[i].second)) continue;
        merge(paths[i].first, paths[i].second);
    }
    paths.clear();
    vector <bool> seen(n, false);
    int ans = 0;
    for (int i = 0; i < n; i++) {
        if (seen[myFind(i)]) continue;
        seen[myFind(i)] = true;
        ans++;
    }
    cout << ans - 1 << '\n';

    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define int unsigned int
using namespace std;
vector <int> parents;

int myFind(int i) {
    if (parents[i] == i) return i;
    return parents[i] = myFind(parents[i]);
}
void merge(int a, int b) {
    int from = myFind(a);
    int to = myFind(b);
    parents[from] = parents[to];
}
signed main () {
    int n, m, a, b;
    cin >> n >> m;
    vector <pair <int, int> > paths;
    set <pair <int, int> > numbers;
    for (int i = 0; i < m; i++) {
        cin >> a >> b;
        a--;
        b--;
        if (a > b) swap(a, b);
        numbers.insert(mp(a, b));
    }

    vector <vector <bool> > points(n, vector<bool>(n, true));
    for (auto it = numbers.begin(); it != numbers.end(); it++) {
        pair <int, int> tmp = *it;
        points[tmp.first][tmp.second] = false;
    }
    numbers.clear();

    for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
        if (points[i][j]) paths.pb(mp(i, j));

    points.clear();
    for (int i = 0; i < n; i++)
        parents.pb(i);

    for (int i = 0; i < paths.size(); i++) {
        if (myFind(paths[i].first) == myFind(paths[i].second)) continue;
        merge(paths[i].first, paths[i].second);
    }
    paths.clear();
    vector <bool> seen(n, false);
    int ans = 0;
    for (int i = 0; i < n; i++) {
        if (seen[myFind(i)]) continue;
        seen[myFind(i)] = true;
        ans++;
    }
    cout << ans - 1 << '\n';

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=200100;
vector<long long>num[maxn];
long long check[maxn];
long long a[maxn];
long long n;
int main()
{
    cin>>n;
    cin>>a[1];
    int flag=1;
    num[flag].push_back(a[1]);
    check[flag++]=a[1];
    for(int i=2; i<=n; i++)
    {
        cin>>a[i];
        if(check[flag-1]>a[i])
        {
            num[flag].push_back(a[i]);
            check[flag++]=a[i];
            continue;
        }
        if(check[1]<a[i])
        {
            num[1].push_back(a[i]);
            check[1]=a[i];
            continue;
        }
        int left=1,right=flag-1;
        while(right-left>3)
        {
            int mid=(left+right)/2;
            if(check[mid]<a[i])
                right=mid;
            else
                left=mid;
        }
        int ans=right;
        for(int j=right; j>=left; j--)
        {
            if(check[j]<a[i])
                ans=j;
        }
        num[ans].push_back(a[i]);
        check[ans]=a[i];
    }
    for(int i=1; i<flag; i++)
    {
        for(int j=0; j<=(num[i].size()-1); j++)
        {
            if(j==0)
            {
                cout<<num[i][j];
            }
            else
            {
                cout<<" "<<num[i][j];
            }
        }
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;

int head[maxn],nxt[maxn],E[maxn],Ecnt;
int f[maxn],fn;
int a[maxn];
int tmp[maxn],num;
int n;

void Add_Edge(int x,int y) {nxt[++Ecnt]=head[x];head[x]=Ecnt;E[Ecnt]=y;}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	f[1]=a[1];fn=1;
	Add_Edge(1,a[1]);
	for (int i=2;i<=n;i++)
	{
		int l=1,r=fn,mid;
		while (l<r)
		{
			mid=(l+r)>>1;
			if (a[i]<=f[mid]) l=mid+1;else r=mid;
		}
		if (l==fn && a[i]<=f[l]) f[++fn]=a[i],Add_Edge(fn,a[i]);
		else f[l]=a[i],Add_Edge(l,a[i]);
	}
	for (int i=1;i<=fn;i++)
	{
		num=0;
		for (int j=head[i];j;j=nxt[j]) tmp[++num]=E[j];
		for (int j=num;j;j--) printf("%d%c",tmp[j],(j==1)?'\n':' ');
	}
	return 0;
}


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

int a[200010], b[200010];
vector<int> p[200010];
int n, k;

int main() {
    scanf("%d",&n);
    for (int i = 1; i <= n; i++) {
        scanf("%d",&a[i]);
        k = lower_bound(b + 1,b + 1 + n,a[i]) - b;
        k--;
        b[k] = a[i];
        p[k].push_back(a[i]);
    }
    for (int i = n; i >= 1; i--) {
        for (int j = 0; j < p[i].size(); j++) {
            if (j == p[i].size() - 1) printf("%d\n",p[i][j]);
            else printf("%d ",p[i][j]);
        }
    }
}


#include <bits/stdc++.h>

using namespace std;
const int MAXN=200000+10;
int a[MAXN],n;
vector<int> v[MAXN];

bool cmp(int x, int y)
{
    return (a[x]<a[y]);
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        v[i].push_back(0);
    }
    for (int i=1;i<=n;i++)
    {
        int l=1,r=n;
        while (l<r)
        {
            int mid=(l+r)/2;
            if (v[mid].back()<=a[i]) r=mid;
            else l=mid+1;
        }
        v[l].push_back(a[i]);
    }
    for (int i=1;i<=n;i++) if (v[i].size()>1)
    {
        for(int j=1;j<v[i].size();j++) printf("%d ",v[i][j]);
        printf("\n");
    }
    //for (int i=1;i<=n;i++) cout<<b[i]<<" ";
    return 0;
}


//#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <ctime>
#include <numeric>
#include <vector>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <complex>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <string>
#include <sstream>
#include <set>
#include <stack>
#include <queue>
using namespace std;
template<class T> inline T sqr(T x)
{
    return x * x;
}
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
typedef pair<LL, LL> PLL;
typedef pair<LL, int> PLI;
typedef pair<LD, LD> PDD;
#define MP make_pair
#define PB push_back
#define sz(x) ((int)(x).size())
#define clr(ar,val) memset(ar, val, sizeof(ar))
#define istr stringstream
#define FOR(i,n) for(int i=0;i<(n);++i)
#define forIt(mp,it) for(__typeof(mp.begin()) it = mp.begin();it!=mp.end();it++)
const double EPS = 1e-6;
const int INF = 0x3fffffff;
const LL LINF = INF * 1ll * INF;
const double PI = acos(-1.0);

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lowbit(u) (u&(-u))
#define MAXN 200005
using namespace std;

int a[MAXN];
vector <int> ans[MAXN];

int biSearch(int val,int sz)
{
    int l=0,r=sz;
    while(l<r)
    {
        int mid=(l+r)>>1;
        //cout << ans[mid].back() << endl;
        if(ans[mid].back()<=val)
            r=mid;
        else
            l=mid+1;
    }
    if(r==sz)
        return -1;
    return r;
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);
    int cnt=0;
    for(int i=0; i<n; i++)
    {
        if(cnt==0)
        {
            ans[cnt++].PB(a[i]);
            continue;
        }
        int p=biSearch(a[i],cnt);
        if(p==-1)
            ans[cnt++].PB(a[i]);
        else
            ans[p].PB(a[i]);
    }
    for(int i=0; i<cnt; i++)
    {
        for(int j=0; j<ans[i].size(); j++)
        {
            if(j==0)
                printf("%d",ans[i][j]);
            else
                printf(" %d",ans[i][j]);
        }
        puts("");
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
#define R register

using namespace std;

int n,ans[200010],tot,next[200010],head[200010],tol,l[200010],l1,last[200010];

inline int read()
{
	R char c=getchar();R int num=0;
	while (c<48||c>57) c=getchar();
	while (c>47&&c<58) num=num*10+c-48,c=getchar();
	return(num);
 }

int main()
{
	n=read();
	for (R int i=1;i<=n;i++)
	{
		R int x=read();
		R int k=lower_bound(last+1+n-tot,last+1+n,x)-last;
		if (k==n+1) k=n; else if (k==n-tot+1) k=n-tot; else k--;
		if (last[k]==0) tot++;
		last[k]=ans[++tol]=x;
		next[tol]=head[k];
		head[k]=tol;
	}
	for (R int i=n;i>=n-tot+1;i--)
	{
		l1=0;
		for (R int j=head[i];j;j=next[j]) l[++l1]=ans[j];
		for (R int j=l1;j;j--) printf("%d ",l[j]);
		printf("\n");
	}
}


#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n;
int a[400010];
int vis[400010];
int Next[400010];
int Max[2000000],num;
void builine(int l,int r,int nu);
void change(int l,int r,int nu,int x);
int Find(int l,int r,int nu,int L,int R,int x);
int main()
{
	int i,j,k;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	a[n+1]=1000000009;
	num=0;builine(1,n+1,1);
	for(i=1;i<=n;i++)
		if(!vis[i]){
			num=i;vis[num]=1;
			while(num!=n+1){
				j=Find(1,n+1,1,num+1,n+1,a[num]);
				Next[num]=j;vis[j]=1;
				num=j;
				change(1,n+1,1,num);
			}
		}
	memset(vis,0,sizeof(vis));
	for(i=1;i<=n;i++)
		if(!vis[i]){
			num=i;
			while(num!=n+1){
				printf("%d ",a[num]);
				vis[num]=1;
				num=Next[num];
			}
			printf("\n");
		}
	return 0;
}
void builine(int l,int r,int nu){
	if(l==r){
		Max[nu]=a[++num];
		return ;
	}
	int mid=(l+r)>>1;
	builine(l,mid,nu<<1);
	builine(mid+1,r,nu<<1|1);
	Max[nu]=max(Max[nu<<1],Max[nu<<1|1]);
}
void change(int l,int r,int nu,int x){
	if(l==r){
		Max[nu]=0;return ;
	}
	int mid=(l+r)>>1;
	if(x<=mid)
		change(l,mid,nu<<1,x);
	else
		change(mid+1,r,nu<<1|1,x);
	Max[nu]=max(Max[nu<<1],Max[nu<<1|1]);
}
int Find(int l,int r,int nu,int L,int R,int x){
	if(l==r&&Max[nu]>x)
		return l;
	if(l==r&&Max[nu]<=x)
		return n+1;
	int mid=(l+r)>>1;
	if(L<=l&&r<=R){
		if(Max[nu<<1]>x)
			return Find(l,mid,nu<<1,L,R,x);
		return Find(mid+1,r,nu<<1|1,L,R,x);
	}
	int retl=n+1,retr=n+1;
	if(L<=mid)
		retl=Find(l,mid,nu<<1,L,R,x);
	if(R>mid)
		retr=Find(mid+1,r,nu<<1|1,L,R,x);
	if(retl<retr)
		return retl;
	return retr;
}


#include<bits/stdc++.h>

using namespace std;

const int N=200005;

vector<int>mp[N];

int a[N];

template<class T>

inline bool scan(T &ret)

{

    char c;

    int sgn;

    if(c=getchar(),c==EOF) return 0;

    while(c!='-'&&(c<'0'||c>'9')) c=getchar();

    sgn=(c=='-')?-1:1;

    ret=(c=='-')?0:(c-'0');

    while(c=getchar(),c>='0'&&c<='9')ret=ret*10+(c-'0');

    ret*=sgn;

    return 1;

}

int main()

{

    int n;

    scan(n);

    int o=0;

    int i,j;

    for(i=0;i<=n-1;i++)

    {

        int x;

        scan(x);

        if(i==0)

        {

            a[o]=x;

            mp[o].push_back(x);

            o++;

        }

        else

        {

            int index=lower_bound(a,a+o,x,greater<int>())-a;

            //printf("index = %d,,o = %d\n",index,o);

            if(index==o)

            {

                a[o]=x;

                mp[o].push_back(x);

                o++;

            }

            else

            {

                a[index]=x;

                mp[index].push_back(x);

            }

        }

    }

    int len;

    for(i=0; i<=o-1; i++)

    {

        len=(int)mp[i].size()-1;

        for(j=0; j<=len; j++)

        {

            printf("%d ",mp[i][j]);

        }

        printf("\n");

    }

    return 0;

}


#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
#include <queue>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <utility>
#include <functional>
using namespace std;
#define ll long long
#define ull unsigned ll
/*Fast I/O goes here*/
const double EPS = 1e-8;
const ll PR = 19260817;
const ll inv2 = 500000004;
const ll inv3 = 333333336;
int n, a[200005], t[200005*4];
void build(int i=1, int l=1, int r=n) {
	if(l==r) {
		t[i]=a[l];
		return;
	}
	int mid=(l+r)>>1;
	build(i*2, l, mid);
	build(i*2+1, mid+1, r);
	t[i]=max(t[i*2], t[i*2+1]);
}
void modify(int x, int val, int i=1, int l=1, int r=n) {
	if(l==x&&r==x) {
		t[i]=val;
		return;
	}
	int mid=(l+r)>>1;
	if(x<=mid)
		modify(x, val, i*2, l, mid);
	else
		modify(x, val, i*2+1, mid+1, r);
	t[i]=max(t[i*2], t[i*2+1]);
}
int query(int val, int i=1, int l=1, int r=n) {
	if(t[i]<=val)
		return 0;
	if(l==r)
		return l;
	int mid=(l+r)>>1;
	if(t[i*2]>val)
		return query(val, i*2, l, mid);
	else
		return query(val, i*2+1, mid+1, r);
}
int main() {
	scanf("%d", &n);
	for(int i=1; i<=n; i++)
		scanf("%d", &a[i]);
	build();
	int startid=1;
	for(; ; ) {
		for(; startid<=n&&!a[startid]; startid++);
		if(startid>n) break;
		int i=startid;
		for(; ; ) {
			printf("%d ", a[i]);
			int j=query(a[i]);
			a[i]=0;
			modify(i, 0);
			if(!j) break;
			i=j;
		}
		puts("");
	}
	return 0;
}


//  LINK ZADATKA: http://codeforces.com/contest/847/problem/B

#include <queue>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <fstream>
#include <cstring>
#include <string>
#include <climits>

using namespace std;

typedef long long ll;
typedef complex<double> point;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;

#define FOR(k,a,b) for(int k=(a); k<=(b); ++k)
#define REP(k,a) for(int k=0; k<(a);++k)
#define SZ(a) int((a).size())
#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define MP make_pair
#define INF 999999999
#define INFLONG 1000000000000000000
#define MOD 1000000007
#define MAX 100
#define ITERS 100
#define MAXM 200000
#define MAXN 1000000
#define _gcd __gcd
#define eps 1e-9

vector<list<ll> > v;
ll broj = 0;
int main(){
	ll n;
	list<ll> stek;
	scanf("%I64d", &n);
	v.insert(v.begin(), n, stek);
	ll a;
	ll brak;
	REP(i, n){
		scanf("%I64d", &a);
		if(i == 0) v[broj].push_back(a);
		else{
			broj = 0;
			while(v[broj].back() >= a){
				broj++;
				if(v[broj].empty()) break;
			}
			v[broj].push_back(a);
			//printf("%I64d\n", broj);
		}
	}
	//printf("%I64d", v[0].top());
	REP(i, v.size()){
		while(!v[i].empty()){
			printf("%I64d ", v[i].front());
			v[i].pop_front();
		}

		printf("\n");
	}

}


#include <queue>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <fstream>
#include <cstring>
#include <string>
#include <climits>

using namespace std;

typedef long long ll;
typedef complex<double> point;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;

#define FOR(k,a,b) for(int k=(a); k<=(b); ++k)
#define REP(k,a) for(int k=0; k<(a);++k)
#define SZ(a) int((a).size())
#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define MP make_pair
#define INF 999999999
#define INFLONG 1000000000000000000
#define MOD 1000000007
#define MAX 100
#define ITERS 100
#define MAXM 200000
#define MAXN 1000000
#define _gcd __gcd
#define eps 1e-9

vector<list<ll> > v;
int broj = 0, clanova;
vector<int> baka;

int bin(int x){
	int gornjaGranica = baka.size(), donjaGranica = 0, sredina;
	while(gornjaGranica != donjaGranica){
		sredina = (gornjaGranica + donjaGranica) / 2;
		if(baka[sredina] <= x) gornjaGranica = sredina;
		else donjaGranica = sredina+1;
	}
	return donjaGranica;
}

int main(){
	ll n;
	list<ll> stek;
	scanf("%I64d", &n);
	v.insert(v.begin(), n, stek);

	ll a;
	ll brak, saft;
	REP(i, n){
		scanf("%I64d", &a);
		//printf("Velicina: %d\n", baka.size());
		if(i == 0){
			v[broj].push_back(a);
			baka.push_back(a);
		}else{
			broj = bin(a);
			//if(broj == baka.size()) broj--;
			//printf("Broj je%d %I64d\n", broj, v[broj].back());
			if(broj < baka.size()){

				v[broj].push_back(a);
			    baka[broj] = a;
			} else{
				baka.push_back(a);
				v[broj].push_back(a);
			}
			//printf("Broj je%d %I64d\n", broj, v[broj].back());
		}
		//REP(j, baka.size()) printf("%d ", baka[j]);
		//printf("\n");
	}
	//printf("%I64d", v[0].top());
	REP(i, v.size()){
		while(!v[i].empty()){
			printf("%I64d ", v[i].front());
			v[i].pop_front();
		}

		printf("\n");
	}

}


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

const int maxn = 1e5 + 5;
int a[2 * maxn],b[2 * maxn];
vector<int>G[2 * maxn];

int main(){
	int n;
	scanf("%d",&n);
	for(int i = 1;i <= n;i++){
		scanf("%d",&a[i]);
		int pos = lower_bound(b + 1,b + 1 + n,a[i]) - b;
		pos--;
		b[pos] = a[i];
		G[pos].push_back(a[i]);
	}
	for(int i = n;i >= 1;i--){
		for(int j = 0;j < G[i].size();j++){
			if(j == G[i].size() - 1) printf("%d\n",G[i][j]);
			else printf("%d ",G[i][j]);
		}
	}
}


#include <bits/stdc++.h>

#define TASK "b"
#define FI TASK".inp"
#define FO TASK".out"

const int N = 1e6+1;
int tree[N], a[N], n;

void build(int i, int l, int r)
{
	if (l < r){
		int mid = (l+r)>>1;
		build(i*2, l, mid);
		build(i*2+1, mid+1, r);
		tree[i] = std::max(tree[i*2], tree[i*2+1]);
	}else{
		tree[i] = a[l];
	}
}

int get(int i, int l, int r, int p, int v)
{
	// printf("%d %d %d %d %d\n", i, l, r, p, v);
	if (r < p){
		// printf("YES1\n");
		return -1;
	}
	if (l == r){
		if (a[l] > v)
			return l;
		else{
			// printf("YES2\n");
			return -1;
		}
	}
	int mid = (l+r)>>1;
	if (l >= p){
		if (tree[i] < v){
			// printf("YES3\n");
			return -1;
		}else{
			if (tree[i*2] >= v)
				return get(i*2, l, mid, p, v);
			else
				return get(i*2+1, mid+1, r, p, v);
		}
	}
	int a = get(i*2, l, mid, p, v);
	if (a == -1)
		a = get(i*2+1, mid+1, r, p, v);
	return a;
}

void change(int i, int l, int r, int p, int v)
{
	if (r < p || l > p){
		return;
	}
	if (l == r)
		tree[i] = v;
	else{
		int mid = (l+r)>>1;
		change(i*2, l, mid, p, v);
		change(i*2+1, mid+1, r, p, v);
		tree[i] = std::max(tree[i*2], tree[i*2+1]);
	}
}

int main()
{
    #ifndef ONLINE_JUDGE
  	freopen(FI, "r", stdin);
  	freopen(FO, "w", stdout);
  	#endif
  	scanf("%d\n", &n);
  	for (int i = 0; i < n; i++){
  		scanf("%d", a+i);
  	}
  	build(1, 0, n-1);
  	int c = 0;
  	while (c < n){
  		// printf("*****NEWWWWW*****\n");
  		int start = 0, value = 0, tmp = 0;
  		while (tmp != -1){
  			tmp = get(1, 0, n-1, start, value);
  			// printf("%d\n*****\n", tmp);
  			if (tmp != -1){
  				c++;
  			  	printf("%d ", a[tmp]);
  			  	start = tmp;
  			  	value = a[tmp];
  			  	change(1, 0, n-1, tmp, -1);
  			}
  		}
  		printf("\n");
  	}
    return 0;
}


#include <bits/stdc++.h>

#define TASK "b"
#define FI TASK".inp"
#define FO TASK".out"

const int N = 1e7+1;
int tree[N], a[N], n;

void build(int i, int l, int r)
{
	if (l < r){
		int mid = (l+r)>>1;
		build(i*2, l, mid);
		build(i*2+1, mid+1, r);
		tree[i] = std::max(tree[i*2], tree[i*2+1]);
	}else{
		tree[i] = a[l];
	}
}

int get(int i, int l, int r, int p, int v)
{
	// printf("%d %d %d %d %d\n", i, l, r, p, v);
	if (r < p){
		// printf("YES1\n");
		return -1;
	}
	if (l == r){
		if (a[l] > v)
			return l;
		else{
			// printf("YES2\n");
			return -1;
		}
	}
	int mid = (l+r)>>1;
	if (l >= p){
		if (tree[i] < v){
			// printf("YES3\n");
			return -1;
		}else{
			if (tree[i*2] >= v)
				return get(i*2, l, mid, p, v);
			else
				return get(i*2+1, mid+1, r, p, v);
		}
	}
	int a = get(i*2, l, mid, p, v);
	if (a == -1)
		a = get(i*2+1, mid+1, r, p, v);
	return a;
}

void change(int i, int l, int r, int p, int v)
{
	if (r < p || l > p){
		return;
	}
	if (l == r)
		tree[i] = v;
	else{
		int mid = (l+r)>>1;
		change(i*2, l, mid, p, v);
		change(i*2+1, mid+1, r, p, v);
		tree[i] = std::max(tree[i*2], tree[i*2+1]);
	}
}

int main()
{
    #ifndef ONLINE_JUDGE
  	freopen(FI, "r", stdin);
  	freopen(FO, "w", stdout);
  	#endif
  	scanf("%d\n", &n);
  	for (int i = 0; i < n; i++){
  		scanf("%d", a+i);
  	}
  	build(1, 0, n-1);
  	int c = 0;
  	while (c < n){
  		// printf("*****NEWWWWW*****\n");
  		int start = 0, value = 0, tmp = 0;
  		while (tmp != -1){
  			tmp = get(1, 0, n-1, start, value);
  			// printf("%d\n*****\n", tmp);
  			if (tmp != -1){
  				c++;
  			  	printf("%d ", a[tmp]);
  			  	start = tmp+1;
  			  	value = a[tmp];
  			  	change(1, 0, n-1, tmp, -1);
  			}
  		}
  		printf("\n");
  	}
    return 0;
}


#include <bits/stdc++.h>

#define TASK "b"
#define FI TASK".inp"
#define FO TASK".out"

const int N = 1e7+1;
int tree[N], a[N], n;

void build(int i, int l, int r)
{
	if (l < r){
		int mid = (l+r)>>1;
		build(i*2, l, mid);
		build(i*2+1, mid+1, r);
		tree[i] = std::max(tree[i*2], tree[i*2+1]);
	}else{
		tree[i] = a[l];
	}
}

int get(int i, int l, int r, int p, int v)
{
	// printf("%d %d %d %d %d\n", i, l, r, p, v);
	if (r < p){
		// printf("YES1\n");
		return -1;
	}
	if (l == r){
		if (tree[i] > v){
			// printf("YES2 %d\n", tree[i]);
			return l;
		}
		else{
			// printf("YES3\n");
			return -1;
		}
	}
	int mid = (l+r)>>1;
	if (l >= p){
		if (tree[i] < v){
			// printf("YES4\n");
			return -1;
		}else{
			if (tree[i*2] >= v){
				// printf("YES5\n");
				return get(i*2, l, mid, p, v);
			}
			else{
				// printf("YES6\n");
				return get(i*2+1, mid+1, r, p, v);
			}
		}
	}
	// printf("YES7\n");
	int a = get(i*2, l, mid, p, v);
	if (a == -1){
		// printf("COO %d\n", i);
		a = get(i*2+1, mid+1, r, p, v);
	}
	return a;
}

void change(int i, int l, int r, int p, int v)
{
	// printf("%d %d %d %d %d %d\n", i, l, r, p, v, tree[i]);
	if (r < p || l > p){
		// printf("%d %d %d %d %d %d*\n", i, l, r, p, v, tree[i]);
		return;
	}
	if (l == r && l == p){
		// printf("YES\n");
		tree[i] = v;
	}
	else{
		int mid = (l+r)>>1;
		change(i*2, l, mid, p, v);
		change(i*2+1, mid+1, r, p, v);
		tree[i] = std::max(tree[i*2], tree[i*2+1]);
	}
	// printf("%d %d %d %d %d %d**\n", i, l, r, p, v, tree[i]);
}

int main()
{
  	scanf("%d\n", &n);
  	for (int i = 0; i < n; i++){
  		scanf("%d", a+i);
  	}
  	build(1, 0, n-1);
  	int c = 0;
  	while (c < n){
  		// printf("*****NEWWWWW*****\n");
  		int start = 0, value = 0, tmp = 0;
  		while (tmp != -1){
  			tmp = get(1, 0, n-1, start, value);
  			// printf("%d\n", tmp);
  			if (tmp != -1){
  				c++;
  			  	printf("%d ", a[tmp]);
  			  	start = tmp+1;
  			  	value = a[tmp];
  			  	change(1, 0, n-1, tmp, -1);
  			}
  			// printf("*****\n");
  		}
  		printf("\n");
  	}
    return 0;
}


/*Template by hemnath_d(Enlightened by chamow)*/
#include<bits/stdc++.h>
using namespace std;
/*--------------------------------------------------------------*/
typedef long long ll;
typedef long double ld;
typedef vector<ll> vec;
typedef vector<int> vi;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
/*--------------------------------------------------------------*/
#define fastread ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define fx first
#define sx second
#define forw(i,a,n) for(i=a;i<=n;i++)
#define rev(i,a,n) for(i=a;i>=n;i--)
#define For(i,a,n) for(i=a;i<n;i++)
#define Rev(i,a,n) for(i=a;i>n;i--)
#define inf 100000000000000
#define minf -100000000000000
/*--------------------------------------------------------------*/
//Variables and functions for graph
//Undirected graph
//vec adj[100000];
//bool visited[100000]={false};
void add_edge(vec adj[],ll a,ll b)
{
    adj[a].pb(b);
    adj[b].pb(a);
}
/*---------------------------------------------------------------*/
//Comparator class for min-heap using priority_queue
//Change the parameters before use
class myComparator
{
public:
    int operator() (/*Datatype*/int p1,/*Datatype*/int  p2)
    {
        return p1/*value*/ > p2/*value*/;
    }
};
/*----------------------------------------------------------------*/
//END OF TEMPLATE
/*----------------------------------------------------------------*/
vec adj[200008];
ll siz=1;
ll bin_search(ll x)
{
   ll lo=0,hi=siz,mid;
   while(lo<hi)
   {
       mid=(lo+hi)/2;
       ll si=adj[mid].size();
       si-=1;
       if(adj[mid][si]>x)
        lo=mid+1;
       else
        hi=mid;
        //cout<<x<<" "<<lo<<" "<<hi<<endl;
   }
   //cout<<hi<<endl;
   return hi;
}
int main()
{
    fastread;
    ll n;
    cin>>n;
    vec arr;
    ll val,i;
    For(i,0,n)
    {
        cin>>val;
        arr.pb(val);
    }
    adj[0].pb(arr[0]);
    For(i,1,n)
    {
       ll x;
       x=bin_search(arr[i]);
       if(x>siz-1)
       {
        siz++;
        //cout<<"New sequence"<<endl;
       }
       adj[x].pb(arr[i]);
    }
    ll j;
    //cout<<endl;
    forw(i,0,siz-1)
    {
       For(j,0,adj[i].size())
       cout<<adj[i][j]<<" ";
       cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define PI                3.14159265358979323846264338327950
#define pb                push_back
#define mp                make_pair
#define all(a)            (a).begin(), (a).end()
#define clr(a,h)          memset(a, (h), sizeof(a))
#define F first
#define S second
int in(){int r=0,c;for(c=getchar();c<=32;c=getchar());if(c=='-') return -in();for(;c>32;r=(r<<1)+(r<<3)+c-'0',c=getchar());return r;}

using namespace std;

const int INF = int(1e9 + 7);
typedef long long       ll;
typedef pair<int, int>  ii;
typedef vector<int>     vi;
typedef vector<ii>      vii;
typedef vector<ll>      vll;

int main()
{
    std::ios::sync_with_stdio(false); cin.tie(0);
    //freopen("","r",stdin);
    //freopen("","w",stdout);
    vector<vector<int> > res;
    int n;
    cin >> n;
    int v[n];
    for (int i = 0; i < n; i++)
    {
    	cin >> v[i];
    }
    res.pb( vector<int>(1, v[0]) );
   	for (int i = 1; i < n; ++i)
   	{
   		int lo = 0, hi = res.size()-1, mid, pos = -1;
   		while (lo <= hi)
   		{
   			mid = (lo + hi) / 2;
   			if (res[mid][ res[mid].size()-1 ] < v[i])
   			{
   				pos = mid;
   				hi = mid - 1;
   			}
   			else lo = mid + 1;
   		}
   		if (pos != -1) res[pos].pb(v[i]);
   		else res.pb( vector<int>(1, v[i]) );
   	}
   	for (int i = 0; i < res.size(); i++)
   	{
   		for (int j = 0; j < res[i].size(); j++)
   		{
   			cout << res[i][j] << ' ';
   		}
   		cout << '\n';
   	}
    return 0;
}


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

vector< vector<int> > seq;

int main() {
    int n;
    cin >> n;
    for(int i = 0; i < n; i++) {
        int num;
        cin >> num;

        int l = 0, r = seq.size() - 1, res = -1;
        while(l <= r) {
            int mid = (l + r) >> 1;
            if(seq[mid].back() < num) {
                l = mid + 1;
                res = mid;
            }
            else r = mid - 1;
        }
        if(res == -1) { vector<int> a; a.push_back(num); seq.push_back(a); }
        else seq[res].push_back(num);
    }

    for(int i = 0; i < seq.size(); i++) {
        for(int j = 0; j < seq[i].size(); j++) cout << seq[i][j] << " ";
        cout << endl;
    }
}


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

vector< vector<int> > seq;

int main() {
    int n;
    cin >> n;
    for(int i = 0; i < n; i++) {
        int num;
        cin >> num;

        int l = 0, r = seq.size() - 1, res = -1;
        while(l <= r) {
            int mid = (l + r) >> 1;
            if(seq[mid].back() < num) {
                r = mid - 1;
                res = mid;
            }
            else l = mid + 1;
        }
        if(res == -1) { vector<int> a; a.push_back(num); seq.push_back(a); }
        else seq[res].push_back(num);
    }

    for(int i = 0; i < seq.size(); i++) {
        for(int j = 0; j < seq[i].size(); j++) cout << seq[i][j] << " ";
        cout << endl;
    }
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <string>
using namespace std;

const int N = 200005;

#define pb push_back
map<int,int> chain;

int n;
int P[N];
vector<int> V;

const int INF = 0x3f3f3f3f;

int cnt_lanac;
vector<int> E[N];
set<int> S;

void load( ) {
    scanf( "%d", &n );

    for( int i = 0; i < n; i++ ) scanf( "%d", P + i ), V.pb(P[i]);
}

void solve( ) {
    E[cnt_lanac].pb( P[0] );
    S.insert(P[0]);
    chain[P[0]] = cnt_lanac;

    for( int i = 1; i < n; i++ ) {
            set<int>::iterator it = S.upper_bound(P[i]);
            if( it == S.begin() ) {
                    E[++cnt_lanac].pb(P[i]);
                    S.insert(P[i]);
                    chain[P[i]] = cnt_lanac;
            } else {
                it--;
                int which = chain[*it];
                E[which].pb(P[i]);
                S.erase( it );
                S.insert( P[i] );
                chain[P[i]] = which;
            }

    }
    for( int i = 0; i <= cnt_lanac; i++ ) {
            for( int j = 0; j < E[i].size(); j++ ) printf( "%d ", E[i][j] );
            printf( "\n" );
    }
}

int main( void ) {
    load( );
    solve( );
    return 0;
}


#include <bits/stdc++.h>

#define MAX (int)(2e5 + 5)
#define pb(x) push_back(x)

using namespace std;

typedef vector<int> vi;

int array[ MAX ], m[ MAX ];
vi ans[ MAX ];

int binarySearch(int left, int right, int x){
    int mid;
    while(left <= right){
        mid = (left + right) >> 1;
        if(m[ mid ] >= x)
            left = mid + 1;
        else
            right = mid - 1;
    }
    return left;
}

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

	int n, p, l = 0;
	cin >> n;
	for(int i = 0 ; i < n ; cin >> array[ i++ ]);
	m[ 0 ] = array[ 0 ];
	ans[ 0 ].pb(m[ 0 ]);
	for(int i = 1 ; i < n ; i++){
        p = binarySearch(0, l, array[ i ]);
        m[ p ] = array[ i ];
        ans[ p ].pb(array[ i ]);
        l = max(p, l);
	}
	cout << ++l;
	for(int i = 0 ; i < l ; i++){
        cout << endl;
        for(int j = 0 ; j < ans[ i ].size() ; j++)
            cout << (j ? " " : "") << ans[ i ][ j ];
	}
	return 0;
}


#include <bits/stdc++.h>

#define MAX (int)(2e5 + 5)
#define pb(x) push_back(x)

using namespace std;

typedef vector<int> vi;

int array[ MAX ], m[ MAX ];
vi ans[ MAX ];

int binarySearch(int left, int right, int x){
    int mid;
    while(left <= right){
        mid = (left + right) >> 1;
        if(m[ mid ] >= x)
            left = mid + 1;
        else
            right = mid - 1;
    }
    return left;
}

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

	int n, p, l = 0;
	cin >> n;
	for(int i = 0 ; i < n ; cin >> array[ i++ ]);
	m[ 0 ] = array[ 0 ];
	ans[ 0 ].pb(m[ 0 ]);
	for(int i = 1 ; i < n ; i++){
        p = binarySearch(0, l, array[ i ]);
        m[ p ] = array[ i ];
        ans[ p ].pb(array[ i ]);
        l = max(p, l);
	}
	for(int i = 0 ; i <= l ; i++){
        for(int j = 0 ; j < ans[ i ].size() ; j++)
            cout << (j ? " " : "") << ans[ i ][ j ];
        cout << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

inline int read()
{
	int f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
struct data{int num,x;}a[200005];
int n,cnt=1,Max[200005];

inline int cmp(data a,data b)
{
    if(a.num==b.num)return a.x<b.x;
	else return a.num<b.num;
}

int main()
{
	n=read();
	rep(i,1,n)a[i].x=read();
	rep(i,1,n){
		if(a[i].x<Max[cnt]){Max[++cnt]=a[i].x;a[i].num=cnt;continue;}
		rep(j,1,cnt)if(a[i].x>Max[j]){Max[j]=a[i].x;a[i].num=j;break;}
	}
	sort(a+1,a+1+n,cmp);
	printf("%d ",a[1].x);
	rep(i,2,n){
		if(a[i].num!=a[i-1].num)printf("\n");
		printf("%d ",a[i].x);
	}
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

int n,a[200005],cnt=0,Max;

int main()
{
	scanf("%d",&n);
	rep(i,1,n)scanf("%d",&a[i]);
	while(cnt<n){
		Max=0;
		rep(i,1,n)
		    if(a[i]!=0&&a[i]>Max){Max=a[i];printf("%d ",a[i]);cnt++;a[i]=0;}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

inline int read()
{
	int f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int n,a[200005],cnt=0,Max;

int main()
{
	n=read();
	rep(i,1,n)a[i]=read();
	while(cnt<n){
		Max=0;
		rep(i,1,n)
		    if(a[i]!=0&&a[i]>Max){Max=a[i];printf("%d ",a[i]);cnt++;a[i]=0;}
		printf("\n");
	}
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

inline int read()
{
	int f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
struct data{int num,x;}a[200005];
int n,cnt=1,Max[200005];

inline int cmp(data a,data b)
{
    if(a.num==b.num)return a.x<b.x;
	else return a.num<b.num;
}

int main()
{
	n=read();
	rep(i,1,n)a[i].x=read();
	rep(i,1,n){
		rep(j,1,cnt)if(a[i].x>Max[j]){Max[j]=a[i].x;a[i].num=j;break;}
		if(a[i].x<Max[cnt]){Max[++cnt]=a[i].x;a[i].num=cnt;}
	}
	sort(a+1,a+1+n,cmp);
//	rep(i,1,n)cout<<a[i].x<<" "<<a[i].num<<endl;

	printf("%d ",a[1].x);
	rep(i,2,n){
		if(a[i].num!=a[i-1].num)printf("\n");
		printf("%d ",a[i].x);
	}
	return 0;
}
/*	while(cnt<n){
		Max=0;
		rep(i,1,n)
		    if(a[i]!=0&&a[i]>Max){Max=a[i];printf("%d ",a[i]);cnt++;a[i]=0;}
		printf("\n");
	}
	return 0;
}*/


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define ld long double
#define M 998244353
#define f(n) for(long long int x = 0; x < n; x++)

void printprecise(double l,ll precision)
{
    std::cout << std::fixed;
    std::cout << std::setprecision(precision);
    std::cout << l;
}

ll modular(ll x,ll n,ll m)
{
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return modular((x*x)%m,n/2,m);
    else                             //n is odd
        return (x*modular((x*x)%m,(n-1)/2,m))%m;

}

ll gcd(ll a,ll b)
{
	if(b==0)
		return a;
	return gcd(b,a%b);
}

void initialize(ll ar[],ll n)
{
	for(ll x=0;x<n;x++)
		ar[x] = x;
}

void printarray(ll ar[],ll n)
{
	for(ll x=0;x<n;x++)
		cout<<ar[x]<<" ";
	cout<<endl;
}

void printvector(vector <ll> v)
{
	for(ll x=0;x<v.size();x++)
	{
		cout<<v[x]<<" ";
	}
	cout<<endl;
}

ll root(ll i,ll ar[])
{
	while(ar[i]!=i)
	{
		ar[i] = ar[ar[i]];
		i = ar[i];
	}
	return i;
}

void unione(ll a,ll b,ll ar[])
{
	ll p = root(a,ar);
	ll q = root(b,ar);
	ar[p] = ar[q];
}

void bfs(ll n,ll vis[],ll level[],vector <ll> v[])
{
	list <ll> g;
	g.pb(n);
	vis[n]=1;
	level[n]=0;
	while(!g.empty())
	{
		ll a = g.front();
		g.pop_front();
		for(ll x=0;x<v[a].size();x++)
		{
			if(vis[v[a][x]]==0)
			{
				g.pb(v[a][x]);
				vis[v[a][x]]=1;
				level[v[a][x]]=level[a]+1;
			}
		}
	}
}

void dfs(ll n,vector <ll> v[],ll vis[])
{
	vis[n]=1;
	//cout<<n<<" "; //dfs
	for(ll x=0;x<v[n].size();x++)
	{
		if(vis[v[n][x]]==0)
		{
			dfs(v[n][x],v,vis);
			//cout<<n<<" "; //euler tour
		}
	}
	//cout<<n<<" ";  //topological
}

void computelpsarray(string s,ll lps[])
{
	ll i=1;
	ll j=0;
	lps[0] = 0;
	while(i<s.length())
	{
		if(s[i]==s[j])
		{
			lps[i] = j+1;
			i++;
			j++;
		}
		else
		{
			if(j==0)
			{
				lps[i] = 0;
				i++;
			}
			else
			{
				j = lps[j-1];
			}
		}
	}
}

void getZarr(string str, ll Z[])
{
    ll n = str.length();
    ll L, R, k;
    L = R = 0;
    Z[0] = 0;
    for (ll i = 1; i < n; ++i)
    {
        if (i > R)
        {
            L = R = i;
            while (R<n && str[R-L] == str[R])
                R++;
            Z[i] = R-L;
            R--;
        }
        else
        {
            k = i-L;
            if (Z[k] < R-i+1)
                Z[i] = Z[k];
            else
            {
                L = i;
                while (R<n && str[R-L] == str[R])
                    R++;
                Z[i] = R-L;
                R--;
            }
        }
    }
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
	#endif

	ll n;
	cin>>n;
	vector <stack <ll>> v;
	for(ll x = 0; x < n; x++) {
		ll a;
		cin>>a;
		ll low = 0;
		ll high = v.size()-1;
		while(low <= high) {
			ll mid = low + (high - low)/2;
			if(v[mid].top() < a)
				high = mid - 1;
			else
				low = mid + 1;
		}
		if(low == v.size()) {
			stack <ll> s;
			s.push(a);
			v.pb(s);
		}
		else
			v[low].push(a);
	}
	for(ll x = 0; x < v.size(); x++) {
		ll r = v[x].size();
		vector <ll> tmp;
		for(ll y = 0; y < r; y++) {
			tmp.pb(v[x].top());
			v[x].pop();
		}
		for(ll y = r-1; y >= 0; y--)
			cout << tmp[y] << " ";
		cout << endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define ld long double
#define M 998244353
#define f(n) for(long long int x = 0; x < n; x++)

void printprecise(double l,ll precision)
{
    std::cout << std::fixed;
    std::cout << std::setprecision(precision);
    std::cout << l;
}

ll modular(ll x,ll n,ll m)
{
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return modular((x*x)%m,n/2,m);
    else                             //n is odd
        return (x*modular((x*x)%m,(n-1)/2,m))%m;

}

ll gcd(ll a,ll b)
{
	if(b==0)
		return a;
	return gcd(b,a%b);
}

void initialize(ll ar[],ll n)
{
	for(ll x=0;x<n;x++)
		ar[x] = x;
}

void printarray(ll ar[],ll n)
{
	for(ll x=0;x<n;x++)
		cout<<ar[x]<<" ";
	cout<<endl;
}

void printvector(vector <ll> v)
{
	for(ll x=0;x<v.size();x++)
	{
		cout<<v[x]<<" ";
	}
	cout<<endl;
}

ll root(ll i,ll ar[])
{
	while(ar[i]!=i)
	{
		ar[i] = ar[ar[i]];
		i = ar[i];
	}
	return i;
}

void unione(ll a,ll b,ll ar[])
{
	ll p = root(a,ar);
	ll q = root(b,ar);
	ar[p] = ar[q];
}

void bfs(ll n,ll vis[],ll level[],vector <ll> v[])
{
	list <ll> g;
	g.pb(n);
	vis[n]=1;
	level[n]=0;
	while(!g.empty())
	{
		ll a = g.front();
		g.pop_front();
		for(ll x=0;x<v[a].size();x++)
		{
			if(vis[v[a][x]]==0)
			{
				g.pb(v[a][x]);
				vis[v[a][x]]=1;
				level[v[a][x]]=level[a]+1;
			}
		}
	}
}

void dfs(ll n,vector <ll> v[],ll vis[])
{
	vis[n]=1;
	//cout<<n<<" "; //dfs
	for(ll x=0;x<v[n].size();x++)
	{
		if(vis[v[n][x]]==0)
		{
			dfs(v[n][x],v,vis);
			//cout<<n<<" "; //euler tour
		}
	}
	//cout<<n<<" ";  //topological
}

void computelpsarray(string s,ll lps[])
{
	ll i=1;
	ll j=0;
	lps[0] = 0;
	while(i<s.length())
	{
		if(s[i]==s[j])
		{
			lps[i] = j+1;
			i++;
			j++;
		}
		else
		{
			if(j==0)
			{
				lps[i] = 0;
				i++;
			}
			else
			{
				j = lps[j-1];
			}
		}
	}
}

void getZarr(string str, ll Z[])
{
    ll n = str.length();
    ll L, R, k;
    L = R = 0;
    Z[0] = 0;
    for (ll i = 1; i < n; ++i)
    {
        if (i > R)
        {
            L = R = i;
            while (R<n && str[R-L] == str[R])
                R++;
            Z[i] = R-L;
            R--;
        }
        else
        {
            k = i-L;
            if (Z[k] < R-i+1)
                Z[i] = Z[k];
            else
            {
                L = i;
                while (R<n && str[R-L] == str[R])
                    R++;
                Z[i] = R-L;
                R--;
            }
        }
    }
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
	#endif

	ll n;
	cin>>n;
	vector <ll> v[200001];
	vector <ll> max;
	for(ll x = 0; x < n; x++) {
		ll a;
		cin >> a;
		ll low = 0;
		ll high = max.size()-1;
		while(low <= high) {
			ll mid = low + (high - low)/2;
			if(max[mid] < a)
				high = mid - 1;
			else
				low = mid + 1;
		}
		if(low == max.size()) {
			max.pb(a);
			v[max.size()-1].pb(a);
		}
		else {
			max[low] = a;
			v[low].pb(a);
		}
	}
	for(ll x = 0; x < max.size(); x++) {
		for(ll y = 0; y < v[x].size(); y++) {
			cout << v[x][y] << " ";
		}
		cout << endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define ld long double
#define M 998244353
#define f(n) for(long long int x = 0; x < n; x++)

void printprecise(double l,ll precision)
{
    std::cout << std::fixed;
    std::cout << std::setprecision(precision);
    std::cout << l;
}

ll modular(ll x,ll n,ll m)
{
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return modular((x*x)%m,n/2,m);
    else                             //n is odd
        return (x*modular((x*x)%m,(n-1)/2,m))%m;

}

ll gcd(ll a,ll b)
{
	if(b==0)
		return a;
	return gcd(b,a%b);
}

void initialize(ll ar[],ll n)
{
	for(ll x=0;x<n;x++)
		ar[x] = x;
}

void printarray(ll ar[],ll n)
{
	for(ll x=0;x<n;x++)
		cout<<ar[x]<<" ";
	cout<<endl;
}

void printvector(vector <ll> v)
{
	for(ll x=0;x<v.size();x++)
	{
		cout<<v[x]<<" ";
	}
	cout<<endl;
}

ll root(ll i,ll ar[])
{
	while(ar[i]!=i)
	{
		ar[i] = ar[ar[i]];
		i = ar[i];
	}
	return i;
}

void unione(ll a,ll b,ll ar[])
{
	ll p = root(a,ar);
	ll q = root(b,ar);
	ar[p] = ar[q];
}

void bfs(ll n,ll vis[],ll level[],vector <ll> v[])
{
	list <ll> g;
	g.pb(n);
	vis[n]=1;
	level[n]=0;
	while(!g.empty())
	{
		ll a = g.front();
		g.pop_front();
		for(ll x=0;x<v[a].size();x++)
		{
			if(vis[v[a][x]]==0)
			{
				g.pb(v[a][x]);
				vis[v[a][x]]=1;
				level[v[a][x]]=level[a]+1;
			}
		}
	}
}

void dfs(ll n,vector <ll> v[],ll vis[])
{
	vis[n]=1;
	//cout<<n<<" "; //dfs
	for(ll x=0;x<v[n].size();x++)
	{
		if(vis[v[n][x]]==0)
		{
			dfs(v[n][x],v,vis);
			//cout<<n<<" "; //euler tour
		}
	}
	//cout<<n<<" ";  //topological
}

void computelpsarray(string s,ll lps[])
{
	ll i=1;
	ll j=0;
	lps[0] = 0;
	while(i<s.length())
	{
		if(s[i]==s[j])
		{
			lps[i] = j+1;
			i++;
			j++;
		}
		else
		{
			if(j==0)
			{
				lps[i] = 0;
				i++;
			}
			else
			{
				j = lps[j-1];
			}
		}
	}
}

void getZarr(string str, ll Z[])
{
    ll n = str.length();
    ll L, R, k;
    L = R = 0;
    Z[0] = 0;
    for (ll i = 1; i < n; ++i)
    {
        if (i > R)
        {
            L = R = i;
            while (R<n && str[R-L] == str[R])
                R++;
            Z[i] = R-L;
            R--;
        }
        else
        {
            k = i-L;
            if (Z[k] < R-i+1)
                Z[i] = Z[k];
            else
            {
                L = i;
                while (R<n && str[R-L] == str[R])
                    R++;
                Z[i] = R-L;
                R--;
            }
        }
    }
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
	#endif

	ll n;
	cin>>n;
	vector <vector <ll>> v;
	vector <ll> max;
	for(ll x = 0; x < n; x++) {
		ll a;
		cin >> a;
		ll low = 0;
		ll high = max.size()-1;
		while(low <= high) {
			ll mid = low + (high - low)/2;
			if(max[mid] < a)
				high = mid - 1;
			else
				low = mid + 1;
		}
		if(low == max.size()) {
			max.pb(a);
			vector <ll> vi;
			vi.pb(a);
			v.pb(vi);
		}
		else {
			max[low] = a;
			v[low].pb(a);
		}
	}
	for(ll x = 0; x < v.size(); x++) {
		for(ll y = 0; y < v[x].size(); y++) {
			cout << v[x][y] << " ";
		}
		cout << endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define ld long double
#define M 998244353
#define f(n) for(long long int x = 0; x < n; x++)

void printprecise(double l,ll precision)
{
    std::cout << std::fixed;
    std::cout << std::setprecision(precision);
    std::cout << l;
}

ll modular(ll x,ll n,ll m)
{
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return modular((x*x)%m,n/2,m);
    else                             //n is odd
        return (x*modular((x*x)%m,(n-1)/2,m))%m;

}

ll gcd(ll a,ll b)
{
	if(b==0)
		return a;
	return gcd(b,a%b);
}

void initialize(ll ar[],ll n)
{
	for(ll x=0;x<n;x++)
		ar[x] = x;
}

void printarray(ll ar[],ll n)
{
	for(ll x=0;x<n;x++)
		cout<<ar[x]<<" ";
	cout<<endl;
}

void printvector(vector <ll> v)
{
	for(ll x=0;x<v.size();x++)
	{
		cout<<v[x]<<" ";
	}
	cout<<endl;
}

ll root(ll i,ll ar[])
{
	while(ar[i]!=i)
	{
		ar[i] = ar[ar[i]];
		i = ar[i];
	}
	return i;
}

void unione(ll a,ll b,ll ar[])
{
	ll p = root(a,ar);
	ll q = root(b,ar);
	ar[p] = ar[q];
}

void bfs(ll n,ll vis[],ll level[],vector <ll> v[])
{
	list <ll> g;
	g.pb(n);
	vis[n]=1;
	level[n]=0;
	while(!g.empty())
	{
		ll a = g.front();
		g.pop_front();
		for(ll x=0;x<v[a].size();x++)
		{
			if(vis[v[a][x]]==0)
			{
				g.pb(v[a][x]);
				vis[v[a][x]]=1;
				level[v[a][x]]=level[a]+1;
			}
		}
	}
}

void dfs(ll n,vector <ll> v[],ll vis[])
{
	vis[n]=1;
	//cout<<n<<" "; //dfs
	for(ll x=0;x<v[n].size();x++)
	{
		if(vis[v[n][x]]==0)
		{
			dfs(v[n][x],v,vis);
			//cout<<n<<" "; //euler tour
		}
	}
	//cout<<n<<" ";  //topological
}

void computelpsarray(string s,ll lps[])
{
	ll i=1;
	ll j=0;
	lps[0] = 0;
	while(i<s.length())
	{
		if(s[i]==s[j])
		{
			lps[i] = j+1;
			i++;
			j++;
		}
		else
		{
			if(j==0)
			{
				lps[i] = 0;
				i++;
			}
			else
			{
				j = lps[j-1];
			}
		}
	}
}

void getZarr(string str, ll Z[])
{
    ll n = str.length();
    ll L, R, k;
    L = R = 0;
    Z[0] = 0;
    for (ll i = 1; i < n; ++i)
    {
        if (i > R)
        {
            L = R = i;
            while (R<n && str[R-L] == str[R])
                R++;
            Z[i] = R-L;
            R--;
        }
        else
        {
            k = i-L;
            if (Z[k] < R-i+1)
                Z[i] = Z[k];
            else
            {
                L = i;
                while (R<n && str[R-L] == str[R])
                    R++;
                Z[i] = R-L;
                R--;
            }
        }
    }
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
	#endif

	ll n;
	cin>>n;
	vector <vector <ll>> v(200001);
	vector <ll> max;
	for(ll x = 0; x < n; x++) {
		ll a;
		cin >> a;
		ll low = 0;
		ll high = max.size()-1;
		while(low <= high) {
			ll mid = low + (high - low)/2;
			if(max[mid] < a)
				high = mid - 1;
			else
				low = mid + 1;
		}
		if(low == max.size()) {
			max.pb(a);
			v[max.size()-1].pb(a);
		}
		else {
			max[low] = a;
			v[low].pb(a);
		}
	}
	for(ll x = 0; x < max.size(); x++) {
		for(ll y = 0; y < v[x].size(); y++) {
			cout << v[x][y] << " ";
		}
		cout << endl;
	}
}


    #include <bits/stdc++.h>

    using namespace std;

    #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #define mp make_pair
    #define ll long long
    #define db double
    #define pb push_back
    #define ld long double
    #define INF ll_MAX
    #define M LONG_LONG_MAX

    const ll N = 2e5+3;
    const ll mod = 1e9+7;

    ll v1[N]={0};

    ll binsearch(int n,int val)
    {

    	ll low = 0,high = n+1;
    	while(low<high)
    	{
    		ll mid = (low+high)/2;
    		if(v1[mid]>val)
    		{
    			low = mid + 1;
    		}
    		else
    		{
    			high = mid;
    		}
    	}
    	if(low==n+1)
    		return -1;
    	return low;
    }

    int main()
    {
    	 #ifndef ONLINE_JUDGE
    	// for getting input from input.txt
    	freopen("input.txt", "r", stdin);
    	// for writing output to output.txt
    	freopen("output.txt", "w", stdout);
    	#endif

    	IOS
    	ll i=0;
    	vector <ll> v[N];

    	ll n,a;
    	cin>>n;
    	map <int,int> m;
    	ll size = -1;
    	for(i=0;i<n;i++)
    	{
    		cin>>a;
    		ll val = binsearch(size,a);
    		if(val==-1)
    		{
    			size++;
    			v[size].pb(a);
    			v1[size]=a;
    		}
    		else
    		{
    			v[val].pb(a);
    			v1[val]=a;
    		}

    	}

    	for(i=0;i<size+1;i++)
    	{
    		for(int j=0;j<v[i].size();j++)
    		{
    			cout<<v[i][j]<<" ";
    		}
    		cout<<endl;
    	}
    	return 0;
    }


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <list>

using namespace std;

int n;

list <int> l;

int main() {

	cin >> n;

	for (int i = 0; i < n; i++) {
		int x;
		scanf("%d", &x);
		l.push_back(x);
	}

	while (l.size() != 0) {
		int last = -1;
		for (auto i = l.begin(); i != l.end();) {
			if (*i > last) {
				printf("%d ", *i);
				last = *i;
				auto lastI = i;
				i++;
				l.erase(lastI);
			} else
				i++;
		}
		printf("\n");
	}

	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <list>

using namespace std;

int n;

list <int> l;

int main() {

	cin >> n;

	for (int i = 0; i < n; i++) {
		int x;
		scanf("%d", &x);
		l.push_back(x);
	}

	while (l.size() != 0) {
		int last = -1;
		for (auto i = l.begin(); i != l.end();) {
			if (*i > last) {
				cout << *i << ' ';
				last = *i;
				auto lastI = i;
				i++;
				l.erase(lastI);
			} else
				i++;
		}
		cout << endl;
	}

	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <list>

using namespace std;

int n;
int a[1000000]; // 10 ^ 6 norm
vector < vector < int > > ans;

int main() {
	cin >> n;

	fill(cur, cur + int(1e6), -1);

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

	ans.push_back(vector < int >());

	for (int i = 0; i < n; i++) {
		bool added = false;
		for (int j = 0; j < ans.size(); j++) {
			if (ans[j].size() == 0) {
				ans[j].push_back(a[i]);
				added = true;
				break;
			}
			if (ans[j][ans[j].size() - 1] < a[i]) {
				ans[j].push_back(a[i]);
				added = true;
				break;
			}
		}
		if (!added) {
			ans.push_back(vector < int >());
			ans[ans.size() - 1].push_back(a[i]);
		}
	}

	for (int i = 0; i < ans.size(); i++) {
		for (int j = 0; j < ans[i].size(); j++)
			printf("%d ", ans[i][j]);
		printf("\n");
	}

	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <list>

using namespace std;

int n;
int a[1000000]; // 10 ^ 6 norm
vector < vector < int > > ans;

int main() {

	cin >> n;

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

	ans.push_back(vector < int >());

	for (int i = 0; i < n; i++) {
		int l = -1;
		int r = ans.size() - 1;
		while (r - l > 1) {
			int m = (r + l) / 2;
			if (m < ans.size() && (ans[m].size() == 0 || ans[m][ans[m].size() - 1] < a[i]))
				r = m;
			else
				l = m;
		}

		if (r < ans.size() && (ans[r].size() == 0 || ans[r][ans[r].size() - 1] < a[i])) {
			ans[r].push_back(a[i]);
		} else {
			ans.push_back(vector < int >());
			ans[ans.size() - 1].push_back(a[i]);
		}

	}

	for (int i = 0; i < ans.size(); i++) {
		for (int j = 0; j < ans[i].size(); j++)
			printf("%d ", ans[i][j]);
		printf("\n");
	}

	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <list>

using namespace std;

int n;
int a[1000000]; // 10 ^ 6 norm
vector < vector < int > > ans;

int main() {

	cin >> n;

	fill(cur, cur + int(1e6), -1);

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

	ans.push_back(vector < int >());

	for (int i = 0; i < n; i++) {
		bool added = false;
		for (int j = 0; j < ans.size(); j++) {
			if (ans[j].size() == 0) {
				ans[j].push_back(a[i]);
				added = true;
				break;
			}
			if (ans[j][ans[j].size() - 1] < a[i]) {
				ans[j].push_back(a[i]);
				added = true;
				break;
			}
		}
		if (!added) {
			ans.push_back(vector < int >());
			ans[ans.size() - 1].push_back(a[i]);
		}
	}

	for (int i = 0; i < ans.size(); i++) {
		for (int j = 0; j < ans[i].size(); j++)
			printf("%d ", ans[i][j]);
		printf("\n");
	}

	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <list>

using namespace std;

int n;
int a[1000000]; // 10 ^ 6 norm
vector < vector < int > > ans;

int main() {

	cin >> n;

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

	ans.push_back(vector < int >());

	for (int i = 0; i < n; i++) {
		bool added = false;
		for (int j = 0; j < ans.size(); j++) {
			if (ans[j].size() == 0) {
				ans[j].push_back(a[i]);
				added = true;
				break;
			}
			if (ans[j][ans[j].size() - 1] < a[i]) {
				ans[j].push_back(a[i]);
				added = true;
				break;
			}
		}
		if (!added) {
			ans.push_back(vector < int >());
			ans[ans.size() - 1].push_back(a[i]);
		}
	}

	for (int i = 0; i < ans.size(); i++) {
		for (int j = 0; j < ans[i].size(); j++)
			printf("%d ", ans[i][j]);
		printf("\n");
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    list<int> a;
    list<int>::iterator it=a.begin();
    list<int>::iterator it1=a.begin();
    long long int n;
    int elt, f=1;
    cin>>n;
    cin>>elt;
    cout<<elt<<" ";
    int cur=elt;
    for(int i=1;i<n;i++)
    {
        cin>>elt;
        if(elt>cur)
        {
            cout<<elt<<" ";
            cur=elt;
        }
        else
           a.push_back(elt);
    }
    cout<<endl;
    while(a.begin()!=a.end())
    {
        f=0;
        it=a.begin();
        it1=a.begin();
        while(it!=a.end())
        {
            if(f==0)
            {
                cout<<*it<<" ";
                cur=*it;
                it1=it;
                a.erase(it);
                f++;
            }
            else if(*it>cur)
            {
                cout<<*it<<" ";
                cur=*it;
                it1=it;
                a.erase(it);
            }
            it1++;
            it=it1;
        }
/*        if(a.begin()!=a.end()&&*it>cur)
        {
            cout<<*it<<" ";
            cur=*it;
            a.erase(it);
        }*/
        cout<<endl;
    }
    return 0;
}


//Srimate Ramanujaya Namaha AUM Namo Narayanaya Hare Rama Hare Krishna Krishnam Vande Jagath Gurum
#include<bits/stdc++.h>
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define TrCntr(c,i,I) for(int i=1,typeof((c).begin()) I = (c).begin(); I != (c).end(); i++,I++)
#define tr(c,I) for(typeof((c).begin()) I = (c).begin(); I != (c).end(); I++)
#define cpresent(c,x) ((c).find(x) != (c).end())
#define present(c,x) (find(all(c),x) != (c).end())
#define rep(i,j) for(int i=0;i!=j;i++)
#define F first
#define S second
#define itr iterator
#define MOD ((long long)1e9+7)
#define ll long long
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef list<int> li;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef set<int> si;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,tmp;
    cin>>n;
    vector<queue<int> > lst;
    vector<int> sp;
    while(n--)
    {
        cin>>tmp;
        bool b=true;
        vi::reverse_iterator I=lower_bound(sp.rbegin(),sp.rend(),tmp);
        if(I==sp.rbegin())
        {
            queue<int> hj;
            hj.push(tmp);
            lst.push_back(hj);
            sp.pb(tmp);
        }
        else
        {
            I--;
            *I=tmp;
            (lst.rbegin()+(I-sp.rbegin()))->push(tmp);
        }
       /* for(vector<queue<int> >::itr i=lst.begin();i!=lst.end();i++)
        {

            if(*I<tmp)
            {
                i->push(tmp);
                *I=tmp;
                b=false;
                break;
            }
            I++;
        }*/

    }
    for(vector<queue<int> >::itr i=lst.begin();i!=lst.end();i++)
    {
        int z=i->size();
        while(z--)
        {
            cout<<i->front()<<" ";
            i->pop();
        }
        cout<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
//    list<int>::iterator it=a.begin();
    long long int n;
    int a[200000];
    int elt, f=1;
    cin>>n;
    cin>>a[0];
    cout<<a[0]<<" ";
    int cur=a[0];
    int k=0;
    for(int i=1;i<n;i++)
    {
        cin>>elt;
        if(elt>cur)
        {
            cout<<elt<<" ";
            cur=elt;
        }
        else
        {
            a[k]=elt;
            k++;
        }
    }
    cout<<endl;
    int i=1;
    while(f!=0)
    {
        f=0;
        i=1;
        while(i<n)
        {
            if(f==0&&a[i]!=-1)
            {
                cout<<a[i]<<" ";
                cur=a[i];
                if(i!=n-1)
                    a[i]=a[i+1];
                f++;
            }
            else if(a[i]!=-1&&a[i]>cur)
            {
                cout<<a[i]<<" ";
                cur=a[i];
                if(i!=n-1)
                    a[i]=a[i+1];
            }
            i++;
        }
        cout<<endl;
    }
    return 0;
}


//Srimate Ramanujaya Namaha AUM Namo Narayanaya Hare Rama Hare Krishna Krishnam Vande Jagath Gurum
#include<bits/stdc++.h>
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define TrCntr(c,i,I) for(int i=1,typeof((c).begin()) I = (c).begin(); I != (c).end(); i++,I++)
#define tr(c,I) for(typeof((c).begin()) I = (c).begin(); I != (c).end(); I++)
#define cpresent(c,x) ((c).find(x) != (c).end())
#define present(c,x) (find(all(c),x) != (c).end())
#define rep(i,j) for(int i=0;i!=j;i++)
#define F first
#define S second
#define itr iterator
#define MOD ((long long)1e9+7)
#define ll long long
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef list<int> li;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef set<int> si;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,tmp;
    cin>>n;
    vector<queue<int> > lst;
    while(n--)
    {
        cin>>tmp;
        bool b=true;
        for(vector<queue<int> >::itr i=lst.begin();i!=lst.end()&&b;i++)
        {
            if(i->back()<tmp)
            {
                i->push(tmp);
                b=false;
            }
        }
        if(b)
        {
            lst.resize(lst.size()+1);
            lst.rbegin()->push(tmp);
        }
    }
    for(vector<queue<int> >::itr i=lst.begin();i!=lst.end();i++)
    {
        while(i->size())
        {
            cout<<i->front()<<" ";
            i->pop();
        }
        cout<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
//    list<int>::iterator it=a.begin();
    long long int n;
    int a[200000];
    int elt, f=1;
    cin>>n;
    cin>>elt;
    cout<<elt<<" ";
    int cur=elt;
    int k=0;
    for(int i=1;i<n;i++)
    {
        cin>>elt;
        if(elt>cur)
        {
            cout<<elt<<" ";
            cur=elt;
            a[i]=-1;
        }
        else
        {
            a[k]=elt;
            k++;
        }
    }
    cout<<endl;
    int i=0;
    while(f!=0)
    {
        f=0;
        i=0;
        while(i<k)
        {
            if(f==0&&a[i]!=-1)
            {
                cout<<a[i]<<" ";
                cur=a[i];
                a[i]=-1;
                f++;
            }
            else if(a[i]!=-1&&a[i]>cur)
            {
                cout<<a[i]<<" ";
                cur=a[i];
                a[i]=-1;
            }
            i++;
        }
        cout<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
//    list<int>::iterator it=a.begin();
    long long int n;
    int a[200000];
    int elt, f=1;
    cin>>n;
    cin>>a[0];
    cout<<a[0]<<" ";
    int cur=a[0];
    a[0]=-1;
    for(int i=1;i<n;i++)
    {
        cin>>elt;
        if(elt>cur)
        {
            cout<<elt<<" ";
            cur=elt;
            a[i]=-1;
        }
        else
           a[i]=elt;
    }
    cout<<endl;
    int i=1;
    while(f!=0)
    {
        f=0;
        i=1;
        while(i<n)
        {
            if(f==0&&a[i]!=-1)
            {
                cout<<a[i]<<" ";
                cur=a[i];
                a[i]=-1;
                f++;
            }
            else if(a[i]!=-1&&a[i]>cur)
            {
                cout<<a[i]<<" ";
                cur=a[i];
                a[i]=-1;
            }
            i++;
        }
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define EN '\n'
#define vc vector
#define eps 0.0000000001
#define pr pair <ll,ll>
#define inf 1e15
#define m(a , b) (a + b) / 2
using namespace std;
ll last[200000];
vc <vc <ll>> ans;
ll a[200003];
int main(){
    fastio;
    ll n;
    cin >> n;
    for (ll i = 0 ; i < n ; i++)
        cin >> a[i];
    ans = vc <vc <ll>> (n + 2);

    for (ll i = 0 ; i  < n ; i++){
        ll ind = upper_bound(last , last + n , a[i], greater <ll> ()) - last;
        ans[ind].push_back(a[i]);
        last[ind] = a[i];
    }
    for (auto v : ans){
        if(v.size() == 0)
            break;
        for (auto it : v)
            cout << it << " ";
        cout << EN;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define EN '\n'
#define vc vector
#define eps 0.0000000001
#define pr pair <ll,ll>
#define inf 1e15
#define MOD 1000000007
#define m(a , b) (a + b) / 2
#define rst(a, val , n) for (ll k = 0 ; k < n ; k++) a[k] = val
#define NO return cout << "NO", 0
#define init(v , n) v = vc <vc <ll>> (n+2)
#define wr(v) for (auto it : v) cout << it << " "
using namespace std;
ll a[100002];
vc <vc <ll>> v;
set <ll> s;
map <ll, ll> mp;
int main(){
    fastio;
    ll n;
    cin >> n;
    for(ll i = 0 ; i < n ; i++)
        cin >> a[i];
    s.insert(a[0]);
    init(v, n + 2);
    v[0].push_back(a[0]);
    ll in = 0;
    mp[a[0]] = in++;
    for (ll i = 1 ; i < n ; i++){
        auto it = s.lower_bound(a[i]);
        if (it == s.begin()){
            mp[a[i]] = in;
            v[in++].push_back(a[i]);
            s.insert(a[i]);
            continue;
        }
        ll val , ind = inf;
        for (auto itt = s.begin() ; itt != it ; itt++){
            if (ind > mp[*itt]){
                val = *itt;
                ind = mp[*itt];
            }
        }
        v[ind].push_back(a[i]);
        s.erase(val);
        s.insert(a[i]);
        mp[a[i]] = ind;
    }
    for (auto it : v){
        if (it.size() == 0)
            break;
        wr(it);
        cout << EN;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define EN '\n'
#define vc vector
#define eps 0.0000000001
#define pr pair <ll,ll>
#define inf 1e15
#define MOD 1000000007
#define m(a , b) (a + b) / 2
#define rst(a, val , n) for (ll k = 0 ; k < n ; k++) a[k] = val
#define NO return cout << "NO", 0
#define init(v , n) v = vc <vc <ll>> (n+2)
#define wr(v) for (auto it : v) cout << it << " "
using namespace std;
ll a[200002];
vc <vc <ll>> v;
set <ll> s;
map <ll, ll> mp;
int main(){
    fastio;
    ll n;
    cin >> n;
    for(ll i = 0 ; i < n ; i++)
        cin >> a[i];
    s.insert(a[0]);
    init(v, n + 5);
    v[0].push_back(a[0]);
    ll in = 0;
    for (ll i = 1 ; i < n ; i++){
        if (a[i] < v[in].back())
            v[++in].push_back(a[i]);
        else{
            for (ll j = 0 ; j <= in ; j++){
                if (a[i] > v[j].back()){
                    v[j].push_back(a[i]);
                    break;
                }
            }
        }
    }

    for (auto it : v){
        if (it.size() == 0)
            break;
        wr(it);
        cout << EN;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define EN '\n'
#define vc vector
#define eps 0.0000000001
#define pr pair <ll,ll>
#define inf 1e15
#define MOD 1000000007
#define m(a , b) (a + b) / 2
#define rst(a, val , n) for (ll k = 0 ; k < n ; k++) a[k] = val
#define NO return cout << "NO", 0
#define init(v , n) v = vc <vc <ll>> (n+2)
#define wr(v) for (auto it : v) cout << it << " "
using namespace std;
ll a[100002];
vc <vc <ll>> v;
set <ll> s;
map <ll, ll> mp;
int main(){
    fastio;
    ll n;
    cin >> n;
    for(ll i = 0 ; i < n ; i++)
        cin >> a[i];
    s.insert(a[0]);
    init(v, n + 2);
    v[0].push_back(a[0]);
    ll in = 0;
    for (ll i = 1 ; i < n ; i++){
        if (a[i] < v[in].back())
            v[++in].push_back(a[i]);
        else{
            for (ll j = 0 ; j <= in ; j++){
                if (a[i] > v[j].back()){
                    v[j].push_back(a[i]);
                    break;
                }
            }
        }
    }

    for (auto it : v){
        if (it.size() == 0)
            break;
        wr(it);
        cout << EN;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define EN '\n'
#define vc vector
#define eps 0.0000000001
#define pr pair <ll,ll>
#define inf 1e15
#define MOD 1000000007
#define m(a , b) (a + b) / 2
#define rst(a, val , n) for (ll k = 0 ; k < n ; k++) a[k] = val
#define NO return cout << "NO", 0
#define init(v , n) v = vc <vc <ll>> (n+2)
#define wr(v) for (auto it : v) cout << it << " "
using namespace std;
ll a[100002];
vc <vc <ll>> v;
set <ll> s;
map <ll, ll> mp;
int main(){
    ll n;
    cin >> n;
    for(ll i = 0 ; i < n ; i++)
        cin >> a[i];
    s.insert(a[0]);
    init(v, n + 2);
    v[0].push_back(a[0]);
    ll in = 0;
    mp[a[0]] = in++;
    for (ll i = 1 ; i < n ; i++){
        auto it = s.lower_bound(a[i]);
        if (it == s.begin()){
            mp[a[i]] = in;
            v[in++].push_back(a[i]);
            s.insert(a[i]);
            continue;
        }
        ll val , ind = inf;
        for (auto itt = s.begin() ; itt != it ; itt++){
            if (ind > mp[*itt]){
                val = *itt;
                ind = mp[*itt];
            }
        }
        v[ind].push_back(a[i]);
        s.erase(val);
        s.insert(a[i]);
        mp[a[i]] = ind;
    }
    for (auto it : v){
        if (it.size() == 0)
            break;
        wr(it);
        cout << EN;
    }
    return 0;
}


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

long int bserc(long int key,long int cnt,long int in[])
{
    in[0]=99999999999;
    in[cnt+1]=0;
   long int low=1,high=cnt,ind=-1;

   if(in[cnt]>key)
   return -1;

    while(high>=low)
    {
        long int mid=(low+high)/2;
        if(in[mid]>key&&in[mid+1]<key)
        {
            ind=mid+1;
            break;
        }
        if(key>in[mid]&&in[mid-1]>key)
        {
           // printf("hi ");
            ind=mid;
            break;
        }
        if(in[mid]>key)
        {
            low=mid+1;
            ind=low;
        }
        else
        {
           // printf("g ");
        high=mid-1;
        ind=high;
        }

    }

    return ind;

}

int main()
{
    vector<long int>vec[1000000];
    long int n;
    scanf("%ld",&n);
    long int a[n],i,j;
    for(int i=1;i<=n;i++)
    {
        scanf("%ld",&a[i]);
    }
    long int cnt=1;
    vec[1].push_back(a[1]);
    long int in[n];
    in[1]=a[1];
    for(long int i=2;i<=n;i++)
    {
       long int k=bserc(a[i],cnt,in);

     //  cout<<k<<"k ";

       if(k==-1)
       {
           cnt++;
           vec[cnt].push_back(a[i]);
           in[cnt]=a[i];
       }
       else{
          vec[k].push_back(a[i]);
          in[k]=a[i];
       }

    }
    for(i=1;i<=cnt;i++)
    {
        for(j=0;j<vec[i].size();j++)
        {
            printf("%ld ",vec[i][j]);
        }
        printf("\n");
    }

    return 0;
}


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

long int bserc(long int key,long int cnt,long int in[])
{
    in[0]=99999999999;
    in[cnt+1]=0;
   long int low=1,high=cnt,ind=-1;

   if(in[cnt]>key)
   return -1;

    while(high>=low)
    {
        long int mid=(low+high)/2;
        if(in[mid]>key&&in[mid+1]<key)
        {
            ind=mid+1;
            break;
        }
        if(key>in[mid]&&in[mid-1]>key)
        {
           // printf("hi ");
            ind=mid;
            break;
        }
        if(in[mid]>key)
        {
            high=mid+1;
            ind=high;
        }
        else
        {
           // printf("g ");
        low=mid-1;
        ind=low;
        }

    }

    return ind;

}

int main()
{
    vector<long int>vec[1000000];
    long int n;
    scanf("%ld",&n);
    long int a[n],i,j;
    for(int i=1;i<=n;i++)
    {
        scanf("%ld",&a[i]);
    }
    long int cnt=1;
    vec[1].push_back(a[1]);
    long int in[n];
    in[1]=a[1];
    for(long int i=2;i<=n;i++)
    {
       long int k=bserc(a[i],cnt,in);

     //  cout<<k<<"k ";

       if(k==-1)
       {
           cnt++;
           vec[cnt].push_back(a[i]);
           in[cnt]=a[i];
       }
       else{
          vec[k].push_back(a[i]);
          in[k]=a[i];
       }

    }
    for(i=1;i<=cnt;i++)
    {
        for(j=0;j<vec[i].size();j++)
        {
            printf("%ld ",vec[i][j]);
        }
        printf("\n");
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    vector<int>vec[1000000];
    int n;
    scanf("%d",&n);
    int a[n],i,j;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    int cnt=0,f=0;
    vec[0].push_back(a[1]);
    for(int i=2;i<=n;i++)
    {
       // printf("%di ",i);
        f=0;
        for(j=0;j<=cnt;j++)
        {
           // printf("%dj ",j);
            if(vec[j].back()<a[i])
            {
             //  printf("a");
                vec[j].push_back(a[i]);
                f=1;
                break;
            }
        }
        if(f==0)
        {

            cnt++;
            vec[cnt].push_back(a[i]);
        }
    }
    for(i=0;i<=cnt;i++)
    {
        for(j=0;j<vec[i].size();j++)
        {
            printf("%d ",vec[i][j]);
        }
        printf("\n");
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    vector<long int>vec[1000000];
    long int n;
    scanf("%ld",&n);
    long int a[n],i,j;
    for(int i=1;i<=n;i++)
    {
        scanf("%ld",&a[i]);
    }
    long int cnt=0,f=0;
    vec[0].push_back(a[1]);
    for(long int i=2;i<=n;i++)
    {
       // printf("%di ",i);
        f=0;
        for(j=0;j<=cnt;j++)
        {
           // printf("%dj ",j);
            if(vec[j].back()<a[i])
            {
             //  printf("a");
                vec[j].push_back(a[i]);
                f=1;
                break;
            }
        }
        if(f==0)
        {

            cnt++;
            vec[cnt].push_back(a[i]);
        }
    }
    for(i=0;i<=cnt;i++)
    {
        for(j=0;j<vec[i].size();j++)
        {
            printf("%ld ",vec[i][j]);
        }
        printf("\n");
    }

    return 0;
}


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

long int bserc(long int key,long int cnt,long int in[])
{
   long int low=1,high=cnt,ind=-1;

   if(in[cnt]>key)
   return -1;

    while(high>=low)
    {
        long int mid=(low+high)/2;
        if(in[mid]>key&&in[mid+1]<key)
        {
            ind=mid+1;
            break;
        }
        if(key>in[mid]&&in[mid-1]>key)
        {
           // printf("hi ");
            ind=mid;
            break;
        }
        if(in[mid]>key)
        {
            high=mid+1;
            ind=high;
        }
        else
        {
           // printf("g ");
        low=mid-1;
        ind=low;
        }

    }

    return ind;

}

int main()
{
    vector<long int>vec[1000000];
    long int n;
    scanf("%ld",&n);
    long int a[n],i,j;
    for(int i=1;i<=n;i++)
    {
        scanf("%ld",&a[i]);
    }
    long int cnt=1;
    vec[1].push_back(a[1]);
    long int in[n];
    in[1]=a[1];
    for(long int i=2;i<=n;i++)
    {
       long int k=bserc(a[i],cnt,in);

     //  cout<<k<<"k ";

       if(k==-1)
       {
           cnt++;
           vec[cnt].push_back(a[i]);
           in[cnt]=a[i];
       }
       else{
          vec[k].push_back(a[i]);
          in[k]=a[i];
       }

    }
    for(i=1;i<=cnt;i++)
    {
        for(j=0;j<vec[i].size();j++)
        {
            printf("%ld ",vec[i][j]);
        }
        printf("\n");
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N =2e5+5;
const long long M = (long long)1e9+7;
typedef     long long       ll;
int Int() {
    int x;
    scanf("%d",&x);
    return x;
}
ll Long() {
    ll x;
    scanf("%I64d",&x);
    return x;
}
void Char(char* ch) {
    scanf("%s",ch);
}
template<typename t>t abs(t a) {
    if(a >= 0)return a;
    return -a ;
}
template<typename t>t Range(vector<t> v, t x) {
    ///Number of elements in v which is equal to x ;
    auto u = equal_range(v.begin(),v.end(),x);
    return u.second - u.first;
}
///error///
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s,char c) {
    vector<string>v;
    stringstream ss(s);
    string x;
    while(getline(ss,x,c))
        v.emplace_back(x);
    return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T,typename... Args>
void err(vector<string>::iterator it,T a,Args...args) {
    cerr<<it->substr((*it)[0]==' ',it->length())<< " = " << a << " ";
    err(++it,args...);
}
///This DEBUGGER-tool need c++11///
#define     Int         Int()
#define     Long        Long()
#define     pii         pair<int,int>
#define     ff          first
#define     ss          second
#define     vi          vector<int>
#define     vl          vector<ll>
#define     pb          push_back
#define     all(x)      x.begin(),x.end()
#define     sq(a)       (a)*(a)

int a[N];
vector<int>res[N],pos;
int index(int v) {
    int l = 0 , r = (int)p.size() - 1 , mid = 0 ;
    while(l <= r) {
        mid = (l + r) >> 1 ;
        if(pos[mid] <= v) {
            r = mid - 1 ;
        } else {
            l = mid + 1 ;
        }
    }
    return l ;
}
int main() {
    //freopen("input.txt","r",stdin);
    int n = Int ;
  //  vector<int>pos ;
    int id = 0 ;
    for(int i = 1 ; i <= n ; i++) {
            a[i] = Int;
            if(i == 1) {
            pos.pb(a[i]);
            res[id].pb(a[i]);
        } else {
            int f = index(a[i]);
            if(f<0) f = 0 ;
            if(f == (int)pos.size()) {
                pos.pb(a[i]);
                res[++id].pb(a[i]);
            } else {
                pos[f] = a[i] ;
                res[f].pb(a[i]);
            }
        }
    }
   // cout<<(int)pos.size()<<endl;
    for(int i = 0 ; i <= id ; i++) {
        for(int j = 0 ; j < (int)res[i].size() ; j++) {
            printf("%d ",res[i][j]);
        }
        puts("");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N =1e5+5;
const long long M = (long long)1e9+7;
typedef     long long       ll;
int Int() {
    int x;
    scanf("%d",&x);
    return x;
}
ll Long() {
    ll x;
    scanf("%I64d",&x);
    return x;
}
void Char(char* ch) {
    scanf("%s",ch);
}
template<typename t>t abs(t a) {
    if(a >= 0)return a;
    return -a ;
}
template<typename t>t Range(vector<t> v, t x) {
    ///Number of elements in v which is equal to x ;
    auto u = equal_range(v.begin(),v.end(),x);
    return u.second - u.first;
}
///error///
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s,char c) {
    vector<string>v;
    stringstream ss(s);
    string x;
    while(getline(ss,x,c))
        v.emplace_back(x);
    return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T,typename... Args>
void err(vector<string>::iterator it,T a,Args...args) {
    cerr<<it->substr((*it)[0]==' ',it->length())<< " = " << a << " ";
    err(++it,args...);
}
///This DEBUGGER-tool need c++11///
#define     Int         Int()
#define     Long        Long()
#define     pii         pair<int,int>
#define     ff          first
#define     ss          second
#define     vi          vector<int>
#define     vl          vector<ll>
#define     pb          push_back
#define     all(x)      x.begin(),x.end()
#define     sq(a)       (a)*(a)

int a[N];
vector<int>res[N];
int index(vector<int>p, int v) {
    int l = 0 , r = (int)p.size() - 1 , mid = 0 ;
    while(l <= r) {
        int mid = (l + r) >> 1 ;
        if(p[mid] <= v) {
            r = mid - 1 ;
        } else {
            l = mid + 1 ;
        }
    }
    return l ;
}
int main() {
    //freopen("input.txt","r",stdin);
    int n = Int ;
    for(int i = 1 ; i <= n ; i++) {
        a[i] = Int ;
    }
    vector<int>pos ;
    int id = 0 ;
    for(int i = 1 ; i <= n ; i++) {
        if(i == 1) {
            pos.pb(a[i]);
            res[id].pb(a[i]);
        } else {
            int f = index(pos,a[i] - 1);
            if(f == (int)pos.size()) {
                pos.pb(a[i]);
                res[++id].pb(a[i]);
            } else {
                pos[f] = a[i] ;
                res[f].pb(a[i]);
            }
        }
    }
    cout<<(int)pos.size()<<endl;
    for(int i = 0 ; i <= id ; i++) {
        for(auto u : res[i]) {
            printf("%d ",u);
        }
        puts("");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N =2e5+5;
const long long M = (long long)1e9+7;
typedef     long long       ll;
int Int() {
    int x;
    scanf("%d",&x);
    return x;
}
ll Long() {
    ll x;
    scanf("%I64d",&x);
    return x;
}
void Char(char* ch) {
    scanf("%s",ch);
}
template<typename t>t abs(t a) {
    if(a >= 0)return a;
    return -a ;
}
template<typename t>t Range(vector<t> v, t x) {
    ///Number of elements in v which is equal to x ;
    auto u = equal_range(v.begin(),v.end(),x);
    return u.second - u.first;
}
///error///
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s,char c) {
    vector<string>v;
    stringstream ss(s);
    string x;
    while(getline(ss,x,c))
        v.emplace_back(x);
    return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T,typename... Args>
void err(vector<string>::iterator it,T a,Args...args) {
    cerr<<it->substr((*it)[0]==' ',it->length())<< " = " << a << " ";
    err(++it,args...);
}
///This DEBUGGER-tool need c++11///
#define     Int         Int()
#define     Long        Long()
#define     pii         pair<int,int>
#define     ff          first
#define     ss          second
#define     vi          vector<int>
#define     vl          vector<ll>
#define     pb          push_back
#define     all(x)      x.begin(),x.end()
#define     sq(a)       (a)*(a)

int a[N];
vector<int>res[N];
int index(vector<int>p, int v) {
    int l = 0 , r = (int)p.size() - 1 , mid = 0 ;
    while(l <= r) {
        int mid = (l + r) >> 1 ;
        if(p[mid] <= v) {
            r = mid - 1 ;
        } else {
            l = mid + 1 ;
        }
    }
    return l ;
}
int main() {
    //freopen("input.txt","r",stdin);
    int n = Int ;
    vector<int>pos ;
    int id = 0 ;
    for(int i = 1 ; i <= n ; i++) {
            a[i] = Int;
            if(i == 1) {
            pos.pb(a[i]);
            res[id].pb(a[i]);
        } else {
            int f = index(pos,a[i]);
            if(f == (int)pos.size()) {
                pos.pb(a[i]);
                res[++id].pb(a[i]);
            } else {
                pos[f] = a[i] ;
                res[f].pb(a[i]);
            }
        }
    }
   // cout<<(int)pos.size()<<endl;
    for(int i = 0 ; i <= id ; i++) {
        for(int j = 0 ; j < (int)res[i].size() ; j++) {
            printf("%d ",res[i][j]);
        }
        puts("");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N =2e5+5;
const long long M = (long long)1e9+7;
typedef     long long       ll;
int Int() {
    int x;
    scanf("%d",&x);
    return x;
}
ll Long() {
    ll x;
    scanf("%I64d",&x);
    return x;
}
void Char(char* ch) {
    scanf("%s",ch);
}
template<typename t>t abs(t a) {
    if(a >= 0)return a;
    return -a ;
}
template<typename t>t Range(vector<t> v, t x) {
    ///Number of elements in v which is equal to x ;
    auto u = equal_range(v.begin(),v.end(),x);
    return u.second - u.first;
}
///error///
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s,char c) {
    vector<string>v;
    stringstream ss(s);
    string x;
    while(getline(ss,x,c))
        v.emplace_back(x);
    return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T,typename... Args>
void err(vector<string>::iterator it,T a,Args...args) {
    cerr<<it->substr((*it)[0]==' ',it->length())<< " = " << a << " ";
    err(++it,args...);
}
///This DEBUGGER-tool need c++11///
#define     Int         Int()
#define     Long        Long()
#define     pii         pair<int,int>
#define     ff          first
#define     ss          second
#define     vi          vector<int>
#define     vl          vector<ll>
#define     pb          push_back
#define     all(x)      x.begin(),x.end()
#define     sq(a)       (a)*(a)

int a[N];
vector<int>res[N],pos;
int index(vector<int>p, int v) {
    int l = 0 , r = (int)p.size() - 1 , mid = 0 ;
    while(l <= r) {
        int mid = (l + r) >> 1 ;
        if(p[mid] <= v) {
            r = mid - 1 ;
        } else {
            l = mid + 1 ;
        }
    }
    return l ;
}
int main() {
    //freopen("input.txt","r",stdin);
    int n = Int ;
  //  vector<int>pos ;
    int id = 0 ;
    for(int i = 1 ; i <= n ; i++) {
            a[i] = Int;
            if(i == 1) {
            pos.pb(a[i]);
            res[id].pb(a[i]);
        } else {
            int f = index(pos,a[i]);
            if(f<0) f = 0 ;
            if(f == (int)pos.size()) {
                pos.pb(a[i]);
                res[++id].pb(a[i]);
            } else {
                pos[f] = a[i] ;
                res[f].pb(a[i]);
            }
        }
    }
   // cout<<(int)pos.size()<<endl;
    for(int i = 0 ; i <= id ; i++) {
        for(int j = 0 ; j < (int)res[i].size() ; j++) {
            printf("%d ",res[i][j]);
        }
        puts("");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N =2e5+5;
const long long M = (long long)1e9+7;
typedef     long long       ll;
int Int() {
    int x;
    scanf("%d",&x);
    return x;
}
ll Long() {
    ll x;
    scanf("%I64d",&x);
    return x;
}
void Char(char* ch) {
    scanf("%s",ch);
}
template<typename t>t abs(t a) {
    if(a >= 0)return a;
    return -a ;
}
template<typename t>t Range(vector<t> v, t x) {
    ///Number of elements in v which is equal to x ;
    auto u = equal_range(v.begin(),v.end(),x);
    return u.second - u.first;
}
///error///
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s,char c) {
    vector<string>v;
    stringstream ss(s);
    string x;
    while(getline(ss,x,c))
        v.emplace_back(x);
    return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T,typename... Args>
void err(vector<string>::iterator it,T a,Args...args) {
    cerr<<it->substr((*it)[0]==' ',it->length())<< " = " << a << " ";
    err(++it,args...);
}
///This DEBUGGER-tool need c++11///
#define     Int         Int()
#define     Long        Long()
#define     pii         pair<int,int>
#define     ff          first
#define     ss          second
#define     vi          vector<int>
#define     vl          vector<ll>
#define     pb          push_back
#define     all(x)      x.begin(),x.end()
#define     sq(a)       (a)*(a)

int a[N];
vector<int>res[N];
int index(vector<int>p, int v) {
    int l = 0 , r = (int)p.size() - 1 , mid = 0 ;
    while(l <= r) {
        int mid = (l + r) >> 1 ;
        if(p[mid] <= v) {
            r = mid - 1 ;
        } else {
            l = mid + 1 ;
        }
    }
    return l ;
}
int main() {
    //freopen("input.txt","r",stdin);
    int n = Int ;
    for(int i = 1 ; i <= n ; i++) {
        a[i] = Int ;
    }
    vector<int>pos ;
    int id = 0 ;
    for(int i = 1 ; i <= n ; i++) {
        if(i == 1) {
            pos.pb(a[i]);
            res[id].pb(a[i]);
        } else {
            int f = index(pos,a[i] - 1);
            if(f == (int)pos.size()) {
                pos.pb(a[i]);
                res[++id].pb(a[i]);
            } else {
                pos[f] = a[i] ;
                res[f].pb(a[i]);
            }
        }
    }
   // cout<<(int)pos.size()<<endl;
    for(int i = 0 ; i <= id ; i++) {
        for(auto u : res[i]) {
            printf("%d ",u);
        }
        puts("");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N =2e5+5;
const long long M = (long long)1e9+7;
typedef     long long       ll;
int Int() {
    int x;
    scanf("%d",&x);
    return x;
}
ll Long() {
    ll x;
    scanf("%I64d",&x);
    return x;
}
void Char(char* ch) {
    scanf("%s",ch);
}
template<typename t>t abs(t a) {
    if(a >= 0)return a;
    return -a ;
}
template<typename t>t Range(vector<t> v, t x) {
    ///Number of elements in v which is equal to x ;
    auto u = equal_range(v.begin(),v.end(),x);
    return u.second - u.first;
}
///error///
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s,char c) {
    vector<string>v;
    stringstream ss(s);
    string x;
    while(getline(ss,x,c))
        v.emplace_back(x);
    return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T,typename... Args>
void err(vector<string>::iterator it,T a,Args...args) {
    cerr<<it->substr((*it)[0]==' ',it->length())<< " = " << a << " ";
    err(++it,args...);
}
///This DEBUGGER-tool need c++11///
#define     Int         Int()
#define     Long        Long()
#define     pii         pair<int,int>
#define     ff          first
#define     ss          second
#define     vi          vector<int>
#define     vl          vector<ll>
#define     pb          push_back
#define     all(x)      x.begin(),x.end()
#define     sq(a)       (a)*(a)

int a[N];
vector<int>res[N];
int index(vector<int>p, int v) {
    int l = 0 , r = (int)p.size() - 1 , mid = 0 ;
    while(l <= r) {
        int mid = (l + r) >> 1 ;
        if(p[mid] <= v) {
            r = mid - 1 ;
        } else {
            l = mid + 1 ;
        }
    }
    return l ;
}
int main() {
    //freopen("input.txt","r",stdin);
    int n = Int ;
    vector<int>pos ;
    int id = 0 ;
    for(int i = 1 ; i <= n ; i++) {
            a[I] = Int;
            if(i == 1) {
            pos.pb(a[i]);
            res[id].pb(a[i]);
        } else {
            int f = index(pos,a[i]);
            if(f == (int)pos.size()) {
                pos.pb(a[i]);
                res[++id].pb(a[i]);
            } else {
                pos[f] = a[i] ;
                res[f].pb(a[i]);
            }
        }
    }
   // cout<<(int)pos.size()<<endl;
    for(int i = 0 ; i <= id ; i++) {
        for(int j = 0 ; j < (int)res[i].size() ; j++) {
            printf("%d ",res[i][j]);
        }
        puts("");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N =2e5+5;
const long long M = (long long)1e9+7;
typedef     long long       ll;
int Int() {
    int x;
    scanf("%d",&x);
    return x;
}
ll Long() {
    ll x;
    scanf("%I64d",&x);
    return x;
}
void Char(char* ch) {
    scanf("%s",ch);
}
template<typename t>t abs(t a) {
    if(a >= 0)return a;
    return -a ;
}
template<typename t>t Range(vector<t> v, t x) {
    ///Number of elements in v which is equal to x ;
    auto u = equal_range(v.begin(),v.end(),x);
    return u.second - u.first;
}
///error///
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s,char c) {
    vector<string>v;
    stringstream ss(s);
    string x;
    while(getline(ss,x,c))
        v.emplace_back(x);
    return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T,typename... Args>
void err(vector<string>::iterator it,T a,Args...args) {
    cerr<<it->substr((*it)[0]==' ',it->length())<< " = " << a << " ";
    err(++it,args...);
}
///This DEBUGGER-tool need c++11///
#define     Int         Int()
#define     Long        Long()
#define     pii         pair<int,int>
#define     ff          first
#define     ss          second
#define     vi          vector<int>
#define     vl          vector<ll>
#define     pb          push_back
#define     all(x)      x.begin(),x.end()
#define     sq(a)       (a)*(a)

int a[N];
vector<int>res[N],pos;
int index(int v) {
    int l = 0 , r = (int)pos.size() - 1 , mid = 0 ;
    while(l <= r) {
        mid = (l + r) >> 1 ;
        if(pos[mid] <= v) {
            r = mid - 1 ;
        } else {
            l = mid + 1 ;
        }
    }
    return l ;
}
int main() {
    //freopen("input.txt","r",stdin);
    int n = Int ;
  //  vector<int>pos ;
    int id = 0 ;
    for(int i = 1 ; i <= n ; i++) {
            a[i] = Int;
            if(i == 1) {
            pos.pb(a[i]);
            res[id].pb(a[i]);
        } else {
            int f = index(a[i]);
            if(f<0) f = 0 ;
            if(f == (int)pos.size()) {
                pos.pb(a[i]);
                res[++id].pb(a[i]);
            } else {
                pos[f] = a[i] ;
                res[f].pb(a[i]);
            }
        }
    }
   // cout<<(int)pos.size()<<endl;
    for(int i = 0 ; i <= id ; i++) {
        for(int j = 0 ; j < (int)res[i].size() ; j++) {
            printf("%d ",res[i][j]);
        }
        puts("");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N =1e5+5;
const long long M = (long long)1e9+7;
typedef     long long       ll;
int Int() {
    int x;
    scanf("%d",&x);
    return x;
}
ll Long() {
    ll x;
    scanf("%I64d",&x);
    return x;
}
void Char(char* ch) {
    scanf("%s",ch);
}
template<typename t>t abs(t a) {
    if(a >= 0)return a;
    return -a ;
}
template<typename t>t Range(vector<t> v, t x) {
    ///Number of elements in v which is equal to x ;
    auto u = equal_range(v.begin(),v.end(),x);
    return u.second - u.first;
}
///error///
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s,char c) {
    vector<string>v;
    stringstream ss(s);
    string x;
    while(getline(ss,x,c))
        v.emplace_back(x);
    return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T,typename... Args>
void err(vector<string>::iterator it,T a,Args...args) {
    cerr<<it->substr((*it)[0]==' ',it->length())<< " = " << a << " ";
    err(++it,args...);
}
///This DEBUGGER-tool need c++11///
#define     Int         Int()
#define     Long        Long()
#define     pii         pair<int,int>
#define     ff          first
#define     ss          second
#define     vi          vector<int>
#define     vl          vector<ll>
#define     pb          push_back
#define     all(x)      x.begin(),x.end()
#define     sq(a)       (a)*(a)

int a[N];
vector<int>res[N];
int index(vector<int>p, int v) {
    int l = 0 , r = (int)p.size() - 1 , mid = 0 ;
    while(l <= r) {
        int mid = (l + r) >> 1 ;
        if(p[mid] <= v) {
            r = mid - 1 ;
        } else {
            l = mid + 1 ;
        }
    }
    return l ;
}
int main() {
    //freopen("input.txt","r",stdin);
    int n = Int ;
    for(int i = 1 ; i <= n ; i++) {
        a[i] = Int ;
    }
    vector<int>pos ;
    int id = 0 ;
    for(int i = 1 ; i <= n ; i++) {
        if(i == 1) {
            pos.pb(a[i]);
            res[id].pb(a[i]);
        } else {
            int f = index(pos,a[i] - 1);
            if(f == (int)pos.size()) {
                pos.pb(a[i]);
                res[++id].pb(a[i]);
            } else {
                pos[f] = a[i] ;
                res[f].pb(a[i]);
            }
        }
    }
   // cout<<(int)pos.size()<<endl;
    for(int i = 0 ; i <= id ; i++) {
        for(auto u : res[i]) {
            printf("%d ",u);
        }
        puts("");
    }
    return 0;
}