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

class BIT{
    ll bit[100010] = {};
public:
    BIT(){;}
    void insert(ll k) {
        ll tmp = k;
        while (tmp < 100010) {
            bit[tmp]++;
            tmp += -tmp & tmp;
        }
    }
    ll find(ll k) {
        ll tmp = k;
        ll ret = 0;
        while (tmp > 0) {
            ret += bit[tmp];
            tmp -= -tmp & tmp;
        }
        return ret;
    }
};

ll n;
ll a[100010];
ll l[100010];
ll r[100010];

int main() {
    BIT b1, b2;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];

    for (int i = 0; i < n; i++) {
        l[i] = i - b1.find(a[i]);
        b1.insert(a[i]);
    }

    reverse(a,a+n);
    for (int i = 0; i < n; i++) {
        r[i] = i - b2.find(a[i]);
        b2.insert(a[i]);
    }
    reverse(r,r+n);

    ll ans = 0;
    for (int i = 0; i < n; i++) {
        ans += min(l[i], r[i]);
    }

    cout << ans << endl;
    return 0;
}

