Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int dp[81][81][13000];
int find_it(int dif_sum, int i, int j, int &n, int &m,
            vector<vector<pair<int, int>>> &matrix) {
  if (i == n - 1 && j == m - 1) {
    return min(abs(dif_sum + matrix[i][j].first - matrix[i][j].second),
               abs(dif_sum + matrix[i][j].second - matrix[i][j].first));
  }
  if (dp[i][j][dif_sum] != -1) {
    return dp[i][j][dif_sum];
  }
  int minvalue = INT_MAX;
  if (i + 1 < n) {
    minvalue = min(minvalue,
                   find_it(dif_sum + (matrix[i][j].first - matrix[i][j].second),
                           i + 1, j, n, m, matrix));
    minvalue = min(minvalue,
                   find_it(dif_sum + (matrix[i][j].second - matrix[i][j].first),
                           i + 1, j, n, m, matrix));
  }
  if (j + 1 < m) {
    minvalue = min(minvalue,
                   find_it(dif_sum + (matrix[i][j].second - matrix[i][j].first),
                           i, j + 1, n, m, matrix));
    minvalue = min(minvalue,
                   find_it(dif_sum + (matrix[i][j].first - matrix[i][j].second),
                           i, j + 1, n, m, matrix));
  }
  return dp[i][j][dif_sum] = minvalue;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> matrix(n, vector<pair<int, int>>(m));

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> matrix[i][j].first;
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> matrix[i][j].second;
    }
  }
  memset(dp, -1, sizeof(dp));
  cout << find_it(0, 0, 0, n, m, matrix);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[81][81][6400 * 2 + 2];
int find_it(int dif_sum, int i, int j, int &n, int &m,
            vector<vector<pair<int, int>>> &matrix) {
  if (i == n - 1 && j == m - 1) {
    return min(abs(dif_sum + matrix[i][j].first - matrix[i][j].second),
               abs(dif_sum + matrix[i][j].second - matrix[i][j].first));
  }
  if (dp[i][j][dif_sum] != -1) {
    return dp[i][j][dif_sum];
  }
  int minvalue = INT_MAX;
  if (i + 1 < n) {
    minvalue = min(minvalue,
                   find_it(dif_sum + (matrix[i][j].first - matrix[i][j].second),
                           i + 1, j, n, m, matrix));
    minvalue = min(minvalue,
                   find_it(dif_sum + (matrix[i][j].second - matrix[i][j].first),
                           i + 1, j, n, m, matrix));
  }
  if (j + 1 < m) {
    minvalue = min(minvalue,
                   find_it(dif_sum + (matrix[i][j].second - matrix[i][j].first),
                           i, j + 1, n, m, matrix));
    minvalue = min(minvalue,
                   find_it(dif_sum + (matrix[i][j].first - matrix[i][j].second),
                           i, j + 1, n, m, matrix));
  }
  return dp[i][j][dif_sum] = minvalue;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> matrix(n, vector<pair<int, int>>(m));

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> matrix[i][j].first;
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> matrix[i][j].second;
    }
  }
  memset(dp, -1, sizeof(dp));
  cout << find_it(0, 0, 0, n, m, matrix);

  return 0;
}