#include<bits/stdc++.h>
using namespace std;

#define f1(a,b,c) for(int(a)=(b);(a)<(c);(a)++)
#define f2(a,b,c) for(int(a)=(b);(a)<=(c);(a)++)
#define f3(a,b,c) for(int(a)=(b);(a)>(c);(a)--)
#define f4(a,b,c) for(int(a)=(b);(a)>=(c);(a)--)
#define fi(a,b) for(auto(a)=b.begin();(a)!=b.end();(a)++)
#define pb push_back
#define all(v) v.begin(),v.end()
#define nper next_permutation
#define pper prev_permutation
#define mkp make_pair
#define F first
#define S second
#define int long long
#define mod 1000000007

typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef vector<pii> vpii;
typedef set<int> si;
typedef unordered_set<int> usi;
typedef multiset<int> msi;
typedef map<int,int> mi;
typedef unordered_map<int,int> umi;
typedef multimap<int,int> mmi;

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin>>n;
	vvi v(2);
	f1(i,0,n)
	{
		int x;
		cin>>x;
		v[x].pb(i);
	}
	int k=v[0].size();
	vvi dp(n-k+1,vi (k+1,0));
	f1(i,1,n-k+1)
	{
		f1(j,0,k+1)
		{
			if(j<i) dp[i][j]=INT_MAX;
			else dp[i][j]=min(dp[i][j-1],abs(v[1][i-1]-v[0][j-1])+dp[i-1][j-1]);
		}
	}
	cout<<dp[n-k][k];
	return 0;
}


















