There are n armchairs, numbered from 1 to n from left to right. Some armchairs are occupied by people (at most one person per armchair), others are not. The number of occupied armchairs is not greater than n/2.

For some reason, you would like to tell people to move from their armchairs to some other ones. If the i-th armchair is occupied by someone and the j-th armchair is not, you can tell the person sitting in the i-th armchair to move to the j-th armchair. The time it takes a person to move from the i-th armchair to the j-th one is |i - j| minutes. You may perform this operation any number of times, but these operations must be done sequentially, i. e. you cannot tell a person to move until the person you asked to move in the last operation has finished moving to their destination armchair.

You want to achieve the following situation: every seat that was initially occupied must be free. What is the minimum time you need to do it?

Input

The first line contains one integer n (2 ≤ n ≤ 5000) — the number of armchairs.

The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 1). a_i = 1 means that the i-th armchair is initially occupied, a_i = 0 means that it is initially free. The number of occupied armchairs is at most n/2.

Output

Print one integer — the minimum number of minutes you have to spend to achieve the following situation: every seat that was initially occupied must be free.

Examples

Input


7
1 0 0 1 0 0 1


Output


3


Input


6
1 1 1 0 0 0


Output


9


Input


5
0 0 0 0 0


Output


0

Note

In the first test, you can perform the following sequence:

  1. ask a person to move from armchair 1 to armchair 2, it takes 1 minute; 
  2. ask a person to move from armchair 7 to armchair 6, it takes 1 minute; 
  3. ask a person to move from armchair 4 to armchair 5, it takes 1 minute. 



In the second test, you can perform the following sequence:

  1. ask a person to move from armchair 1 to armchair 4, it takes 3 minutes; 
  2. ask a person to move from armchair 2 to armchair 6, it takes 4 minutes; 
  3. ask a person to move from armchair 4 to armchair 5, it takes 1 minute; 
  4. ask a person to move from armchair 3 to armchair 4, it takes 1 minute. 



