#include #define PPque priority_queue, vector>, greater>> #define Pque priority_queue, vector>, greater>> #define pque priority_queue, greater> #define umap unordered_map #define uset unordered_set #define rep(i, s, f) for(ll i = s; i <= f; i++) #define per(i, s, f) for(ll i = s; i >= f; i--) #define all0(x) (x).begin() ,(x).end() #define all(x) (x).begin() + 1, (x).end() #define vvvi vector>> #define vvvl vector>> #define vvvc vector>> #define vvvd vector>> #define vvi vector> #define vvl vector> #define vvs vector> #define vvc vector> #define vvp vector>> #define vvb vector> #define vvd vector> #define vp vector> #define vi vector #define vl vector #define vs vector #define vc vector #define vb vector #define vd vector #define P pair #define TU tuple #define rrr(l, r) mt()%(r-l+1)+l #define ENDL '\n' #define ull unsigned long long typedef long long ll; using namespace std; //////////////////////////////////////////////////////////////////////////////////////////////////////////// //これが本当の組み込み関数ってね(笑) template T or_less(vector &A, T x) { //x以下で最大要素の添字 前提: sort済み 存在しない: -1 return distance(A.begin(), upper_bound(A.begin(), A.end(), x)-1); } template T under(vector &A, T x) { //x未満の最大要素の添字 前提: sort済み 存在しない: -1 return distance(A.begin(), lower_bound(A.begin(), A.end(), x)-1); } template T or_more(vector &A, T x) { //x以上で最小要素の添字  前提: sort済み 存在しない: N . //distanceのA.beginは添字を出すために常にA.begin() NG: A.begin() + 1 return distance(A.begin(), lower_bound(A.begin(), A.end(), x)); } template T over(vector &A, T x) { //xより大きい最小要素の添字前提: sort済み 存在しない: N return distance(A.begin(), upper_bound(A.begin(), A.end(), x)); } void compress(vector &A) {//小さい順に順位、大きい順にしたいならreverseはNG最後に変換 vector temp = A; sort(temp.begin()+1, temp.end()); for (int i = 1; i <= int(A.size()-1); i++) { A.at(i) = distance(temp.begin(), lower_bound(temp.begin()+1, temp.end(), A.at(i))); } } ////////////////////////////////////////////////////////////////////// //数学系 /////////////////////////////////////////////////////////////////////// ll round(ll x, ll i) {return ll(x + 5 * pow(10, i-1))/ll(pow(10, i)) * ll(pow(10, i));} ll nto10(string S, ll base) { ll res = 0; reverse(all0(S)); while(!S.empty()) { ll num = S.back() - '0'; if(num < 0 || num > 9) num = 9 + S.back() - 'a' + 1; res = res * base + num; S.pop_back(); } return res; } string toN(ll N, ll base) { if(N == 0) return "0"; string ans =""; ll MOD = abs(base); while(N != 0) { ll first = N % MOD; while(first < 0) first += MOD; ans += to_string(first); N -= first; N /= base; } reverse(all0(ans)); return ans; } vp insu_bunkai(ll N) { vp res; for (int i = 2; i * i <= N; i++) { ll cnt = 0; while(N % i == 0) { cnt++; N /= i; } if(cnt != 0) res.push_back(P(i, cnt)); } if(N != 1) res.push_back(P(N, 1)); return res; } vvl comb_slow(ll n, ll mod) {//計算にO(N^2) 読み取りにO(1) vvl v(n+1, vl(n+1, 0)); rep(i, 0, v.size() - 1) {v.at(i).at(0) = 1;v.at(i).at(i) = 1;} rep(i, 1, v.size()-1) { rep(j, 1, i) { v.at(i).at(j) = v.at(i-1).at(j-1) + v.at(i-1).at(j);v.at(i).at(j) %= mod;}} return v; } struct comb_fast {//must素数 vl fac; vl facinv; vl inv; ll mod_comb; comb_fast (ll n, ll mod) { mod_comb = mod; fac.assign(n+1, 1); facinv.assign(n+1, 1); inv.assign(n+1, 1); rep(i, 2, n) { fac.at(i) = fac.at(i-1) * i % mod_comb; inv.at(i) = mod_comb - inv.at(mod_comb%i) * (mod_comb/i)%mod_comb; facinv.at(i) = facinv.at(i-1) * inv.at(i) % mod_comb; } } ll get(ll n, ll k) { if(n < k) return 0; if(n < 0 || k < 0) return 0; return fac.at(n) * (facinv.at(k) * facinv.at(n-k)%mod_comb)%mod_comb; } }; double KAKUDO(double rad){return rad * 180 / 3.141592653589793;} double RAD(double kakudo) {return kakudo / 180 * 3.141592653589793;} ll extgcd (ll a, ll b, ll &x, ll &y) { if(b == 0) { x = 1;y = 0;return a;} ll d = extgcd(b, a%b, y, x); y -= a/b * x; return d; } template T ceil(T a, T b) { assert(b != 0); if(a % b == 0) return a / b; if((a <= 0 && b < 0) || (a >= 0 && b > 0)) return a/b + 1; else return a / b; } template T floor(T a, T b) { assert(b != 0); if(a % b == 0) return a / b; if((a <= 0 && b < 0) || (a >= 0 && b > 0)) return a/b; else return a/b - 1; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //グローバル変数を置くところ(情報工学意識高め) ll int_max = 1001001001; ll ll_max = 1001001001001001001LL; const double pi = 3.141592653589793; vl dx{0, 1, 0, -1, 0, 1, 1, -1, -1}; // (番兵) → ↓ ← ↑ ※ 右から時計回り ↗️ vl dy{0, 0, -1, 0, 1, 1, -1, -1, 1}; //#pragma GCC optimize ("-O3") // const ll mod = 1000000007; // const ll mod = 998244353; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// struct Edge { int to; ll cap; int rev; }; struct info { int from, to; ll cap, flow; }; struct MaxFlow { vector> G; int siz; vector> rireki; vector level; vector iter; MaxFlow(int N) { siz = N; G.resize(N+1); level.assign(N+1, -1); iter.assign(N+1, 0); } void add_edge(int a, int b, ll c) {//O(1) int Current_Ga_idx = G.at(a).size(); int Current_Gb_idx = G.at(b).size(); G.at(a).push_back(Edge{b, c, Current_Gb_idx}); G.at(b).push_back(Edge{a, 0, Current_Ga_idx});//revは、逆向きの辺の添え字 流した時、残余グラフの容量を増やす為 rireki.emplace_back(a, Current_Ga_idx); } info get_edge(int i) {//O(1) auto [a, idx_Ga] = rireki.at(i-1); int b = G.at(a).at(idx_Ga).to; int idx_Gb = G.at(a).at(idx_Ga).rev; info res; res.from = a; res.to = b; res.cap = G.at(a).at(idx_Ga).cap + G.at(b).at(idx_Gb).cap; res.flow = G.at(b).at(idx_Gb).cap; return res; } void change_edge(int i, ll new_cap, ll new_flow) {//O(1) assert(new_cap >= new_flow); auto [a, idx_Ga] = rireki.at(i-1); int b = G.at(a).at(idx_Ga).to; int idx_Gb = G.at(a).at(idx_Ga).rev; G.at(a).at(idx_Ga) = Edge{b, new_cap-new_flow, idx_Gb}; G.at(b).at(idx_Gb) = Edge{a, new_flow, idx_Ga}; } void bfs(int s) {//グラフのレベルを割り振る rep(i, 0, siz) level.at(i) = -1; queue que; level.at(s) = 0; que.push(s); while(!que.empty()) { int now = que.front(); que.pop(); for(auto [TO, CAP, REV] : G[now]) { if(CAP > 0 && level[TO] < 0) { level[TO] = level[now] + 1; que.push(TO); } } } } ll dfs(int now, int goal, ll F) {// posからgoalまでの1パス分の最大流量を返す F = 暫定流せる水量 if (now == goal) { return F; } for (int &i = iter[now]; i < G[now].size(); i++) {//行き先なくなったらここ無視されるようになって、そこがゴールでないならば0が返される。よってflow >= 1を満たさないのでその道筋は無視される。ゴールならばFが帰る。 auto &e = G[now][i]; if(e.cap > 0 && level[now] < level[e.to]) { ll d = dfs(e.to, goal, min(F, e.cap)); if(d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } ll max_flow(int s, int t) {//1パス分の最大流量を合計する O(辺 * (流量)) ll Total_Flow = 0; while (true) { bfs(s); if(level[t] < 0) return Total_Flow; rep(i, 0, siz) iter.at(i) = 0; ll F = 0; while((F = dfs(s, t, ll_max)) > 0) { Total_Flow += F; } } } }; void solve() { ll H, W; cin >> H >> W; vvl G(H+1, vl(W+1, 0)); rep(i, 1, H) { rep(j, 1, W) { cin >> G.at(i).at(j); } } vl R(H+1); vl C(W+1); rep(i, 1, H) { cin >> R.at(i); } rep(j, 1, W) { cin >> C.at(j); } MaxFlow flow(H * W + H + W + 1); ll s = 0; ll t = H * W + H + W + 1; rep(i, 1, H) { rep(j, 1, W) { flow.add_edge(s, (i - 1) * W + j, G.at(i).at(j)); } } rep(i, 1, H){ ll masu = H * W + i; flow.add_edge(masu, t, R.at(i)); } rep(j, 1, W) { ll masu = H * W + H + j; flow.add_edge(masu, t, C.at(j)); } rep(i, 1, H) { rep(j, 1, W) { flow.add_edge((i-1) * W + j , H * W + i , ll_max); } } rep(j, 1, W) { rep(i, 1, H) { flow.add_edge((i-1) * W + j, H * W + H + j, ll_max); } } ll base = 0; rep(i, 1, H) { base += R.at(i); } rep(j, 1, W) { base += C.at(j); } cout << base - flow.max_flow(s, t) << endl; } //座標を2で割った時の”切り捨て側(左側)”を求めるには 誤:(x / 2) マイナスの時!!! 正:floor(x, 2); //stringでの数字の下から1桁目は 正:S.at(N-1) 誤:S.at(0) //if(S.at(i) == 1 ← charなのに1...? // modは取りましたか...?(´・ω・`) int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); cout << fixed << setprecision(15); solve(); return 0; }