#include <bits/stdc++.h>
typedef long long ll;

#define pb push_back
#define _io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;

#define PROB "Armchairs"
#define MAXN 5005

int a[MAXN];
ll dp[MAXN][MAXN];

int main(){
    _io
    //freopen(PROB".inp", "r", stdin);
    //freopen(PROB".out", "w", stdout);
    int n; cin >> n;
    assert(2 <= n && n <= 5000);
    vector <int> arr[2];
    arr[0].pb(0);
    arr[1].pb(0);
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        assert(0 <= a[i] && a[i] <= 1);
        arr[a[i]].pb(i);
    }
    int m1 = arr[1].size();
    int m2 = arr[0].size();
    for(int i = 0; i <= n; i++)
        for(int j = 0; j <= n; j++)
            dp[i][j] = INT_MAX;
    for(int i = 0; i < m2; i++)
        dp[0][i] = 0;
    for(int i = 1; i < m1; i++)
        for(int j = 1; j < m2; j++){
            int u = arr[1][i], v = arr[0][j];
            dp[i][j] = dp[i][j-1];
            dp[i][j] = min(dp[i][j], dp[i-1][j-1] + abs(u-v));
        }
    cout << dp[m1-1][m2-1];
}
