// created time: 2026-11-18 07:15:09
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define fi first
#define se second
#define mkp make_pair
#define pb emplace_back
#define popcnt __builtin_popcountll
const int mod = 998244353;
inline ll read(){
ll x=0, f=1; int ch=getchar();
while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
while(ch>='0' && ch<='9') x=x*10+ch-'0', ch=getchar();
return x*f;
}
inline int lg2(int x){ return 31^__builtin_clz(x); }
inline ll lg2(ll x){ return 63^__builtin_clzll(x); }
template<typename T>inline void addmod(T &x){ if(x >= mod) x -= mod; }
template<typename T>inline void chkmax(T &a,T b){ a=max(a,b); }
template<typename T>inline void chkmin(T &a,T b){ a=min(a,b); }
inline ll qpow(ll a,ll b){
ll ans=1, base=a;
while(b){
if(b&1) ans=ans*base%mod;
base=base*base%mod; b>>=1;
}
return ans;
}
inline ll INV(ll x){ return qpow(x, mod-2); }
template <class T>
struct MCMF {
public:
MCMF() : n(), dirty() {}
MCMF(int n) : n((assert(n >= 0), n)), dirty(), ed(n + 1), par(n + 1), dis(n + 1), pi(n + 1) {}
std::pair<int, int> add(int from, int to, T cap, T cost) {
assert(1 <= from && from <= n);
assert(1 <= to && to <= n);
assert(cap >= 0);
dirty |= (cap && pi[from] != inf && pi[from] + cost < pi[to]);
int id = (int)ed[from].size(), rev = (int)ed[to].size() + (from == to);
ed[from].push_back(edge(to, rev, cap, cost));
ed[to].push_back(edge(from, id, 0, -cost));
return {from, id};
}
template <class F>
std::pair<int, int> add_convex(int from, int to, T cap, F cost) {
assert(cap >= 0);
auto id = add(from, to, cap, cap ? cost(T(1)) - cost(T(0)) : T(0));
fun.emplace_back(std::move(cost));
auto [u, i] = id;
ed[u][i].fn = (int)fun.size();
ed[ed[u][i].to][ed[u][i].rev].fn = -(int)fun.size();
return id;
}
std::pair<T, T> max_flow(int s, int t, T lim = inf) {
assert(1 <= s && s <= n);
assert(1 <= t && t <= n);
assert(s != t && lim >= 0);
setpi(s);
std::pair<T, T> res = {0, 0};
while (res.first < lim && path(s, t)) {
T flw = lim - res.first;
for (int i = t, j, r; i != s; i = j) {
r = ed[i][par[i]].rev, j = ed[i][par[i]].to;
flw = std::min(flw, ed[j][r].cap);
if (ed[j][r].fn) { flw = std::min(flw, T(1)); }
}
res.first += flw, res.second += flw * (pi[t] - pi[s]);
for (int i = t, j, r; i != s; i = j) {
r = ed[i][par[i]].rev, j = ed[i][par[i]].to;
ed[i][par[i]].cap += flw;
ed[j][r].cap -= flw;
if (ed[j][r].fn) { update(j, r); }
}
}
return res;
}
std::pair<T, T> get_flow(std::pair<int, int> id) {
auto [u, i] = id;
assert(1 <= u && u <= n);
assert(0 <= i && i < (int)ed[u].size());
edge &e = ed[u][i], &r = ed[e.to][e.rev];
return {r.cap, e.cap + r.cap};
}
void reserve(int u, int m) {
assert(1 <= u && u <= n && m >= 0);
ed[u].reserve(m);
}
private:
static constexpr T inf = std::numeric_limits<T>::max() / 2;
struct edge {
int to, rev, fn;
T cap, cost;
edge() {}
edge(int to, int rev, T cap, T cost) : to(to), rev(rev), fn(), cap(cap), cost(cost) {}
};
int n;
bool dirty;
std::vector<std::vector<edge>> ed;
std::vector<int> par;
std::vector<T> dis, pi;
std::vector<std::function<T(T)>> fun;
void update(int u, int i) {
if (ed[u][i].fn < 0) {
int v = ed[u][i].to, j = ed[u][i].rev;
u = v, i = j;
}
edge &e = ed[u][i], &r = ed[e.to][e.rev];
auto& cost = fun[e.fn - 1];
T f = r.cap, val = cost(f);
if (e.cap) { e.cost = cost(f + 1) - val; }
if (r.cap) { r.cost = cost(f - 1) - val; }
}
bool path(int s, int t) {
std::fill(dis.begin(), dis.end(), inf);
dis[s] = 0;
std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<std::pair<T, int>>> qu;
qu.push({dis[s], s});
while (!qu.empty()) {
auto [di, u] = qu.top(); qu.pop();
if (di != dis[u]) { continue; }
for (edge& e : ed[u]) {
if (!e.cap) { continue; }
T val = di + e.cost + pi[u] - pi[e.to];
if (dis[e.to] > val) {
dis[e.to] = val;
par[e.to] = e.rev;
qu.push({dis[e.to], e.to});
}
}
}
for (int i = 1; i <= n; ++i) {
pi[i] = (dis[i] == inf ? inf : pi[i] + dis[i]);
}
return dis[t] != inf;
}
void setpi(int s) {
if (!dirty && pi[s] != inf) { return ; }
dirty = 0;
std::fill(pi.begin(), pi.end(), inf);
pi[s] = 0;
int t = n, flg = 1;
while (flg-- && t--) {
for (int i = 1; i <= n; ++i) {
if (pi[i] != inf) {
for (auto& e : ed[i]) {
if (e.cap && pi[e.to] > pi[i] + e.cost) {
pi[e.to] = pi[i] + e.cost, flg = 1;
}
}
}
}
}
assert(t >= 0);
}
};
int n,a[305],c[3],tc[305][3];
vector<pair<int,int>>vec;
int code(int i,int j){
return i*(c[0]+1)+j+2;
}
void procedure(){
n=read();
for(int i=1;i<=n;i++){
++c[a[i]=read()];
memcpy(tc[i],c,sizeof(c));
if(a[i]==1)vec.pb(c[2],c[0]);
}
int S=1,T=code(0,c[0]),id=code(c[2],c[0]),ans=0;
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)if(a[i]==2&&a[j]==0){
++id,++ans;
}
MCMF<int>F(id);
id=code(c[2],c[0]);
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)if(a[i]==2&&a[j]==0){
int p=tc[i][2],q=tc[j][0];
++id;
F.add(code(p-1,q-1),id,1,0);
F.add(code(p,q),id,1,0);
F.add(code(p,q-1),id,1,0);
F.add(id,code(p-1,q),1,0);
}
for(int i=0;i<=c[2];i++)
for(int j=0;j<=c[0];j++){
if(i) F.add(code(i,j),code(i-1,j),1e9,1);
if(j<c[0]) F.add(code(i,j),code(i,j+1),1e9,1);
}
for(auto [x,y]:vec) F.add(S,code(x,y),1,0);
printf("%d\n",ans+F.max_flow(S,T).se);
}
int main(){
#ifdef LOCAL
assert(freopen("test.in","r",stdin));
assert(freopen("test.out","w",stdout));
#endif
ll T=1;
// math_init();
while(T--) procedure();
return 0;
}