#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int MAX = 100005;
int a[2002];
int fdp[2002][2002][2];
int rdp[2002][2002][2];
int fsolve(int b, int e, int last) {
  if (b > e) return 0;
  int &ret = fdp[b][e][last];
  if (ret != -1) return ret;
  ret = fsolve(b + 1, e, last);
  if (last <= a[b]) ret = max(ret, 1 + fsolve(b + 1, e, a[b]));
  return ret;
}
int rsolve(int b, int e, int last) {
  if (b > e) return 0;
  int &ret = rdp[b][e][last];
  if (ret != -1) return ret;
  ret = rsolve(b, e - 1, last);
  if (last <= a[e]) ret = max(ret, 1 + rsolve(b, e - 1, a[e]));
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i], a[i]--;
  memset(fdp, -1, sizeof(fdp));
  memset(rdp, -1, sizeof(rdp));
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      ans =
          max(ans, fsolve(1, i - 1, 0) + rsolve(i, j, 0) + fsolve(j + 1, n, 0));
      ans =
          max(ans, fsolve(1, i - 1, 0) + rsolve(i, j, 0) + fsolve(j + 1, n, 1));
      ans =
          max(ans, fsolve(1, i - 1, 0) + rsolve(i, j, 1) + fsolve(j + 1, n, 1));
    }
  }
  cout << ans << endl;
  return 0;
}
