0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-25 19:52:02 +00:00
OI-codes/Luogu/P1678/P1678.cpp

27 lines
534 B
C++
Raw Normal View History

2020-12-30 11:27:59 +00:00
#include <bits/stdc++.h>
using namespace std;
int m, n, q, ans, a[100005];
int main() {
cin >> m >> n;
for (int i = 0; i < m; i++) {
cin >> a[i];
}
sort(a, a + m);
for (int i = 0; i < n; i++) {
cin >> q;
int p = lower_bound(a, a + m, q) - a;
if (p == m) {
ans += q - a[m - 1];
2021-11-19 09:01:13 +00:00
} else if (p == 0) {
2020-12-30 11:27:59 +00:00
ans += a[0] - q;
2021-11-19 09:01:13 +00:00
} else {
2020-12-30 11:27:59 +00:00
ans += min(abs(a[p] - q), abs(q - a[p - 1]));
}
}
cout << ans << endl;
return 0;
}