Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define pp pair<ll, ll>
#define pq_min priority_queue<ll, vector<ll>, greater<ll>>
#define pq_max priority_queue<ll>
#define forx(i, a, b) for (ll i = 0; i < a; i++)
#define ld long double
const ll mod = 1e9 + 7;
const ll N = 15;
ll power(ll a, ll b) {
  a = a % mod;
  ll ans = 1;
  while (b > 0) {
    if (b % 2 == 1) {
      ans = (ans * a) % mod;
    }
    b = b / 2;
    a = (a * a) % mod;
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  srand(time(NULL));
  ll n, m;
  cin >> n >> m;
  ll a[n][m];
  ll b[n][m];
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < m; j++) {
      cin >> b[i][j];
    }
  }

  ll ans[n][m][13000];
  memset(ans, 0, sizeof(ans));
  ll z = abs(a[0][0] - b[0][0]);
  ans[0][0][z] = 1;
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < m; j++) {
      ll f = abs(a[i][j] - b[i][j]);
      if (i > 0) {
        for (ll k = 0; k < 13000; k++) {
          if (ans[i - 1][j][k] == 1) {
            ans[i][j][k + f] = 1;
            ans[i][j][abs(k - f)] = 1;
          }
        }
      }
      if (j > 0) {
        for (ll k = 0; k < 13000; k++) {
          if (ans[i][j - 1][k] == 1) {
            ans[i][j][k + f] = 1;
            ans[i][j][abs(k - f)] = 1;
          }
        }
      }
    }
  }
  for (ll i = 0; i < 13000; i++) {
    if (ans[n - 1][m - 1][i] == 1) {
      cout << i << endl;
      return 0;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define pp pair<ll, ll>
#define pq_min priority_queue<ll, vector<ll>, greater<ll>>
#define pq_max priority_queue<ll>
#define forx(i, a, b) for (ll i = 0; i < a; i++)
#define ld long double
const ll mod = 1e9 + 7;
const ll N = 15;
ll power(ll a, ll b) {
  a = a % mod;
  ll ans = 1;
  while (b > 0) {
    if (b % 2 == 1) {
      ans = (ans * a) % mod;
    }
    b = b / 2;
    a = (a * a) % mod;
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  srand(time(NULL));
  ll n, m;
  cin >> n >> m;
  ll a[n][m];
  ll b[n][m];
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < m; j++) {
      cin >> b[i][j];
    }
  }

  bool ans[n][m][13000];
  memset(ans, 0, sizeof(ans));
  ll z = abs(a[0][0] - b[0][0]);
  ans[0][0][z] = 1;
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < m; j++) {
      ll f = abs(a[i][j] - b[i][j]);
      if (i > 0) {
        for (ll k = 0; k < 13000; k++) {
          if (ans[i - 1][j][k] == 1) {
            ans[i][j][k + f] = 1;
            ans[i][j][abs(k - f)] = 1;
          }
        }
      }
      if (j > 0) {
        for (ll k = 0; k < 13000; k++) {
          if (ans[i][j - 1][k] == 1) {
            ans[i][j][k + f] = 1;
            ans[i][j][abs(k - f)] = 1;
          }
        }
      }
    }
  }
  for (ll i = 0; i < 13000; i++) {
    if (ans[n - 1][m - 1][i] == 1) {
      cout << i << endl;
      return 0;
    }
  }
}