Ivan has an array consisting of n different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.

Ivan represent his array with increasing sequences with help of the following algorithm.

While there is at least one unused number in array Ivan repeats the following procedure:  iterate through array from the left to the right;  Ivan only looks at unused numbers on current iteration;  if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down. 

For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one — [2, 4].

Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above.


-----Input-----

The first line contains a single integer n (1 ≤ n ≤ 2·10^5) — the number of elements in Ivan's array.

The second line contains a sequence consisting of distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9) — Ivan's array.


-----Output-----

Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.


-----Examples-----
Input
5
1 3 2 5 4

Output
1 3 5 
2 4 

Input
4
4 3 2 1

Output
4 
3 
2 
1 

Input
4
10 30 50 101

Output
10 30 50 101

Difficulty: competition

Starter code:

Solutions:
n = int(input())
from bisect import bisect_left


a = list(map(int, input().split()))
ss = []
ms = []
for i in range(n):
    k = a[i]
    ind = bisect_left(ms, -k)
    if ind == len(ms):
        ss.append([])
        ms.append(0)
    ss[ind].append(k)
    ms[ind] = -k

for s in ss:
    print(' '.join([str(i) for i in s]))

n = int(input())

a = []
arr = list(map(int, input().split()))

for i in range(n):
    if len(a) == 0:
        a.append([arr[i]])
        continue
    el = arr[i]
    l, r = 0, len(a) - 1
    while r - l >= 1:
        m = (l + r) // 2
        if el > a[m][-1]:
            r = m
        else:
            l = m + 1
    if el < a[l][-1]:
        a.append([el])
    else:
        a[l].append(el)

for x in a:
    for el in x:
        print(el, end=' ')
    print()

ans = []
n = int(input())
arr = list(map(int,input().split()))
for x in arr:
    lo,hi = 0,len(ans)
    while(lo < hi):
        mid = (lo+hi)//2
        if(ans[mid][-1] < x):
            hi = mid
        else:
            lo = mid+1
    if(lo == len(ans)):
        ans.append([x])
    else:
        ans[lo].append(x)
for line in ans:
    print(' '.join([str(i) for i in line]))


n = int(input())
from bisect import bisect_left


a = list(map(int, input().split()))
ss = []
ms = []
for i in range(n):
    k = a[i]
    ind = bisect_left(ms, -k)
    if ind == len(ms):
        ss.append([])
        ms.append(0)
    ss[ind].append(k)
    ms[ind] = -k

for s in ss:
    print(' '.join([str(i) for i in s]))


n = int(input())

from bisect import bisect_left





a = list(map(int, input().split()))

ss = []

ms = []

for i in range(n):

    k = a[i]

    ind = bisect_left(ms, -k)

    if ind == len(ms):

        ss.append([])

        ms.append(0)

    ss[ind].append(k)

    ms[ind] = -k



for s in ss:

    print(' '.join([str(i) for i in s]))



# Made By Mostafa_Khaled


n = int(input())
lst = list(map(int, input().strip().split()))

ans = [[lst[0]],]
head = [lst[0]]

added = False
for i in range(1, n):
    t = lst[i]
    if (head[len(head)-1]) > t:
        ans.append([t])
        head.append(t)
    else:
        l, r = 0, len(head)-1
        while r-l > 1:
            mm = l + int((r-l)/2)
            if head[mm] < t:
                r = mm
            else:
                l = mm
        if head[l] < t:
            ans[l].append(t)
            head[l] = t
        else:
            ans[r].append(t)
            head[r] = t

for ls in ans:
    print(' ' .join(str(x) for x in ls))

from bisect import bisect_left
n = int(input())
a = list(map(int, input().split()))

p = []
result = []
for x in a:
    r = bisect_left(p, -x)
    if r == len(result):
        result.append([])
        p.append(0)

    result[r].append(x)
    p[r] = -x

for arr in result:
    for i in range(len(arr)-1):
        print(arr[i], end=" ")
    print(arr[-1])

from sys import stdin
from collections import deque
from bisect import bisect_right  as br
n=int(stdin.readline().strip())
s=list(map(int,stdin.readline().strip().split()))
arr=deque([s[0]])
ans=deque([[s[0]]])
for i in range(1,n):
    x=br(arr,s[i])
    if x==0:
        arr.appendleft(s[i])
        ans.appendleft([s[i]])
    else:
        arr[x-1]=s[i]
        ans[x-1].append(s[i])
ans.reverse()
for i in ans:
    print(*i)
    


