#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;

template<typename Abel> struct BIT {
    const Abel UNITY_SUM = 0;
    vector<Abel> dat;
    BIT(int n) : dat(n, UNITY_SUM) { }  // [0, n)
    inline void add(int i, Abel x) {
        while (i < dat.size()) { dat[i] += x; i |= i + 1; }
    }
    inline Abel sum(int i) {    // [0, i]
        Abel res = UNITY_SUM;
        while (i >= 0) { res += dat[i]; i = (i & (i + 1)) - 1; }
        return res;
    }
    inline Abel sum(int a, int b) { return sum(b - 1) - sum(a - 1); }   // [a, b)
    Abel operator[](int i) { return sum(i, i + 1); }
    friend ostream& operator<<(ostream& os, BIT<Abel> &bit) {
        for (int i = 0; i < bit.dat.size(); i++) os << bit[i] << " ";
        return os;
    }
};

int main() {
    int n; cin >> n;
    vector<int> a(n);
    for (auto &ai: a) cin >> ai;
    map<int, vector<int>> m;
    BIT<long long> bit(n);
    for (int i = 0; i < n; i++) {
        m[a[i]].emplace_back(i);
        bit.add(i, 1);
    }
    long long cnt = 0;
    for (auto &p: m) {
        for (int i: p.second) bit.add(i, -1);
        for (int i: p.second) cnt += min(bit.sum(0, i), bit.sum(i + 1, n));
    }
    cout << cnt << endl;
    return 0;
}
