#include <bits/stdc++.h>
using namespace std;
int leftt[2000000];
int rightt[2000000];
int N;
long long avi[2000000], arr[2000000];
void upd(long long v, int i) {
  while (i <= N + 1) {
    avi[i] += v;
    i += (i & -i);
  }
}
long long get(int i) {
  long long f = 0;
  while (i > 0) {
    f += avi[i];
    i -= (i & -i);
  }
  return f;
}
int main() {
  cin >> N;
  int i, j, flef = 0, lrigh = 0;
  for (i = 1; i <= N; i++) {
    cin >> j;
    upd(1, i);
    if (j == 1) {
      rightt[i] = flef;
      flef = i;
    }
    arr[i] = j;
  }
  for (i = N; i; i--) {
    if (!arr[i]) {
      leftt[i] = lrigh;
      lrigh = i;
    }
  }
  long long total = 0;
  long long cant1 = 0, cant2 = 0;
  while (lrigh || flef) {
    if (lrigh && flef) {
      cant1 = get(lrigh);
      cant1--;
      cant2 = get(N);
      cant2 -= get(flef);
      if (cant1 < cant2) {
        total += cant1;
        upd(-1, lrigh);
        lrigh = leftt[lrigh];
      } else {
        total += cant2;
        upd(-1, flef);
        flef = rightt[flef];
      }
    } else if (lrigh) {
      cant1 = get(lrigh);
      cant1--;
      total += cant1;
      upd(-1, lrigh);
      lrigh = leftt[lrigh];
    } else {
      cant2 = get(N);
      cant2 -= get(flef);
      total += cant2;
      upd(-1, flef);
      flef = rightt[flef];
    }
  }
  cout << total << endl;
  return 0;
}
