#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define MAX_N 3010 #define LOG 21 #define PI 3.141592653589 #define EPS 1e-6 #define MOD 1000000007 #define YJSNPI 810 #define INF (1 << 30) #define ADD(a, b) a = (a + (ll)b) % MOD #define MUL(a, b) a = (a * (ll)b) % MOD #define MAX(a, b) a = max(a, b) #define MIN(a, b) a = min(a, b) #define REP(i, a, b) for(int i = a; i < b; i++) #define RER(i, a, b) for(int i = a - 1; i >= b; i--) using namespace std; typedef long long ll; typedef pair pi; void debug() {cout << endl; } template void debug(FIRST arg, REST... rest) { cout << arg << " "; debug(rest...); } template void showary(T begin, T end) { while(begin != end) { cout << *begin << " "; begin++; } cout << endl; } int N; int A[MAX_N]; int dp[MAX_N][MAX_N][2]; int loop(int l, int r, int k) { if(dp[l][r][k] != -1) return dp[l][r][k]; if(l == r) return 0; int res = 0; if(k == 0) { MAX(res, loop(l, r - 1, 0)); if(A[l] < A[r]) MAX(res, loop(l + 1, r, 1) + 1); } else { MAX(res, loop(l + 1, r, 1)); if(A[l] > A[r]) MAX(res, loop(l, r - 1, 0) + 1); } //debug(l, r, k, res); return dp[l][r][k] = res; } int main() { scanf("%d", &N); REP(i, 0, N) scanf("%d", &A[i + 1]); memset(dp, -1, sizeof(dp)); printf("%d\n", max(loop(0, N + 1, 0), loop(0, N + 1, 1))); }