Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> Pii;
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define INF 1000000000
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define fs first
#define ss second
inline int Scan() {
  int x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

template <typename T, typename U> static inline void amin(T &x, U y) {
  if (y < x)
    x = y;
}

// call by reference is used in x
template <typename T, typename U> static inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}
//////////////////////////////////////end of
///template//////////////////////////////////////

bool dp[81][81][25600];
int a[81][81];

int main() {

  int h, w;
  cin >> h >> w;

  int x;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      cin >> a[i][j];
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      cin >> x;
      a[i][j] = abs(a[i][j] - x);
    };

  for (int i = 0; i <= 80; i++)
    for (int j = 0; j <= 80; j++)
      for (int k = 0; k <= 12800; k++)
        dp[i][j][k] = false;

  dp[1][1][a[1][1]] = true;

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      for (int k = 0; k <= 12800; k++) {
        if (dp[i][j][k]) {
          dp[i][j + 1][k + a[i][j + 1]] = true;
          dp[i][j + 1][abs(k - a[i][j + 1])] = true;

          dp[i + 1][j][k + a[i + 1][j]] = true;
          dp[i + 1][j][abs(k - a[i + 1][j])] = true;
        }
      }
    }
  }

  for (int i = 0; i <= 12800; i++) {
    if (dp[h][w][i]) {
      cout << i << endl;
      break;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> Pii;
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define INF 1000000000
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define fs first
#define ss second
inline int Scan() {
  int x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

template <typename T, typename U> static inline void amin(T &x, U y) {
  if (y < x)
    x = y;
}

// call by reference is used in x
template <typename T, typename U> static inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}
//////////////////////////////////////end of
///template//////////////////////////////////////

bool dp[90][90][25600];
int a[90][90];

int main() {

  int h, w;
  cin >> h >> w;

  int x;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      cin >> a[i][j];
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      cin >> x;
      a[i][j] = abs(a[i][j] - x);
    };

  for (int i = 0; i <= 80; i++)
    for (int j = 0; j <= 80; j++)
      for (int k = 0; k <= 12800; k++)
        dp[i][j][k] = false;

  dp[1][1][a[1][1]] = true;

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      for (int k = 0; k <= 12800; k++) {
        if (dp[i][j][k]) {
          dp[i][j + 1][k + a[i][j + 1]] = true;
          dp[i][j + 1][abs(k - a[i][j + 1])] = true;

          dp[i + 1][j][k + a[i + 1][j]] = true;
          dp[i + 1][j][abs(k - a[i + 1][j])] = true;
        }
      }
    }
  }

  for (int i = 0; i <= 12800; i++) {
    if (dp[h][w][i]) {
      cout << i << endl;
      break;
    }
  }

  return 0;
}