Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define reprrev(i, a, b) for (int i = b - 1; i >= a; i--) // [a, b)
#define reprev(i, n) reprrev(i, 0, n)
typedef long long ll;
typedef unsigned long long ull;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll mod = 1e9 + 7;

void chmod(ll &M) {
  if (M >= mod)
    M %= mod;
  else if (M < 0) {
    M += (abs(M) / mod + 1) * mod;
    M %= mod;
  }
}

ll modpow(ll x, ll n) {
  if (n == 0)
    return 1;
  ll res = modpow(x, n / 2);
  if (n % 2 == 0)
    return res * res % mod;
  else
    return res * res % mod * x % mod;
}

ll power(ll x, ll n) {
  if (n == 0)
    return 1;

  ll res = power(x, n / 2);
  if (n % 2 == 0)
    return res * res;
  else
    return res * res * x;
}

int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; };
int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; };

// 線分 ab の偏角 返り値は[-π, π]
double argument(const pair<double, double> &a, const pair<double, double> &b) {
  double ax = a.first, ay = a.second, bx = b.first, by = b.second;
  return atan2(by - ay, bx - ax);
}

/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int H, W;
  cin >> H >> W;
  vector<vector<int>> a(H + 5, vector<int>(W + 5, 0)),
      b(H + 5, vector<int>(W + 5, 0));
  repr(i, 1, H + 1) repr(j, 1, W + 1) cin >> a[i][j];
  repr(i, 1, H + 1) repr(j, 1, W + 1) cin >> b[i][j];

  vector<vector<vector<bool>>> dp(
      H + 5, vector<vector<bool>>(W + 5, vector<bool>(H * W * 81, false)));
  dp[1][1][abs(a[1][1] - b[1][1])] = true;

  repr(i, 1, H + 1) repr(j, 1, W + 1) rep(k, H * W * 81) if (dp[i][j][k]) {
    if (i + 1 <= H) {
      dp[i + 1][j][abs(k + a[i + 1][j] - b[i + 1][j])] = true;
      dp[i + 1][j][abs(k - a[i + 1][j] + b[i + 1][j])] = true;
    }
    if (j + 1 <= W) {
      dp[i][j + 1][abs(k + a[i][j + 1] - b[i][j + 1])] = true;
      dp[i][j + 1][abs(k + b[i][j + 1] - a[i][j + 1])] = true;
    }
  }

  rep(k, H * W * 81) if (dp[H][W][k]) {
    cout << k << endl;
    return 0;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define reprrev(i, a, b) for (int i = b - 1; i >= a; i--) // [a, b)
#define reprev(i, n) reprrev(i, 0, n)
typedef long long ll;
typedef unsigned long long ull;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll mod = 1e9 + 7;

void chmod(ll &M) {
  if (M >= mod)
    M %= mod;
  else if (M < 0) {
    M += (abs(M) / mod + 1) * mod;
    M %= mod;
  }
}

ll modpow(ll x, ll n) {
  if (n == 0)
    return 1;
  ll res = modpow(x, n / 2);
  if (n % 2 == 0)
    return res * res % mod;
  else
    return res * res % mod * x % mod;
}

ll power(ll x, ll n) {
  if (n == 0)
    return 1;

  ll res = power(x, n / 2);
  if (n % 2 == 0)
    return res * res;
  else
    return res * res * x;
}

int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; };
int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; };

// 線分 ab の偏角 返り値は[-π, π]
double argument(const pair<double, double> &a, const pair<double, double> &b) {
  double ax = a.first, ay = a.second, bx = b.first, by = b.second;
  return atan2(by - ay, bx - ax);
}

/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int H, W;
  cin >> H >> W;
  vector<vector<int>> a(H + 5, vector<int>(W + 5, 0)),
      b(H + 5, vector<int>(W + 5, 0));
  repr(i, 1, H + 1) repr(j, 1, W + 1) cin >> a[i][j];
  repr(i, 1, H + 1) repr(j, 1, W + 1) cin >> b[i][j];

  vector<vector<vector<bool>>> dp(
      H + 5, vector<vector<bool>>(W + 5, vector<bool>(H * W * 81, false)));
  dp[1][1][abs(a[1][1] - b[1][1])] = true;

  repr(i, 1, H + 1) repr(j, 1, W + 1) rep(k, (H + W) * 81) if (dp[i][j][k]) {
    if (i + 1 <= H) {
      dp[i + 1][j][abs(k + a[i + 1][j] - b[i + 1][j])] = true;
      dp[i + 1][j][abs(k - a[i + 1][j] + b[i + 1][j])] = true;
    }
    if (j + 1 <= W) {
      dp[i][j + 1][abs(k + a[i][j + 1] - b[i][j + 1])] = true;
      dp[i][j + 1][abs(k + b[i][j + 1] - a[i][j + 1])] = true;
    }
  }

  rep(k, H * W * 81) if (dp[H][W][k]) {
    cout << k << endl;
    return 0;
  }

  return 0;
}