Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, N) for (int(i) = 0; (i) < (N); (i)++)
#define revrep(i, N) for (int(i) = (N)-1; (i) >= 0; (i)--)
#define dump(vec)                                                              \
  for (int i = 0; i < (vec).size(); i++) {                                     \
    cout << vec.at(i) << ",";                                                  \
  }                                                                            \
  cout << endl

typedef long long ll;

int main(void) {
  int w, h;
  cin >> h >> w;
  const ll max = w * h * 80;
  vector<vector<vector<bool>>> dp(
      h, vector<vector<bool>>(w, vector<bool>(max, false)));
  vector<vector<int>> A(h, vector<int>(w, 0));
  vector<vector<int>> B(h, vector<int>(w, 0));

  rep(i, h) {
    rep(j, w) {
      int a;
      cin >> a;
      A.at(i).at(j) = a;
    }
  }
  rep(i, h) {
    rep(j, w) {
      int b;
      cin >> b;
      B.at(i).at(j) = b;
    }
  }

  rep(i, h) {
    rep(j, w) {
      int diff = abs(A.at(i).at(j) - B.at(i).at(j));
      if (i == 0 && j == 0) {
        dp.at(0).at(0).at(diff) = true;
      }
      if (i > 0) {
        rep(k, max) {
          if (!dp.at(i - 1).at(j).at(k))
            continue;
          dp.at(i).at(j).at(abs(k - diff)) = true;
          dp.at(i).at(j).at(abs(k + diff)) = true;
        }
      }
      if (j > 0) {
        rep(k, max) {
          if (!dp.at(i).at(j - 1).at(k))
            continue;
          dp.at(i).at(j).at(abs(k - diff)) = true;
          dp.at(i).at(j).at(abs(k + diff)) = true;
        }
      }

      // cout << "(" << i << "," << j << ")";
      // rep(k,10){
      //     cout  << dp.at(i).at(j).at(k) << ",";
      // }cout << endl;
    }
  }

  rep(k, max) {
    if (!dp.at(h - 1).at(w - 1).at(k))
      continue;
    cout << k << endl;
    break;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, N) for (int(i) = 0; (i) < (N); (i)++)
#define revrep(i, N) for (int(i) = (N)-1; (i) >= 0; (i)--)
#define dump(vec)                                                              \
  for (int i = 0; i < (vec).size(); i++) {                                     \
    cout << vec.at(i) << ",";                                                  \
  }                                                                            \
  cout << endl

typedef long long ll;

int main(void) {
  int w, h;
  cin >> h >> w;
  const ll max = (w + h) * 80;
  vector<vector<vector<bool>>> dp(
      h, vector<vector<bool>>(w, vector<bool>(max, false)));
  vector<vector<int>> A(h, vector<int>(w, 0));
  vector<vector<int>> B(h, vector<int>(w, 0));

  rep(i, h) {
    rep(j, w) {
      int a;
      cin >> a;
      A.at(i).at(j) = a;
    }
  }
  rep(i, h) {
    rep(j, w) {
      int b;
      cin >> b;
      B.at(i).at(j) = b;
    }
  }

  rep(i, h) {
    rep(j, w) {
      int diff = abs(A.at(i).at(j) - B.at(i).at(j));
      if (i == 0 && j == 0) {
        dp.at(0).at(0).at(diff) = true;
      }
      if (i > 0) {
        rep(k, max) {
          if (!dp.at(i - 1).at(j).at(k))
            continue;
          dp.at(i).at(j).at(abs(k - diff)) = true;
          dp.at(i).at(j).at(abs(k + diff)) = true;
        }
      }
      if (j > 0) {
        rep(k, max) {
          if (!dp.at(i).at(j - 1).at(k))
            continue;
          dp.at(i).at(j).at(abs(k - diff)) = true;
          dp.at(i).at(j).at(abs(k + diff)) = true;
        }
      }

      // cout << "(" << i << "," << j << ")";
      // rep(k,10){
      //     cout  << dp.at(i).at(j).at(k) << ",";
      // }cout << endl;
    }
  }

  rep(k, max) {
    if (!dp.at(h - 1).at(w - 1).at(k))
      continue;
    cout << k << endl;
    break;
  }

  return 0;
}