#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<unordered_map>
#include<climits>
#include<cstdlib>
#include<cmath>
#include<string>
#include<iomanip>
#include<bitset>

using namespace std;

#define ll long long int

template<typename T>
class SegmentTree{
    int n;
    int realsize;
    T initialvalue;
    vector<T> tree;

    public:
    
    SegmentTree(int size,T initial):realsize(size),initialvalue(initial){
        n = 1;
        while(n < size)n *= 2;

        tree = vector<T>(2*n + 1,initial);
    }

    //1-indexed
    void insert(int i,T data){
        int ind = n+i-1;
        tree[ind] = data;
        ind >>= 1;
        while(ind > 0){
            tree[ind] = tree[ind*2] + tree[ind*2+1];
            ind >>= 1;
        }
    }

    void add(int i,T data){
        int ind = n+i-1;
        tree[ind] += data;
        ind >>= 1;
        while(ind > 0){
            tree[ind] = tree[ind*2] + tree[ind*2+1];
            ind >>= 1;
        }
    }

    T get(int l,int r){
        if(l > realsize || r > realsize || l <= 0 || r <= 0)return initialvalue;
        l = n+l-1;
        r = n+r-1;
        if(l == r)return tree[l];
        int prel = l;
        int prer = r;
        T lnum = tree[l];
        T rnum = tree[r];
        l >>= 1;
        r >>= 1;
        while(l != r){
            if(l*2 == prel){
                lnum += tree[l*2+1];
            }
            if(r*2+1 == prer){
                rnum += tree[r*2];
            }
            prel = l;
            prer = r;
            l >>= 1;
            r >>= 1;
        }
        return lnum+rnum;
    }
};

ll const MOD = 1000000007;
ll const INF = (long long int)1 << 61;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll n;
    cin >> n;
    vector<pair<ll,ll>> a;
    for(int i = 0; i < n; i++){
        ll t;
        cin >> t;
        a.push_back(make_pair(t,i));
    }

    vector<pair<ll,ll>> asc = a;
    sort(asc.begin(),asc.end());

    SegmentTree<ll> st = SegmentTree<ll>(n,0);

    int idx = 0;

    ll ans = 0;

    while(idx < n){
        deque<ll> dq;
        ll t = asc[idx].first;
        dq.push_back(asc[idx].second);
        idx++;
        while(idx < n && asc[idx].first == t){
            dq.push_back(asc[idx].second);
            idx++;
        }
        while(!dq.empty()){
            ll fr = dq.front();
            ll bc = dq.back();
            ll frleft = fr - st.get(1,fr);
            ll frright = n-fr-1 - st.get(fr+2,n);
            ll bcleft = bc - st.get(1,bc);
            ll bcright = n-bc-1 - st.get(bc+2,n);
            ll frm =min(frleft,frright);
            ll bcm = min(bcleft,bcright);
            if(frm > bcm){
                ans += bcm;
                st.add(bc+1,1);
                dq.pop_back();
            }else{
                ans += frm;
                st.add(fr+1,1);
                dq.pop_front();
            }
        }
    }

    cout << ans << endl;

    
    return 0;
}
