Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

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

  int H, W;
  cin >> H >> W;

  vector<vs32> A(H, vs32(W));
  vector<vs32> B(H, vs32(W));
  rep(h, H) rep(w, W) cin >> A[h][w];
  rep(h, H) rep(w, W) cin >> B[h][w];

  vector<vector<vector<bool>>> dp(
      H, vector<vector<bool>>(W, vector<bool>((H + W) * 80 + 1, false)));
  dp[0][0][abs(A[0][0] - B[0][0])] = true;
  for (int h = 0; h < H; ++h) {
    for (int w = 0; w < W; ++w) {
      for (int k = 0; k <= (H + W) * 80; ++k) {
        int d = abs(A[h][w] - B[h][w]);
        if (h > 0) {
          dp[h][w][abs(k - d)] = dp[h][w][abs(k - d)] || dp[h - 1][w][k];
          dp[h][w][abs(k + d)] = dp[h][w][abs(k - d)] || dp[h - 1][w][k];
        }
        if (w > 0) {
          dp[h][w][abs(k - d)] = dp[h][w][abs(k - d)] || dp[h][w - 1][k];
          dp[h][w][abs(k + d)] = dp[h][w][abs(k + d)] || dp[h][w - 1][k];
        }
      }
    }
  }

  int ans;
  for (ans = 0; ans <= (H + W) * 80 && !dp[H - 1][W - 1][ans]; ++ans)
    ;
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

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

  int H, W;
  cin >> H >> W;

  vector<vs32> A(H, vs32(W));
  vector<vs32> B(H, vs32(W));
  rep(h, H) rep(w, W) cin >> A[h][w];
  rep(h, H) rep(w, W) cin >> B[h][w];

  vector<vector<vector<bool>>> dp(
      H, vector<vector<bool>>(W, vector<bool>((H + W) * 80 + 1, false)));
  dp[0][0][abs(A[0][0] - B[0][0])] = true;
  for (int h = 0; h < H; ++h) {
    for (int w = 0; w < W; ++w) {
      for (int k = 0; k <= (H + W) * 80; ++k) {
        if (dp[h][w][k]) {
          if (h < H - 1) {
            int d = abs(A[h + 1][w] - B[h + 1][w]);
            dp[h + 1][w][abs(k - d)] = true;
            dp[h + 1][w][abs(k + d)] = true;
          }
          if (w < W - 1) {
            int d = abs(A[h][w + 1] - B[h][w + 1]);
            dp[h][w + 1][abs(k - d)] = true;
            dp[h][w + 1][abs(k + d)] = true;
          }
        }
      }
    }
  }

  int ans;
  for (ans = 0; ans <= (H + W) * 80 && !dp[H - 1][W - 1][ans]; ++ans)
    ;
  cout << ans << "\n";
  return 0;
}