Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = int_fast64_t;
using ld = long double;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
const ll INF = 1LL << 60;
void solve();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  solve();
}
#define foreach(i, a) for (auto &i : a)
#define SELECTOR(_1, _2, _3, _4, SELECT, ...) SELECT
#define rep(...) SELECTOR(__VA_ARGS__, _rep2, _rep1, _rep0)(__VA_ARGS__)
#define _rep0(i, n) for (ll i = 0; i < n; ++i)
#define _rep1(i, k, n) for (ll i = k; i < n; ++i)
#define _rep2(i, k, n, d)                                                      \
  for (ll i = k; d != 0 && d > 0 ? i < n : i > n; i += d)
template <class T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template <class... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void solve() {
  ll h, w;
  cin >> h >> w;
  auto A = make_v(h, w, 0);
  auto B = make_v(h, w, 0);
  rep(i, h) rep(j, w) cin >> A[i][j];
  rep(i, h) rep(j, w) cin >> B[i][j];
  ll M = 10000;
  vector<vector<vector<bool>>> dp = make_v(h, w, M, false);
  ll x = abs(A[h - 1][w - 1] - B[h - 1][w - 1]);
  dp[h - 1][w - 1][x] = true;
  rep(i, h - 1, -1, -1) rep(j, w - 1, -1, -1) {
    ll x = abs(A[i][j] - B[i][j]);
    if (i + 1 < h) {
      rep(k, M) {
        if (dp[i + 1][j][k]) {
          dp[i][j][k + x] = true;
          dp[i][j][abs(k - x)] = true;
        }
      }
    }
    if (j + 1 < w) {
      rep(k, M) {
        if (dp[i][j + 1][k]) {
          dp[i][j][k + x] = true;
          dp[i][j][abs(k - x)] = true;
        }
      }
    }
  }
  rep(k, M) {
    if (dp[0][0][k]) {
      cout << k << "\n";
      return;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = int_fast64_t;
using ld = long double;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
const ll INF = 1LL << 60;
void solve();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  solve();
}
#define foreach(i, a) for (auto &i : a)
#define SELECTOR(_1, _2, _3, _4, SELECT, ...) SELECT
#define rep(...) SELECTOR(__VA_ARGS__, _rep2, _rep1, _rep0)(__VA_ARGS__)
#define _rep0(i, n) for (ll i = 0; i < n; ++i)
#define _rep1(i, k, n) for (ll i = k; i < n; ++i)
#define _rep2(i, k, n, d)                                                      \
  for (ll i = k; d != 0 && d > 0 ? i < n : i > n; i += d)
template <class T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template <class... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void solve() {
  ll h, w;
  cin >> h >> w;
  auto A = make_v(h, w, 0);
  auto B = make_v(h, w, 0);
  rep(i, h) rep(j, w) cin >> A[i][j];
  rep(i, h) rep(j, w) cin >> B[i][j];
  ll M = 20000;
  vector<vector<vector<bool>>> dp = make_v(h, w, M, false);
  ll x = abs(A[h - 1][w - 1] - B[h - 1][w - 1]);
  dp[h - 1][w - 1][x] = true;
  rep(i, h - 1, -1, -1) rep(j, w - 1, -1, -1) {
    ll x = abs(A[i][j] - B[i][j]);
    if (i + 1 < h) {
      rep(k, M) {
        if (dp[i + 1][j][k]) {
          dp[i][j][k + x] = true;
          dp[i][j][abs(k - x)] = true;
        }
      }
    }
    if (j + 1 < w) {
      rep(k, M) {
        if (dp[i][j + 1][k]) {
          dp[i][j][k + x] = true;
          dp[i][j][abs(k - x)] = true;
        }
      }
    }
  }
  rep(k, M) {
    if (dp[0][0][k]) {
      cout << k << "\n";
      return;
    }
  }
}