#include #include using namespace std; using ll = long long; template istream& operator >> (istream& is, vector& vec) { for(T& x : vec) is >> x; return is; } int main(){ ios::sync_with_stdio(false); cin.tie(0); int h, w; cin >> h >> w; vector> G(h, vector(w)); vector R(h), C(w); cin >> G >> R >> C; atcoder::mf_graph g(h + w + 2); int src = h + w, sink = src + 1; ll ans = 0; for(int y = 0; y < h; y++){ ll s = accumulate(G[y].begin(), G[y].end(), 0ll); ll mn = min((ll)(R[y]), s); ans += R[y] - mn; g.add_edge(src, y, s - mn); for(int x = 0; x < w; x++){ g.add_edge(y, x + h, G[y][x]); } } for(int x = 0; x < w; x++){ g.add_edge(x + h, sink, C[x]); ans += C[x]; } ans -= g.flow(src, sink); cout << ans << '\n'; }