#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; int main() { int m, n; cin >> m >> n; vector a(m), b(n); REP(i, m) cin >> a[i]; sort(ALL(a)); REP(i, n) cin >> b[i]; sort(ALL(b)); vector closest(m); REP(i, m) { closest[i] = lower_bound(ALL(b), a[i]) - b.begin(); if (closest[i] == n || (closest[i] > 0 && a[i] - b[closest[i] - 1] <= b[closest[i]] - a[i])) --closest[i]; } for (int k = 1; k <= m; ++k) { ll ans = 0; vector> used(n); REP(i, m) { if (used[closest[i]].size() < k) { used[closest[i]].emplace_back(i); ans += abs(b[closest[i]] - a[i]); } else { int l = closest[i] - 1, r = closest[i] + 1; while (l >= 0 && used[l].size() == k) --l; while (r < n && used[r].size() == k) ++r; ll l_cost = LINF, r_cost = LINF; if (l >= 0) { l_cost = 0; FOR(j, l, r) { if (j + 1 < n && !used[j + 1].empty()) { l_cost -= abs(b[j + 1] - a[used[j + 1].front()]); l_cost += abs(b[j] - a[used[j + 1].front()]); } } l_cost += abs(b[r == n || used[r].empty() ? r - 1 : r] - a[i]); } if (r < n) r_cost = abs(b[r] - a[i]); if (l_cost <= r_cost) { ans += l_cost; FOR(j, l, r) { if (j + 1 < n && !used[j + 1].empty()) { used[j].emplace_back(used[j + 1].front()); used[j + 1].pop_front(); } } used[r == n || used[r].empty() ? r - 1 : r].emplace_back(i); } else { ans += r_cost; used[r].emplace_back(i); } } } cout << ans << '\n'; } return 0; }