#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#define ll long long

using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6+5;

int a[maxn],y[maxn],my[maxn],ycnt = 0,mycnt = 0;
int dp[5005][5005]; 
int main()
{
	int n; cin>>n;
	for(int i = 1; i <= n;i++)  {
		cin>>a[i];
		if(a[i]) y[++ycnt] = i;
		else     my[++mycnt] = i; 
	}
	dp[0][0] = 0;
	for(int i = 1; i <= ycnt; i++) {
		dp[i][i] = dp[i-1][i-1]+abs(y[i]-my[i]);
		for(int j = i+1; j <= mycnt; j++) {
			dp[i][j] = min(dp[i][j-1],dp[i-1][j-1]+abs(y[i]-my[j]));
		}
	}
	if(!ycnt) puts("0");else
	cout<<dp[ycnt][mycnt]<<"\n";
	return 0;
}

