import scala.io.StdIn object Main extends App { val N = StdIn.readInt() val A = StdIn.readLine().split(" ").map(_.toInt) val dpL, dpR = Array.fill(N)(Array.fill(N)(1)) (1 until N).foreach { d => (0 until N - d).foreach { l => val r = l + d val pdpL = dpL(l)(r - 1) val pdpR = dpR(l + 1)(r) dpL(l)(r) = if (A(l) >= A(r)) pdpL else Seq(pdpL, (pdpR + 1)).max dpR(l)(r) = if (A(l) <= A(r)) pdpR else Seq(pdpR, (pdpL + 1)).max } } println(((0 until N).map(dpL(_)(N - 1)) ++ dpR(0)).max) }