liukelin @ 2023-10-07 20:12:41
```cpp
#include<bits/stdc++.h>
using namespace std;
long long ans,a[100005],m,n,b[100005],i,j,l,r,mid;
long long check(long long x){
l=0;r=m;
while(l<=r){
mid=(l+r)/2;
if(b[mid]>=x)r=mid-1;
else l=mid+1;
// cout<<l<<"=L "<<r<<"=r "<<mid<<"\n";
}
if(l==0){
return abs(b[l]-x);
}
else{
int q=abs(x-b[l]),p=abs(b[r]-x);/*cout<<p<<" "<<q<<"\n";*/
return min(p,q);
}
// cout<<a[l]<<" "<<x<<"\n";
}
int main(){
scanf("%lld %lld",&m,&n);
for(i=0;i<m;i++)scanf("%lld",&b[i]);
sort(b,b+m);
for(i=0;i<n;i++)scanf("%lld",&a[i]);
sort(a,a+n);
for(i=0;i<n;i++){
ans+=check(a[i]);
// printf("%lld\n",ans);
}
printf("%lld",ans);
return 0;
}
```