#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 ima;

long long Resi(int N, int *A, int *B) {
    n = N;
    ll res = 0;
    for(int i=0; i<n; i++){
        niz[i] = A[i];
        niz2[i] = B[i];
        svi.pb({niz[i],i,1});
        svi.pb({niz2[i],i,2});
    }
    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;
}