#include <iostream>
#include <algorithm>
#include <vector>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;
typedef pair<int, int> P;

struct BIT {
	int n;
	int v[100001];
	void init(int n) { this->n = n; int i; rep(i, n + 1) v[i] = 0; }
	void add(int pos, int x) { for (int p = pos; p <= n; p += p & -p) v[p] += x; }
	int sum(int pos) { int ret = 0; for (int p = pos; p > 0; p -= p & -p) ret += v[p]; return ret; }	//a[1] + … + a[pos]
	int ssum(int l, int r) { return sum(r) - sum(l - 1); }	//a[l] + … + a[r] (1-indexed)
};

int n;
int a[100000];
vector<P> pa;
BIT flag;

signed main() {
	int i;
	
	cin >> n;
	rep(i, n) cin >> a[i];
	rep(i, n) pa.push_back(P(a[i], i));
	sort(pa.begin(), pa.end());
	
	flag.init(n);
	rep(i, n) flag.add(i + 1, 1);
	
	int ans = 0;
	int j = 0;
	rep(i, n) {
		int val = pa[i].first;
		int pos = pa[i].second;
		
		for (; j < n && pa[j].first <= val; j++) {
			int del_pos = pa[j].second;
			flag.add(del_pos + 1, -1);
		}
		
		int left = flag.sum(pos);
		int right = flag.ssum(pos + 2, n);
		
		ans += min(left, right);
	}
	cout << ans << endl;
	return 0;
}
