#include <bits/stdc++.h>
using namespace std;
int a[2005];
int dp1[2005][2005], dp2[2005][2005], tmp[2005];
int main() {
  int n, len, maxx = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    len = 0;
    memset(tmp, 0, sizeof(tmp));
    for (int j = i; j <= n; j++) {
      if (a[j] >= tmp[len])
        tmp[++len] = a[j];
      else {
        int pos = upper_bound(tmp + 1, tmp + len + 1, a[j]) - tmp;
        tmp[pos] = a[j];
      }
      dp1[i][j] = len;
    }
  }
  for (int i = n; i >= 1; i--) {
    len = 0;
    memset(tmp, 0, sizeof(tmp));
    for (int j = i; j >= 1; j--) {
      if (a[j] >= tmp[len])
        tmp[++len] = a[j];
      else {
        int pos = upper_bound(tmp + 1, tmp + len + 1, a[j]) - tmp;
        tmp[pos] = a[j];
      }
      dp2[j][i] = len;
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = i; j <= n; j++)
      maxx = max(maxx, dp1[1][n] - dp1[i][j] + dp2[i][j]);
  cout << maxx << endl;
  return 0;
}
