Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define nmax_def 15000
#define nmax_def_2 7500
#define Inf 2000000000
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;

vector<vector<vb>> DP;

int main() {
  int H, W;
  cin >> H >> W;
  vector<vi> A(H);
  vector<vi> B(H);
  vector<vi> difflist(H);
  rep(i, H) {
    difflist[i].resize(W);
    A[i].resize(W);
    B[i].resize(W);
  }
  rep(i, H) {
    rep(j, W) { cin >> A[i][j]; }
  }
  rep(i, H) {
    rep(j, W) { cin >> B[i][j]; }
  }
  rep(i, H) {
    rep(j, W) { difflist[i][j] = A[i][j] - B[i][j]; }
  }

  DP.resize(H + 1);
  rep(i, H + 1) {
    DP[i].resize(W + 1);
    rep(j, W + 1) {
      DP[i][j].resize(nmax_def);
      rep(k, nmax_def) { DP[i][j][k] = false; }
    }
  }
  DP[1][1][difflist[0][0] + nmax_def_2] =
      true; // 左上が(1, 1)で差はdifflist[0][0]->(要素では)+=7500
  DP[1][1][-difflist[0][0] + nmax_def_2] =
      true; // 左上が(1, 1)で差は-difflist[0][0]->(要素では)+=7500
  rep(i, H) {
    rep(j, W) {
      rep(k, nmax_def) {
        if (DP[i + 1][j][k]) {
          DP[i + 1][j + 1][k + difflist[i][j]] = true;
          DP[i + 1][j + 1][k - difflist[i][j]] = true;
        }
        if (DP[i][j + 1][k]) {
          DP[i + 1][j + 1][k + difflist[i][j]] = true;
          DP[i + 1][j + 1][k - difflist[i][j]] = true;
        }
      }
    }
  }
  int ans = Inf;
  rep(k, nmax_def) {
    if (DP[H][W][k]) {
      int anstmp = abs(k - nmax_def_2);
      ans = min(ans, anstmp);
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define nmax_def 30000
#define nmax_def_2 15000
#define Inf 2000000000
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;

vector<vector<vb>> DP;

int main() {
  int H, W;
  cin >> H >> W;
  vector<vi> A(H);
  vector<vi> B(H);
  vector<vi> difflist(H);
  rep(i, H) {
    difflist[i].resize(W);
    A[i].resize(W);
    B[i].resize(W);
  }
  rep(i, H) {
    rep(j, W) { cin >> A[i][j]; }
  }
  rep(i, H) {
    rep(j, W) { cin >> B[i][j]; }
  }
  rep(i, H) {
    rep(j, W) { difflist[i][j] = A[i][j] - B[i][j]; }
  }

  DP.resize(H + 1);
  rep(i, H + 1) {
    DP[i].resize(W + 1);
    rep(j, W + 1) {
      DP[i][j].resize(nmax_def);
      rep(k, nmax_def) { DP[i][j][k] = false; }
    }
  }
  DP[1][1][difflist[0][0] + nmax_def_2] =
      true; // 左上が(1, 1)で差はdifflist[0][0]->(要素では)+=7500
  DP[1][1][-difflist[0][0] + nmax_def_2] =
      true; // 左上が(1, 1)で差は-difflist[0][0]->(要素では)+=7500
  rep(i, H) {
    rep(j, W) {
      rep(k, nmax_def) {
        if (DP[i + 1][j][k]) {
          DP[i + 1][j + 1][k + difflist[i][j]] = true;
          DP[i + 1][j + 1][k - difflist[i][j]] = true;
        }
        if (DP[i][j + 1][k]) {
          DP[i + 1][j + 1][k + difflist[i][j]] = true;
          DP[i + 1][j + 1][k - difflist[i][j]] = true;
        }
      }
    }
  }
  int ans = Inf;
  rep(k, nmax_def) {
    if (DP[H][W][k]) {
      int anstmp = abs(k - nmax_def_2);
      ans = min(ans, anstmp);
    }
  }
  cout << ans << endl;
  return 0;
}