Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;

typedef pair<int, int> P;

#define bit(n) (1LL << (n))

// #define int long long

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); }

#define mod (ll)(1e9 + 7)
const long long INF = 1LL << 60;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll h, w;
  cin >> h >> w;

  vvll a(h, vll(w));
  rep(y, h) {
    rep(x, w) { cin >> a[y][x]; }
  }

  rep(y, h) {
    rep(x, w) {
      ll tmp;
      cin >> tmp;
      a[y][x] = abs(a[y][x] - tmp);
    }
  }

  const int max_val = 80 * h * w;
  using vvvi = vector<vvi>;
  vvvi dp(h, vvi(w, vi(max_val + 1)));

  using vvb = vector<vb>;
  vvb visited(h, vb(w));

  queue<P> que;
  dp[0][0][a[0][0]] = 1;
  que.emplace(0, 1);
  que.emplace(1, 0);

  while (!que.empty()) {
    auto q = que.front();
    que.pop();
    int x = q.first;
    int y = q.second;

    if (x > 0) {
      for (int i = 0; i <= max_val; i++) {
        dp[y][x][i + a[y][x]] |= dp[y][x - 1][i];
        dp[y][x][abs(i - a[y][x])] |= dp[y][x - 1][i];
      }
    }
    if (y > 0) {
      for (int i = 0; i <= max_val; i++) {
        dp[y][x][i + a[y][x]] |= dp[y - 1][x][i];
        dp[y][x][abs(i - a[y][x])] |= dp[y - 1][x][i];
      }
    }

    if (y < h - 1) {
      if (!visited[y + 1][x]) {
        visited[y + 1][x] = true;
        que.emplace(x, y + 1);
      }
    }

    if (x < w - 1) {
      if (!visited[y][x + 1]) {
        visited[y][x + 1] = true;
        que.emplace(x + 1, y);
      }
    }
  }

  int ans = 123456;
  for (int i = 0; i <= max_val; i++) {
    if (dp[h - 1][w - 1][i]) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;

typedef pair<int, int> P;

#define bit(n) (1LL << (n))

// #define int long long

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); }

#define mod (ll)(1e9 + 7)
const long long INF = 1LL << 60;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll h, w;
  cin >> h >> w;

  vvll a(h, vll(w));
  rep(y, h) {
    rep(x, w) { cin >> a[y][x]; }
  }

  rep(y, h) {
    rep(x, w) {
      ll tmp;
      cin >> tmp;
      a[y][x] = abs(a[y][x] - tmp);
    }
  }

  const int max_val = 80 * (h + w);
  using vvvi = vector<vvi>;
  vvvi dp(h, vvi(w, vi(max_val + 1)));

  using vvb = vector<vb>;
  vvb visited(h, vb(w));

  queue<P> que;
  dp[0][0][a[0][0]] = 1;
  que.emplace(0, 1);
  que.emplace(1, 0);

  while (!que.empty()) {
    auto q = que.front();
    que.pop();
    int x = q.first;
    int y = q.second;

    if (x > 0) {
      for (int i = 0; i <= max_val; i++) {
        dp[y][x][i + a[y][x]] |= dp[y][x - 1][i];
        dp[y][x][abs(i - a[y][x])] |= dp[y][x - 1][i];
      }
    }
    if (y > 0) {
      for (int i = 0; i <= max_val; i++) {
        dp[y][x][i + a[y][x]] |= dp[y - 1][x][i];
        dp[y][x][abs(i - a[y][x])] |= dp[y - 1][x][i];
      }
    }

    if (y < h - 1) {
      if (!visited[y + 1][x]) {
        visited[y + 1][x] = true;
        que.emplace(x, y + 1);
      }
    }

    if (x < w - 1) {
      if (!visited[y][x + 1]) {
        visited[y][x + 1] = true;
        que.emplace(x + 1, y);
      }
    }
  }

  int ans = 123456;
  for (int i = 0; i <= max_val; i++) {
    if (dp[h - 1][w - 1][i]) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;

  return 0;
}