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].

Source: codeforces | Difficulty: MEDIUM
Tags: Dynamic programming

Sorting

Data structures


Solutions:
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))


from typing import List
from bisect import bisect_right

class Solution:

	def patienceSort(self, a: List[int]) -> int:
		n = len(a)
		piles = [n + 1 for i in range(n)]
		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)
n = int(input().strip())
a = list(map(int, input().strip().split()))
s = Solution()
print(s.maxIndependentSetSize(a))


MXusl = int
MXuso = input
MXusL = map
MXusr = min
MXusI = print
n = MXusl(MXuso())
a = [1000000.0] * (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)


import bisect
n = int(input())
a = list(map(int, input().split()))
INF = 10 ** 18
dp = [INF] * n
for i in range(n):
	dp[bisect.bisect_left(dp, a[i])] = a[i]
print(bisect.bisect_left(dp, INF))


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):
	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
print(LongestIncreasingSubsequenceLength(a, len(a)))


from sys import stdin
input = stdin.readline

class BIT:

	def __init__(self, n):
		self.n = n
		self.tree = [0] * (n + 1)

	def sum(self, i):
		ans = 0
		i += 1
		while i > 0:
			ans = max(ans, self.tree[i])
			i -= i & -i
		return ans

	def update(self, i, value):
		i += 1
		while i <= self.n:
			self.tree[i] = max(value, self.tree[i])
			i += i & -i

def f(a):
	newind = 0
	maxs = 0
	ans = 0
	ft = BIT(2 * 10 ** 5 + 5)
	for i in a:
		maxs = ft.sum(i - 1)
		ft.update(i, maxs + 1)
		ans = max(ans, maxs + 1)
	return ans
a = input()
l = list(map(int, input().strip().split()))
print(f(l))


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))


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()


import sys
sys.setrecursionlimit(1000000)

def solve():
	(n,) = rv()
	(a,) = rl(1)
	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]
	res = 0
	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_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)


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))


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))


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))


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
a = int(input())
z = list(map(int, input().split()))
print(Lis(z, a))


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)))


def lis(a):
	b = []
	for c in a:
		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
				else:
					r = m
			if b[l] < c:
				l += 1
			b[l] = c
	return len(b)
n = int(input())
a = list(map(int, input().split()))
print(lis(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):
	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 = int(input())
List = [int(x) for x in input().split()]
print(LongestIncreasingSubsequenceLength(List, N))


n = int(input())
num_list = list(map(int, input().split()))
import bisect

def LongestIncreasingSubsequence(a, n):
	min_lis = []
	for i in range(n):
		pos = bisect.bisect_left(min_lis, a[i])
		if pos == len(min_lis):
			min_lis.append(a[i])
		else:
			min_lis[pos] = a[i]
	return len(min_lis)
print(LongestIncreasingSubsequence(num_list, n))