#include <bits/stdc++.h>
#define xx first
#define yy second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll,ll>pii;
int n;
struct kut{
    int x;
    int y;
    int z;
};
bool cmp(kut a, kut b){
    return a.x < b.x;
}
vector<kut>svi;
ll niz[100005];
ll niz2[100005];
ll bit[200005];
vector<ll>tren;
int bit1[200005];
int bit2[200005];
int kutije[200500];
int kraj = 200005;
int seg[900005];
int trpoc = 100001;
int trkraj = 100002;
int maks = 200050;
int bitq1(int x){
    int res = 0;
    for(int i=x; i>0; i -= (i&-i)){
        res = res + bit1[i];
    }
    return res;
}
int bitq2(int x){
    int res = 0;
    for(int i=x; i>0; i -= (i&-i)){
        res = res + bit2[i];
    }
    return res;
}
void addq1(int x){
    for(int i=x; i<=maks; i += (i&-i)){
        bit1[i]++;
    }
}
void addq2(int x){
    for(int i=x; i<=maks; i += (i&-i)){
        bit2[i]++;
    }
}
int tr;
ll gde[100005];
void update(int l, int r, int ind, int val, int poz){
    if(l == r){
        seg[ind] = val;
        return;
    }
    int mid = (l + r) / 2;
    if(poz <= mid){
        update(l,mid,ind * 2,val,poz);
    }
    else update(mid+1,r,ind*2+1,val,poz);
    seg[ind] = seg[ind * 2] + seg[ind * 2 + 1];
}
ll query(int l, int r, int ind, int levo, int desno){
    if(r < levo || l > desno)return 0;
    if(l >= levo && r <= desno)return seg[ind];
    int mid = (l + r)/2;
    return query(l,mid,ind*2,levo,desno) + query(mid+1,r,ind*2+1,levo,desno);
}
long long Resi(int N, int *A, int *B) {
    n = N;
    ll res = 0;
    ll rilres = 0;
    for(int i=0; i<n; i++){
        niz[i] = A[i];
        niz2[i] = B[i];
        niz[i]+=2;
        niz2[i]+=2;
        svi.pb({niz[i],i,1});
        svi.pb({niz2[i],i,2});
    }
    sort(svi.begin(), svi.end(), cmp);
    //vector<int>tren;
    for(auto c:svi){
        //for(auto k:tren)cout << k << " ";
        //cout << endl << endl;
        if(c.z == 1){
            int kolko = 0;
            int vreme = niz2[c.y];
            kolko = bitq1(vreme) - bitq2(vreme);
            //cout <<".."<< kolko << endl;
            int pom = tr/2;
            if(kolko > pom){
                ///kraj
                kutije[trkraj] = 1;
                gde[c.y] = trkraj;
                update(0,kraj,1,1,trkraj);
                trkraj++;
                //tren.pb(c.y);
            }
            else{
                kutije[trpoc] = 1;
                gde[c.y] = trpoc;
                update(0,kraj,1,1,trpoc);
                trpoc--;
                //vector<int>novi;
                //novi.pb(c.y);
                //for(auto k:tren)novi.pb(k);
                //tren.clear();
                //for(auto k:novi)tren.pb(k);
            }
            addq1(niz2[c.y]);
        }
        else{
            /*vector<int>novi;
            for(int i=0; i<tren.size();i++){
                if(tren[i] == c.y){
                    rilres += i;
                    continue;
                }
                novi.pb(tren[i]);
            }
            tren.clear();
            for(auto k:novi)tren.pb(k);*/
            int pozi = gde[c.y];
            ll pom = query(0,kraj,1,0,pozi) - 1;
            res += pom;
            update(0,kraj,1,0,pozi);
            addq2(niz2[c.y]);
        }
    }
    //cout << rilres << endl;
	return res;
}