intmain(){ int n; while (~scanf("%d", &n)) { for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } fill(g, g + n + 1, inf); for (int i = 0; i < n; ++i) { int k = int(lower_bound(g + 1, g + n + 1, a[i]) - g); d1[i] = k; g[k] = a[i]; } fill(g, g + n + 1, inf); for (int i = n - 1; i >= 0; --i) { int k = int(lower_bound(g + 1, g + n + 1, a[i]) - g); d2[i] = k; g[k] = a[i]; } int ans = 0; for (int i = 0; i < n; i++) { ans = max(ans, (min(d1[i], d2[i]) << 1) - 1); } printf("%d\n", ans); } return0; }