#include<bits/stdc++.h>
using namespace std;
const int maxn=6000;
int que[maxn],stk[maxn],n,a[maxn];
int f[maxn];
int main() {
	cin>>n;
	memset(f,63,sizeof(f));
	f[0]=0;
	for(int i=1; i<=n; i++)
		cin>>a[i];
	for(int i=1; i<=n; i++) {
		int tot0=0,tot1=0;
		int head=1,tail=0,top=0,ans=0;
		for(int j=i; j; j--) {
			if(a[j]==0) {
				tot0++;
				if(head<=tail) {
					ans+=que[head]-j;
					head++;
				}
				else stk[++top]=j;
			}
			if(a[j]==1) {
				tot1++;
				if(top) {
					ans+=stk[top]-j;
					top--;
				}
				else que[++tail]=j;
			}
			if(tot1<=tot0)
				f[i]=min(f[i],f[j-1]+ans);
		}
	}
	cout<<f[n]<<endl;
}