# Binary Indexed Tree (Fenwick Tree) # Binary Indexed Tree (Fenwick Tree) class BIT_max: def __init__(self, n): self.n = n self.bit = [0]*(n+1) self.el = [0]*(n+1) def query(self, i): s = 0 while i > 0: s = max(s,self.bit[i]) i -= i & -i return s def update(self, i, x): # assert i > 0 self.el[i] = max(self.el[i],x) while i <= self.n: self.bit[i] = max(self.bit[i],x) i += i & -i N = int(input()) A = list(map(int, input().split())) def compress(arr): *XS, = set(arr) XS.sort() return {e: i+1 for i, e in enumerate(XS)} tra = compress(A) for i in range(N): A[i] = tra[A[i]] def calc(A,N): bit = BIT_max(N) lis1,lis2 = [0]*N,[0]*N for i in range(N): lis1[i] = bit.query(A[i]-1) bit.update(A[i],lis1[i]+1) bit = BIT_max(N) for i in range(N-1,-1,-1): lis2[i] = bit.query(A[i]-1) bit.update(A[i],lis2[i]+1) ans = 0 for i in range(N): ans = max(ans, min(lis1[i],lis2[i])) return ans a = calc(A,N) b = calc(list(map(lambda x:-x+N+1,A)),N) print(max(a,b))