//Just have Patience + 1...
#include<bits/stdc++.h>
using namespace std;

#define tarzan ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define int long long 

int rr[] = {0, 1, 1, 1, 0, -1, -1, -1};
int cc[] = {1, 1, 0, -1, -1, -1, 0, 1};

const int mod = 1e9 + 7;
const int N = 5050;

int dp[N][N];
vector<int> ones, zeros;

int solve(int n, int m) {
    if(dp[n][m] != -1) return dp[n][m];
    if(n == 0) return 0;
    if(m == 0) return INT_MAX;

    int ans = INT_MAX;
    if(n < m) ans = solve(n, m - 1);
    ans = min(ans, solve(n - 1, m - 1) + abs(ones[n - 1] - zeros[m - 1]));
    dp[n][m] = ans;
    return dp[n][m];
}

signed main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    tarzan;
    int test = 1;
    //cin >> test;
    while(test--) {
        memset(dp, -1, sizeof dp);
        int n;
        cin >> n;

        int a[n];
        for(int i = 0; i < n; i++) {
            cin >> a[i];

            if(a[i] == 0) zeros.push_back(i);
            else ones.push_back(i);
        }

        int O = ones.size(), Z = zeros.size();
        cout << solve(O, Z) << '\n';
    }
    return 0;
}
