#include <bits/stdc++.h>
using namespace std;
clock_t startTime;
double getCurrentTime() {
  return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const pair<T, U> &_p) {
  _out << _p.first << " " << _p.second;
  return _out;
}
template <typename T, typename U>
inline istream &operator>>(istream &_in, pair<T, U> &_p) {
  _in >> _p.first >> _p.second;
  return _in;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const vector<T> &_v) {
  if (_v.empty()) return _out;
  _out << _v.front();
  for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) _out << ' ' << *_it;
  return _out;
}
template <typename T>
inline istream &operator>>(istream &_in, vector<T> &_v) {
  for (auto &_i : _v) _in >> _i;
  return _in;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const set<T> &_s) {
  if (_s.empty()) return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it;
  return _out;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const multiset<T> &_s) {
  if (_s.empty()) return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it;
  return _out;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const unordered_set<T> &_s) {
  if (_s.empty()) return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it;
  return _out;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const unordered_multiset<T> &_s) {
  if (_s.empty()) return _out;
  _out << *_s.begin();
  for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it;
  return _out;
}
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const map<T, U> &_m) {
  if (_m.empty()) return _out;
  _out << "{\"" << _m.begin()->first << "\", \"" << _m.begin()->second << "\"}";
  for (auto _it = ++_m.begin(); _it != _m.end(); ++_it)
    _out << ", { \"" << _it->first << "\", \"" << _it->second << "\"}";
  return _out;
}
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const unordered_map<T, U> &_m) {
  if (_m.empty()) return _out;
  _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')';
  for (auto _it = ++_m.begin(); _it != _m.end(); ++_it)
    _out << ", (" << _it->first << ": " << _it->second << ')';
  return _out;
}
template <typename T>
void out(const vector<T> &a, int n) {
  for (int i = 1; i <= n; ++i) {
    cout << a[i] << ' ';
  }
  cout << "\n";
}
template <typename T>
void out(const vector<vector<T>> &a, int n, int m) {
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cout << a[i][j] << ' ';
    }
    cout << '\n';
  }
}
vector<int> p;
int n;
void calc(vector<vector<int>> &dp) {
  multiset<int>::iterator it;
  for (int l = 1; l <= n; ++l) {
    multiset<int> s;
    for (int r = l; r <= n; ++r) {
      s.insert(p[r]);
      it = s.upper_bound(p[r]);
      if (it != s.end()) {
        s.erase(it);
      }
      dp[l][r] = s.size();
    }
  }
}
vector<vector<int>> dp, dp2, pre, suf;
void solve() {
  cin >> n;
  p.resize(n + 1), dp.assign(n + 1, vector<int>(n + 1, -1));
  pre.assign(n + 2, vector<int>(2)), suf.assign(n + 2, vector<int>(2)),
      dp2.assign(n + 1, vector<int>(n + 1, -1));
  for (int i = 1; i <= n; ++i) {
    cin >> p[i];
  }
  calc(dp);
  reverse(p.begin() + 1, p.end());
  calc(dp2);
  reverse(p.begin() + 1, p.end());
  for (int i = 1; i <= n; ++i) {
    pre[i][0] = (p[i] == 1) + pre[i - 1][0];
    pre[i][1] = (p[i] == 2) + pre[i - 1][1];
  }
  for (int i = n; i >= 1; --i) {
    suf[i][0] = (p[i] == 1) + suf[i + 1][0];
    suf[i][1] = (p[i] == 2) + suf[i + 1][1];
  }
  auto lis = [](int l, int r, int x) {
    if (l > r) return 0;
    if (x == 1) {
      return dp[l][r];
    }
    return dp2[n - r + 1][n - l + 1];
  };
  int ans = 0;
  for (int l = 1; l <= n; ++l) {
    for (int r = l; r <= n; ++r) {
      ans = max({ans, lis(1, l - 1, 1) + suf[l][1],
                 pre[l - 1][0] + lis(l, r, 2) + suf[r + 1][1],
                 pre[r][0] + lis(r + 1, n, 1)});
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  startTime = clock();
  int T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
