Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <string>
#define ll int
using namespace std;
const ll l = 1000;
const ll r = 19000;
ll f[9][9][20000], a[90][90];
ll n, m, i, j, k, t, x, y, ans = 1e9;
ll read() {
  ll f = 0, c = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-')
      c = -1;
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    f = f * 10 + ch - '0';
    ch = getchar();
  }
  return c * f;
}
void dp(ll x, ll y) {
  //	cout<<a[x][y]<<" "<<x<<" "<<y<<" "<<f[0][1][10000]<<endl;
  for (ll i = l; i < r; ++i) {
    //		cout<<f[x][y][i];
    //		cout<<i<<" "<<a[x][y]<<" "<<x<<" "<<y<<" "<<f[x][y][i]<<endl;
    f[x][y][i] = f[x - 1][y][i - a[x][y]] | f[x - 1][y][i + a[x][y]] |
                 f[x][y - 1][i - a[x][y]] | f[x][y - 1][i + a[x][y]];
    /*		f[x][y][i]|=f[x-1][y][i-a[x][y]];
                    cout<<"fa";
                    f[x][y][i]|=f[x-1][y][i+a[x][y]];
                    cout<<"fa";
                    f[x][y][i]|=f[x][y-1][i-a[x][y]];
                    cout<<"fa";
                    f[x][y][i]|=f[x][y-1][i+a[x][y]];
                    cout<<"fa";*/
    //		cout<<i<<" "<<f[x][y][i]<<endl;
  }
}
int main() {
  //	freopen("test.in","r",stdin);
  //	freopen("test.out","w",stdout);
  f[0][1][10000] = 1;
  n = read();
  m = read();
  for (ll i = 1; i <= n; ++i) {
    for (ll j = 1; j <= m; ++j) {
      a[i][j] = read();
    }
  }
  for (ll i = 1; i <= n; ++i) {
    for (ll j = 1; j <= m; ++j) {
      a[i][j] = abs(a[i][j] - read());
      //			cout<<a[i][j]<<" ";
    }
    //		cout<<endl;
  }
  for (ll i = 1; i <= n; ++i) {
    for (ll j = 1; j <= m; ++j) {
      dp(i, j);
    }
  }
  for (ll i = l; i <= r; ++i) {
    if (f[n][m][i])
      ans = min(ans, abs(i - 10000));
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <string>
#define ll int
using namespace std;
const ll l = 1000;
const ll r = 19000;
ll f[90][90][20000], a[90][90];
ll n, m, i, j, k, t, x, y, ans = 1e9;
ll read() {
  ll f = 0, c = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-')
      c = -1;
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    f = f * 10 + ch - '0';
    ch = getchar();
  }
  return c * f;
}
void dp(ll x, ll y) {
  //	cout<<a[x][y]<<" "<<x<<" "<<y<<" "<<f[0][1][10000]<<endl;
  for (ll i = l; i < r; ++i) {
    //		cout<<f[x][y][i];
    //		cout<<i<<" "<<a[x][y]<<" "<<x<<" "<<y<<" "<<f[x][y][i]<<endl;
    f[x][y][i] = f[x - 1][y][i - a[x][y]] | f[x - 1][y][i + a[x][y]] |
                 f[x][y - 1][i - a[x][y]] | f[x][y - 1][i + a[x][y]];
    /*		f[x][y][i]|=f[x-1][y][i-a[x][y]];
                    cout<<"fa";
                    f[x][y][i]|=f[x-1][y][i+a[x][y]];
                    cout<<"fa";
                    f[x][y][i]|=f[x][y-1][i-a[x][y]];
                    cout<<"fa";
                    f[x][y][i]|=f[x][y-1][i+a[x][y]];
                    cout<<"fa";*/
    //		cout<<i<<" "<<f[x][y][i]<<endl;
  }
}
int main() {
  //	freopen("test.in","r",stdin);
  //	freopen("test.out","w",stdout);
  f[0][1][10000] = 1;
  n = read();
  m = read();
  for (ll i = 1; i <= n; ++i) {
    for (ll j = 1; j <= m; ++j) {
      a[i][j] = read();
    }
  }
  for (ll i = 1; i <= n; ++i) {
    for (ll j = 1; j <= m; ++j) {
      a[i][j] = abs(a[i][j] - read());
      //			cout<<a[i][j]<<" ";
    }
    //		cout<<endl;
  }
  for (ll i = 1; i <= n; ++i) {
    for (ll j = 1; j <= m; ++j) {
      dp(i, j);
    }
  }
  for (ll i = l; i <= r; ++i) {
    if (f[n][m][i])
      ans = min(ans, abs(i - 10000));
  }
  cout << ans;
  return 0;
}