Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a_1, a_2, ..., a_{n} in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). 

procedure bubbleSortGraph()

    build a graph G with n vertices and 0 edges

    repeat

        swapped = false

        for i = 1 to n - 1 inclusive do:

            if a[i] > a[i + 1] then

                add an undirected edge in G between a[i] and a[i + 1]

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

                swapped = true

            end if

        end for

    until not swapped 

    /* repeat the algorithm as long as swapped value is true. */ 

end procedure



For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph.


-----Input-----

The first line of the input contains an integer n (2 ≤ n ≤ 10^5). The next line contains n distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ n).


-----Output-----

Output a single integer — the answer to the problem. 


-----Examples-----
Input
3
3 1 2

Output
2



-----Note-----

Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2].

Difficulty: interview

Starter code:

Solutions:
def lis(a):
    b = []
    for c in a:
        # if len(b) == 0 or c > b[-1]
        if len(b) == 0 or c > b[-1]:
            b.append(c)
        else:
            l = 0
            r = len(b)
            while l < r-1:
                m = l+r>>1
                # if b[m] <= c: l = m
                if b[m] < c: l = m
                else: r = m
            # if b[l] <= c: l += 1
            if b[l] < c: l += 1
            b[l] = c
        
    return len(b)
                    

n = int(input())

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

print(lis(a))


from bisect import bisect_right
s, n = [0], input()
for i in map(int, input().split()):
    if i > s[-1]: s.append(i)
    else: s[bisect_right(s, i)] = i
print(len(s) - 1)

import sys; sys.setrecursionlimit(1000000)

def solve():
    # 3 1 2 4
    # 1 2 3 4

    # 2 1 3 5 4
    # 1 2 3 4 5

    n, = rv()
    a, = rl(1)



    # 3 1 2 7 4 6 5
    # [ , 1 ,  , , , ]
    # [ , 1 , 2 , , , ]
    # [ , 1 , 2 , 4, 5, ]

    mem = [10000000] * (n + 1)
    mem[0] = a[0]
    for i in range(1, n):
        left, right = 0, n - 1
        while left < right:
            mid = (left + right) // 2
            if a[i] < mem[mid]: right = mid
            else: left = mid + 1
        mem[left] = a[i]
        # for j in range(n):
        #     if a[i] < mem[j]:
        #         mem[j] = a[i]
        #         break
    res = 0
    # print(mem)
    for i in range(1, n):
        if mem[i] != 10000000: res = i
    print(res + 1)




def rv(): return list(map(int, input().split()))
def rl(n): return [list(map(int, input().split())) for _ in range(n)]
if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
solve()




from bisect import bisect_left, bisect_right, insort
R = lambda: map(int, input().split())
n, arr = int(input()), list(R())
dp = []
for i in range(n):
    idx = bisect_left(dp, arr[i])
    if idx >= len(dp):
        dp.append(arr[i])
    else:
        dp[idx] = arr[i]
print(len(dp))

n = int(input())
num_list = list(map(int, input().split()))
# def lower_bound(min_lis, x): 
#     #goal return the position of the first element >= x
#     left = 0 
#     right = len(min_lis) - 1
#     res = -1
#     while left <= right:
#         mid = (left + right) // 2 
#         if min_lis[mid] < x: 
#             left = mid + 1
#         else:
#             res = mid
#             right = mid - 1
#     return res
import bisect

def LongestIncreasingSubsequence(a, n):
    min_lis = []
    #lis = [0 for i in range(n)]
    for i in range(n): 
        pos = bisect.bisect_left(min_lis, a[i])
        if pos == len(min_lis):
            #lis[i] = len(min_lis) + 1
            min_lis.append(a[i])
        else:
            #lis[i] = pos + 1
            min_lis[pos] = a[i]
        #print(*min_lis)
    return (len(min_lis))

print(LongestIncreasingSubsequence(num_list, n))


import math
import sys
from bisect import bisect_right, bisect_left, insort_right
from collections import Counter, defaultdict
from heapq import heappop, heappush
from itertools import accumulate, permutations, combinations
from sys import stdout

R = lambda: map(int, input().split())
n = int(input())
arr = list(R())
tps = [(0, 0)]
for x in arr:
    i = bisect_left(tps, (x, -1)) - 1
    tps.insert(i + 1, (x, tps[i][1] + 1))
    if i + 2 < len(tps) and tps[i + 1][1] >= tps[i + 2][1]:
        del tps[i + 2]
print(max(x[1] for x in tps))

"""
http://codeforces.com/problemset/problem/341/B
"""

from typing import List
from bisect import bisect_right


class Solution:
    def patienceSort(self, a: List[int]) -> int:
        # patience sort can find longest increasing subsequence in O(NlogN)
        # https://en.wikipedia.org/wiki/Patience_sorting
        n = len(a)
        piles = [n + 1 for i in range(n)]  # To find length of LIS, we're only interested in the top card of each pile
        nextPileIdx = 0
        for i in a:
            idx = bisect_right(piles, i, 0, nextPileIdx)
            piles[idx] = i
            if idx >= nextPileIdx:
                nextPileIdx += 1

        return nextPileIdx

    def maxIndependentSetSize(self, a: List[int]) -> int:
        return self.patienceSort(a)


