#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(0); int N; cin >> N; vector A(N); rep(i,N) cin >> A[i]; auto f = [N](vector A) { auto LIS = [N](vector A) { int INF = numeric_limits::max(); vector dp(N, INF), rank(N); rep(i,N) { int p = lower_bound(dp.begin(), dp.end(), A[i]) - dp.begin(); dp[p] = A[i]; rank[i] = p + 1; } return rank; }; vector L = LIS(A); reverse(A.begin(), A.end()); vector R = LIS(A); reverse(A.begin(), A.end()); reverse(R.begin(), R.end()); int ans = 0; rep(i,N) ans = max(ans, min(L[i], R[i]) - 1); return ans; }; int ans = 0; rep(_,2) { ans = max(ans, f(A)); rep(i,N) A[i] *= -1; } cout << ans << endl; }