// Exported by Exporter.exe // Included from A.cpp #include using namespace std; #define PB push_back #define F first #define S second #define MP make_pair #define MTP make_tuple #define R Read #define RD Read_Digit #define RP Read_P #define RL Read_Loop #define RLD Read_Loop_Digit #define RLP Read_Loop_P #ifdef ONLINE_JUDGE #define Debug(x) ; #define Debugln(x) ; #define Debug_Array(n,x) ; #define Debugln_Array(n,x) ; #define NL ; #else #define Debug(x) printf("%s :", (#x)); _Debug(x) #define Debugln(x) printf("%s :", (#x)); _Debugln(x) #define Debug_Array(n,x) printf("%s :", (#x)); _Debug_Array((n), (x)) #define Debugln_Array(n,x) printf("%s :", (#x)); _Debugln_Array((n), (x)) #define NL printf("\n") #endif typedef long long int ll; typedef unsigned long long int ull; constexpr int kN = int(2E3 + 10); // constexpr int kMod = 998244353; // constexpr int kMod = int(1E9 + 7); // constexpr int kInf = 0x3f3f3f3f; constexpr ll kInf = 0x3f3f3f3f3f3f3f3f; // constexpr double kPi = acos(-1); // constexpr double kEps = 1E-9; // Included from C:\Users\ianli\Desktop\CP\template\Various\Fast_IO\Fast_IO.cpp // --- Get --- static inline char Get_Raw_Char() { static char buf[1 << 16], *p = buf, *end = buf; if (p == end) { if ((end = buf + fread(buf, 1, 1 << 16, stdin)) == buf) return '\0'; p = buf; } return *p++; } // --- Read --- template static inline void Read_P(T &n) { static_assert(is_integral::value); char c; while (!isdigit(c = Get_Raw_Char())) ; n = int(c - '0'); while (isdigit(c = Get_Raw_Char())) n = n * 10 + int(c - '0'); return ; } template static inline void Read(T &n) { static_assert(is_integral::value); char c; bool neg = false; while (!isdigit(c = Get_Raw_Char())) if (c == '-') neg = true; n = int(c - '0'); while (isdigit(c = Get_Raw_Char())) n = n * 10 + int(c - '0'); if (neg) n = -n; return ; } template static inline void Read_Digit(T &n) { static_assert(is_integral::value); char c; while (!isdigit(c = Get_Raw_Char())) ; n = int(c - '0'); return ; } // --- Read multiple --- template static inline void Read(T &n, Targs&... Fargs) {Read(n); return Read(Fargs...);} template static inline void Read_Digit(T &n, Targs&... Fargs) {Read_Digit(n); return Read_Digit(Fargs...);} template static inline void Read_P(T &n, Targs&... Fargs) {Read_P(n); return Read_P(Fargs...);} // --- Read Loop --- template static inline void Read_Loop_i(int i, T *a) {return Read(a[i]);} template static inline void Read_Loop_i(int i, T *a, Targs*... Fargs) {Read(a[i]); return Read_Loop_i(i, Fargs...);} template static inline void Read_Loop(int n, Targs*... Fargs) {for (int i = 1; i <= n; i++) Read_Loop_i(i, Fargs...);} template static inline void Read_Loop_Digit_i(int i, T *a) {return Read_Digit(a[i]);} template static inline void Read_Loop_Digit_i(int i, T *a, Targs*... Fargs) {Read_Digit(a[i]); return Read_Loop_Digit_i(i, Fargs...);} template static inline void Read_Loop_Digit(int n, Targs*... Fargs) {for (int i = 1; i <= n; i++) Read_Loop_Digit_i(i, Fargs...);} template static inline void Read_Loop_P_i(int i, T *a) {return Read_P(a[i]);} template static inline void Read_Loop_P_i(int i, T *a, Targs*... Fargs) {Read_P(a[i]); return Read_Loop_P_i(i, Fargs...);} template static inline void Read_Loop_P(int n, Targs*... Fargs) {for (int i = 1; i <= n; i++) Read_Loop_P_i(i, Fargs...);} // --- Float --- template static inline void Read(T &n) { char c; bool neg = false; while (!isdigit(c = Get_Raw_Char())) if (c == '-') neg = true; n = int(c - '0'); while (isdigit(c = Get_Raw_Char())) n = n * 10 + int(c - '0'); int cnt = 0; if (c == '.') { while (isdigit(c = Get_Raw_Char())) { n = n * 10 + int(c - '0'); cnt++; } } while (cnt++ < mul) n = n * 10; if (neg) n = -n; return ; } template static inline void Read_P(T &n) { char c; while (!isdigit(c = Get_Raw_Char())) ; n = int(c - '0'); while (isdigit(c = Get_Raw_Char())) n = n * 10 + int(c - '0'); int cnt = 0; if (c == '.') { while (isdigit(c = Get_Raw_Char())) { n = n * 10 + int(c - '0'); cnt++; } } while (cnt++ < mul) n = n * 10; return ; } template static inline void Read(T &n, Targs&... Fargs) {Read(n); return Read(Fargs...);} template static inline void Read_P(T &n, Targs&... Fargs) {Read_P(n); return Read_P(Fargs...);} // --- init --- inline void IOS() {ios::sync_with_stdio(false); cin.tie(0);} inline void Freopen(const char *in, const char *out) {freopen(in, "r", stdin); freopen(out, "w", stdout);} // --- Output --- template void Print(T x) { if (x < 0) { printf("-"); x = -x; } if (x == 0) printf("0"); else { static int val[100]; int idx = -1; while (x) { val[++idx] = x % 10; x /= 10; } while (idx >= 0) printf("%d", val[idx--]); } } // End of C:\Users\ianli\Desktop\CP\template\Various\Fast_IO\Fast_IO.cpp // Included from C:\Users\ianli\Desktop\CP\template\Various\Useful_Functions\Useful_Functions.cpp template inline void sort(vector &v) {return sort(v.begin(), v.end());} template inline void sort_r(vector &v) {return sort(v.begin(), v.end(), greater());} template inline void reverse(vector &v) {return reverse(v.begin(), v.end());} template inline void Merge(vector &a, vector &b, vector &c) { if (c.size() < a.size() + b.size()) c.resize(a.size() + b.size()); merge(a.begin(), a.end(), b.begin(), b.end(), c.begin()); return ; } template inline void Discrete(vector &v) {sort(v); v.resize(unique(v.begin(), v.end()) - v.begin());} template using PQ = priority_queue; template using PQ_R = priority_queue, greater>; template inline T ABS(T n) {return n >= 0 ? n : -n;} template inline T gcd(T a, T b) {return b ? gcd(b, a % b) : a;} template inline T lcm(T a, T b) {return a * b / gcd(a, b);} template inline T min(T a, T b, T c, Targs... args) {return min(a, min(b, c, args...));} template inline T max(T a, T b, T c, Targs... args) {return max(a, max(b, c, args...));} template inline void chmin(T &a, T b) {a = min(a, b); return ;} template inline void chmax(T &a, T b) {a = max(a, b); return ;} template inline void chmin(T &a, T b, T c, Targs... args) {a = min(a, min(b, c, args...)); return ;} template inline void chmax(T &a, T b, T c, Targs... args) {a = max(a, max(b, c, args...)); return ;} template inline int Digit_Sum(T a) { int ans = 0; while (a) { ans += a % 10; a /= 10; } return ans; } template inline int Num_Length(T a) { int ans = 1; while (a /= 10) ans++; return ans; } // End of C:\Users\ianli\Desktop\CP\template\Various\Useful_Functions\Useful_Functions.cpp // Included from C:\Users\ianli\Desktop\CP\template\Various\Debug\Debug.cpp void _print(int x) {printf("%d", x);} void _print(long long int x) {printf("%lld", x);} void _print(double x) {printf("%lf", x);} template void _print(T x) {return x.out();} template void _print(pair x) {printf("("); _print(x.first); printf(", "); _print(x.second); printf(")");} template void _Debug(T x) {_print(x); printf("\n");} template void _Debug(vector v) { if (v.empty()) printf(" empty"); else for (T i : v) printf(" "), _print(i); printf("\n"); } template void _Debug(priority_queue pq) { if (pq.empty()) printf(" empty"); else { while (!pq.empty()) { printf(" "); _print(pq.top()); pq.pop(); } } printf("\n"); } template void _Debug(queue q) { if (q.empty()) printf(" empty"); else { while (!q.empty()) { printf(" "); _print(q.front()); q.pop(); } } printf("\n"); } template void _Debug(stack st) { if (st.empty()) printf(" empty"); else { while (!st.empty()) { printf(" "); _print(st.top()); st.pop(); } } printf("\n"); } template void _Debug(deque dq) { if (dq.empty()) printf(" empty"); else { while (!dq.empty()) { printf(" "); _print(dq.front()); dq.pop_front(); } } printf("\n"); } template void _Debugln(vector v) { if (v.empty()) printf(" empty\n"); else { for (T i : v) printf("\n"), _print(i); printf("\n"); } } template void _Debugln(priority_queue pq) { if (pq.empty()) printf(" empty"); else { while (!pq.empty()) { printf("\n"); _print(pq.top()); pq.pop(); } } printf("\n"); } template void _Debugln(queue q) { if (q.empty()) printf(" empty"); else { while (!q.empty()) { printf("\n"); _print(q.front()); q.pop(); } } printf("\n"); } template void _Debugln(stack st) { if (st.empty()) printf(" empty"); else { while (!st.empty()) { printf("\n"); _print(st.top()); st.pop(); } } printf("\n"); } template void _Debugln(deque dq) { if (dq.empty()) printf(" empty"); else { while (!dq.empty()) { printf("\n"); _print(dq.front()); dq.pop_front(); } } printf("\n"); } template void _Debug_Array(int n, const T *x) {for (int i = 1; i <= n; i++) printf(" "), _print(x[i]); printf("\n");} template void _Debugln_Array(int n, const T *x) {printf("\n"); for (int i = 1; i <= n; i++) _print(x[i]), printf("\n");} // End of C:\Users\ianli\Desktop\CP\template\Various\Debug\Debug.cpp // Included from C:\Users\ianli\Desktop\CP\template\Flow\MCMF\Atcoder.cpp namespace atcoder { namespace internal { template struct csr { std::vector start; std::vector elist; explicit csr(int n, const std::vector>& edges) : start(n + 1), elist(edges.size()) { for (auto e : edges) start[e.first + 1]++; for (int i = 1; i <= n; i++) start[i] += start[i - 1]; auto counter = start; for (auto e : edges) elist[counter[e.first]++] = e.second; } }; } template struct MCMF { public: MCMF() {} explicit MCMF(int n) : _n(n) {} void init(int n) {_n = n; return _edges.clear();} void AddEdge(int from, int to, Cap cap, Cost cost) {return _edges.push_back(Edge(from, to, cap, cost));} struct Edge { int from, to; Cap cap, flow; Cost cost; Edge(int a, int b, Cap f, Cost c) : from(a), to(b), cap(f), flow(0), cost(c) {} }; Edge GetEdge(int i) const {return _edges[i];} std::vector Edges() const {return _edges;} std::pair flow(int s, int t) {return flow(s, t, std::numeric_limits::max());} std::pair flow(int s, int t, Cap flow_limit) {return slope(s, t, flow_limit).back();} std::vector> slope(int s, int t) {return slope(s, t, std::numeric_limits::max());} std::vector> slope(int s, int t, Cap flow_limit) { int m = int(_edges.size()); std::vector edge_idx(m); auto g = [&]() { std::vector degree(_n), redge_idx(m); std::vector> elist; elist.reserve(m << 1); for (int i = 0; i < m; i++) { auto e = _edges[i]; edge_idx[i] = degree[e.from]++; redge_idx[i] = degree[e.to]++; elist.push_back(make_pair(e.from, _Edge(e.to, -1, e.cap - e.flow, e.cost))); elist.push_back(make_pair(e.to, _Edge(e.from, -1, e.flow, -e.cost))); } auto _g = internal::csr<_Edge>(_n, elist); for (int i = 0; i < m; i++) { auto e = _edges[i]; edge_idx[i] += _g.start[e.from]; redge_idx[i] += _g.start[e.to]; _g.elist[edge_idx[i]].rev = redge_idx[i]; _g.elist[redge_idx[i]].rev = edge_idx[i]; } return _g; }(); auto result = slope(g, s, t, flow_limit); for (int i = 0; i < m; i++) _edges[i].flow = _edges[i].cap - g.elist[edge_idx[i]].cap; return result; } private: int _n; std::vector _edges; struct _Edge { int to, rev; Cap cap; Cost cost; _Edge() {} _Edge(int a, int b, Cap f, Cost c): to(a), rev(b), cap(f), cost(c) {} }; std::vector> slope(internal::csr<_Edge>& g, int s, int t, Cap flow_limit) { std::vector> dual_dist(_n); std::vector prev_e(_n); std::vector vis(_n); struct Q { Cost key; int to; Q(Cost a, int b): key(a), to(b) {} bool operator < (Q r) const {return key > r.key;} }; std::vector que_min; std::vector que; auto dual_ref = [&]() { for (int i = 0; i < _n; i++) dual_dist[i].second = std::numeric_limits::max(); std::fill(vis.begin(), vis.end(), false); que_min.clear(); que.clear(); size_t heap_r = 0; dual_dist[s].second = 0; que_min.push_back(s); while (!que_min.empty() || !que.empty()) { int v; if (!que_min.empty()) { v = que_min.back(); que_min.pop_back(); } else { while (heap_r < que.size()) { heap_r++; std::push_heap(que.begin(), que.begin() + heap_r); } v = que.front().to; std::pop_heap(que.begin(), que.end()); que.pop_back(); heap_r--; } if (vis[v]) continue; vis[v] = true; if (v == t) break; Cost dual_v = dual_dist[v].first, dist_v = dual_dist[v].second; for (int i = g.start[v]; i < g.start[v + 1]; i++) { auto e = g.elist[i]; if (!e.cap) continue; Cost cost = e.cost - dual_dist[e.to].first + dual_v; if (dual_dist[e.to].second - dist_v > cost) { Cost dist_to = dist_v + cost; dual_dist[e.to].second = dist_to; prev_e[e.to] = e.rev; if (dist_to == dist_v) que_min.push_back(e.to); else que.push_back(Q(dist_to, e.to)); } } } if (!vis[t]) return false; for (int v = 0; v < _n; v++) if (vis[v]) dual_dist[v].first -= dual_dist[t].second - dual_dist[v].second; return true; }; Cap flow = 0; Cost cost = 0, prev_cost_per_flow = -1; std::vector> result = {make_pair(Cap(0), Cost(0))}; while (flow < flow_limit) { if (!dual_ref()) break; Cap c = flow_limit - flow; for (int v = t; v != s; v = g.elist[prev_e[v]].to) c = std::min(c, g.elist[g.elist[prev_e[v]].rev].cap); for (int v = t; v != s; v = g.elist[prev_e[v]].to) { auto &e = g.elist[prev_e[v]]; e.cap += c; g.elist[e.rev].cap -= c; } Cost d = -dual_dist[s].first; flow += c; cost += c * d; if (prev_cost_per_flow == d) result.pop_back(); result.push_back(make_pair(flow, cost)); prev_cost_per_flow = d; } return result; } }; } // End of C:\Users\ianli\Desktop\CP\template\Flow\MCMF\Atcoder.cpp using namespace atcoder; int a[kN], b[kN]; ll ans[kN]; deque dq[kN]; int to[kN], closest[kN]; int main() { int m, n; RP(m, n); RLP(m, a); RLP(n, b); sort(a + 1, a + m + 1); sort(b + 1, b + n + 1); for (int i = 1, j = 1; i <= m; i++) { while (j < n && ABS(a[i] - b[j]) > ABS(a[i] - b[j + 1])) j++; closest[i] = j; } for (int k = 1; k <= m; k++) { for (int i = 1; i <= n; i++) dq[i].clear(); for (int i = 1; i <= m; i++) { if (int(dq[closest[i]].size()) < k) { to[i] = closest[i]; dq[closest[i]].PB(i); } else { int l = closest[i] - 1, r = closest[i] + 1; while (l >= 1 && int(dq[l].size()) == k) l--; while (r <= n && int(dq[r].size()) == k) r++; ll ta = 0, tb = kInf; if (r <= n) chmin(tb, ABS(a[i] - b[r])); if (l == 0) ta = kInf; else { for (int j = l + 1; j < r; j++) ta += ABS(a[dq[j].front()] - b[j - 1]) - ABS(a[dq[j].front()] - b[j]); ta += ABS(a[i] - b[r - 1]); } if (tb <= ta) { dq[r].PB(i); to[i] = r; } else { for (int j = l + 1; j < r; j++) { to[dq[j].front()] = j - 1; dq[j - 1].PB(dq[j].front()); dq[j].pop_front(); } to[i] = r - 1; dq[r - 1].PB(i); } } } //Debug_Array(m, to); for (int i = 1; i <= m; i++) ans[k] += ABS(a[i] - b[to[i]]); } for (int i = 1; i <= m; i++) printf("%lld\n", ans[i]); } // End of A.cpp