#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int INF = 1e9;
const int MOD = 1e9+7;
vector<int>zero;
vector<int>one;
ll dp[5000][5000];
int solve(int i,int j,int n,int m){
	if(i>=n)
		return 0;
	int req = n-i;
	int av = m-j;
	if(req>av)
		return INF;
	if(dp[i][j]!=-1)
		return dp[i][j];
	int ans1 = solve(i,j+1,n,m);
	int ans2 = abs(one[i]-zero[j])+solve(i+1,j+1,n,m);
	return dp[i][j] = min(ans1,ans2);
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin>>n;
	for(int i = 0;i<n;i++){
		int a;
		cin>>a;
		if(a==1)
			one.push_back(i);
		else
			zero.push_back(i);
	}
	memset(dp,-1,sizeof dp);
	int n1 = one.size();
	int m = zero.size();
	cout<<solve(0,0,n1,m);
}