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,a in enumerate(a): to=bisect.bisect_left(dp,a) dp[to]=a 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]*=-1 ans=max(ans,f(N,A)) print(ans)