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); auto r = n.iota.map!(i => [calc.calcA(i, n-1), calc.calcB(i, 0)]).joiner.maxElement; writeln(r); } class Calc { size_t n; int[] ai; int[][] dpa, dpb; this(size_t n, int[] ai) { this.n = n; this.ai = ai; dpa = new int[][](n, n); dpb = new int[][](n, n); } int calcA(size_t i, size_t m) { if (dpa[i][m]) return dpa[i][m]; auto a = ai[i], r = 0; foreach (j; i+1..m+1) if (ai[j] > a) r = max(r, calcB(j, i+1)); return dpa[i][m] = r + 1; } int calcB(size_t i, size_t m) { if (dpb[i][m]) return dpb[i][m]; auto a = ai[i], r = 0; foreach (j; m..i) if (ai[j] > a) r = max(r, calcA(j, i-1)); return dpb[i][m] = r + 1; } }