#include <bits/stdc++.h>
const double pi = acos(-1.0);
using namespace std;
inline long long MAX2(long long a, long long b) {
  return (a) > (b) ? (a) : (b);
}
inline long long MAX3(long long a, long long b, long long c) {
  return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long MIN2(long long a, long long b) {
  return (a) < (b) ? (a) : (b);
}
inline long long MIN3(long long a, long long b, long long c) {
  return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}
int mod = 1e9 + 7;
int64_t extGcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {
  if (!a) {
    x = 0;
    y = 1;
    return b;
  }
  int64_t x1, y1;
  int64_t d = extGcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return d;
}
inline int addMod(int a, int b, int m = mod) { return ((int64_t)a + b) % m; }
inline int mulMod(int a, int b, int m = mod) { return ((int64_t)a * b) % m; }
inline int divMod(int a, int b, int m = mod) {
  int64_t x, y;
  int64_t g = extGcd(b, m, x, y);
  if (g != 1) {
    cerr << "Bad gcd!" << '\n';
    for (;;)
      ;
  }
  x = (x % m + m) % m;
  return mulMod(a, x, m);
}
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
inline long long exp(long long a, long long b) {
  if (a == 0) return 0ll;
  long long r = 1LL;
  while (b > 0) {
    if (b & 1) {
      r = r * (a % mod);
      r = (r + mod) % mod;
    }
    b /= 2;
    a = (a % mod) * (a % mod);
    a = (a + mod) % mod;
  }
  return (r + mod) % mod;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  if (a == 0) return b;
  return gcd(b, a % b);
}
unsigned long int setbits(long long n) {
  unsigned long int count = 0;
  while (n) {
    n &= (n - 1);
    count++;
  }
  return count;
}
int main() {
  int n;
  cin >> n;
  int a[n];
  for (long long i = (long long)(0); i < (long long)(n); i++) cin >> a[i];
  long long dp[n + 1][4];
  memset((dp), (0), sizeof(dp));
  for (long long i = (long long)(1); i < (long long)(n + 1); i++) {
    dp[i][0] = dp[i - 1][0] + (a[i - 1] == 1);
    dp[i][1] = MAX2(dp[i - 1][0], dp[i - 1][1]) + (a[i - 1] == 2);
    dp[i][2] = MAX2(dp[i - 1][1], dp[i - 1][2]) + (a[i - 1] == 1);
    dp[i][3] = MAX2(dp[i - 1][2], dp[i - 1][3]) + (a[i - 1] == 2);
  }
  cout << MAX3(dp[n][3], dp[n][2], dp[n][1]) << '\n';
}

### Prompt

Please formulate 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>
const double pi = acos(-1.0);
using namespace std;
inline long long MAX2(long long a, long long b) {
  return (a) > (b) ? (a) : (b);
}
inline long long MAX3(long long a, long long b, long long c) {
  return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long MIN2(long long a, long long b) {
  return (a) < (b) ? (a) : (b);
}
inline long long MIN3(long long a, long long b, long long c) {
  return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}
int mod = 1e9 + 7;
int64_t extGcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {
  if (!a) {
    x = 0;
    y = 1;
    return b;
  }
  int64_t x1, y1;
  int64_t d = extGcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return d;
}
inline int addMod(int a, int b, int m = mod) { return ((int64_t)a + b) % m; }
inline int mulMod(int a, int b, int m = mod) { return ((int64_t)a * b) % m; }
inline int divMod(int a, int b, int m = mod) {
  int64_t x, y;
  int64_t g = extGcd(b, m, x, y);
  if (g != 1) {
    cerr << "Bad gcd!" << '\n';
    for (;;)
      ;
  }
  x = (x % m + m) % m;
  return mulMod(a, x, m);
}
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
inline long long exp(long long a, long long b) {
  if (a == 0) return 0ll;
  long long r = 1LL;
  while (b > 0) {
    if (b & 1) {
      r = r * (a % mod);
      r = (r + mod) % mod;
    }
    b /= 2;
    a = (a % mod) * (a % mod);
    a = (a + mod) % mod;
  }
  return (r + mod) % mod;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  if (a == 0) return b;
  return gcd(b, a % b);
}
unsigned long int setbits(long long n) {
  unsigned long int count = 0;
  while (n) {
    n &= (n - 1);
    count++;
  }
  return count;
}
int main() {
  int n;
  cin >> n;
  int a[n];
  for (long long i = (long long)(0); i < (long long)(n); i++) cin >> a[i];
  long long dp[n + 1][4];
  memset((dp), (0), sizeof(dp));
  for (long long i = (long long)(1); i < (long long)(n + 1); i++) {
    dp[i][0] = dp[i - 1][0] + (a[i - 1] == 1);
    dp[i][1] = MAX2(dp[i - 1][0], dp[i - 1][1]) + (a[i - 1] == 2);
    dp[i][2] = MAX2(dp[i - 1][1], dp[i - 1][2]) + (a[i - 1] == 1);
    dp[i][3] = MAX2(dp[i - 1][2], dp[i - 1][3]) + (a[i - 1] == 2);
  }
  cout << MAX3(dp[n][3], dp[n][2], dp[n][1]) << '\n';
}

```