#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) 
#define X first
#define Y second 
#define nl '\n'
#define AC return 0
#define pb(a) push_back(a) 
#define mst(a,b) memset(a, b, sizeof a)
#define rep(i,n) for(int i = 0; (i)<(n); i++)
#define rep1(i,n) for(int i = 1; (i)<=(n); i++)
#define scd(a) scanf("%lld", &a)
#define scdd(a,b) scanf("%lld%lld", &a, &b)
#define scs(s) scanf("%s", s)
//#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const ll INF = (ll)0x3f3f3f3f3f3f3f, MAX = 9e18, MIN = -9e18;
const int N = 5e3+10, M = 2e6+10, mod = 1e9+7, inf = 0x3f3f3f3f;
ll a[N], dp[N][N]; 
vector<ll> p1, p2;

int main()
{
	IOS;
	int _;
	// cin>>_;
	_ = 1;
	while(_--)
	{
		ll ans = 0, n;
		cin>>n;
		rep1(i, n)
		{
			cin>>a[i];
			if(a[i])
				p1.pb(i);
			else
			 	p2.pb(i);
		}
		rep1(i, p1.size())
			rep(j, p2.size()+1)
				dp[i][j] = MAX;
		//这里dp[0][i] = 0
		//dp[i][j]表示前i个1与前j个0匹配的最优解
		rep1(i, p1.size())
			rep1(j, p2.size())
			{
				dp[i][j] = dp[i][j-1];
				dp[i][j] = min(dp[i][j], dp[i-1][j-1]+abs(p1[i-1]-p2[j-1]));	
			}
		cout<<dp[p1.size()][p2.size()]<<nl;
	}
	
	
	AC;
} 
