#include <bits/stdc++.h>
const long long INF = (1LL << 60) - 1;
using namespace std;
long long power(long long x, long long y) {
  long long res = 1;
  x = x % 1000000007;
  while (y > 0) {
    if (y & 1) res = (res * x) % 1000000007;
    y = y >> 1;
    x = (x * x) % 1000000007;
  }
  return res;
}
long long BINARY_SEARCH(long long dp[], long long n, long long key) {
  long long s = 1;
  long long e = n;
  while (s <= e) {
    long long mid = (s + e) / 2;
    if (dp[mid] == key)
      return mid;
    else if (dp[mid] > key)
      e = mid - 1;
    else
      s = mid + 1;
  }
  return -1;
}
string CONVERT_TO_BINARY(long long s) {
  string res = "";
  while (s != 0) {
    res += (char)('0' + s % 2);
    s /= 2;
  }
  reverse(res.begin(), res.end());
  return res;
}
bool PALIN(string s) {
  long long i = 0;
  long long j = s.length() - 1;
  while (i <= j) {
    if (s[i] != s[j]) return false;
    j--, i++;
  }
  return true;
}
long long STOI(string s) {
  long long num = 0;
  long long po = 1;
  for (long long i = s.length() - 1; i >= 0; i--) {
    num += po * (s[i] - '0');
    po *= 10;
  }
  return num;
}
long long modInverse(long long a, long long m) { return power(a, m - 2); }
int findLongestSub(string bin) {
  int n = bin.length(), i;
  int sum = 0;
  unordered_map<int, int> prevSum;
  int maxlen = 0;
  int currlen;
  for (i = 0; i < n; i++) {
    if (bin[i] == '1')
      sum++;
    else
      sum--;
    if (sum > 0) {
      maxlen = i + 1;
    } else if (sum <= 0) {
      if (prevSum.find(sum - 1) != prevSum.end()) {
        currlen = i - prevSum[sum - 1];
        maxlen = max(maxlen, currlen);
      }
    }
    if (prevSum.find(sum) == prevSum.end()) prevSum[sum] = i;
  }
  return maxlen;
}
vector<long long> prefix_KMP(string &s) {
  vector<long long> pi;
  pi[0] = 0;
  for (long long i = 1; i <= s.length() - 1; i++) {
    long long j = pi[i - 1];
    while (j > 0 && s[i] != s[j]) {
      j = pi[j - 1];
    }
    if (s[i] == s[j]) {
      j++;
    }
    pi[i] = j;
  }
  return pi;
}
long long checkLis(vector<long long> &a, long long n) {
  vector<long long> dp(n + 1, 0);
  for (long long i = 0; i < n; i++) {
    dp[i] = 1;
  }
  for (long long i = 1; i < n; i++) {
    for (long long j = 0; j < i; j++) {
      if (a[i] >= a[j] && dp[i] < 1 + dp[j]) {
        dp[i] = 1 + dp[j];
      }
    }
  }
  return *max_element(dp.begin(), dp.end());
}
long long dp[2005][2005][4];
long long rdp[2005][2005][4];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long a[n + 100];
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = i; j <= n; j++) {
      if (j == i) {
        if (a[j] == 1) {
          dp[i][j][0] = 1;
        } else {
          dp[i][j][2] = 1;
        }
        continue;
      }
      if (a[j] == 1) {
        dp[i][j][0] = dp[i][j - 1][0] + 1;
        dp[i][j][1] = dp[i][j - 1][1];
        dp[i][j][2] = dp[i][j - 1][2];
      } else {
        dp[i][j][0] = dp[i][j - 1][0];
        dp[i][j][1] = max(dp[i][j - 1][0], dp[i][j - 1][1]) + 1;
        dp[i][j][2] = dp[i][j - 1][2] + 1;
      }
    }
  }
  for (long long i = n; i >= 1; i--) {
    for (long long j = i; j >= 1; j--) {
      if (i == j) {
        if (a[j] == 1)
          rdp[i][j][0] = 1;
        else {
          rdp[i][j][2] = 1;
        }
        continue;
      } else {
        if (a[j] == 1) {
          rdp[i][j][0] = rdp[i][j + 1][0] + 1;
          rdp[i][j][1] = rdp[i][j + 1][1];
          rdp[i][j][2] = rdp[i][j + 1][2];
        } else {
          rdp[i][j][0] = rdp[i][j + 1][0];
          rdp[i][j][1] = max(rdp[i][j + 1][0], rdp[i][j + 1][1]) + 1;
          rdp[i][j][2] = rdp[i][j + 1][2] + 1;
        }
      }
    }
  }
  long long ans = max(dp[1][n][0], max(dp[1][n][1], dp[1][n][2]));
  for (long long i = 1; i <= n; i++) {
    for (long long j = i; j <= n; j++) {
      long long a0, a1, a2;
      a0 = dp[1][i - 1][0], a1 = dp[1][i - 1][1], a2 = dp[1][i - 1][2];
      long long b0, b1, b2;
      b0 = rdp[j][i][0], b1 = rdp[j][i][1], b2 = rdp[j][i][2];
      long long c0, c1, c2;
      c0 = dp[j + 1][n][0], c1 = dp[j + 1][n][1], c2 = dp[j + 1][n][2];
      long long d0, d1, d2;
      d0 = a0 + b0 + c0;
      d1 = a0 + b0 + c1;
      d1 = max(d1, a0 + b1 + c2);
      d1 = max(d1, a0 + b0 + c2);
      d1 = max(d1, a1 + b2 + c2);
      d1 = max(d1, a0 + b2 + c2);
      d1 = max(d1, a2 + b2 + c2);
      ans = max(ans, max(d0, d1));
    }
  }
  cout << ans;
}

