#include #include using namespace std; using namespace atcoder; #define rep(i,n)for (int i = 0; i < int(n); ++i) #define rrep(i,n)for (int i = int(n)-1; i >= 0; --i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template void chmax(T& a, const T& b) {a = max(a, b);} template void chmin(T& a, const T& b) {a = min(a, b);} using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; int main() { ios::sync_with_stdio(false); cin.tie(0); ll a[2010], b[2010]; int n, m; cin >> m >> n; rep(i, m) cin >> a[i], a[i]--; rep(i, n) cin >> b[i], b[i]--; map dtoi; rep(i, m) dtoi[a[i]] = 0; rep(i, n) dtoi[b[i]] = 0; int sz = 0; VI itod; for(auto& [k, v]: dtoi) v = sz++, itod.push_back(k); for(int c = 1; c <= m; c++) { mcf_graph g(sz + 2); rep(i, sz - 1) { ll d = itod[i + 1] - itod[i]; g.add_edge(i, i + 1, m, d), g.add_edge(i + 1, i, m, d); } rep(i, m) g.add_edge(sz, dtoi[a[i]], 1, 0); rep(i, n) g.add_edge(dtoi[b[i]], sz + 1, c, 0); auto [cap, cost] = g.flow(sz, sz + 1); cout << cost << '\n'; } }