#include <bits/stdc++.h>
#define MOD 1000000007
typedef long long ll;
using namespace std;

class SegmentTree{
public:
    int n;
    vector<int> tree;
    SegmentTree(int size){
        n=1;
        while(n<size) n*=2;
        tree.resize(n,0);
    }

    SegmentTree(vector<int> v){
        n=1;
        while(n<v.size()) n*=2;
        tree.resize(2*n-1,0);
        for(int i=0;i<v.size();i++) tree[i+n-1]=v[i];
        for(int i=n-2;i>=0;i--) tree[i]=tree[2*i+1]+tree[2*i+2];
    }

    void update(int index,int val){
        index+=n-1;

        tree[index]=val;
        while(index>0){
            index=(index-1)/2;
            tree[index]=tree[2*index+1]+tree[2*index+2];
        }
    }

    int getSum(int a,int b,int k=0,int l=0,int r=-1){
        if(r<0) r=n;

        if(r<=a||b<=l) return 0;
        else if(a<=l&&r<=b) return tree[k];
        else{
            int lval=getSum(a,b,2*k+1,l,(l+r)/2);
            int rval=getSum(a,b,2*k+2,(l+r)/2,r);
            return lval+rval;
        }
    }
};


int main(){
  int N;
  cin>>N;
  vector<int> A(N);
  for(int i=0;i<N;i++) cin>>A[i];

  vector<pair<int,int>> v;
  for(int i=0;i<N;i++) v.push_back(make_pair(A[i],i));
  sort(v.begin(),v.end());

  ll ans=0;
  SegmentTree seg(vector<int> (N,1));
  int tar=0;
  for(int i=0;i<N;i++){
      int now=v[i].second;
      while(tar<N&&v[tar].first==v[i].first){
          int idx=v[tar].second;
          seg.update(idx,0);
          tar++;
      }
      int lval=seg.getSum(0,now);
      int rval=seg.getSum(now+1,N);
      ans+=min(lval,rval);
  }

  cout<<ans<<endl;


  return 0;
}

