import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } // point update, range product // T: monoid // get a, update a, mulL a, mulR a: 0 <= a < n // rangeProd [a, b): 0 <= a <= b <= n class SegmentTree(T, alias op, T ide) { import std.functional : binaryFun; alias opFun = binaryFun!op; int n; T[] ts; this(int n_) { for (n = 1; n < n_; n <<= 1) {} ts = new T[n << 1]; ts[] = ide; } this(int n_, T[] ini) { for (n = 1; n < n_; n <<= 1) {} ts = new T[n << 1]; ts[0 .. n] = ide; ts[n .. n + n_] = ini[]; ts[n + n_ .. n << 1] = ide; foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]); } const(T) get(int a) const { return ts[a + n]; } void update(int a, T val) { ts[a += n] = val; for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]); } void mulL(int a, in T val) { update(a, opFun(val, ts[a + n])); } void mulR(int a, in T val) { update(a, opFun(ts[a + n], val)); } T rangeProd(int a, int b) const { T prodL = ide, prodR = ide; for (a += n, b += n; a < b; a >>= 1, b >>= 1) { if (a & 1) prodL = opFun(prodL, ts[a++]); if (b & 1) prodR = opFun(ts[--b], prodR); } return opFun(prodL, prodR); } } int N; long[] A; void main() { try { for (; ; ) { N = readInt(); A = new long[N]; foreach (i; 0 .. N) { A[i] = readLong(); } auto As = A.dup.sort.uniq.array; const AsLen = cast(int)(As.length); alias Entry = Tuple!(int, "val", int, "bef", int, "pos"); Entry[] min(in Entry[] as, in Entry[] bs) { Entry[] cs; int cnt2; foreach (e; merge!"a > b"(as, bs)) { const cnt = cs.count!(ee => (ee.bef == e.bef)); if (cnt < 2) { cs ~= e; if (cnt + 1 == 2) { if (++cnt2 == 2) { break; } } } } return cs; } enum Entry[] ide = []; auto segU = new SegmentTree!(Entry[], min, ide)(AsLen); auto segD = new SegmentTree!(Entry[], min, ide)(AsLen); int ans; foreach (i; 0 .. N) { const pos = As.lowerBound(A[i]); { const res = segD.rangeProd(0, pos); debug { writefln("segD.rangeProd %s %s = %s", 0, pos, res); } Entry[] es; bool[int] app; int cnt; foreach (e; res) { if (e.bef != pos) { chmax(ans, e.val + 1); if (e.pos !in app) { es ~= Entry(e.val + 1, e.pos, pos); app[e.pos] = true; if (++cnt == 2) { break; } } } } if (cnt < 2) { es ~= Entry(1, -1, pos); } debug { writefln("dpU[%s] = %s", i, es); } segU.mulR(pos, es); } { const res = segU.rangeProd(pos + 1, AsLen); debug { writefln("segU.rangeProd %s %s = %s", 0, pos, res); } Entry[] es; bool[int] app; int cnt; foreach (e; res) { if (e.bef != pos) { chmax(ans, e.val + 1); if (e.pos !in app) { es ~= Entry(e.val + 1, e.pos, pos); app[e.pos] = true; if (++cnt == 2) { break; } } } } if (cnt < 2) { es ~= Entry(1, -1, pos); } debug { writefln("dpD[%s] = %s", i, es); } segD.mulR(pos, es); } } if (ans < 3) { ans = 0; } writeln(ans); } } catch (EOFException e) { } }