import sys input = sys.stdin.readline from collections import * from bisect import * def lis(l): dp = [10**18]*(len(l)+1) dp[0] = -10**18 res = [-1]*N for i in range(len(l)): j = bisect_left(dp, l[i]) dp[j] = l[i] res[i] = bisect_left(dp, 10**18)-1 return res N = int(input()) A = list(map(int, input().split())) ans = 0 res1 = lis(A) A.reverse() res2 = lis(A) res2.reverse() A.reverse() for i in range(N): ans = max(ans, min(res1[i], res2[i])-1) A = list(map(lambda x: -x, A)) res1 = lis(A) A.reverse() res2 = lis(A) res2.reverse() A.reverse() for i in range(N): ans = max(ans, min(res1[i], res2[i])-1) print(ans)