#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];
vector<ll>tren;
int mm = 200005;
int ima;
int bit[200005];
int query(int x){
    int res = 0;
    for(int i=x; i>0; i -= (i&-i)){
        res += bit[i];
    }
    return res;
}
void add(int x){
    for(int i=x; i<=mm; i += (i&-i)){
        bit[i]++;
    }
}
int poc = 100001;
int kraj = 100002;
int kutije[300005];
int tr;
int praviniz[200005];
int seg[400005];
int gde[200005];
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 segq(int l, int r, int ind, int levo, int desno){
    if(r<levo || r>desno)return 0;
    if(l >= levo && r<= desno)return seg[ind];
    int mid = (l+r)/2;
    return segq(l,mid,ind*2,levo,desno) + segq(mid+1,r,ind*2+1,levo,desno);
}
long long Resi(int N, int *A, int *B) {
    n = N;
    ll res = 0;
    ll maks = 0;
    ll mini = 1e9;
    for(int i=0; i<n; i++){
        niz[i] = A[i];
        niz[i]++;
        maks = max(maks, niz[i]);
        niz2[i] = B[i];
        niz2[i]++;
        mini = min(mini, niz2[i]);
        svi.pb({niz[i],i,1});
        svi.pb({niz2[i],i,2});
    }
    if(maks < mini){
        for(auto c:svi){
            if(c.z == 1){
                int kolko = query(niz2[c.y]);
                if(kolko > tr/2){
                    kutije[kraj] = c.y;
                    kraj++;
                }
                else{
                    kutije[poc] = c.y;
                    poc--;
                }
                add(niz2[c.y]);
            }
            if(c.z == 2)break;
        }
        for(int i=poc+1; i<kraj; i++){
            praviniz[i - poc - 1] = kutije[i];
        }
        for(int i=0; i<n; i++){
            gde[praviniz[i]] = i;
            update(0,n-1,1,1,i);
        }
        for(auto c:svi){
            if(c.z == 1)continue;
            int pp = gde[c.y];
            res += segq(0,n-1,1,0,pp)-1;
            update(0,n-1,1,0,pp);
        }
        return res;
    }
    sort(svi.begin(), svi.end(), cmp);
    for(auto c:svi){
        if(c.z == 1){
            if(tren.empty()){
                tren.pb(c.y);
                continue;
            }
            int kolko = 0;
            for(auto k:tren){
                if(niz2[k] > niz2[c.y])kolko++;
            }
            int pom = tren.size();
            pom /= 2;
            if(kolko > pom){
                /// pocetak
                vector<ll> novi;
                novi.pb(c.y);
                for(auto k:tren)novi.pb(k);
                tren.clear();
                for(auto k:novi)tren.pb(k);
            }
            else{
                /// kraj
                tren.pb(c.y);
            }
        }
        else{
            vector<ll> novi;
            for(int i=0; i<tren.size(); i++){
                if(tren[i] == c.y){
                    res += i;
                    continue;
                }
                novi.pb(tren[i]);
            }
            tren.clear();
            for(auto k:novi)tren.pb(k);
        }
    }
	return res;
}