#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
template< typename T >
struct BinaryIndexedTree {
  vector< T > data;
  BinaryIndexedTree(int sz) {
    data.assign(++sz, 0);
  }
  T sum(int k) {
    T ret = 0;
    for(++k; k > 0; k -= k & -k) ret += data[k];
    return (ret);
  }
  void add(int k, T x) {
    for(++k; k < data.size(); k += k & -k) data[k] += x;
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n; cin >> n;
  vector<int> a(n);
  vector<P> v(n);
  for(int i=0;i<n;++i) {
    cin >> a[i];
    v[i].first = a[i];
    v[i].second = i;
  }
  BinaryIndexedTree<int> bit(n+1);
  for(int i=0;i<n;++i) {
    bit.add(i, 1);
  }
  sort(v.begin(), v.end());
  vector<int> nowidx;
  int now = v[0].first;
  nowidx.emplace_back(v[0].second);
  bit.add(v[0].second, -1);
  ll cnt = 0;
  for(int i=1;i<n;++i) {
    int nxt = v[i].first, idx = v[i].second;
    if(nxt == now) {
      bit.add(idx, -1);
      nowidx.emplace_back(idx);
    } else {
      for(int j=0;j<(int)(nowidx.size());++j) {
        cnt += min(bit.sum(nowidx[j]), bit.sum(n-1) - bit.sum(nowidx[j]));
      }
      nowidx.clear();
      now = nxt;
      nowidx.emplace_back(idx);
      bit.add(idx, -1);
    }
  }
  for(int i=0;i<(int)(nowidx.size());++i) {
    cnt += min(bit.sum(nowidx[i]), bit.sum(n-1) - bit.sum(nowidx[i]));
  }
  cout << cnt << endl;
}

