Problem p02839 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 58;
const ll mod = 1e9 + 7;

int main() {
  int i, j, k;
  int h, w;
  cin >> h >> w;
  int a[h][w];
  int b[h][w];
  int c[h][w];
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      cin >> b[i][j];
    }
  }
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      c[i][j] = b[i][j] - a[i][j];
      // cout<<c[i][j];
    }
    // cout<<endl;
  }

  int r = 80 * 162;
  VVVI v(h, VVI(w, VI(2 * r, 0)));
  v[0][0][r + c[0][0]] = 1;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (i >= 1) {
        for (k = 0; k < 2 * r; k++) {
          if (k - c[i][j] >= 0 && k - c[i][j] < 2 * r &&
              v[i - 1][j][k - c[i][j]] == 1) {
            v[i][j][k] = 1;
          }
        }
        for (k = 0; k < 2 * r; k++) {
          if (k + c[i][j] >= 0 && k + c[i][j] < 2 * r &&
              v[i - 1][j][k + c[i][j]] == 1) {
            v[i][j][k] = 1;
          }
        }
      }
      if (j >= 1) {
        for (k = 0; k < 2 * r; k++) {
          if (k - c[i][j] >= 0 && k - c[i][j] < 2 * r &&
              v[i][j - 1][k - c[i][j]] == 1) {
            v[i][j][k] = 1;
          }
        }
        for (k = 0; k < 2 * r; k++) {
          if (k + c[i][j] >= 0 && k + c[i][j] < 2 * r &&
              v[i][j - 1][k + c[i][j]] == 1) {
            v[i][j][k] = 1;
          }
        }
      }
    }
  }
  int ans = inf;
  for (i = 0; i < 2 * r; i++) {
    if (v[h - 1][w - 1][i] == 1)
      ans = min(ans, abs(i - r));
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<bool>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 58;
const ll mod = 1e9 + 7;

int main() {
  int i, j, k;
  int h, w;
  cin >> h >> w;
  int a[h][w];
  int b[h][w];
  int c[h][w];
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      cin >> b[i][j];
    }
  }
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      c[i][j] = b[i][j] - a[i][j];
      // cout<<c[i][j];
    }
    // cout<<endl;
  }

  int r = 80 * 162;
  VVVI v(h, VVI(w, VI(2 * r, 0)));
  v[0][0][r + c[0][0]] = 1;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (i >= 1) {
        for (k = 0; k < 2 * r; k++) {
          if (k - c[i][j] >= 0 && k - c[i][j] < 2 * r &&
              v[i - 1][j][k - c[i][j]] == 1) {
            v[i][j][k] = 1;
          }
        }
        for (k = 0; k < 2 * r; k++) {
          if (k + c[i][j] >= 0 && k + c[i][j] < 2 * r &&
              v[i - 1][j][k + c[i][j]] == 1) {
            v[i][j][k] = 1;
          }
        }
      }
      if (j >= 1) {
        for (k = 0; k < 2 * r; k++) {
          if (k - c[i][j] >= 0 && k - c[i][j] < 2 * r &&
              v[i][j - 1][k - c[i][j]] == 1) {
            v[i][j][k] = 1;
          }
        }
        for (k = 0; k < 2 * r; k++) {
          if (k + c[i][j] >= 0 && k + c[i][j] < 2 * r &&
              v[i][j - 1][k + c[i][j]] == 1) {
            v[i][j][k] = 1;
          }
        }
      }
    }
  }
  int ans = inf;
  for (i = 0; i < 2 * r; i++) {
    if (v[h - 1][w - 1][i] == 1)
      ans = min(ans, abs(i - r));
  }
  cout << ans << endl;

  return 0;
}