import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto ai = readln.split.to!(int[]); auto calc = new Calc(n, ai); writeln(max(calc.calcA(0), calc.calcB(n-1))); } class Calc { size_t n; int[] ai, dpa, dpb; this(size_t n, int[] ai) { this.n = n; this.ai = ai; dpa = new int[](n); dpb = new int[](n); } int calcA(size_t i) { if (dpa[i]) return dpa[i]; auto a = ai[i], r = 0; foreach (j; i+1..n) if (ai[j] > a) r = max(r, calcB(j)); return dpa[i] = r + 1; } int calcB(size_t i) { if (dpb[i]) return dpb[i]; auto a = ai[i], r = 0; foreach (j; 0..i) if (ai[j] > a) r = max(r, calcA(j)); return dpb[i] = r + 1; } }