#include <bits/stdc++.h>
#define MAXN 200010
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define all(x) begin(x),end(x)
#define lld long long
#define PRINT(x) cerr<<#x<<'='<<x<<endl;
using namespace std;
lld rez;
int levo[MAXN], sadrzi[MAXN], bit[2*MAXN];
void update(int idx, int val) {
    while(idx < 2*MAXN) {
        bit[idx] += val;
        idx += idx&-idx;
    }
}
int query(int idx) {
    int rez = 0;
    while(idx) {
        rez += bit[idx];
        idx -= idx&-idx;
    }
    return rez;
}
vector<pii> in;
long long Resi(int N, int *A, int *B) {
    for(int i = 0; i < N; i++) {
        in.pb({A[i], B[i]});
    }
    sort(all(in));
    for(int i = 0; i < N; i++) {
        sadrzi[in[i].fi] = query(2*MAXN-1) - query(in[i].se);
        levo[in[i].fi] = query(in[i].se) - query(in[i].fi);
        update(in[i].se, +1);
    }
    for(int i = 0; i < N; i++) {
        PRINT(levo[A[i]]);
        PRINT(sadrzi[A[i]]);
        rez += min(levo[A[i]], sadrzi[A[i]]);
    }
    return rez;
}