#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 20000;
const int INF = 2147483647;
int N;
int a[MAXN + 1];
int fr[3][MAXN + 1];
int bc[3][MAXN + 1];
int t[MAXN + 1];
int tr[MAXN * 4 + 1];
void build(int rt, int l, int r) {
  if (l == r) {
    tr[rt] = t[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(rt << 1, l, mid);
  build(rt << 1 | 1, mid + 1, r);
  tr[rt] = max(tr[rt << 1], tr[rt << 1 | 1]);
}
int GM(int rt, int l, int r, int L, int R) {
  if (L <= l && R >= r) return tr[rt];
  int mid = (l + r) >> 1, ans = -INF;
  if (mid >= L) ans = max(GM(rt << 1, l, mid, L, R), ans);
  if (R > mid) ans = max(ans, GM(rt << 1 | 1, mid + 1, r, L, R));
  return ans;
}
int main() {
  N = read();
  for (int i = 1; i <= N; i++) a[i] = read();
  for (int i = 1; i <= N; i++) {
    fr[1][i] = fr[1][i - 1];
    fr[2][i] = fr[2][i - 1];
    if (a[i] == 1)
      fr[1][i]++;
    else
      fr[2][i]++;
  }
  for (int i = N; i >= 1; i--) {
    bc[1][i] = bc[1][i + 1];
    bc[2][i] = bc[2][i + 1];
    if (a[i] == 1)
      bc[1][i]++;
    else
      bc[2][i]++;
  }
  int ans = 0;
  for (int i = 1; i <= N; i++) t[i] = fr[2][i] - fr[1][i];
  build(1, 1, N);
  for (int i = 1; i <= N; i++) ans = max(ans, fr[1][i] + bc[2][i]);
  ans = max(ans, max(fr[2][N], fr[1][N]));
  for (int i = 1; i <= N; i++) {
    for (int j = i + 1; j <= N; j++) {
      int tmp = fr[1][i - 1] + bc[2][j + 1] + fr[1][j] - fr[2][i - 1];
      ans = max(ans, tmp + GM(1, 1, N, i, j));
    }
  }
  printf("%d\n", ans);
  return 0;
}

### 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;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 20000;
const int INF = 2147483647;
int N;
int a[MAXN + 1];
int fr[3][MAXN + 1];
int bc[3][MAXN + 1];
int t[MAXN + 1];
int tr[MAXN * 4 + 1];
void build(int rt, int l, int r) {
  if (l == r) {
    tr[rt] = t[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(rt << 1, l, mid);
  build(rt << 1 | 1, mid + 1, r);
  tr[rt] = max(tr[rt << 1], tr[rt << 1 | 1]);
}
int GM(int rt, int l, int r, int L, int R) {
  if (L <= l && R >= r) return tr[rt];
  int mid = (l + r) >> 1, ans = -INF;
  if (mid >= L) ans = max(GM(rt << 1, l, mid, L, R), ans);
  if (R > mid) ans = max(ans, GM(rt << 1 | 1, mid + 1, r, L, R));
  return ans;
}
int main() {
  N = read();
  for (int i = 1; i <= N; i++) a[i] = read();
  for (int i = 1; i <= N; i++) {
    fr[1][i] = fr[1][i - 1];
    fr[2][i] = fr[2][i - 1];
    if (a[i] == 1)
      fr[1][i]++;
    else
      fr[2][i]++;
  }
  for (int i = N; i >= 1; i--) {
    bc[1][i] = bc[1][i + 1];
    bc[2][i] = bc[2][i + 1];
    if (a[i] == 1)
      bc[1][i]++;
    else
      bc[2][i]++;
  }
  int ans = 0;
  for (int i = 1; i <= N; i++) t[i] = fr[2][i] - fr[1][i];
  build(1, 1, N);
  for (int i = 1; i <= N; i++) ans = max(ans, fr[1][i] + bc[2][i]);
  ans = max(ans, max(fr[2][N], fr[1][N]));
  for (int i = 1; i <= N; i++) {
    for (int j = i + 1; j <= N; j++) {
      int tmp = fr[1][i - 1] + bc[2][j + 1] + fr[1][j] - fr[2][i - 1];
      ans = max(ans, tmp + GM(1, 1, N, i, j));
    }
  }
  printf("%d\n", ans);
  return 0;
}

```