#include "shoes.h"
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl "\n"
using namespace std;
ll tree[800005];
void update(ll node,ll l,ll r,ll id,ll val){
if(id<l || id>r) return;
if(l==r){
tree[node]=val;
return;
}
ll mid=(l+r)/2;
update(node*2,l,mid,id,val);
update(node*2+1,mid+1,r,id,val);
tree[node]=tree[node*2]+tree[node*2+1];
}
ll query(ll node,ll l,ll r,ll L,ll R){
if(R<l || r<L) return 0;
if(L<=l && r<=R) return tree[node];
ll mid=(l+r)/2;
return query(node*2,l,mid,L,R)+query(node*2+1,mid+1,r,L,R);
}
ll count_swaps(vector<int> s){int n=s.size();ll ans=0;
unordered_map<int,vector<int> > ump;
unordered_map<int,int> mp;
for(int i=0;i<n;i++){
ump[s[i]].pb(i+1);
update(1,1,n,i+1,1);
}
for(int i=0;i<n;i++){
if(query(1,1,n,i+1,i+1)==0) continue;
int x=s[i];
int y=-x;
int e=ump[x][mp[x]];
int e1=ump[y][mp[y]];
if(e1<e) swap(e,e1);
ll g=query(1,1,n,e+1,e1-1);
if(x>0) g++;
ans+=g;
update(1,1,n,e,0);
update(1,1,n,e1,0);
mp[x]++;
mp[y]++;
}
return ans;
}