Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(i) i.begin(), i.end()
template <class T, class U> bool cmax(T &a, U b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T, class U> bool cmin(T &a, U b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}

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

  ll h, w;
  cin >> h >> w;
  vector<vl> a(h, vl(w));
  vector<vl> b(h, vl(w));

  rep(i, h) {
    rep(j, w) { cin >> a[i][j]; }
  }
  rep(i, h) {
    rep(j, w) { cin >> b[i][j]; }
  }

  vector<vl> c(h, vl(w));
  rep(i, h) {
    rep(j, w) {
      c[i][j] = a[i][j] - b[i][j];
      // cerr << c[i][j] << " ";
    }
    // cerr << endl;
  }

  //( h+ w) * max(ai) * 2 * 2(+-)
  ll ma = (80 + 80) * 80 * 2 * 2 * 2;
  ll ze = ma / 2;
  ma *= 2;
  ma += 1000;

  vector<vector<vector<bool>>> dp(h, vector<vector<bool>>(w, vector<bool>(ma)));
  dp[0][0][c[0][0] + ze] = true;

  rep(i, h) {
    rep(j, w) {
      for (ll k = -ze; k <= ze; k++) {
        if (i - 1 >= 0 && k + c[i][j] + ze >= 0) {
          if (dp[i - 1][j][k + c[i][j] + ze])
            dp[i][j][k + ze] = 1;
        }
        if (i - 1 >= 0 && k - c[i][j] + ze >= 0) {
          if (dp[i - 1][j][k - c[i][j] + ze])
            dp[i][j][k + ze] = 1;
        }
        if (j - 1 >= 0 && k + c[i][j] + ze >= 0) {
          if (dp[i][j - 1][k + c[i][j] + ze])
            dp[i][j][k + ze] = 1;
        }
        if (j - 1 >= 0 && k - c[i][j] + ze >= 0) {
          if (dp[i][j - 1][k - c[i][j] + ze])
            dp[i][j][k + ze] = 1;
        }
      }
    }
  }

  ll ans = 1e20;
  for (ll k = -ze; k <= ze; k++) {
    if (dp[h - 1][w - 1][k + ze]) {
      cmin(ans, abs(k));
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(i) i.begin(), i.end()
template <class T, class U> bool cmax(T &a, U b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T, class U> bool cmin(T &a, U b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}

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

  ll h, w;
  cin >> h >> w;
  vector<vl> a(h, vl(w));
  vector<vl> b(h, vl(w));

  rep(i, h) {
    rep(j, w) { cin >> a[i][j]; }
  }
  rep(i, h) {
    rep(j, w) { cin >> b[i][j]; }
  }

  vector<vl> c(h, vl(w));
  rep(i, h) {
    rep(j, w) {
      c[i][j] = a[i][j] - b[i][j];
      // cerr << c[i][j] << " ";
    }
    // cerr << endl;
  }

  //( h+ w) * max(ai) * 2 * 2(+-)
  ll ma = (80 + 80) * 80 * 2 * 2;
  ll ze = ma / 2;
  ma *= 2;
  ma += 1000;

  vector<vector<vector<bool>>> dp(h, vector<vector<bool>>(w, vector<bool>(ma)));
  dp[0][0][c[0][0] + ze] = true;

  rep(i, h) {
    rep(j, w) {
      for (ll k = -ze; k <= ze; k++) {
        if (i - 1 >= 0 && k + c[i][j] + ze >= 0) {
          if (dp[i - 1][j][k + c[i][j] + ze])
            dp[i][j][k + ze] = 1;
        }
        if (i - 1 >= 0 && k - c[i][j] + ze >= 0) {
          if (dp[i - 1][j][k - c[i][j] + ze])
            dp[i][j][k + ze] = 1;
        }
        if (j - 1 >= 0 && k + c[i][j] + ze >= 0) {
          if (dp[i][j - 1][k + c[i][j] + ze])
            dp[i][j][k + ze] = 1;
        }
        if (j - 1 >= 0 && k - c[i][j] + ze >= 0) {
          if (dp[i][j - 1][k - c[i][j] + ze])
            dp[i][j][k + ze] = 1;
        }
      }
    }
  }

  ll ans = 1e20;
  for (ll k = -ze; k <= ze; k++) {
    if (dp[h - 1][w - 1][k + ze]) {
      cmin(ans, abs(k));
    }
  }

  cout << ans << endl;
}