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;
 
using int64 = long long;
 
template< typename T >
struct Dinic {
  struct edge {
    int to;
    T cap;
    int rev;
  };
 
  const T INF;
  vector< vector< edge > > graph;
  vector< T > min_cost;
  vector< int > iter;
 
  Dinic(int n) : INF(numeric_limits< T >::max()) {
    graph.resize(n);
  }
 
  void add_edge(int from, int to, T cap) {
    graph[from].push_back((edge) {to, cap, (int) graph[to].size()});
    graph[to].push_back((edge) {from, 0, (int) graph[from].size() - 1});
  }
 
  bool bfs(int s, int t) {
    min_cost.assign(graph.size(), -1);
    queue< int > que;
    min_cost[s] = 0;
    que.push(s);
    while(!que.empty()) {
      int p = que.front();
      que.pop();
      for(auto &e : graph[p]) {
        if(e.cap > 0 && min_cost[e.to] == -1) {
          min_cost[e.to] = min_cost[p] + 1;
          que.push(e.to);
        }
      }
    }
    return (min_cost[t] != -1);
  }
 
  T dfs(int idx, const int t, T flow) {
    if(idx == t) return (flow);
    for(int &i = iter[idx]; i < graph[idx].size(); i++) {
      edge &e = graph[idx][i];
      if(e.cap > 0 && min_cost[idx] < min_cost[e.to]) {
        T d = dfs(e.to, t, min(flow, e.cap));
        if(d > 0) {
          e.cap -= d;
          graph[e.to][e.rev].cap += d;
          return (d);
        }
      }
    }
    return (0);
  }
 
  T max_flow(int s, int t) {
    T flow = 0;
    while(bfs(s, t)) {
      iter.assign(graph.size(), 0);
      T f = 0;
      while((f = dfs(s, t, INF)) > 0) {
        flow += f;
      }
    }
    return (flow);
  }
};
 
int main() {
  int H, W, X;
  int B[150][150];
  vector< pair< int, int > > arr[150][150];
  memset(B, -1, sizeof(B));
 
  cin >> H >> W >> X;
  int64 all = 0;
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) {
      cin >> B[i * 2][j * 2];
      all += B[i * 2][j * 2];
    }
  }
  int C[150][150];
  int usagi[100000];
  memset(C, -1, sizeof(C));
  int ptr = 0, proc = 0;
  for(int i = 0; i + 1 < H; i++) {
    bool add = false;
    for(int j = 0; j + 1 < W; j++) {
      if((i + j) % 2 == 0) {
        add = true;
        C[i * 2 + 1][j * 2 + 1] = ptr++;
      }
    }
    usagi[i * 2 + 1] = proc;
    usagi[i * 2 + 1] %= 2;
    proc += add;
  }
  for(int i = 0; i < H * 2; i++) {
    bool add = false;
    for(int j = 0; j < W * 2; j++) {
      if(C[i][j] == -1) continue; // 電球
      add = true;
      for(int k = -1; k <= 1; k++) {
        for(int l = -1; l <= 1; l++) {
          int ni = k + i, nj = l + j;
          if(ni < 0 || nj < 0) continue;
          if(B[ni][nj] == -1) continue;
          arr[ni][nj].emplace_back(C[i][j], usagi[i]);
        }
      }
    }
    proc += add;
  }
 
 
  Dinic< int64 > flow(ptr + 2);
  int S = ptr, T = ptr + 1;
 
 
  for(int i = 0; i < H * 2; i++) {
    for(int j = 0; j < W * 2; j++) {
      if(C[i][j] != -1) {
        if(usagi[i] == 1) { // ONのときW失う
          flow.add_edge(C[i][j], T, X);
        } else { // 0のときW失う
          flow.add_edge(S, C[i][j], X);
        }
      }
      if(B[i][j] == -1) continue;
      if(arr[i][j].size() == 0) {
        all -= B[i][j];
      } else if(arr[i][j].size() == 1) {
        if(arr[i][j][0].second == 0) {
          flow.add_edge(arr[i][j][0].first, T, B[i][j]);
        } else {
          flow.add_edge(S, arr[i][j][0].first, B[i][j]);
        }
      } else {
        if(arr[i][j][0].second == 1) {
          swap(arr[i][j][0], arr[i][j][1]);
        }
        flow.add_edge(arr[i][j][0].first, arr[i][j][1].first, B[i][j]);
      }
    }
  }
  cout << all - flow.max_flow(S, T) << endl;
}