#include <bits/stdc++.h>
using namespace std;
const int nsz = 2e3, asz = 1e5;
int n, sz, a[nsz + 5], res[2][nsz + 5], ans;
map<int, int> to;
struct binary_index_tree {
  int tr[nsz + 5];
  void inline upd(int u, int val) {
    for (; u <= sz; u += u & -u) {
      tr[u] += val;
    }
  }
  int inline qry(int u) {
    int res = 0;
    for (; u; u -= u & -u) {
      res += tr[u];
    }
    return res;
  }
};
binary_index_tree tr[2];
int inline read() {
  int res = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); res *= 10, res += c ^ 48, c = getchar())
    ;
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  n = read();
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    to[a[i]] = 0;
  }
  for (__typeof(to.begin()) it = to.begin(); it != to.end(); ++it) {
    it->second = ++sz;
  }
  for (int i = 1; i <= n; ++i) {
    int id = to[a[i]];
    res[0][i] = tr[0].qry(id);
    tr[0].upd(id + 1, 1);
  }
  for (int i = n; i >= 1; --i) {
    int id = to[a[i]];
    res[1][i] = tr[1].qry(id);
    tr[1].upd(id + 1, 1);
  }
  for (int i = 1; i <= n; ++i) {
    ans += min(res[0][i], res[1][i]);
  }
  cout << ans << '\n';
}
