Problem p02839 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define pt(sth) cout << sth << "\n"
#define chmax(a, b)                                                            \
  {                                                                            \
    if (a < b)                                                                 \
      a = b;                                                                   \
  }
#define chmin(a, b)                                                            \
  {                                                                            \
    if (a > b)                                                                 \
      a = b;                                                                   \
  }
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
static const ll INF = 1e18;
static const ll MOD = 1e9 + 7;
static const ll MAX = 101010;

/*
 for(i=0; i<N; i++)
   cin >> a[i];
*/

ll dp[88][88][88 * (88 + 88) * 2];

int main(void) {
  ll i, j, k, l;

  ll H, W;
  cin >> H >> W;
  ll a[88][88], b[88][88], c[88][88];
  ll BD = 82 * (82 + 82);

  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++)
      cin >> a[i][j];
  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++)
      cin >> b[i][j];

  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++)
      c[i][j] = abs(a[i][j] - b[i][j]);

  memset(dp, 0, sizeof(dp));

  dp[0][0][BD + c[0][0]] = 1;
  dp[0][0][BD - c[0][0]] = 1;
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      for (k = 0; k < 85 * (85 + 85) * 2; k++) {
        if (dp[i][j][k]) {
          chmax(dp[i + 1][j][k + c[i + 1][j]], dp[i][j][k]);
          chmax(dp[i + 1][j][k - c[i + 1][j]], dp[i][j][k]);
          chmax(dp[i][j + 1][k + c[i][j + 1]], dp[i][j][k]);
          chmax(dp[i][j + 1][k - c[i][j + 1]], dp[i][j][k]);
        }
      }
    }
  }

  ll ans = INF;
  for (k = 0; k < 85 * (85 + 85) * 2; k++) {
    if (dp[H - 1][W - 1][k])
      chmin(ans, abs(k - BD));
  }

  pt(ans);
}


Fixed submission:
#include <bits/stdc++.h>
#define pt(sth) cout << sth << "\n"
#define chmax(a, b)                                                            \
  {                                                                            \
    if (a < b)                                                                 \
      a = b;                                                                   \
  }
#define chmin(a, b)                                                            \
  {                                                                            \
    if (a > b)                                                                 \
      a = b;                                                                   \
  }
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
static const ll INF = 1e18;
static const ll MOD = 1e9 + 7;
static const ll MAX = 101010;

/*
 for(i=0; i<N; i++)
   cin >> a[i];
*/

int dp[88][88][88 * (88 + 88) * 2];

int main(void) {
  ll i, j, k, l;

  ll H, W;
  cin >> H >> W;
  ll a[88][88], b[88][88], c[88][88];
  ll BD = 82 * (82 + 82);

  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++)
      cin >> a[i][j];
  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++)
      cin >> b[i][j];

  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++)
      c[i][j] = abs(a[i][j] - b[i][j]);

  memset(dp, 0, sizeof(dp));

  dp[0][0][BD + c[0][0]] = 1;
  dp[0][0][BD - c[0][0]] = 1;
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      for (k = 0; k < 85 * (85 + 85) * 2; k++) {
        if (dp[i][j][k]) {
          chmax(dp[i + 1][j][k + c[i + 1][j]], dp[i][j][k]);
          chmax(dp[i + 1][j][k - c[i + 1][j]], dp[i][j][k]);
          chmax(dp[i][j + 1][k + c[i][j + 1]], dp[i][j][k]);
          chmax(dp[i][j + 1][k - c[i][j + 1]], dp[i][j][k]);
        }
      }
    }
  }

  ll ans = INF;
  for (k = 0; k < 85 * (85 + 85) * 2; k++) {
    if (dp[H - 1][W - 1][k])
      chmin(ans, abs(k - BD));
  }

  pt(ans);
}