n=int(input())
arr=list(map(int,input().split()))
ans=[0]*1
ans[0]=[]
ans[0].append(arr[0])
for i in range(1,n,1):
	lo,hi=0,len(ans)
	idx=-1
	while(lo<=hi):
		mid=(lo+hi)//2
		#print(lo,hi,i)
		if lo!=len(ans) and ans[mid][len(ans[mid])-1]<arr[i]:
			idx=mid
			hi=mid-1
		else:
			lo=mid+1
	if len(ans)==hi:
		ans.append([])
		ans[hi].append(arr[i])
	else:
		ans[idx].append(arr[i])
for i in range(len(ans)):
	for j in range(len(ans[i])):
		print(ans[i][j],end=" ")
	print()


class CodeforcesTask847BSolution:
    def __init__(self):
        self.result = ''
        self.n = 0
        self.sequence = []

    def read_input(self):
        self.n = int(input())
        self.sequence = [int(x) for x in input().split(" ")]

    def process_task(self):
        mx = 2000001
        ans = [[] for x in range(mx)]
        crf = [0] * mx
        cnt = 0
        id = 0
        for x in range(self.n):
            if not x:
                ans[cnt].append(self.sequence[x])
                crf[cnt] = self.sequence[x]
            else:
                if self.sequence[x] <= crf[cnt]:
                    cnt += 1
                    id = cnt
                else:
                    l = 0
                    r = cnt
                    while l < r:
                        mid = (l + r) // 2
                        if crf[mid] >= self.sequence[x]:
                            l = mid + 1
                        else:
                            r = mid
                    id = r
                ans[id].append(self.sequence[x])
                crf[id] = self.sequence[x]
        self.result = "\n".join([" ".join([str(x) for x in row]) for row in ans[:cnt + 1]])


    def get_result(self):
        return self.result


def __starting_point():
    Solution = CodeforcesTask847BSolution()
    Solution.read_input()
    Solution.process_task()
    print(Solution.get_result())

__starting_point()

from bisect import bisect_left

n = int(input())
A = list(map(int,input().split()))

max_vals = []
result_lists = []

for i in range(n):
    val = -A[i]
    idx = bisect_left(max_vals,val)
    if idx == len(max_vals):
        max_vals.append(0)
        result_lists.append([])
    max_vals[idx] = val
    result_lists[idx].append(-val)

for res in result_lists:
    print(" ".join([str(i) for i in res]))


n = int(input())

a = []
arr = list(map(int, input().split()))

for i in range(n):
    if len(a) == 0:
        a.append([arr[i]])
        continue
    el = arr[i]
    l, r = 0, len(a) - 1
    while r - l >= 1:
        m = (l + r) // 2
        if el > a[m][-1]:
            r = m
        else:
            l = m + 1
    if el < a[l][-1]:
        a.append([el])
    else:
        a[l].append(el)

for x in a:
    for el in x:
        print(el, end=' ')
    print()

n = int(input())
a = list(map(int,input().split()))
groups = []
groupTail = []

for elem in a:
    if not groups:
        groups.append([elem])
        groupTail.append(elem)
    else:
        l = 0
        r = len(groups)
        while l <= r:
            m = (l + r) // 2
            if m == len(groups):
                break
            if l == r:
                break
            if groupTail[m] < elem:
                r = m
            else:
                l = m + 1
        if m == len(groups):
            groups.append([elem])
            groupTail.append(elem)
        else:
            groups[m].append(elem)
            groupTail[m] = elem

for line in groups:
    print(" ".join(map(str,line)))

n=int(input())
a=list(map(int,input().split()))
ans=[]
for i in range(n):
    l=0
    r=len(ans)
    while l<r:
        mid=l+(r-l)//2
        if ans[mid][-1]<a[i]:
            r=mid
        else:
            l=mid+1
    if l==len(ans):
        ans.append([a[i]])
    else:
        ans[l].append(a[i])
for x in ans:
    print(*x)




def readInt():
    return int(input())

def readInts():
    return [ int(i) for i in input().split() ]
    
n = readInt()
a = readInts()

nexts = [-1] * n
heads = [-1] * n
lasts = [-1] * n
cnt = 0

for i in range(n):
    value = a[i]
    
    left = -1
    right = cnt
    
    while right - left > 1:
        mid = (right + left) // 2
        if a[lasts[mid]] < value:
            right = mid
        else:
            left = mid
            
    if right == cnt:
        heads[right] = i
        cnt += 1
    else:
        last = lasts[right]
        nexts[last] = i
    
    lasts[right] = i

for i in range(cnt):
    index = heads[i]
    while index != -1:
        print(a[index], end = ' ')
        index = nexts[index]
    print()