#include "bits/stdc++.h" using namespace std; // コンテスト中のみ //#define int long long #define ll long long #define rep(i,n) for(int i = 0; i < (n); i++) #define P pair #define ld long double ll INF = (1LL << 60); int MOD = 1000000007; ll N; ll A[100010], B[100010]; ll lLIS[100010], lLDS[100010], rLIS[100010], rLDS[100010]; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; rep(i, N)cin >> A[i]; rep(i, N)B[i] = -A[i]; vectordp(N, INF); rep(i, N) { *lower_bound(dp.begin(), dp.end(), A[i]) = A[i]; lLIS[i] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin(); } rep(i, N)dp[i] = INF; for (int i = N - 1; i >= 0; i--) { *lower_bound(dp.begin(), dp.end(), A[i]) = A[i]; rLIS[i] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin(); } rep(i, N)dp[i] = INF; rep(i, N) { *lower_bound(dp.begin(), dp.end(), B[i]) = B[i]; lLDS[i] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin(); } rep(i, N)dp[i] = INF; for (int i = N - 1; i >= 0; i--) { *lower_bound(dp.begin(), dp.end(), B[i]) = B[i]; rLDS[i] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin(); } ll ans = 0; rep(i, N) { ans = max(ans, min(lLDS[i], rLIS[i]) - 1); ans = max(ans, min(lLIS[i], rLDS[i]) - 1); } cout << ans << endl; return 0; }