Problem p02839 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define repi(i, a, b) for (int i = (ll)(a); i < (ll)(b); i++)
#define rrepi(i, a, b) for (int i = (ll)(b); i >= (ll)(a); i--)

#define all(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll h, w;
  cin >> h >> w;
  vector<vector<ll>> a(h, vector<ll>(w));
  ll geta = 12800, sz = geta * 2 + 7;
  vector<vector<vector<ll>>> dp(h, vector<vector<ll>>(w, vector<ll>(sz)));
  auto tdp = dp;
  auto b = a;
  rep(i, h) rep(j, w) cin >> a[i][j];
  rep(i, h) rep(j, w) cin >> b[i][j];
  auto isin = [&](ll y, ll x) {
    if (y >= 0 && y < h && x >= 0 && x < w)
      return 1;
    return 0;
  };
  ll d = abs(a[0][0] - b[0][0]);

  dp[0][0][geta - d] = 1;
  dp[0][0][geta + d] = 1;

  rep(i, h) rep(j, w) {
    if (i == 0 && j == 0)
      continue;
    ll d = abs(a[i][j] - b[i][j]);
    if (isin(i - 1, j)) {
      rep(k, sz) {
        if (dp[i - 1][j][k] == 1) {
          dp[i][j][k - d] = 1;
          dp[i][j][k + d] = 1;
        }
      }
    }
    if (isin(i, j - 1)) {
      rep(k, sz) {
        if (dp[i][j - 1][k]) {
          dp[i][j][k - d] = 1;
          dp[i][j][k + d] = 1;
        }
      }
    }
  }
  ll res = 1e9 + 7;
  rep(i, sz) {
    if (dp[h - 1][w - 1][i])
      chmin(res, abs(i - geta));
  }
  cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define repi(i, a, b) for (int i = (ll)(a); i < (ll)(b); i++)
#define rrepi(i, a, b) for (int i = (ll)(b); i >= (ll)(a); i--)

#define all(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll h, w;
  cin >> h >> w;
  vector<vector<ll>> a(h, vector<ll>(w));
  ll geta = 12800, sz = geta * 2 + 7;
  vector<vector<vector<bool>>> dp(h, vector<vector<bool>>(w, vector<bool>(sz)));
  auto tdp = dp;
  auto b = a;
  rep(i, h) rep(j, w) cin >> a[i][j];
  rep(i, h) rep(j, w) cin >> b[i][j];
  auto isin = [&](ll y, ll x) {
    if (y >= 0 && y < h && x >= 0 && x < w)
      return 1;
    return 0;
  };
  ll d = abs(a[0][0] - b[0][0]);

  dp[0][0][geta - d] = 1;
  dp[0][0][geta + d] = 1;

  rep(i, h) rep(j, w) {
    if (i == 0 && j == 0)
      continue;
    ll d = abs(a[i][j] - b[i][j]);
    if (isin(i - 1, j)) {
      rep(k, sz) {
        if (dp[i - 1][j][k] == 1) {
          dp[i][j][k - d] = 1;
          dp[i][j][k + d] = 1;
        }
      }
    }
    if (isin(i, j - 1)) {
      rep(k, sz) {
        if (dp[i][j - 1][k]) {
          dp[i][j][k - d] = 1;
          dp[i][j][k + d] = 1;
        }
      }
    }
  }
  ll res = 1e9 + 7;
  rep(i, sz) {
    if (dp[h - 1][w - 1][i])
      chmin(res, abs(i - geta));
  }
  cout << res << endl;

  return 0;
}