#include using namespace std; typedef long long ll; typedef long double ld; #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (int i = m; i < n; ++i) #define FORR(i, m, n) for (int i = m; i >= n; --i) #define ALL(v) (v).begin(),(v).end() templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b a){ int n=a.size(); vector dp(n,inf),pd(n,inf); vector l(n),r(n); REP(i,n){ int t=lower_bound(ALL(dp),a[i])-dp.begin(); dp[t]=a[i]; l[i]=lower_bound(ALL(dp),inf)-dp.begin(); } REPR(i,n){ int t=lower_bound(ALL(pd),a[i])-pd.begin(); pd[t]=a[i]; r[i]=lower_bound(ALL(pd),inf)-pd.begin(); } int res=0; REP(i,n){ chmax(res,min(l[i],r[i])-1); } return res; } int main(){ cin.tie(0); ios::sync_with_stdio(false); int n;cin >> n; vector a(n); REP(i,n) cin >> a[i]; int ans=0; chmax(ans,f(a)); REP(i,n) a[i]=-a[i]; chmax(ans,f(a)); cout << ans << endl; }