Wuzihao23 @ 2023-08-05 15:09:41
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
long long nsj[200000]={0},msj[200000]={0};
int main(){
int n,m;
long long ans=0;
cin>>m>>n;
for(int i=1;i<=m;i++)
cin>>msj[i];
for(int i=1;i<=n;i++)
cin>>nsj[i];
sort(msj,msj+m);
for(int i=1;i<=n;i++){
int l=1,r=m,o;
if(nsj[i]<=msj[1]){
ans+=msj[1]-nsj[i];
continue;
}
while(l<=r){
o=(r+l)/2;
if(msj[o]>nsj[i])
r=o-1;
else if(msj[o]<nsj[i])
l=o+1;
else
break;
}
long long a=abs(msj[o]-nsj[i]);
if(o>1)a=min(a,abs(msj[o-1]-nsj[i]));
if(o<=m)a=min(a,abs(msj[o+1]-nsj[i]));
ans+=a;
}
cout<<ans;
return 0;
}
by wjrWJR @ 2023-08-05 15:19:34
https://htsrs.top/a3fb9xrY