import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict con = 10 ** 9 + 7; INF = float("inf") import bisect def getlist(): return list(map(int, input().split())) def LIS(N, A): DP = [INF] * N L = [None] * N for i in range(N): x = bisect.bisect_left(DP, A[i]) DP[x] = A[i] L[i] = bisect.bisect_left(DP, INF) return L def LISrev(N, A): DP = [INF] * N L = [None] * N for i in range(N): x = bisect.bisect_left(DP, -A[i]) DP[x] = -A[i] L[i] = bisect.bisect_left(DP, INF) return L #処理内容 def main(): N = int(input()) A = getlist() leftup = LIS(N, A) rightup = LIS(N, A[::-1]) rightup = rightup[::-1] # print(leftup) # print(rightup) leftdown = LISrev(N, A) rightdown = LISrev(N, A[::-1]) rightdown = rightdown[::-1] # print(leftdown) # print(rightdown) ans = 0 for i in range(N): ans = max(ans, min(leftup[i], rightup[i]) - 1) ans = max(ans, min(leftdown[i], rightdown[i]) - 1) print(ans) if __name__ == '__main__': main()