#include using namespace std; using lint = long long int; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for (int i = (begin), i##_end_ = (end); i < i##_end_; i++) #define REP(i, n) FOR(i, 0, n) template ::max() / 2> struct mf_pushrelabel { int _n; struct _edge { int to, rev; Cap cap; }; std::vector> g; std::vector> pos; mf_pushrelabel(int n) : _n(n), g(n) { static_assert(INF > 0, "INF must be positive."); } int add_edge(int from, int to, Cap cap) { assert(0 <= from and from < _n); assert(0 <= to and to < _n); assert(0 <= cap); int m = int(pos.size()); pos.emplace_back(from, int(g[from].size())); int from_id = g[from].size(), to_id = g[to].size() + (from == to); g[from].push_back({to, to_id, cap}); g[to].push_back({from, from_id, Cap(0)}); return m; } std::vector dist; std::vector excess; std::vector> h2v; int max_height; void activate(int i) { h2v[dist[i]].push_back(i); if (dist[i] > max_height) max_height = dist[i]; } Cap flow(int s, int t) { assert(0 <= s and s < _n); assert(0 <= t and t < _n); assert(s != t); dist.assign(_n, 0); dist[s] = _n; excess.assign(_n, 0); excess[s] = INF, excess[t] = -INF; h2v.assign(_n * 2, {}); max_height = -1; for (auto &e : g[s]) push(s, e); while (max_height >= 0) { if (h2v[max_height].empty()) { max_height--; continue; } int i = h2v[max_height].back(); h2v[max_height].pop_back(); int dnxt = _n * 2 - 1; for (auto &e : g[i]) { if (!e.cap) continue; if (dist[e.to] == dist[i] - 1) { push(i, e); if (excess[i] == 0) break; } else { if (dist[e.to] + 1 < dnxt) dnxt = dist[e.to] + 1; } } if (excess[i] > 0) { dist[i] = dnxt; activate(i); } } return INF - excess[s]; } void push(int i, _edge &e) { Cap delta = e.cap < excess[i] ? e.cap : excess[i]; excess[i] -= delta, e.cap -= delta; excess[e.to] += delta, g[e.to][e.rev].cap += delta; if (excess[e.to] > 0 and excess[e.to] <= delta) activate(e.to); } }; int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector> G(H, vector(W)); for (auto &v : G) { for (auto &x : v) cin >> x; } vector R(H), C(W); for (auto &x : R) cin >> x; for (auto &x : C) cin >> x; lint tot = accumulate(ALL(R), 0LL) + accumulate(ALL(C), 0LL); vector EW(W); lint f1 = 0; int Z = 1 + H + W; mf_pushrelabel g(Z + 1); REP(i, H) { lint gtot = accumulate(ALL(G[i]), 0LL); lint f0 = min(gtot, R[i]); f1 += f0; g.add_edge(0, i + 1, gtot - f0); } REP(j, W) g.add_edge(H + 1 + j, Z, C[j]); REP(i, H) REP(j, W) g.add_edge(i + 1, H + 1 + j, G[i][j]); lint f2 = g.flow(0, Z); cout << tot - f1 - f2 << endl; }