#include using namespace std; #define REP(i,n) for(int i=0; i<(int)(n); i++) #define FOR(i,b,e) for (int i=(int)(b); i<(int)(e); i++) #define ALL(x) (x).begin(), (x).end() const double PI = acos(-1); const int INF = 1<<30; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector a(n); REP (i, n) cin >> a[i]; vector pref(n), suff(n); vector dp(n); // mountain int r1 = 0; fill(dp.begin(), dp.end(), INF); REP (i, n) { int j = lower_bound(dp.begin(), dp.end(), a[i]) - dp.begin(); dp[j] = a[i]; pref[i] = j; } fill(dp.begin(), dp.end(), INF); REP (i, n) { int j = lower_bound(dp.begin(), dp.end(), a[n-1-i]) - dp.begin(); dp[j] = a[n-1-i]; suff[n-1-i] = j; } REP (i, n) r1 = max(r1, min(pref[i], suff[i])); // valley int r2 = 0; fill(dp.begin(), dp.end(), INF); REP (i, n) { int j = lower_bound(dp.begin(), dp.end(), -a[i]) - dp.begin(); dp[j] = -a[i]; pref[i] = j; } fill(dp.begin(), dp.end(), INF); REP (i, n) { int j = lower_bound(dp.begin(), dp.end(), -a[n-1-i]) - dp.begin(); dp[j] = -a[n-1-i]; suff[n-1-i] = j; } REP (i, n) r2 = max(r2, min(pref[i], suff[i])); cout << max(r1, r2) << endl; return 0; }