#include int ri() { int n; scanf("%d", &n); return n; } int64_t fast_one(const std::vector &a, const std::vector &b) { int n = a.size(); int m = b.size(); int used[n + 1]; for (auto &i : used) i = -1; // std::cerr << "start" << std::endl; int closest = 0; for (int i = 0; i < m; i++) { // std::cerr << i << ":" << b[i] << std::endl; while (closest + 1 < n && std::abs(b[i] - a[closest]) >= std::abs(b[i] - a[closest + 1])) closest++; if (used[closest] != -1) { int r = closest; int l = closest; while (l && used[l - 1] != -1) l--; while (r + 1 < n && used[r + 1] != -1) r++; r++; used[r] = i; // std::cerr << "conflict [" << l << ", " << r << ")" << std::endl; int64_t shift_delta = 0; for (int j = l; j <= r; j++) shift_delta += std::abs(a[j - 1] - b[used[j]]) - std::abs(a[j] - b[used[j]]); if (shift_delta < 0) { // std::cerr << " shift" << std::endl; for (int j = l; j <= r; j++) { used[j - 1] = used[j]; used[j] = -1; } } } else used[closest] = i; } int64_t res = 0; for (int i = 0; i < n; i++) if (used[i] != -1) res += std::abs(a[i] - b[used[i]]); return res; } std::vector fast(std::vector a_, std::vector b_) { std::sort(a_.begin(), a_.end()); std::sort(b_.begin(), b_.end()); std::vector a(a_.begin(), a_.end()); std::vector b(b_.begin(), b_.end()); int n = a.size(); int m = b.size(); a.insert(a.begin(), -1000000000000000000); a.insert(a.end(), 1000000000000000000); b.insert(b.begin(), -1000000000000000000); b.insert(b.end(), 1000000000000000000); std::vector res; for (int i = 1; i <= m; i++) { std::vector aa; for (auto j : a) for (int k = 0; k < i; k++) aa.push_back(j); res.push_back(fast_one(aa, b)); } return res; } int main() { int m = ri(); int n = ri(); std::vector a(m); std::vector b(n); for (auto &i : a) i = ri(); for (auto &i : b) i = ri(); auto res = fast(b, a); for (auto i : res) printf("%" PRId64 "\n", i); return 0; }