Problem: problem

There is a light bulb at the grid point $ (x, y) $ that meets $ 1 \ leq x \ leq h, 1 \ leq y \ leq w $.

The power supply is installed at coordinates $ (i + 0.5, j + 0.5) (1 \ leq i <h, 1 \ leq j <w, i + j $ is even) (corrected at 14:21). Coordinates $ When the power supply installed at (i + 0.5, j + 0.5) $ is turned on, the coordinates $ (i, j), (i + 1, j), (i, j + 1), (i + 1) , j + 1) $ 4 $ light bulbs in $ glow.

If you have more than $ 1 $ connected to a power supply with a light bulb on $ (i, j) $, you get $ B_ {i, j} $ cleanliness. You have the total cleanliness. Also, if you turn on each power supply, it costs $ W $ for electricity per $ 1 $.

Turn on some power to maximize "total income-total electricity bill".



output

Output the maximum value of "total income-total electricity bill". Also, output a line break at the end.

Example

Input

4 4 10
100 100 100 100
100 100 100 100
1 100 100 1
1 1 1 1


Output

970

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
//#define DEBUG_MODE
#define endl '\n'
#ifdef DEBUG_MODE
#define DEBUG(X) debug_func(X, #X)
#define DEBUG_ENDL endl
#define DEBUG_SEPARATOR_LINE cout<<"=================\n"
#else
#define DEBUG(X) 0
#define DEBUG_ENDL 0
#define DEBUG_SEPARATOR_LINE 0
#endif
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define DEBUG_ENDL_S(S) ((S).size() ? "\n" : "")
template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T, typename U> using P = pair<T, U>;
using ll = int64_t;
using PLL = P<ll, ll>;
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename Head, typename... Tail> const Head& var_min(const Head &head, const Tail&... tail) { return min(head, var_min(tail...)); }
template <typename Head, typename... Tail> const Head& var_max(const Head &head, const Tail&... tail) { return max(head, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
void debug_func_preffix(const string &s) { if(s.size()) cout << s << " = "; }
template <typename T>
void debug_func(const T &t, const string &s = "") {
    debug_func_preffix(s);
    cout << t << DEBUG_ENDL_S(s);
}
template <typename T, typename U>
void debug_func(const P<T, U> &p, const string &s = "") {
    debug_func_preffix(s);
    cout << "(";
    debug_func(p.first);
    cout << ", ";
    debug_func(p.second);
    cout << ")" << DEBUG_ENDL_S(s);
}
template <typename T>
void debug_func(const V<T> &v, const string &s = "") {
    for(ll i = 0; i < v.size(); i++) {
        string t = s + "[" + to_string(i) + "]";
        debug_func(v[i], t);
    }
}

struct Edge{
    ll to, cap, rev_index;
};

class FordFulkerson {
    private:
        vector<vector<Edge>> v;
        ll n;
        bool *used;
        const ll INF = numeric_limits<ll>::max();

        int dfs(ll now, ll goal, ll f){
            if(now == goal) return f;
            used[now] = 1;
            for(Edge &e : v[now]){
                if(used[e.to]){
                    continue;
                }
                if(e.cap <= 0){
                    continue;
                }
                ll d = dfs(e.to, goal, min(f, e.cap));
                if(d > 0){
                    e.cap -= d;
                    v[e.to][e.rev_index].cap += d;
                    return d;
                }
            }
            return 0;
        }

    public:
        FordFulkerson(const vector<vector<pair<ll, ll>>> &v){
            this->n = v.size();
            for(int i = 0; i < n; i++){
                this->v.push_back(vector<Edge>(0));
            }
            for(int i = 0; i < n; i++){
                for(auto e : v[i]){
                    this->v[i].push_back((Edge){e.first, e.second, (ll)this->v[e.first].size()});
                    this->v[e.first].push_back((Edge){i, 0, (ll)this->v[i].size() - 1});
                }
            }
            used = new bool[n];
        }

        ll max_flow(ll start, ll goal){
            ll ret = 0;
            while(1){
                memset(used, 0, n);
                ll add = dfs(start, goal, INF);
                if(add == 0){
                    return ret;
                }
                ret += add;
            }
        }
};

struct Node {
    ll h, w, b;
};

int main() {
    map<PLL, ll> ID;
    ll h, w, W;
    cin >> h >> w >> W;
    VV<ll> B(h, V<ll>(w));
    for(V<ll> &v : B) for(ll &e : v) cin >> e;
    V<Node> odds, evens;
    ll ans = 0;
    for(ll i = 1; i < h; i++) {
        for(ll j = 1; j < w; j++) {
            if((i + j) % 2 == 1) continue;
            ll bsum = 0;
            bsum += B[i - 1][j - 1];
            bsum += B[i - 1][j];
            bsum += B[i][j - 1];
            bsum += B[i][j];
            if(bsum > W) {
                ans += bsum - W;
                DEBUG(PLL(i, j));
                (i & 1 ? odds : evens).push_back(Node{i, j, bsum - W});
                ll tmp = ID.size();
                ID[PLL(i, j)] = tmp + 1;
            }
        }
    }
    ll start = 0, goal = ID.size() + 1;
    VV<PLL> edges(goal + 1);
    for(const auto &e : odds) {
        ll id = ID[PLL(e.h, e.w)];
        edges[start].emplace_back(id, 0);
        edges[id].emplace_back(goal, e.b);
    }
    for(const auto &e : evens) {
        ll id = ID[PLL(e.h, e.w)];
        edges[start].emplace_back(id, e.b);
        edges[id].emplace_back(goal, 0);
    }
    for(const auto &o : odds) {
        for(const auto &e : evens) {
            if(abs(o.h - e.h) == 1 && abs(o.w - e.w) == 1) {
                ll cost = B[min(o.h, e.h)][min(o.w, e.w)];
                ll ido = ID[PLL(o.h, o.w)];
                ll ide = ID[PLL(e.h, e.w)];
                edges[ide].emplace_back(ido, cost);
            }
        }
    }
    cout << ans - FordFulkerson(edges).max_flow(start, goal) << endl;
    return 0;
}