金城武 @ 2020-07-10 15:20:45
#include <bits/stdc++.h>
using namespace std;
long long m,n,sum=0;
long long a[100005],b[100005],bb[100005],u[100005];
void binarysearch(long long x,long long f)
{
long long left=0,right=m-1;
while(left<=right)
{ long long mid=(left+right)/2;
if(x==a[mid])
{
bb[f]=a[mid];
u[f]=0;
return;
}
else if(x<a[mid])
{
right=mid-1;
}
else
{
left=mid+1;
};
}
//cout<<left<<" "<<right<<endl;
if(right==-1)
{
u[f]=a[0]-b[f];
//cout<<u[f]<<endl;
}
if(left==m)
{
u[f]=a[m-1]-b[f];
}
else
{
for (long long j=right;j<=left;j++)
{
if (abs(a[j]-b[f])<u[f])
{
u[f]=abs(a[j]-b[f]);
//cout<<u[f]<<endl;
}
}
}
return;
}
int main ()
{ memset(u,9999999,sizeof(u));
memset(bb,0,sizeof(bb));
cin>>m>>n;
for (int i=0;i<m;i++)
cin>>a[i];
for (int i=1;i<=n;i++)
{
cin>>b[i];
}
sort(a,a+m);
for (int i=1;i<=n;i++)
{
binarysearch(b[i],i);
}
for (int i=1;i<=n;i++)
{
sum+=u[i];
}
cout<<sum;
}