### Prompt

Your task is to create 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 long long INF = (1LL << 60) - 1;
using namespace std;
long long power(long long x, long long y) {
  long long res = 1;
  x = x % 1000000007;
  while (y > 0) {
    if (y & 1) res = (res * x) % 1000000007;
    y = y >> 1;
    x = (x * x) % 1000000007;
  }
  return res;
}
long long BINARY_SEARCH(long long dp[], long long n, long long key) {
  long long s = 1;
  long long e = n;
  while (s <= e) {
    long long mid = (s + e) / 2;
    if (dp[mid] == key)
      return mid;
    else if (dp[mid] > key)
      e = mid - 1;
    else
      s = mid + 1;
  }
  return -1;
}
string CONVERT_TO_BINARY(long long s) {
  string res = "";
  while (s != 0) {
    res += (char)('0' + s % 2);
    s /= 2;
  }
  reverse(res.begin(), res.end());
  return res;
}
bool PALIN(string s) {
  long long i = 0;
  long long j = s.length() - 1;
  while (i <= j) {
    if (s[i] != s[j]) return false;
    j--, i++;
  }
  return true;
}
long long STOI(string s) {
  long long num = 0;
  long long po = 1;
  for (long long i = s.length() - 1; i >= 0; i--) {
    num += po * (s[i] - '0');
    po *= 10;
  }
  return num;
}
long long modInverse(long long a, long long m) { return power(a, m - 2); }
int findLongestSub(string bin) {
  int n = bin.length(), i;
  int sum = 0;
  unordered_map<int, int> prevSum;
  int maxlen = 0;
  int currlen;
  for (i = 0; i < n; i++) {
    if (bin[i] == '1')
      sum++;
    else
      sum--;
    if (sum > 0) {
      maxlen = i + 1;
    } else if (sum <= 0) {
      if (prevSum.find(sum - 1) != prevSum.end()) {
        currlen = i - prevSum[sum - 1];
        maxlen = max(maxlen, currlen);
      }
    }
    if (prevSum.find(sum) == prevSum.end()) prevSum[sum] = i;
  }
  return maxlen;
}
vector<long long> prefix_KMP(string &s) {
  vector<long long> pi;
  pi[0] = 0;
  for (long long i = 1; i <= s.length() - 1; i++) {
    long long j = pi[i - 1];
    while (j > 0 && s[i] != s[j]) {
      j = pi[j - 1];
    }
    if (s[i] == s[j]) {
      j++;
    }
    pi[i] = j;
  }
  return pi;
}
long long checkLis(vector<long long> &a, long long n) {
  vector<long long> dp(n + 1, 0);
  for (long long i = 0; i < n; i++) {
    dp[i] = 1;
  }
  for (long long i = 1; i < n; i++) {
    for (long long j = 0; j < i; j++) {
      if (a[i] >= a[j] && dp[i] < 1 + dp[j]) {
        dp[i] = 1 + dp[j];
      }
    }
  }
  return *max_element(dp.begin(), dp.end());
}
long long dp[2005][2005][4];
long long rdp[2005][2005][4];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long a[n + 100];
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = i; j <= n; j++) {
      if (j == i) {
        if (a[j] == 1) {
          dp[i][j][0] = 1;
        } else {
          dp[i][j][2] = 1;
        }
        continue;
      }
      if (a[j] == 1) {
        dp[i][j][0] = dp[i][j - 1][0] + 1;
        dp[i][j][1] = dp[i][j - 1][1];
        dp[i][j][2] = dp[i][j - 1][2];
      } else {
        dp[i][j][0] = dp[i][j - 1][0];
        dp[i][j][1] = max(dp[i][j - 1][0], dp[i][j - 1][1]) + 1;
        dp[i][j][2] = dp[i][j - 1][2] + 1;
      }
    }
  }
  for (long long i = n; i >= 1; i--) {
    for (long long j = i; j >= 1; j--) {
      if (i == j) {
        if (a[j] == 1)
          rdp[i][j][0] = 1;
        else {
          rdp[i][j][2] = 1;
        }
        continue;
      } else {
        if (a[j] == 1) {
          rdp[i][j][0] = rdp[i][j + 1][0] + 1;
          rdp[i][j][1] = rdp[i][j + 1][1];
          rdp[i][j][2] = rdp[i][j + 1][2];
        } else {
          rdp[i][j][0] = rdp[i][j + 1][0];
          rdp[i][j][1] = max(rdp[i][j + 1][0], rdp[i][j + 1][1]) + 1;
          rdp[i][j][2] = rdp[i][j + 1][2] + 1;
        }
      }
    }
  }
  long long ans = max(dp[1][n][0], max(dp[1][n][1], dp[1][n][2]));
  for (long long i = 1; i <= n; i++) {
    for (long long j = i; j <= n; j++) {
      long long a0, a1, a2;
      a0 = dp[1][i - 1][0], a1 = dp[1][i - 1][1], a2 = dp[1][i - 1][2];
      long long b0, b1, b2;
      b0 = rdp[j][i][0], b1 = rdp[j][i][1], b2 = rdp[j][i][2];
      long long c0, c1, c2;
      c0 = dp[j + 1][n][0], c1 = dp[j + 1][n][1], c2 = dp[j + 1][n][2];
      long long d0, d1, d2;
      d0 = a0 + b0 + c0;
      d1 = a0 + b0 + c1;
      d1 = max(d1, a0 + b1 + c2);
      d1 = max(d1, a0 + b0 + c2);
      d1 = max(d1, a1 + b2 + c2);
      d1 = max(d1, a0 + b2 + c2);
      d1 = max(d1, a2 + b2 + c2);
      ans = max(ans, max(d0, d1));
    }
  }
  cout << ans;
}

```