#include using namespace std; typedef long long int ll; typedef pair pii; typedef vector vi; typedef vector > vii; #define rrep(i, m, n) for(int (i)=(m); (i)<(n); (i)++) #define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++) #define rep(i, n) for(int (i)=0; (i)<(n); (i)++) #define rev(i, n) for(int (i)=(n)-1; (i)>=0; (i)--) #define vrep(i, c) for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++) #define ALL(v) (v).begin(), (v).end() #define mp make_pair #define pb push_back template inline void minup(T1& m, T2 x){ if(m>x) m=static_cast(x); } template inline void maxup(T1& m, T2 x){ if(m(x); } #define INF 1000000000 #define MOD 1000000007 #define EPS 1E-12 const int MAX_N = 3000; int n; int A[MAX_N]; int dp[2][MAX_N][MAX_N]; int lsolve(int L, int R); int rsolve(int L, int R); int lsolve(int L, int R) { if(dp[0][L][R]) return dp[0][L][R]; if(L == R) return dp[0][L][R] = 1; if(A[L] >= A[R]) return dp[0][L][R] = lsolve(L, R-1); return dp[0][L][R] = max(lsolve(L, R-1), rsolve(L+1, R)+1); } int rsolve(int L, int R) { if(dp[1][L][R]) return dp[1][L][R]; if(L == R) return dp[1][L][R] = 1; if(A[L] <= A[R]) return dp[1][L][R] = rsolve(L+1, R); return dp[1][L][R] = max(rsolve(L+1, R), lsolve(L, R-1)+1); } int main() { cin >> n; rep(i, n) cin >> A[i]; cout << lsolve(0, n-1) << endl; return 0; }