#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 1e5 + 10;
int n;
int a[N], b[N];
int f[N];
vector<int> c;
void ins(int x, int v) {
    while (x <= n) {
        f[x] += v;
        x += x & -x;
    }
}
int get(int x) {
    int v = 0;
    while (x > 0){
        v += f[x];
        x -= x & -x;
    }
    return v;
}
int get(int l, int r) {
    return get(r) - get(l - 1);
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n;i ++)
        scanf("%d", &a[i]), c.push_back(a[i]);
    sort(c.begin(), c.end());
    for (int i = 1; i <= n;i ++) {
        a[i] = lower_bound(c.begin(),c.end(), a[i]) - c.begin() + 1;
        ins(i, 1);
        b[a[i]] = i;
    }
    long long ans = 0;
    for (int i = n; i >= 1; i --) {
        ins(b[i], -1);
        int x = get(1, b[i]), y = get(b[i], n);
        ans += min(x, y);
    }
    printf("%lld\n", ans);
    return 0;
}