Problem p02839 (C++)

Buggy submission (status: Runtime Error):
// By Mohit Gupta
#include <bits/stdc++.h>
using namespace std;
#define printint(v) copy(begin(v), end(v), ostream_iterator<int>(cout, " "))
#define printintv(s, e) copy(s, e, ostream_iterator<int>(cout, " "))
#define forstl(i, v) for (auto &i : v)
#define forn(i, e) for (int i = 0; i < e; i++)
#define forsn(i, s, e) for (int i = s; i < e; i++)
#define rforn(i, s) for (int i = s; i >= 0; i--)
#define rforsn(i, s, e) for (int i = s; i >= e; i--)
#define clz(a) __builtin_clz(a) // count leading zeroes
#define ctz(a) __builtin_ctz(a) // count trailing zeroes
#define popc(a)                                                                \
  __builtin_popcount(a) // count set bits (for ints only diff for ll)
// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html for other in built
#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef map<int, int> m32;
#pragma GCC optimize("-O2")
const int LIM = 5e5 + 5, MOD = 1e9 + 7;
int t, n, m, k, x, y;
const int M = 160 * 80 + 1;
int dp[80][80][M];
int a[81][81];
int b[81][81];

int main() {
  IOS;
  cin >> n >> m;
  forn(i, n) forn(j, m) cin >> a[i][j];
  forn(i, n) forn(j, m) cin >> b[i][j];
  x = abs(a[0][0] - b[0][0]);
  dp[0][0][x] = 1;
  forn(i, n) {
    forn(j, m) {
      forn(k, M) {
        if (dp[i][j][k]) {
          dp[i + 1][j][k + abs(a[i + 1][j] - b[i + 1][j])] = 1;
          dp[i + 1][j][abs(k - abs((a[i + 1][j] - b[i + 1][j])))] = 1;

          dp[i][j + 1][k + abs(a[i][j + 1] - b[i][j + 1])] = 1;
          dp[i][j + 1][abs(k - abs((a[i][j + 1] - b[i][j + 1])))] = 1;
        }
      }
    }
  }
  int ans = 0;
  forn(i, M) {
    if (dp[n - 1][m - 1][i]) {
      ans = i;
      break;
    }
  }
  cout << ans << ln;
}

Fixed submission:
// By Mohit Gupta
#include <bits/stdc++.h>
using namespace std;
#define printint(v) copy(begin(v), end(v), ostream_iterator<int>(cout, " "))
#define printintv(s, e) copy(s, e, ostream_iterator<int>(cout, " "))
#define forstl(i, v) for (auto &i : v)
#define forn(i, e) for (int i = 0; i < e; i++)
#define forsn(i, s, e) for (int i = s; i < e; i++)
#define rforn(i, s) for (int i = s; i >= 0; i--)
#define rforsn(i, s, e) for (int i = s; i >= e; i--)
#define clz(a) __builtin_clz(a) // count leading zeroes
#define ctz(a) __builtin_ctz(a) // count trailing zeroes
#define popc(a)                                                                \
  __builtin_popcount(a) // count set bits (for ints only diff for ll)
// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html for other in built
#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef map<int, int> m32;
#pragma GCC optimize("-O2")
const int LIM = 5e5 + 5, MOD = 1e9 + 7;
int t, n, m, k, x, y;
const int M = 160 * 80 + 1;
int dp[81][81][M];
int a[81][81];
int b[81][81];

int main() {
  IOS;
  cin >> n >> m;
  forn(i, n) forn(j, m) cin >> a[i][j];
  forn(i, n) forn(j, m) cin >> b[i][j];
  x = abs(a[0][0] - b[0][0]);
  dp[0][0][x] = 1;
  forn(i, n) {
    forn(j, m) {
      forn(k, M) {
        if (dp[i][j][k]) {
          dp[i + 1][j][k + abs(a[i + 1][j] - b[i + 1][j])] = 1;
          dp[i + 1][j][abs(k - abs((a[i + 1][j] - b[i + 1][j])))] = 1;

          dp[i][j + 1][k + abs(a[i][j + 1] - b[i][j + 1])] = 1;
          dp[i][j + 1][abs(k - abs((a[i][j + 1] - b[i][j + 1])))] = 1;
        }
      }
    }
  }
  int ans = 0;
  forn(i, M) {
    if (dp[n - 1][m - 1][i]) {
      ans = i;
      break;
    }
  }
  cout << ans << ln;
}