def __starting_point():
    n = int(input().strip())
    a = list(map(int, input().strip().split()))
    s = Solution()
    print(s.maxIndependentSetSize(a))

__starting_point()

MXusl = int
MXuso = input
MXusL = map
MXusr = min
MXusI = print
n = MXusl(MXuso())
a = [1e6] * (n + 1)
s = 1
for x in MXusL(MXusl, MXuso().split()):
    l = 0
    r = s
    while r-l > 1:
        m = (l + r) >> 1
        if a[m] < x:
            l = m
        else:
            r = m
    s += r == s
    a[r] = MXusr(a[r], x)
MXusI(s - 1)


def CeilIndex(A, l, r, key): 
  
    while (r - l > 1): 
      
        m = l + (r - l)//2
        if (A[m] >= key): 
            r = m 
        else: 
            l = m 
    return r 
   
def LIS(A, size): 
    tailTable = [0 for i in range(size + 1)] 
    len = 0 
   
    tailTable[0] = A[0] 
    len = 1
    for i in range(1, size): 
      
        if (A[i] < tailTable[0]): 
            tailTable[0] = A[i] 
   
        elif (A[i] > tailTable[len-1]): 
            tailTable[len] = A[i] 
            len+= 1
   
        else: 
            tailTable[CeilIndex(tailTable, -1, len-1, A[i])] = A[i] 
          
   
    return len

def main():
    n = int(input())
    arr = list(map(int,input().split()))

    print(LIS(arr,n))
    

main()


from bisect import *
s, n = [0], input()
for i in map(int, input().split()):
    if i > s[-1]: s.append(i)
    else: s[bisect_right(s, i)] = i
    
print(len(s) - 1)

# Python program to find 
# length of longest 
# increasing subsequence 
# in O(n Log n) time 

# Binary search (note 
# boundaries in the caller) 
# A[] is ceilIndex 
# in the caller 
def CeilIndex(A, l, r, key): 

	while (r - l > 1): 
	
		m = l + (r - l)//2
		if (A[m] >= key): 
			r = m 
		else: 
			l = m 
	return r 

def Lis(A, size): 

	# Add boundary case, 
	# when array size is one 

	tailTable = [0 for i in range(size + 1)] 
	len = 0 # always points empty slot 

	tailTable[0] = A[0] 
	len = 1
	for i in range(1, size): 
	
		if (A[i] < tailTable[0]): 

			# new smallest value 
			tailTable[0] = A[i] 

		elif (A[i] > tailTable[len-1]): 

			# A[i] wants to extend 
			# largest subsequence 
			tailTable[len] = A[i] 
			len+= 1

		else: 
			# A[i] wants to be current 
			# end candidate of an existing 
			# subsequence. It will replace 
			# ceil value in tailTable 
			tailTable[CeilIndex(tailTable, -1, len-1, A[i])] = A[i] 
		

	return len


# Driver program to 
# test above function 

a=int(input())
z=list(map(int,input().split()))
print(Lis(z,a))


def CeilIndex(A, l, r, key): 
  
    while (r - l > 1): 
      
        m = l + (r - l)//2
        if (A[m] >= key): 
            r = m 
        else: 
            l = m 
    return r 
   
def LongestIncreasingSubsequenceLength(A, size): 
  
    # Add boundary case, 
    # when array size is one 
   
    tailTable = [0 for i in range(size + 1)] 
    len = 0 # always points empty slot 
   
    tailTable[0] = A[0] 
    len = 1
    for i in range(1, size): 
      
        if (A[i] < tailTable[0]): 
  
            # new smallest value 
            tailTable[0] = A[i] 
   
        elif (A[i] > tailTable[len-1]): 
  
            # A[i] wants to extend 
            # largest subsequence 
            tailTable[len] = A[i] 
            len+= 1
   
        else: 
            # A[i] wants to be current 
            # end candidate of an existing 
            # subsequence. It will replace 
            # ceil value in tailTable 
            tailTable[CeilIndex(tailTable, -1, len-1, A[i])] = A[i] 
          
   
    return len
n=int(input())
l=list(map(int,input().split()))
print(LongestIncreasingSubsequenceLength(l, n))


n = int(input())
a= list(map(int,input().split()))
bit = [0]*(n+1)
def update(idx,val):
    while idx<=n:
        bit[idx] = max(bit[idx],val)
        idx+=idx&(-idx)
def query(idx):
    res =0
    while idx>0:
        res = max(res,bit[idx])
        idx-=idx&(-idx)
    return res
b = []
for i in range(n):
    b.append([a[i],-(i+1)])
b.sort()
for i in range(n):
    qe = query(-b[i][1])
    update(-b[i][1],qe+1)
print(query(n))

n=int(input())
a=list(map(lambda x: int(x), input().split()))


