#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; queueRQ; multiset > >RS; for(int r:R)RQ.push(r); for(int b:B) { while(!RQ.empty()&&RQ.front()<=b) { LQ.insert(RQ.front()); RQ.pop(); } while(!RS.empty()&&RS.begin()->first<=b) { pair >p=*RS.begin(); RS.erase(RS.begin()); auto it=LQ.find(p.second.second); if(it!=LQ.end()) { LQ.erase(it); LQ.insert(p.first-p.second.first); } } long l=1e15,r=1e15; if(!LQ.empty())l=b-*--LQ.end(); if(!RQ.empty())r=RQ.front()-b; if(l<=r) { ans+=l; LQ.erase(--LQ.end()); } else { ans+=r; long p=RQ.front(); RQ.pop(); if(l<(long)1e15) { RS.insert(make_pair(p,make_pair(l-r,*--LQ.end()))); } } } } cout<