#include #include using namespace std; #define rep(i,n) for(int i=0;i> n; vector a(n); rep(i, n) cin >> a[i]; vector dp(n, INF); vector front_up(n), back_up(n); vector front_down(n), back_down(n); rep(i, n) { *lower_bound(dp.begin(), dp.end(), a[i]) = a[i]; front_up[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]; back_up[i] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin(); } rep(i, n) dp[i] = INF; rep(i, n) a[i] *= -1; rep(i, n) { *lower_bound(dp.begin(), dp.end(), a[i]) = a[i]; front_down[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]; back_down[i] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin(); } int ans = 0; rep(i, n) { ans = max(ans, min(front_up[i] - 1, back_up[i])); ans = max(ans, min(front_down[i] - 1, back_down[i])); } cout << ans << endl; return 0; }