Problem p02839 (C++)

Buggy submission (status: Runtime Error):
//***Little by little one travels far***

#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (i = 0; i < n; i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it, x) for (auto it : x)
#define PI 3.14159265
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef long double ld;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

ll a[81][81];
ll b[81][81];
int main() {
  ll h, w;
  cin >> h >> w;

  forn(i, h) forn(j, w) cin >> a[i][j];

  forn(i, h) forn(j, w) cin >> b[i][j];

  ll dp[h][w][80 * (h + w) + 1];
  forn(i, h) forn(j, w) forn(k, (ll)80 * (h + w) + 1) dp[i][j][k] = 0;

  dp[0][0][abs(a[0][0] - b[0][0])] = 1;

  forn(i, h) forn(j, w) forn(k, (ll)80 * (h + w) + 1) {
    if ((i - 1) >= 0)
      dp[i][j][k] |= (dp[i - 1][j][k + abs(a[i][j] - b[i][j])] |
                      dp[i - 1][j][abs(k - abs(a[i][j] - b[i][j]))]);
    if ((j - 1) >= 0)
      dp[i][j][k] |= (dp[i][j - 1][k + abs(a[i][j] - b[i][j])] |
                      dp[i][j - 1][abs(k - abs(a[i][j] - b[i][j]))]);
    // cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl;
  }

  ll m = 80 * (h + w);
  forn(k, (ll)80 * (h + w) + 1) if (dp[h - 1][w - 1][k] == 1) m = min(m, k);
  cout << m;
}

Fixed submission:
//***Little by little one travels far***

#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (i = 0; i < n; i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it, x) for (auto it : x)
#define PI 3.14159265
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef long double ld;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

ll a[81][81];
ll b[81][81];
int main() {
  ll h, w;
  cin >> h >> w;

  forn(i, h) forn(j, w) cin >> a[i][j];

  forn(i, h) forn(j, w) cin >> b[i][j];

  bool dp[h][w][80 * (h + w) + 1];
  forn(i, h) forn(j, w) forn(k, (ll)80 * (h + w) + 1) dp[i][j][k] = 0;

  dp[0][0][abs(a[0][0] - b[0][0])] = 1;

  forn(i, h) forn(j, w) forn(k, (ll)80 * (h + w) + 1) {
    if ((i - 1) >= 0)
      dp[i][j][k] |= (dp[i - 1][j][k + abs(a[i][j] - b[i][j])] |
                      dp[i - 1][j][abs(k - abs(a[i][j] - b[i][j]))]);
    if ((j - 1) >= 0)
      dp[i][j][k] |= (dp[i][j - 1][k + abs(a[i][j] - b[i][j])] |
                      dp[i][j - 1][abs(k - abs(a[i][j] - b[i][j]))]);
    // cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl;
  }

  ll m = 80 * (h + w);
  forn(k, (ll)80 * (h + w) + 1) if (dp[h - 1][w - 1][k] == 1) m = min(m, k);
  cout << m;
}