#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int n,a[N],sa,sb,l[N],r[N];//a男b女,lr记录差值为下标时的左右端点
int ans;
int main(){
freopen("teen.in","r",stdin);
freopen("teen.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
sa+=(1==a[i]);
sb+=(0==a[i]);
int t=sa-sb+n;
if (!l[t]&&t!=n) l[t]=i; else r[t]=i;
}
for (int i=0;i<=2*n;i++) ans=max(ans,r[i]-l[i]);
cout<<ans<<endl;
return 0;
}