#include <bits/stdc++.h>
using namespace std;
int const N = 1e6 + 10;
long long int INF = LLONG_MAX;
vector<bool> primes(N, true);
long long int gcd(long long int a, long long int b) {
  return b ? gcd(b, a % b) : a;
}
long long int inv_mod(long long int a, long long int b) {
  long long int x = 0, y = 1, r, q, m = a;
  while (b) {
    q = a / b;
    r = a - q * b;
    a = b;
    b = r;
    r = x;
    x = y;
    y = r - q * y;
  }
  x += m;
  return x % m;
}
long long int pow_mod(long long int a, long long int e, long long int m) {
  long long int res = 1;
  a %= m;
  while (e) {
    if (e & 1) res = res * a % m;
    a = a * a % m;
    e >>= 1;
  }
  return res;
}
void cal_primes() {
  primes[0] = primes[1] = false;
  for (int i = 2; i * i < N; i++) {
    if (primes[i]) {
      for (int j = i * i; j < N; j += i) {
        primes[j] = false;
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));
  vector<int> ocnt(n + 1, 0), zcnt(n + 1, 0);
  vector<int> arr(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    zcnt[i] = zcnt[i - 1];
    ocnt[i] = ocnt[i - 1];
    if (arr[i] == 1)
      zcnt[i]++;
    else
      ocnt[i]++;
  }
  int tans = 0;
  for (int i = 1; i <= n; i++) {
    if (arr[i] == 1) {
      tans = max(tans, zcnt[i] + ocnt[n] - ocnt[i]);
    }
  }
  for (int r = 1; r <= n; r++) {
    dp[r][r] = (zcnt[r] + ocnt[n] - ocnt[r]) * (arr[r] == 1);
    for (int l = r + 1; l <= n; l++) {
      dp[l][r] = 0;
    }
  }
  for (int r = 1; r <= n; r++) {
    for (int l = r - 1; l > 0; l--) {
      if (arr[l] == 1) {
        dp[l][r] = max(dp[l + 1][r] - 1, zcnt[r] + ocnt[n] - ocnt[r]);
      } else
        dp[l][r] = dp[l + 1][r] + 1;
      dp[l][r] * (zcnt[r] > zcnt[l - 1]);
    }
  }
  int ans = max(tans, ocnt[n]);
  for (int l = 1; l <= n; l++) {
    for (int r = 1; r <= n; r++) {
      ans = max(dp[l][r], ans);
    }
  }
  cout << ans << endl;
}
