#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; vector Cost(H, vector(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> Cost[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]; using Cap = long long; mf_graph G(H + W + 2); const int s = H + W; const int g = H + W + 1; for (int h = 0; h < H; h++) { int cur = h; long long sum = 0; for (int w = 0; w < W; w++) sum += Cost[h][w]; G.add_edge(s, cur, sum); G.add_edge(cur, g, R[h]); } for (int w = 0; w < W; w++) { int cur = H + w; G.add_edge(s, cur, 0); G.add_edge(cur, g, C[w]); } for (int h = 0; h < H; h++) { for (int w = 0; w < W; w++) { int from = h; int to = H + w; G.add_edge(from, to, Cost[h][w]); } } long long ans = accumulate(R.begin(), R.end(), 0LL) + accumulate(C.begin(), C.end(), 0LL) - G.flow(s, g); cout << ans << endl; } int main() { bokusunny; solve(); return 0; }