import bisect N=int(input()) A=list(map(int,input().split())) def f(n,a): ret=0 def g(n,a): dp=[10**20]*n A=[0]*n for i in range(n): to=bisect.bisect_left(dp,a[i]) dp[to]=a[i] A[i]=to return A L=g(n,a) R=g(n,a[::-1])[::-1] for i in range(n): ret=max(ret,min(L[i],R[i])) return ret ans=f(N,A) for i in range(N): A[i]=-A[i] ans=max(ans,f(N,A)) print(ans)