#include #include #define rep(i, n) for (int i = 0; i < n; ++i) using namespace std; using namespace atcoder; bool chmin(int& a, int b) { if (a > b) { a = b; return true; } return false; } bool chmax(int& a, int b) { if (a < b) { a = b; return true; } return false; } using ll = long long; constexpr int INF = (int)2e9; constexpr ll INFll = (ll)9e18; constexpr int MOD = 998244353; using P = pair; struct edge { P to; int cap, rev; }; using graph = map>; void add_edge(graph &G, P from, P to, int cap) { G[from].push_back(edge{to, cap, G[to].size()}); G[to].push_back(edge{from, 0, G[from].size() - 1}); } void bfs(graph &G, map &levels, P s) { levels[s] = 0; queue

que; que.push(s); while (!que.empty()) { P p = que.front(); que.pop(); for (auto e : G[p]) { if (e.cap > 0 && !levels.count(e.to)) { levels[e.to] = levels[p] + 1; que.push(e.to); } } } } int dfs(graph &G, map &levels, map &iter, P v, P t, int f) { if (v == t) return f; for (int &i = iter[v]; i < G[v].size(); ++i) { auto &e = G[v][i]; if (e.cap > 0 && levels[v] < levels[e.to]) { int d = dfs(G, levels, iter, e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(graph &G, P s, P t) { int flow = 0; for (;;) { map levels; bfs(G, levels, s); if (!levels.count(t)) return flow; map iter; int f; while ((f = dfs(G, levels, iter, s, t, INF)) > 0) { flow += f; } } } P col(int w) { return P{-1, w}; } P row(int h) { return P{h, -1}; } int main() { int H, W; cin >> H >> W; vector> Gs(H, vector(W, 0)); for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) cin >> Gs[i][j]; vector R(H), C(W); for (int i = 0; i < H; ++i) cin >> R[i]; for (int i = 0; i < W; ++i) cin >> C[i]; graph G; P s {-1, -1}, t {H, W}; for (int h = 0; h < H; ++h) for (int w = 0; w < W; ++w) { P p = P{h,w}, r = row(h), c = col(w); add_edge(G, s, p, Gs[h][w]); add_edge(G, p, r, INF); add_edge(G, p, c, INF); } ll res = 0; for (int h = 0; h < H; ++h) { P r = row(h); add_edge(G, r, t, R[h]); res += R[h]; } for (int w = 0; w < W; ++w) { P c = col(w); add_edge(G, c, t, C[w]); res += C[w]; } res -= max_flow(G, s, t); cout << res << endl; }