import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; int[] lds = new int[n + 1]; int maxD = 0; lds[0] = Integer.MAX_VALUE; int[] lis = new int[n + 1]; Arrays.fill(lis, Integer.MAX_VALUE); lis[0] = 0; int maxI = 0; int[] leftMaxesI = new int[n]; int[] leftMaxesD = new int[n]; int left; int right; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); left = 0; right = n; while (right - left > 1) { int m = (left + right) / 2; if (lis[m] < arr[i]) { left = m; } else { right = m; } } lis[right] = Math.min(lis[right], arr[i]); maxI = Math.max(maxI, right); leftMaxesI[i] = maxI; left = 0; right = n; while (right - left > 1) { int m = (left + right) / 2; if (lds[m] > arr[i]) { left = m; } else { right = m; } } lds[right] = Math.max(lds[right], arr[i]); maxD = Math.max(maxD, right); leftMaxesD[i] = maxD; } Arrays.fill(lds, 0); lds[0] = Integer.MAX_VALUE; Arrays.fill(lis, Integer.MAX_VALUE); lis[0] = 0; maxD = 0; maxI = 0; int ans = 0; for (int i = n - 1; i >= 0; i--) { left = 0; right = n; while (right - left > 1) { int m = (left + right) / 2; if (lis[m] < arr[i]) { left = m; } else { right = m; } } lis[right] = Math.min(lis[right], arr[i]); maxI = Math.max(maxI, right); ans = Math.max(ans, Math.min(leftMaxesI[i], maxI) - 1); left = 0; right = n; while (right - left > 1) { int m = (left + right) / 2; if (lds[m] > arr[i]) { left = m; } else { right = m; } } lds[right] = Math.max(lds[right], arr[i]); maxD = Math.max(maxD, right); ans = Math.max(ans, Math.min(leftMaxesD[i], maxD) - 1); } System.out.println(ans); } }