#include #include #include #include #include #include #include #include #include static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template constexpr T INF = ::numeric_limits::max()/32*15+208; int main() { int n; cin >> n; vector A(n); for (auto &&i : A) scanf("%d", &i); vector lis(n, INF); vector l(n), r(n), li(n), ri(n); for (int i = 0; i < n; ++i) { int x = lower_bound(lis.begin(),lis.end(), A[i]) - lis.begin(); l[i] = x; lis[x] = A[i]; } fill(lis.begin(),lis.end(), INF); for (int i = n - 1; i >= 0; --i) { int x = lower_bound(lis.begin(),lis.end(), A[i]) - lis.begin(); r[i] = x; lis[x] = A[i]; } fill(lis.begin(),lis.end(), -INF); for (int i = 0; i < n; ++i) { int x = lower_bound(lis.begin(),lis.end(), A[i], greater<>()) - lis.begin(); li[i] = x; lis[x] = A[i]; } fill(lis.begin(),lis.end(), -INF); for (int i = n - 1; i >= 0; --i) { int x = lower_bound(lis.begin(),lis.end(), A[i], greater<>()) - lis.begin(); ri[i] = x; lis[x] = A[i]; } int ans = 0; for (int i = 0; i < n; ++i) { ans = max({ans, min(l[i], r[i]), min(li[i], ri[i])}); } cout << ans << "\n"; return 0; }