#include using namespace std; #define bokusunny ios::sync_with_stdio(false), cin.tie(nullptr); #include using namespace atcoder; void solve() { int H, W; cin >> H >> W; using Grid = vector>; Grid grid(H, vector(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> grid[i][j]; } } vector R(H); for (int i = 0; i < H; i++) cin >> R[i]; vector C(W); for (int i = 0; i < W; i++) cin >> C[i]; using Cap = long long; mf_graph G(H + W + 2); const int s = H + W, t = H + W + 1; for (int h = 0; h < H; h++) { G.add_edge(s, h, R[h]); long long tot = 0; for (auto &g : grid[h]) tot += g; G.add_edge(h, t, tot); } for (int w = 0; w < W; w++) { G.add_edge(s, H + w, C[w]); for (int h = 0; h < H; h++) { G.add_edge(H + w, h, grid[h][w]); } } auto ans = accumulate(R.begin(), R.end(), 0LL) + accumulate(C.begin(), C.end(), 0LL) - G.flow(s, t); cout << ans << endl; } int main() { bokusunny; solve(); return 0; }