using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("6");
WillReturn.Add("1 2 3 3 2 1");
//5
}
else if (InputPattern == "Input2") {
WillReturn.Add("4");
WillReturn.Add("1 2 3 4");
//4
}
else if (InputPattern == "Input3") {
WillReturn.Add("5");
WillReturn.Add("3 3 3 3 3");
//1
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] AArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
int UB = AArr.GetUpperBound(0);
Dictionary<int, int> LISLenDictSei = DeriveLISLenDict(AArr);
Array.Reverse(AArr);
Dictionary<int, int> LISLenDictRev = DeriveLISLenDict(AArr);
var AnswerKouho = new List<int>();
for (int I = 0; I <= UB; I++) {
int RevInd = UB - I;
AnswerKouho.Add(LISLenDictSei[I] + LISLenDictRev[RevInd] - 1);
}
Console.WriteLine(AnswerKouho.Max());
}
// 配列を引数として、LIS長さ[添字]なDictを返す
static Dictionary<int, int> DeriveLISLenDict(int[] pArr)
{
var WillReturn = new Dictionary<int, int>();
// LISの最終値の最小値[LISの長さ] なDP表
var DPSortedList = new SortedList<int, int>();
int MaxLISLen = 1;
for (int I = 0; I <= pArr.GetUpperBound(0); I++) {
if (DPSortedList.Count == 0) {
DPSortedList[1] = pArr[I];
MaxLISLen = Math.Max(MaxLISLen, 1);
}
else {
int UpsertKeyInd = ExecNibunhou(DPSortedList, pArr[I]);
int CurrUB = DPSortedList.Count - 1;
var Keys = DPSortedList.Keys;
// 更新する位置によって分岐
if (UpsertKeyInd <= CurrUB) {
DPSortedList[Keys[UpsertKeyInd]] = pArr[I];
MaxLISLen = Math.Max(MaxLISLen, Keys[UpsertKeyInd]);
}
else {
int PrevKey = Keys[CurrUB];
DPSortedList[PrevKey + 1] = pArr[I];
MaxLISLen = Math.Max(MaxLISLen, PrevKey + 1);
}
}
WillReturn[I] = MaxLISLen;
}
return WillReturn;
}
// 二分法で、引数の値を設定する、キーの配列の添字を返す
static int ExecNibunhou(SortedList<int, int> pDPSortedList, int pTargetVal)
{
int UB = pDPSortedList.Count - 1;
var Keys = pDPSortedList.Keys;
// 最小値以下の場合
if (pTargetVal <= pDPSortedList[Keys[0]]) {
return 0;
}
// 最大値より大きい場合
if (pTargetVal > pDPSortedList[Keys[UB]]) {
return UB + 1;
}
int L = 0;
int R = UB;
while (L + 1 < R) {
int Mid = (L + R) / 2;
if (pDPSortedList[Keys[Mid]] < pTargetVal) {
L = Mid;
}
else {
R = Mid;
}
}
return R;
}
}