Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const ll INF = 1000000000000000000;
typedef pair<int, int> P;

bool dp[100][100][10000];
int A[100][100], B[100][100], C[100][100];
int num;

int main() {
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> B[i][j];
      C[i][j] = abs(A[i][j] - B[i][j]);
    }
  }
  dp[0][0][C[0][0]] = true;
  num = C[0][0];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (i - 1 >= 0) {
        for (int k = 0; k <= num; k++) {
          if (dp[i - 1][j][k]) {
            dp[i][j][abs(k - C[i][j])] = true;
            dp[i][j][k + C[i][j]] = true;
            num = max(num, k + C[i][j]);
          }
        }
      }
      if (j - 1 >= 0) {
        for (int k = 0; k <= num; k++) {
          if (dp[i][j - 1][k]) {
            dp[i][j][abs(k - C[i][j])] = true;
            dp[i][j][k + C[i][j]] = true;
            num = max(num, k + C[i][j]);
          }
        }
      }
    }
  }
  /*for (int i = 0; i < H; i++) {
          for (int j = 0; j < W; j++) {
                  cout << i << " " << j << " ";
                  for (int k = 0; k < 160; k++) {
                          if (dp[i][j][k]) {
                                  cout << k << " ";
                          }
                  }
                  cout << endl;
          }
  }*/
  for (int i = 0; i < 160; i++) {
    if (dp[H - 1][W - 1][i]) {
      cout << i << endl;
      return 0;
    }
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const ll INF = 1000000000000000000;
typedef pair<int, int> P;

bool dp[100][100][20000];
int A[100][100], B[100][100], C[100][100];
int num;

int main() {
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> B[i][j];
      C[i][j] = abs(A[i][j] - B[i][j]);
    }
  }
  dp[0][0][C[0][0]] = true;
  num = C[0][0];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (i - 1 >= 0) {
        for (int k = 0; k <= num; k++) {
          if (dp[i - 1][j][k]) {
            dp[i][j][abs(k - C[i][j])] = true;
            dp[i][j][k + C[i][j]] = true;
            num = max(num, k + C[i][j]);
          }
        }
      }
      if (j - 1 >= 0) {
        for (int k = 0; k <= num; k++) {
          if (dp[i][j - 1][k]) {
            dp[i][j][abs(k - C[i][j])] = true;
            dp[i][j][k + C[i][j]] = true;
            num = max(num, k + C[i][j]);
          }
        }
      }
    }
  }
  /*for (int i = 0; i < H; i++) {
          for (int j = 0; j < W; j++) {
                  cout << i << " " << j << " ";
                  for (int k = 0; k < 160; k++) {
                          if (dp[i][j][k]) {
                                  cout << k << " ";
                          }
                  }
                  cout << endl;
          }
  }*/
  for (int i = 0; i < 160; i++) {
    if (dp[H - 1][W - 1][i]) {
      cout << i << endl;
      return 0;
    }
  }
}