#include using namespace std; using int64 = long long; int main() { int H, W; cin >> H >> W; vector< vector< int64 > > G(H, vector< int64 >(W)); for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) cin >> G[i][j]; } vector< int64 > R(H), C(W); for(int i = 0; i < H; i++) cin >> R[i]; for(int i = 0; i < W; i++) cin >> C[i]; vector< pair< int, int > > ord; for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { ord.emplace_back(i, j); } } sort(begin(ord), end(ord), [&](auto p, auto q) { return G[p.first][p.second] < G[q.first][q.second]; }); vector< int64 > row_sum(H); for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) row_sum[i] += G[i][j]; } int64 get = accumulate(begin(R), end(R), 0LL) + accumulate(begin(C), end(C), 0LL); int64 sum_cut = 0; vector< int64 > row_cap(H), col_cap(C); for(int i = 0; i < H; i++) { int64 cut = min(R[i], row_sum[i]); row_cap[i] = row_sum[i] - cut; sum_cut += cut; } for(auto &p : ord) { int64 cut = min({G[p.first][p.second], row_cap[p.first], col_cap[p.second]}); row_cap[p.first] -= cut; col_cap[p.second] -= cut; sum_cut += cut; } cout << get - sum_cut << endl; }