#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
long double eps = 1e-10;
long long int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
long long int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
long long int n, res = 0, k = 0, h, ans = 0, m, level = 0;
;
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long int lcm(long long int x, long long int y) {
  return (x * y) / gcd(x, y);
}
long long int pot(long long int a, long long int b) {
  long long int r = 1;
  while (b) {
    if (b % 2) r = (r * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return r % mod;
}
void pr(long long int a[], long long int n) {
  for (long long int i = 1; i <= n; i++) cout << a[i] << " ";
  cout << '\n';
}
void read(long long int a[], long long int n) {
  for (long long int i = 1; i <= n; i++) cin >> a[i];
}
void yes() { cout << "Yes\n"; }
void no() { cout << "No\n"; }
vector<long long int> v[200005], vnode[200005];
void add_edge(long long int x, long long int y) {
  v[x].push_back(y);
  v[y].push_back(x);
}
string s2, ch = "", s, s1 = "", t;
vector<pair<string, long long int> > vs;
vector<pair<pair<long long int, long long int>, long long int> > vvv, vvv2;
vector<pair<long long int, long long int> > vv, vv2;
long long int a[200005], dp[3][200005];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long int i = 0, x = 0, z = 0, y = 0, j = 0, q, mx = 0, mm, idx = 0,
                ok = 0, l = 0, r, negatif = 0, positif = 0;
  set<pair<long long int, long long int> >::iterator itw, ith;
  set<long long int>::iterator it;
  multiset<long long int> mst1, mst2;
  cin >> n;
  read(a, n);
  x = 0;
  y = 0;
  a[0] = 0;
  for (i = 1; i <= (long long int)(n); i++) {
    x += (a[i] == 1);
    y += (a[i] == 2);
  }
  res = max(x, y);
  dp[1][n + 1] = 0;
  dp[2][n + 1] = 0;
  long long int nb1 = 0, nb2 = 0;
  for (i = n; i >= 1; i--) {
    dp[2][i] = dp[2][i + 1] + (a[i] == 2);
    dp[1][i] = dp[1][i + 1];
    if (a[i] == 1) {
      dp[1][i] = max(1 + dp[1][i + 1], 1 + dp[2][i + 1]);
    }
  }
  nb1 = 0;
  nb2 = 0;
  for (i = 0; i <= (long long int)(n); i++) {
    nb1 += (a[i] == 1);
    long long int cur = nb1 + max(dp[1][i + 1], dp[2][i + 1]);
    res = max(res, cur);
  }
  nb1 = 0;
  nb2 = 0;
  for (i = 1; i <= (long long int)(n); i++) {
    long long int cnt2 = 0, cnt1 = 0;
    for (j = i; j <= n; j++) {
      if (a[j] == 1) cnt1 = 1 + max(cnt1, cnt2);
      cnt2 += (a[j] == 2);
      long long int cur = nb1 + max(cnt1, cnt2) + dp[2][j + 1];
      res = max(res, cur);
    }
    nb1 += (a[i] == 1);
  }
  cout << res;
  return 0;
}

### Prompt

Please create a solution in Cpp 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 int mod = 1e9 + 7;
long double eps = 1e-10;
long long int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
long long int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
long long int n, res = 0, k = 0, h, ans = 0, m, level = 0;
;
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long int lcm(long long int x, long long int y) {
  return (x * y) / gcd(x, y);
}
long long int pot(long long int a, long long int b) {
  long long int r = 1;
  while (b) {
    if (b % 2) r = (r * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return r % mod;
}
void pr(long long int a[], long long int n) {
  for (long long int i = 1; i <= n; i++) cout << a[i] << " ";
  cout << '\n';
}
void read(long long int a[], long long int n) {
  for (long long int i = 1; i <= n; i++) cin >> a[i];
}
void yes() { cout << "Yes\n"; }
void no() { cout << "No\n"; }
vector<long long int> v[200005], vnode[200005];
void add_edge(long long int x, long long int y) {
  v[x].push_back(y);
  v[y].push_back(x);
}
string s2, ch = "", s, s1 = "", t;
vector<pair<string, long long int> > vs;
vector<pair<pair<long long int, long long int>, long long int> > vvv, vvv2;
vector<pair<long long int, long long int> > vv, vv2;
long long int a[200005], dp[3][200005];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long int i = 0, x = 0, z = 0, y = 0, j = 0, q, mx = 0, mm, idx = 0,
                ok = 0, l = 0, r, negatif = 0, positif = 0;
  set<pair<long long int, long long int> >::iterator itw, ith;
  set<long long int>::iterator it;
  multiset<long long int> mst1, mst2;
  cin >> n;
  read(a, n);
  x = 0;
  y = 0;
  a[0] = 0;
  for (i = 1; i <= (long long int)(n); i++) {
    x += (a[i] == 1);
    y += (a[i] == 2);
  }
  res = max(x, y);
  dp[1][n + 1] = 0;
  dp[2][n + 1] = 0;
  long long int nb1 = 0, nb2 = 0;
  for (i = n; i >= 1; i--) {
    dp[2][i] = dp[2][i + 1] + (a[i] == 2);
    dp[1][i] = dp[1][i + 1];
    if (a[i] == 1) {
      dp[1][i] = max(1 + dp[1][i + 1], 1 + dp[2][i + 1]);
    }
  }
  nb1 = 0;
  nb2 = 0;
  for (i = 0; i <= (long long int)(n); i++) {
    nb1 += (a[i] == 1);
    long long int cur = nb1 + max(dp[1][i + 1], dp[2][i + 1]);
    res = max(res, cur);
  }
  nb1 = 0;
  nb2 = 0;
  for (i = 1; i <= (long long int)(n); i++) {
    long long int cnt2 = 0, cnt1 = 0;
    for (j = i; j <= n; j++) {
      if (a[j] == 1) cnt1 = 1 + max(cnt1, cnt2);
      cnt2 += (a[j] == 2);
      long long int cur = nb1 + max(cnt1, cnt2) + dp[2][j + 1];
      res = max(res, cur);
    }
    nb1 += (a[i] == 1);
  }
  cout << res;
  return 0;
}

```