#include <bits/stdc++.h>
using namespace std;
int N, arr[2002], revarr[2002];
int ones[2002], twos[2002], dp[2002][2002][3];
int revdp[2002][2002][3], ans;
long long int solve_for_rev(int limit, int idx, int prev) {
  if (idx < limit) return 0;
  if (revdp[limit][idx][prev] != -1) return revdp[limit][idx][prev];
  if (revarr[idx] <= prev) {
    int poss1 = 1 + solve_for_rev(limit, idx - 1,
                                  (prev <= revarr[idx] ? prev : revarr[idx]));
    int poss2 = solve_for_rev(limit, idx - 1, prev);
    return revdp[limit][idx][prev] = (poss1 >= poss2 ? poss1 : poss2);
  }
  return revdp[limit][idx][prev] = solve_for_rev(limit, idx - 1, prev);
}
int solve_for(int limit, int idx, int prev) {
  if (idx < limit) return 0;
  if (dp[limit][idx][prev] != -1) return dp[limit][idx][prev];
  if (arr[idx] <= prev) {
    int poss1 =
        1 + solve_for(limit, idx - 1, (prev <= arr[idx] ? prev : arr[idx]));
    int poss2 = solve_for(limit, idx - 1, prev);
    return dp[limit][idx][prev] = (poss1 >= poss2 ? poss1 : poss2);
  }
  return dp[limit][idx][prev] = solve_for(limit, idx - 1, prev);
}
int main() {
  memset(revdp, -1, sizeof revdp);
  memset(dp, -1, sizeof dp);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> arr[i];
    revarr[N - i + 1] = arr[i];
    ones[i] = ones[i - 1];
    twos[i] = twos[i - 1];
    if (arr[i] == 1)
      ones[i]++;
    else
      twos[i]++;
  }
  for (int i = 1; i <= N; i++) {
    solve_for(i, N, 2);
    solve_for_rev(i, N, 2);
  }
  for (int i = 1; i <= N; i++) {
    for (int j = i; j <= N; j++) {
      int poss1 = dp[1][i - 1][2] + twos[N] - twos[i - 1];
      ans = (ans >= poss1 ? ans : poss1);
      int poss2 =
          ones[i - 1] + revdp[N + 1 - j][N + 1 - i][2] + twos[N] - twos[j];
      ans = (ans >= poss2 ? ans : poss2);
      int poss3 = ones[j];
      if (j + 1 <= N) {
        poss3 += dp[j + 1][N][2];
      }
      ans = (ans >= poss3 ? ans : poss3);
    }
  }
  cout << ans << endl;
  return 0;
}
