#include using namespace std; #define rep(i,n) for(int i = 0; i < (n);i++) #define sz(x) int(x.size()) typedef long long ll; typedef long double ld; typedef pair P; template struct SegmentTree { int n; vector node; T f(T a, T b) {return max(a, b);} SegmentTree (int sz, T init = 0) { n = 1; while (n < sz) n <<= 1; node.assign(2*n, init); for (int i = n - 2; i >= 0; i--) node[i] = f(node[2*i+1], node[2*i+2]); } void update(int k, T x) { k += (n - 1); node[k] += x; while (k > 0) { k = (k - 1) >> 1; node[k] = f(node[2*k+1], node[2*k+2]); } } void clear(T init = 0) { for (int i = 0; i < n; i++) node[i] = init; } T query(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; if (r <= a || b <= l) return 0; if (a <= l && r <= b) return node[k]; T vl = query(a, b, 2*k+1, l, (l+r)/2); T vr = query(a, b, 2*k+2, (l+r)/2, r); return f(vl, vr); } T getvalue(int k) { return query(k,k+1); } }; int main() { int n; cin >> n; vector a(n); rep(i,n) cin >> a[i]; auto b = a; sort(b.begin(), b.end()); b.erase(unique(b.begin(), b.end()), b.end()); int m = b.size(); int res = 0; SegmentTree seg(m); vector dpl(n, 0), dpr(n, 0); auto make_id = [&](int x){ return lower_bound(b.begin(), b.end(), x) - b.begin(); }; for (int i = 0; i < n; i++) { int id = make_id(a[i]); dpl[i] = seg.query(id + 1, m) + 1; seg.update(id, dpl[i]); } seg.clear(); for (int i = n - 1; i >= 0; i--) { int id = make_id(a[i]); dpr[i] = seg.query(id + 1, m) + 1; seg.update(id, dpr[i]); } for (int i = 0; i < n; i++) res = max(res, min(dpl[i], dpr[i])); for (int i = 0; i < n; i++) dpl[i] = 0, dpr[i] = 0; seg.clear(); for (int i = n - 1; i >= 0; i--) { int id = make_id(a[i]); dpr[i] = seg.query(0, id) + 1; seg.update(id, dpr[i]); } seg.clear(); for (int i = 0; i <= 0; i++) { int id = make_id(a[i]); dpl[i] = seg.query(0, id) + 1; seg.update(id, dpl[i]); } for (int i = 0; i < n; i++) res = max(res, min(dpl[i], dpr[i])); cout << res - 1 << endl; return 0; }