Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RANGE(i, a, b) for (int i = (a); i < (b); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)

#define endl '\n'
#define pb push_back
#define eb emplace_back
#define pque priority_queue
#define umap unordered_map
#define uset unordered_set
#define BIG 2000000000
#define VERYBIG 1000000000000000ll
#define PI 3.141592653589793238
#define dcout cout << fixed << setprecision(50)
#define _popcnt __builtin_popcount

const long long dx[] = {1, 0, -1, 0, 1, 1, -1, -1},
                dy[] = {0, -1, 0, 1, 1, -1, 1, -1};
const long long MOD = 1e9 + 7;

// typedef long long ll;
#define int long long
#define double long double

template <typename T> inline T GCD(T a, T b) {
  T c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
template <typename T> inline T LCM(T a, T b) {
  T c = GCD(a, b);
  a /= c;
  return a * b;
}
template <typename T> inline T nCr(T a, T b) {
  T i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= (a + 1 - i);
    r /= i;
  }
  return r;
}
template <typename T> inline T nHr(T a, T b) { return nCr(a + b - 1, b); }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef pair<int, int> INTP;
typedef vector<int> INTV;
struct edge2 {
  int to, cost;
};
struct edge3 {
  int from, to, cost;
};

bool dp[81][81][30000];

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int H, W, A[80][80], B[80][80];
  cin >> H >> W;
  REP(i, H) REP(j, W) cin >> A[i][j];
  REP(i, H) REP(j, W) cin >> B[i][j];

  int dh[2] = {1, 0}, dw[2] = {0, 1};
  dp[0][0][15000 + A[0][0] - B[0][0]] = 1;
  dp[0][0][15000 - A[0][0] + B[0][0]] = 1;
  REP(i, H) REP(j, W) REP(bias, 30000) {
    if (i == H - 1 and j == W - 1)
      continue;
    if (!dp[i][j][bias])
      continue;
    REP(dir, 2) {
      int nxh = i + dh[dir], nxw = j + dw[dir];
      int nxb_Ared = bias + A[nxh][nxw] - B[nxh][nxw];
      int nxb_Ablu = bias - A[nxh][nxw] + B[nxh][nxw];
      dp[nxh][nxw][nxb_Ared] |= dp[i][j][bias];
      dp[nxh][nxw][nxb_Ablu] |= dp[i][j][bias];
    }
  }
  int ans = BIG;
  REP(i, 30000) {
    if (dp[H - 1][W - 1][i])
      chmin(ans, abs(i - 15000));
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RANGE(i, a, b) for (int i = (a); i < (b); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)

#define endl '\n'
#define pb push_back
#define eb emplace_back
#define pque priority_queue
#define umap unordered_map
#define uset unordered_set
#define BIG 2000000000
#define VERYBIG 1000000000000000ll
#define PI 3.141592653589793238
#define dcout cout << fixed << setprecision(50)
#define _popcnt __builtin_popcount

const long long dx[] = {1, 0, -1, 0, 1, 1, -1, -1},
                dy[] = {0, -1, 0, 1, 1, -1, 1, -1};
const long long MOD = 1e9 + 7;

// typedef long long ll;
#define int long long
#define double long double

template <typename T> inline T GCD(T a, T b) {
  T c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
template <typename T> inline T LCM(T a, T b) {
  T c = GCD(a, b);
  a /= c;
  return a * b;
}
template <typename T> inline T nCr(T a, T b) {
  T i, r = 1;
  for (i = 1; i <= b; i++) {
    r *= (a + 1 - i);
    r /= i;
  }
  return r;
}
template <typename T> inline T nHr(T a, T b) { return nCr(a + b - 1, b); }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef pair<int, int> INTP;
typedef vector<int> INTV;
struct edge2 {
  int to, cost;
};
struct edge3 {
  int from, to, cost;
};

bool dp[81][81][30000];

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int H, W, A[80][80], B[80][80];
  cin >> H >> W;
  REP(i, H) REP(j, W) cin >> A[i][j];
  REP(i, H) REP(j, W) cin >> B[i][j];

  int dh[2] = {1, 0}, dw[2] = {0, 1};
  dp[0][0][15000 + A[0][0] - B[0][0]] = 1;
  dp[0][0][15000 - A[0][0] + B[0][0]] = 1;
  REP(i, H) REP(j, W) REP(bias, 30000) {
    if (i == H - 1 and j == W - 1)
      continue;
    if (!dp[i][j][bias])
      continue;
    REP(dir, 2) {
      int nxh = i + dh[dir], nxw = j + dw[dir];
      int nxb_Ared = bias + A[nxh][nxw] - B[nxh][nxw];
      int nxb_Ablu = bias - A[nxh][nxw] + B[nxh][nxw];
      if (0 > nxb_Ared or nxb_Ared >= 30000)
        continue;
      if (0 > nxb_Ablu or nxb_Ablu >= 30000)
        continue;
      dp[nxh][nxw][nxb_Ared] |= dp[i][j][bias];
      dp[nxh][nxw][nxb_Ablu] |= dp[i][j][bias];
    }
  }
  int ans = BIG;
  REP(i, 30000) {
    if (dp[H - 1][W - 1][i])
      chmin(ans, abs(i - 15000));
  }
  cout << ans << endl;
}