Problem p02839 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define rng(a) a.begin(), a.end()
#define ina(n, a)                                                              \
  cin >> n;                                                                    \
  for (int i = 1; i <= n; i++)                                                 \
  cin >> a[i]
#define sz(x) (int)(x).size()
#define se second
#define fi first
#define prev coyhhhhhhyoc
#define next sdNNNmNNNNNNNmds
#define y0 hNNNNy_yNNNNNN_sNh
#define y1 mNNNNNdtdNNNNNNtsNNm
#define yn mNNNNNNNNy___smNNNms
#define tm oooooosyysooooot
#define read tyhyt
#define rank ytmNmo
#define index yyy
#define pb push_back
#define pcnt __builtin_popcountll
#define rrep(i, a, b) for (int i = (b); i >= (a); i--)
#define rall(x, a) for (auto x : a)
#define MOD 1000000007
#define endl "\n"
typedef long long ll;
using namespace std;

const int N = 88, X = 22222;
int a[N][N], b[N][N];
int dp[N][N][2 * X];
int n, m;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  rep(i, 1, n) {
    rep(j, 1, m) { cin >> a[i][j]; }
  }
  rep(i, 1, n) {
    rep(j, 1, m) { cin >> b[i][j]; }
  }
  dp[0][1][X] = true;
  rep(i, 1, n) {
    rep(j, 1, m) {
      int diff = a[i][j] - b[i][j];
      rep(px, 0, 2 * X - 1) {
        int x = px + diff;
        if (x >= 0 && x < 2 * X && dp[i - 1][j][px] || dp[i][j - 1][px]) {
          dp[i][j][x] = true;
        }
        x = px - diff;
        if (x >= 0 && x < 2 * X && dp[i - 1][j][px] || dp[i][j - 1][px]) {
          dp[i][j][x] = true;
        }
      }
    }
  }
  int ans = 1e9;
  rep(x, 0, 2 * X - 1) {
    if (dp[n][m][x]) {
      ans = min(ans, abs(x - X));
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define rng(a) a.begin(), a.end()
#define ina(n, a)                                                              \
  cin >> n;                                                                    \
  for (int i = 1; i <= n; i++)                                                 \
  cin >> a[i]
#define sz(x) (int)(x).size()
#define se second
#define fi first
#define prev coyhhhhhhyoc
#define next sdNNNmNNNNNNNmds
#define y0 hNNNNy_yNNNNNN_sNh
#define y1 mNNNNNdtdNNNNNNtsNNm
#define yn mNNNNNNNNy___smNNNms
#define tm oooooosyysooooot
#define read tyhyt
#define rank ytmNmo
#define index yyy
#define pb push_back
#define pcnt __builtin_popcountll
#define rrep(i, a, b) for (int i = (b); i >= (a); i--)
#define rall(x, a) for (auto x : a)
#define MOD 1000000007
#define endl "\n"
typedef long long ll;
using namespace std;

const int N = 88, X = 12721;
int a[N][N], b[N][N];
int dp[N][N][2 * X];
int n, m;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  rep(i, 1, n) {
    rep(j, 1, m) { cin >> a[i][j]; }
  }
  rep(i, 1, n) {
    rep(j, 1, m) { cin >> b[i][j]; }
  }
  dp[0][1][X] = true;
  rep(i, 1, n) {
    rep(j, 1, m) {
      int diff = a[i][j] - b[i][j];
      rep(px, 0, 2 * X - 1) {
        int x = px + diff;
        if (x >= 0 && x < 2 * X && dp[i - 1][j][px] || dp[i][j - 1][px]) {
          dp[i][j][x] = true;
        }
        x = px - diff;
        if (x >= 0 && x < 2 * X && dp[i - 1][j][px] || dp[i][j - 1][px]) {
          dp[i][j][x] = true;
        }
      }
    }
  }
  int ans = 1e9;
  rep(x, 0, 2 * X - 1) {
    if (dp[n][m][x]) {
      ans = min(ans, abs(x - X));
    }
  }
  cout << ans << endl;
  return 0;
}