Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

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

  int H, W;
  cin >> H >> W;
  vector<vector<int>> aij(H, vector<int>(W));
  vector<vector<int>> bij(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int a;
      cin >> a;
      aij.at(i).at(j) = a;
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int b;
      cin >> b;
      bij.at(i).at(j) = b;
    }
  }
  vector<vector<vector<bool>>> dp(
      H, vector<vector<bool>>(W, vector<bool>(6401, false)));
  dp.at(0).at(0).at(abs(aij.at(0).at(0) - bij.at(0).at(0))) = true;
  for (int i = 1; i < H + W - 1; i++) {
    int currentH = i + 1;
    int currentW = -1;
    while (currentH >= 0) {
      currentH--;
      currentW++;
      if (currentH >= H || currentH < 0 || currentW >= W || currentW < 0) {
        continue;
      }
      int ababs =
          abs(aij.at(currentH).at(currentW) - bij.at(currentH).at(currentW));
      for (int j = 0; j < max(i * 80 + 1, 6321); j++) {
        if (currentH > 0) {
          dp.at(currentH).at(currentW).at(j) =
              dp.at(currentH).at(currentW).at(j) |
              dp.at(currentH - 1).at(currentW).at(abs(j - ababs));
          dp.at(currentH).at(currentW).at(j) =
              dp.at(currentH).at(currentW).at(j) |
              dp.at(currentH - 1).at(currentW).at(j + ababs);
        }
        if (currentW > 0) {
          dp.at(currentH).at(currentW).at(j) =
              dp.at(currentH).at(currentW).at(j) |
              dp.at(currentH).at(currentW - 1).at(abs(j - ababs));
          dp.at(currentH).at(currentW).at(j) =
              dp.at(currentH).at(currentW).at(j) |
              dp.at(currentH).at(currentW - 1).at(j + ababs);
        }
      }
    }
  }
  for (int i = 0; i < 81; i++) {
    if (dp.at(H - 1).at(W - 1).at(i)) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

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

  int H, W;
  cin >> H >> W;
  vector<vector<int>> aij(H, vector<int>(W));
  vector<vector<int>> bij(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int a;
      cin >> a;
      aij.at(i).at(j) = a;
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int b;
      cin >> b;
      bij.at(i).at(j) = b;
    }
  }
  vector<vector<vector<bool>>> dp(
      H, vector<vector<bool>>(W, vector<bool>(6401, false)));
  dp.at(0).at(0).at(abs(aij.at(0).at(0) - bij.at(0).at(0))) = true;
  for (int i = 1; i < H + W - 1; i++) {
    int currentH = i + 1;
    int currentW = -1;
    while (currentH >= 0) {
      currentH--;
      currentW++;
      if (currentH >= H || currentH < 0 || currentW >= W || currentW < 0) {
        continue;
      }
      int ababs =
          abs(aij.at(currentH).at(currentW) - bij.at(currentH).at(currentW));
      for (int j = 0; j < min((i + 1) * 80 + 1, 6321); j++) {
        if (currentH > 0) {
          dp.at(currentH).at(currentW).at(j) =
              dp.at(currentH).at(currentW).at(j) |
              dp.at(currentH - 1).at(currentW).at(abs(j - ababs));
          dp.at(currentH).at(currentW).at(j) =
              dp.at(currentH).at(currentW).at(j) |
              dp.at(currentH - 1).at(currentW).at(j + ababs);
        }
        if (currentW > 0) {
          dp.at(currentH).at(currentW).at(j) =
              dp.at(currentH).at(currentW).at(j) |
              dp.at(currentH).at(currentW - 1).at(abs(j - ababs));
          dp.at(currentH).at(currentW).at(j) =
              dp.at(currentH).at(currentW).at(j) |
              dp.at(currentH).at(currentW - 1).at(j + ababs);
        }
      }
    }
  }
  for (int i = 0; i < 81; i++) {
    if (dp.at(H - 1).at(W - 1).at(i)) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}