In the third test, no seat is occupied so your goal is achieved instantly.
#include <bits/stdc++.h>
using namespace std;
vector<long long int> shortestpath(
    vector<pair<long long int, long long int>> adj[], long long int V,
    long long int src) {
  priority_queue<pair<long long int, long long int>,
                 vector<pair<long long int, long long int>>,
                 greater<pair<long long int, long long int>>>
      pq;
  vector<long long int> dist(V, 9e+18);
  pq.push(make_pair(0, src));
  dist[src] = 0;
  while (!pq.empty()) {
    long long int u = pq.top().second;
    pq.pop();
    for (auto x : adj[u]) {
      long long int v = x.first;
      long long int weight = x.second;
      if (dist[v] > dist[u] + weight) {
        dist[v] = dist[u] + weight;
        pq.push(make_pair(dist[v], v));
      }
    }
  }
  return dist;
}
struct Trie {
  struct Trie *children[26];
  bool endOfWord;
};
struct Trie *getnode(void) {
  struct Trie *pnode = new Trie;
  pnode->endOfWord = false;
  for (long long int i = 0; i < 26; i++) {
    pnode->children[i] = NULL;
  }
  return pnode;
}
void insertstring(struct Trie *root, string s) {
  struct Trie *p = root;
  long long int n = s.length();
  for (long long int i = 0; i < n; i++) {
    long long int k = s[i] - 'a';
    if (!p->children[k]) {
      p->children[k] = getnode();
    }
    p = p->children[k];
  }
  p->endOfWord = true;
}
bool searchstring(struct Trie *root, string s) {
  struct Trie *p = root;
  long long int n = s.length();
  for (long long int i = 0; i < n; i++) {
    long long int k = s[i] - 'a';
    if (p->children[k] == NULL) {
      return false;
    }
    p = p->children[k];
  }
  return (p->endOfWord && p != NULL);
}
void buildsegment(long long int arr[], long long int tree[], long long int n) {
  for (long long int i = 0; i < n; i++) tree[n + i] = arr[i];
  for (long long int i = n - 1; i > 0; --i)
    tree[i] = tree[i << 1] + tree[i << 1 | 1];
}
void updatesegment(long long int p, long long int value, long long int tree[],
                   long long int n) {
  tree[p + n] = value;
  p = p + n;
  for (long long int i = p; i > 1; i >>= 1)
    tree[i >> 1] = tree[i] + tree[i ^ 1];
}
long long int querysegment(long long int l, long long int r,
                           long long int tree[], long long int n) {
  long long int res = 0;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1) res += tree[l++];
    if (r & 1) res += tree[--r];
  }
  return res;
}
long long int findpar(long long int numb, long long int par[]) {
  if (par[numb] == numb)
    return numb;
  else
    return par[numb] = findpar(par[numb], par);
}
void setpar(long long int u1, long long int v1, long long int par[]) {
  long long int s1 = findpar(v1, par);
  long long int s2 = findpar(u1, par);
  if (s1 < s2) {
    par[s2] = s1;
  } else {
    par[s1] = s2;
  }
}
long long int extended_GCD(long long int a, long long int b, long long int &x,
                           long long int &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long int x1, y1;
  long long int gcd = extended_GCD(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
}
long long int modinv(long long int a, long long int mod = 1000000007) {
  long long int x, y;
  extended_GCD(a, mod, x, y);
  if (x < 0) x += mod;
  return x;
}
long long int power(long long int a, long long int b,
                    long long int m = 1000000007) {
  a %= m;
  long long int ans = 1;
  while (b > 0) {
    if (b & 1) ans = ans * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return ans;
}
vector<long long int> nge(long long int arr[], long long int n) {
  stack<long long int> s;
  vector<long long int> ans(n, -1);
  s.push(0);
  for (long long int i = 1; i < n; i++) {
    if (s.empty()) {
      s.push(i);
      continue;
    }
    while (s.empty() == false && arr[s.top()] < arr[i]) {
      ans[s.top()] = i;
      s.pop();
    }
    s.push(i);
  }
  return ans;
}
string sumstring(string str1, string str2) {
  if (str1.length() > str2.length()) {
    string ext = str1;
    str1 = str2;
    str2 = ext;
  }
  string str = "";
  long long int n1 = str1.length(), n2 = str2.length();
  reverse(str1.begin(), str1.end());
  reverse(str2.begin(), str2.end());
  long long int carry = 0;
  for (long long int i = 0; i < n1; i++) {
    long long int sum = ((str1[i] - '0') + (str2[i] - '0') + carry);
    str.push_back(sum % 10 + '0');
    carry = sum / 10;
  }
  for (long long int i = n1; i < n2; i++) {
    long long int sum = ((str2[i] - '0') + carry);
    str.push_back(sum % 10 + '0');
    carry = sum / 10;
  }
  if (carry) str.push_back(carry + '0');
  reverse(str.begin(), str.end());
  return str;
}
void swap(long long int &a, long long int &b) {
  long long int temp = a;
  a = b;
  b = temp;
}
void dfsb(vector<vector<long long int>> &adj, long long int curind,
          long long int p) {
  for (long long int i = 0; i < adj[curind].size(); i++) {
    if (adj[curind][i] != p) {
      dfsb(adj, adj[curind][i], curind);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1, n;
  while (t--) {
    cin >> n;
    long long int a[n];
    for (long long int i = 0; i < n; i++) {
      cin >> a[i];
    }
    stack<long long int> on, zer;
    long long int ans = 0;
    vector<long long int> vis(n, 0);
    for (long long int i = 0; i < n; i++) {
      if (a[i] == 0) {
        if (on.size() == 0) {
          zer.push(i);
        } else {
          vis[on.top()] = (long long int)abs(i - on.top());
          on.pop();
        }
      } else {
        if (zer.size() == 0) {
          on.push(i);
        } else {
          vis[i] = (long long int)abs(zer.top() - i);
          zer.pop();
        }
      }
    }
    for (long long int i = n - 1; i >= 0; i--) {
      if (a[i] == 0) {
        if (on.size() == 0) {
          zer.push(i);
        } else {
          vis[on.top()] =
              min(vis[on.top()], (long long int)abs((i - on.top())));
          on.pop();
        }
      } else {
        if (zer.size() == 0) {
          on.push(i);
        } else {
          vis[i] = min(vis[i], (long long int)abs(zer.top() - i));
          zer.pop();
        }
      }
    }
    for (auto x : vis) ans += x;
    cout << ans << "\n";
  }
  return 0;
}
