#include <bits/stdc++.h>
#define pii pair<int, int>
#define mp make_pair
using namespace std;
int n, a[2005], tmp[2005], upto[2005], post[2005], t = 0;
pii arr[2005];
void print(){
for(int j = 0; j < t; j++)
cout << "(" << arr[j].first << " " << arr[j].second << ") ";
cout << endl;
}
int rev(int l, int r){
for(int j = 0; j < n; j++)
tmp[j] = a[j];
for(int j = l; j <= (r + l)/2; j++)
swap(tmp[j], tmp[r + l - j]);
t = 0;
for(int j = 0; j < n; j++){
int c = tmp[j], cnt = 0;
while(j < n && c == tmp[j]) cnt++, j++;
j--;
arr[t++] = mp(c, cnt);
}
//print();
int ans = 0;
upto[t] = 0;
upto[t-1] = arr[t-1].second;
ans = upto[t-1];
for(int j = t-2; j >= 0; j--){
int cur = arr[j].second;
if(arr[j].first == 1) upto[j] = max(upto[j+2]+cur, upto[j+1]+cur);
else upto[j] = cur + upto[j+2];
if(ans < upto[j]) ans = upto[j];
}
//cout << l << " " << r << " " << ans << endl;
return ans;
}
int main(){
scanf("%d",&n);
for(int j = 0; j < n; j++)
scanf("%d",&a[j]);
int ans = rev(0, 0);
for(int j = 0; j < n; j++){
for(int k = j+1; k < n; k++){
ans = max(ans, rev(j, k));
}
}
printf("%d\n",ans);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgcGlpIHBhaXI8aW50LCBpbnQ+CiNkZWZpbmUgbXAgbWFrZV9wYWlyCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG4sIGFbMjAwNV0sIHRtcFsyMDA1XSwgdXB0b1syMDA1XSwgcG9zdFsyMDA1XSwgdCA9IDA7CgpwaWkgYXJyWzIwMDVdOwoKdm9pZCBwcmludCgpewogICAgZm9yKGludCBqID0gMDsgaiA8IHQ7IGorKykKICAgICAgICBjb3V0IDw8ICIoIiA8PCBhcnJbal0uZmlyc3QgPDwgIiAiIDw8IGFycltqXS5zZWNvbmQgPDwgIikgICI7CiAgICBjb3V0IDw8IGVuZGw7Cn0KCmludCByZXYoaW50IGwsIGludCByKXsKICAgIGZvcihpbnQgaiA9IDA7IGogPCBuOyBqKyspCiAgICAgICAgdG1wW2pdID0gYVtqXTsKICAgIGZvcihpbnQgaiA9IGw7IGogPD0gKHIgKyBsKS8yOyBqKyspCiAgICAgICAgc3dhcCh0bXBbal0sIHRtcFtyICsgbCAtIGpdKTsKCiAgICB0ID0gMDsKICAgIGZvcihpbnQgaiA9IDA7IGogPCBuOyBqKyspewogICAgICAgIGludCBjID0gdG1wW2pdLCBjbnQgPSAwOwogICAgICAgIHdoaWxlKGogPCBuICYmIGMgPT0gdG1wW2pdKSBjbnQrKywgaisrOwogICAgICAgIGotLTsKICAgICAgICBhcnJbdCsrXSA9IG1wKGMsIGNudCk7CiAgICB9CiAgICAvL3ByaW50KCk7CgogICAgaW50IGFucyA9IDA7CiAgICB1cHRvW3RdID0gMDsKICAgIHVwdG9bdC0xXSA9IGFyclt0LTFdLnNlY29uZDsKICAgIGFucyA9IHVwdG9bdC0xXTsKICAgIGZvcihpbnQgaiA9IHQtMjsgaiA+PSAwOyBqLS0pewogICAgICAgIGludCBjdXIgPSBhcnJbal0uc2Vjb25kOwogICAgICAgIGlmKGFycltqXS5maXJzdCA9PSAxKSB1cHRvW2pdID0gbWF4KHVwdG9baisyXStjdXIsIHVwdG9baisxXStjdXIpOwogICAgICAgIGVsc2UgdXB0b1tqXSA9IGN1ciArIHVwdG9baisyXTsKICAgICAgICBpZihhbnMgPCB1cHRvW2pdKSBhbnMgPSB1cHRvW2pdOwogICAgfQogICAgLy9jb3V0IDw8IGwgPDwgIiAiIDw8IHIgPDwgIiAiIDw8IGFucyA8PCBlbmRsOwogICAgcmV0dXJuIGFuczsKfQoKaW50IG1haW4oKXsKICAgIHNjYW5mKCIlZCIsJm4pOwogICAgZm9yKGludCBqID0gMDsgaiA8IG47IGorKykKICAgICAgICBzY2FuZigiJWQiLCZhW2pdKTsKICAgIGludCBhbnMgPSByZXYoMCwgMCk7CgogICAgZm9yKGludCBqID0gMDsgaiA8IG47IGorKyl7CiAgICAgICAgZm9yKGludCBrID0gaisxOyBrIDwgbjsgaysrKXsKICAgICAgICAgICAgYW5zID0gbWF4KGFucywgcmV2KGosIGspKTsKICAgICAgICB9CiAgICB9CiAgICBwcmludGYoIiVkXG4iLGFucyk7CiAgICByZXR1cm4gMDsKfQ==