#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y);
bool isPrime(long long n);
long long modInv(long long a, long long b);
long long gcdExtended(long long a, long long b, long long *x, long long *y);
long long mpower(long long a, long long b, long long p);
const long long inf = (long long)1e17;
const long long N = (long long)2e3 + 5;
long long dp1[N][N], dp2[N][N], dp3[N][N], dp4[N][N];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long n;
  cin >> n;
  vector<long long> v(n);
  for (long long i = 0; i < n; i++) {
    cin >> v[i];
  }
  for (long long i = 0; i < n; i++) {
    long long c1 = 0, c2 = 0;
    for (long long j = i; j < n; j++) {
      if (v[j] == 1)
        c1++;
      else
        c2++;
      dp1[i][j] = c1;
      dp2[i][j] = c2;
    }
  }
  for (long long i = 0; i < n; i++) {
    long long c1 = 0, c2 = 0;
    for (long long j = i; j < n; j++) {
      if (v[j] == 1) {
        c1++;
      } else {
        long long ans = 0;
        if (c1) {
          ans = max(ans, c1 + 1);
        }
        if (c2) {
          ans = max(ans, 1 + c2);
        }
        c2 = max(c2, ans);
        dp3[i][j] = ans;
      }
    }
    c1 = 0, c2 = 0;
    for (long long j = i; j < n; j++) {
      if (v[j] == 2) {
        c2++;
      } else {
        long long ans = 0;
        if (c2) {
          ans = max(ans, c2 + 1);
        }
        if (c1) {
          ans = max(ans, c1 + 1);
        }
        c1 = max(ans, c1);
        dp4[i][j] = ans;
      }
    }
  }
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    for (long long j = i + 1; j < n; j++) {
      long long c1 = dp1[0][i];
      long long c2 = 0;
      long long c3 = dp2[j][n - 1];
      if (i + 1 <= j - 1) {
        c2 = max(dp3[i + 1][j - 1], dp4[i + 1][j - 1]);
      }
      ans = max(ans, c1 + c2 + c3);
      c1 = max(dp3[0][i], dp4[0][i]);
      c2 = 0;
      c3 = dp2[j][n - 1];
      if (i + 1 <= j - 1) {
        c2 = dp2[i + 1][j - 1];
      }
      ans = max(ans, c1 + c2 + c3);
      c1 = dp1[0][i];
      c2 = 0;
      c3 = max(dp3[j][n - 1], dp4[j][n - 1]);
      if (i + 1 <= j - 1) {
        c2 = dp1[i + 1][j - 1];
      }
      ans = max(ans, c1 + c2 + c3);
    }
  }
  ans = max(ans, dp3[0][n - 1]);
  ans = max(ans, dp4[0][n - 1]);
  cout << max(ans, max(dp2[0][n - 1], dp1[0][n - 1]));
  return 0;
}
long long modInv(long long a, long long m) {
  long long x, y;
  long long g = gcdExtended(a, m, &x, &y);
  long long res = (x % m + m) % m;
  return res;
}
long long gcdExtended(long long a, long long b, long long *x, long long *y) {
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }
  long long x1, y1;
  long long gcd = gcdExtended(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
long long mpower(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long power(long long x, long long y) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) res = res * x;
    y = y >> 1;
    x = x * x;
  }
  return res;
}
bool isPrime(long long n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  long long p = sqrt(n);
  for (long long i = 5; i <= p; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}

### Prompt

Your challenge is to write a cpp solution to the following problem:

A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.

A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.

Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.

A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.

Input

The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.

The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).

Output

Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.

Examples

Input

4
1 2 1 2


Output

4


Input

10
1 1 2 2 2 1 1 2 2 1


Output

9

Note

In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.

In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y);
bool isPrime(long long n);
long long modInv(long long a, long long b);
long long gcdExtended(long long a, long long b, long long *x, long long *y);
long long mpower(long long a, long long b, long long p);
const long long inf = (long long)1e17;
const long long N = (long long)2e3 + 5;
long long dp1[N][N], dp2[N][N], dp3[N][N], dp4[N][N];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long n;
  cin >> n;
  vector<long long> v(n);
  for (long long i = 0; i < n; i++) {
    cin >> v[i];
  }
  for (long long i = 0; i < n; i++) {
    long long c1 = 0, c2 = 0;
    for (long long j = i; j < n; j++) {
      if (v[j] == 1)
        c1++;
      else
        c2++;
      dp1[i][j] = c1;
      dp2[i][j] = c2;
    }
  }
  for (long long i = 0; i < n; i++) {
    long long c1 = 0, c2 = 0;
    for (long long j = i; j < n; j++) {
      if (v[j] == 1) {
        c1++;
      } else {
        long long ans = 0;
        if (c1) {
          ans = max(ans, c1 + 1);
        }
        if (c2) {
          ans = max(ans, 1 + c2);
        }
        c2 = max(c2, ans);
        dp3[i][j] = ans;
      }
    }
    c1 = 0, c2 = 0;
    for (long long j = i; j < n; j++) {
      if (v[j] == 2) {
        c2++;
      } else {
        long long ans = 0;
        if (c2) {
          ans = max(ans, c2 + 1);
        }
        if (c1) {
          ans = max(ans, c1 + 1);
        }
        c1 = max(ans, c1);
        dp4[i][j] = ans;
      }
    }
  }
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    for (long long j = i + 1; j < n; j++) {
      long long c1 = dp1[0][i];
      long long c2 = 0;
      long long c3 = dp2[j][n - 1];
      if (i + 1 <= j - 1) {
        c2 = max(dp3[i + 1][j - 1], dp4[i + 1][j - 1]);
      }
      ans = max(ans, c1 + c2 + c3);
      c1 = max(dp3[0][i], dp4[0][i]);
      c2 = 0;
      c3 = dp2[j][n - 1];
      if (i + 1 <= j - 1) {
        c2 = dp2[i + 1][j - 1];
      }
      ans = max(ans, c1 + c2 + c3);
      c1 = dp1[0][i];
      c2 = 0;
      c3 = max(dp3[j][n - 1], dp4[j][n - 1]);
      if (i + 1 <= j - 1) {
        c2 = dp1[i + 1][j - 1];
      }
      ans = max(ans, c1 + c2 + c3);
    }
  }
  ans = max(ans, dp3[0][n - 1]);
  ans = max(ans, dp4[0][n - 1]);
  cout << max(ans, max(dp2[0][n - 1], dp1[0][n - 1]));
  return 0;
}
long long modInv(long long a, long long m) {
  long long x, y;
  long long g = gcdExtended(a, m, &x, &y);
  long long res = (x % m + m) % m;
  return res;
}
long long gcdExtended(long long a, long long b, long long *x, long long *y) {
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }
  long long x1, y1;
  long long gcd = gcdExtended(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
long long mpower(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long power(long long x, long long y) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) res = res * x;
    y = y >> 1;
    x = x * x;
  }
  return res;
}
bool isPrime(long long n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  long long p = sqrt(n);
  for (long long i = 5; i <= p; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}

```