#include #include #include #include #include #include #include using namespace std; typedef long long ll; /* {{{ Variables */ ll H, W; vector> G; vector R, C; /* }}} */ int argmax(vector v) { int len = v.size(); ll max_val = LLONG_MIN; int max_arg = -1; for (int i = 0; i < len; ++i) { if (max_val < v[i]) { max_val = v[i]; max_arg = i; } } return max_arg; } int main(int argc, char *argv[]) { /* {{{ Input */ cin >> H >> W; for (int i = 0; i < H; ++i) { ll g; G.emplace_back(vector()); for (int j = 0; j < W; ++j) { cin >> g; G[i].emplace_back(g); } } for (int i = 0; i < H; ++i) { ll r; cin >> r; R.emplace_back(r); } for (int i = 0; i < W; ++i) { ll c; cin >> c; C.emplace_back(c); } /* }}} */ /* {{{ Output */ vector ROW(R), COL(C); for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { ROW[i] -= G[i][j]; COL[j] -= G[i][j]; } } ll cost_sum = 0; while (1) { ll r_argmax = argmax(ROW); ll c_argmax = argmax(COL); ll cost; if (ROW[r_argmax] > COL[c_argmax]) { cost = ROW[r_argmax]; ROW[r_argmax] = LLONG_MIN; for (int i = 0; i < W; ++i) { COL[i] += G[r_argmax][i]; G[r_argmax][i] = 0; } } else { cost = COL[c_argmax]; COL[c_argmax] = LLONG_MIN; for (int i = 0; i < H; ++i) { ROW[i] += G[i][c_argmax]; G[i][c_argmax] = 0; } } if (cost < 0) { break; } cost_sum += cost; } cout << cost_sum << endl; /* }}} */ return 0; }