#include #include #include #include #include #include using namespace std; int N,M,K; map,vector > >mp; main() { cin>>N>>M>>K; for(int i=0;i>A; if(iB,R; B.swap(it->second.first); R.swap(it->second.second); if(N>M)B.swap(R); if(B.size()>R.size()||N==M&&B.size()!=R.size()) { cout<<-1<LQ; multiset >RS,RSS; for(int r:R) { RS.insert(make_pair(r,0)); RSS.insert(make_pair(r+0,0)); } for(int b:B) { while(!RS.empty()&&RS.begin()->first<=b) { pairp=*RS.begin(); RS.erase(RS.begin()); RSS.erase(RSS.find(make_pair(p.first+p.second,p.second))); LQ.push(p.first-p.second); } long l=1e15,r=1e15; if(!LQ.empty())l=b-LQ.top(); if(!RSS.empty())r=RSS.begin()->first-b; if(l<=r) { ans+=l; LQ.pop(); } else { ans+=r; pairp=*RSS.begin(); RSS.erase(RSS.begin()); RS.erase(RS.find(make_pair(p.first-p.second,p.second))); if(l<(long)1e15) { RS.insert(make_pair(p.first-p.second,l-r)); RSS.insert(make_pair(p.first-p.second+l-r,l-r)); } } } } cout<