2024/3/24 蓝桥杯
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] a = new int[n+1];//学校
int[] b = new int[m+1];//学生
for(int i=1;i<=n;i++) {
a[i] = sc.nextInt();
}
for(int i=1;i<=m;i++) {
b[i] = sc.nextInt();
}
Arrays.sort(a,1,n+1);
Arrays.sort(b,1,m+1);
long sum = 0;
for(int i=1;i<=m;i++) {//枚举学生
int l = 1, r = n;
while(l<r) {
int mid = (l+r)/2;
if(a[mid]>=b[i]) r = mid;
else l = mid+1;
}//找到第一个大于等于学生分数的学校
if(l == 1)
sum+=a[1]-b[i];
if(l>=2)
sum+=Math.min(Math.abs(a[l]-b[i]),Math.abs(a[l-1]-b[i]));
}
System.out.println(sum);
}
}