rt
70pts
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll max(ll a,int b){return a>b?a:b;}
const int maxn=5e5+10;
int n;
ll f[maxn],tot[maxn];
ll sum,ans;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>f[i];
if(f[i]==0)f[i]=-1;
}
for(int i=1;i<=n;i++){
sum+=f[i];
if(!tot[sum+n])tot[sum+n]=i;
else ans=max(i-(tot[sum+n]+1)+1,ans);
}
printf("%lld\n",ans);
return 0;
}
/*
9
0 1 0 0 0 1 1 0 0
*/