def CeilIndex(A, l, r, key):
    while (r - l > 1):

        m = l + (r - l) // 2
        if (A[m] >= key):
            r = m
        else:
            l = m
    return r


def LongestIncreasingSubsequenceLength(A, size):
    # Add boundary case,
    # when array size is one

    tailTable = [0 for i in range(size + 1)]
    len = 0  # always points empty slot

    tailTable[0] = A[0]
    len = 1
    for i in range(1, size):

        if (A[i] < tailTable[0]):

            # new smallest value
            tailTable[0] = A[i]

        elif (A[i] > tailTable[len - 1]):

            # A[i] wants to extend
            # largest subsequence
            tailTable[len] = A[i]
            len += 1

        else:
            # A[i] wants to be current
            # end candidate of an existing
            # subsequence. It will replace
            # ceil value in tailTable
            tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]

    return len
print(LongestIncreasingSubsequenceLength(a,len(a)))

import sys
input = sys.stdin.readline
I = lambda : list(map(int,input().split()))

def CeilIndex(A, l, r, key): 
    while (r - l > 1): 
      
        m = l + (r - l)//2
        if (A[m] >= key): 
            r = m 
        else: 
            l = m 
    return r 
   
def lis(A, size): 
    tailTable = [0 for i in range(size + 1)] 
    len = 0 # always points empty slot 
   
    tailTable[0] = A[0] 
    len = 1
    for i in range(1, size): 
      
        if (A[i] < tailTable[0]): 
            tailTable[0] = A[i] 
   
        elif (A[i] > tailTable[len-1]): 
            tailTable[len] = A[i] 
            len+= 1
   
        else: 
            tailTable[CeilIndex(tailTable, -1, len-1, A[i])] = A[i] 
    return len


n,=I()
p=I()
an=1
print(lis(p,n))


import sys
input = sys.stdin.readline
I = lambda : list(map(int,input().split()))

def CeilIndex(A, l, r, key): 
    while (r - l > 1): 
      
        m = l + (r - l)//2
        if (A[m] >= key): 
            r = m 
        else: 
            l = m 
    return r 
   
def lis(A, size): 
    tailTable = [0 for i in range(size + 1)] 
    len = 0 
    tailTable[0] = A[0] 
    len = 1
    for i in range(1, size): 
      
        if (A[i] < tailTable[0]): 
            tailTable[0] = A[i] 
   
        elif (A[i] > tailTable[len-1]): 
            tailTable[len] = A[i] 
            len+= 1
   
        else: 
            tailTable[CeilIndex(tailTable, -1, len-1, A[i])] = A[i] 
    return len
n,=I()
p=I()
an=1
print(lis(p,n))


import sys,math as mt
import heapq as hp
import collections as cc
import math as mt
import itertools as it
input=sys.stdin.readline
I=lambda:list(map(int,input().split()))
def CeilIndex(A, l, r, key): 
    while (r - l > 1): 
      
        m = l + (r - l)//2
        if (A[m] >= key): 
            r = m 
        else: 
            l = m 
    return r 
   
def lis(A, size): 
    tailTable = [0 for i in range(size + 1)] 
    len = 0 
    tailTable[0] = A[0] 
    len = 1
    for i in range(1, size): 
      
        if (A[i] < tailTable[0]): 
            tailTable[0] = A[i] 
   
        elif (A[i] > tailTable[len-1]): 
            tailTable[len] = A[i] 
            len+= 1
   
        else: 
            tailTable[CeilIndex(tailTable, -1, len-1, A[i])] = A[i] 
    return len
n,=I()
l=I()
print(lis(l,n))

def CeilIndex(A, l, r, key): 
  
    while (r - l > 1): 
      
        m = l + (r - l)//2
        if (A[m] >= key): 
            r = m 
        else: 
            l = m 
    return r 
   
def LongestIncreasingSubsequenceLength(A, size): 
  
    # Add boundary case, 
    # when array size is one 
   
    tailTable = [0 for i in range(size + 1)] 
    len = 0 # always points empty slot 
   
    tailTable[0] = A[0] 
    len = 1
    for i in range(1, size): 
      
        if (A[i] < tailTable[0]): 
  
            # new smallest value 
            tailTable[0] = A[i] 
   
        elif (A[i] > tailTable[len-1]): 
  
            # A[i] wants to extend 
            # largest subsequence 
            tailTable[len] = A[i] 
            len+= 1
   
        else: 
            # A[i] wants to be current 
            # end candidate of an existing 
            # subsequence. It will replace 
            # ceil value in tailTable 
            tailTable[CeilIndex(tailTable, -1, len-1, A[i])] = A[i] 
          
   
    return len
N = int(input())
List = [int(x) for x in input().split()]

print(LongestIncreasingSubsequenceLength(List,N))


from bisect import bisect_right
def answer(n,A):
    ans=[A[0]]
    for i in range(1,n):
        if ans[-1]<A[i]:
            ans.append(A[i])
        else:
            index=bisect_right(ans,A[i])
            ans[index]=A[i]
            
    return len(ans)


n=int(input())
arr=list(map(int,input().split()))
print(answer(n,arr))