Problem p02839 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

#define int long long
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(a) int((a).size())
#define endl '\n'

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef long long ll;

const int maxn = 85;
const int INF = 1e9 + 10;

ii g[maxn][maxn];
int dp[maxn][maxn][3 * maxn * maxn];
int w, h;

int get_ans(int i, int j, int somaa, int somab, int diff) {
  if (i > h || j > w)
    return INF;
  if (dp[i][j][diff] != -1)
    return dp[i][j][diff];
  if (i == h && j == w) {
    int s1 = (somaa + g[i][j].ff);
    int s2 = (somaa + g[i][j].ss);
    int s3 = (somab + g[i][j].ff);
    int s4 = (somab + g[i][j].ss);
    return min(abs(s1 - s4), abs(s2 - s3));
  }
  int ans1 = get_ans(i + 1, j, somaa + g[i][j].ff, somab + g[i][j].ss,
                     abs((somaa + g[i][j].ff) - (somab + g[i][j].ss)));
  int ans2 = get_ans(i + 1, j, somaa + g[i][j].ss, somab + g[i][j].ff,
                     abs((somaa + g[i][j].ss) - (somab + g[i][j].ff)));
  int ans3 = get_ans(i, j + 1, somaa + g[i][j].ff, somab + g[i][j].ss,
                     abs((somaa + g[i][j].ff) - (somab + g[i][j].ss)));
  int ans4 = get_ans(i, j + 1, somaa + g[i][j].ss, somab + g[i][j].ff,
                     abs((somaa + g[i][j].ss) - (somab + g[i][j].ff)));

  return dp[i][j][diff] = min({ans1, ans2, ans3, ans4});
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif
  cin >> h >> w;
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      cin >> g[i][j].ff;
    }
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      cin >> g[i][j].ss;
    }
  }
  memset(dp, -1, sizeof dp);
  cout << get_ans(1, 1, 0, 0, 0) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define int long long
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(a) int((a).size())
#define endl '\n'

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef long long ll;

const int maxn = 85;
const int INF = 1e9 + 10;

ii g[maxn][maxn];
int dp[maxn][maxn][2 * maxn * maxn];
int w, h;

int get_ans(int i, int j, int somaa, int somab, int diff) {
  if (i > h || j > w)
    return INF;
  if (dp[i][j][diff] != -1)
    return dp[i][j][diff];
  if (i == h && j == w) {
    int s1 = (somaa + g[i][j].ff);
    int s2 = (somaa + g[i][j].ss);
    int s3 = (somab + g[i][j].ff);
    int s4 = (somab + g[i][j].ss);
    return min(abs(s1 - s4), abs(s2 - s3));
  }
  int ans1 = get_ans(i + 1, j, somaa + g[i][j].ff, somab + g[i][j].ss,
                     abs((somaa + g[i][j].ff) - (somab + g[i][j].ss)));
  int ans2 = get_ans(i + 1, j, somaa + g[i][j].ss, somab + g[i][j].ff,
                     abs((somaa + g[i][j].ss) - (somab + g[i][j].ff)));
  int ans3 = get_ans(i, j + 1, somaa + g[i][j].ff, somab + g[i][j].ss,
                     abs((somaa + g[i][j].ff) - (somab + g[i][j].ss)));
  int ans4 = get_ans(i, j + 1, somaa + g[i][j].ss, somab + g[i][j].ff,
                     abs((somaa + g[i][j].ss) - (somab + g[i][j].ff)));

  return dp[i][j][diff] = min({ans1, ans2, ans3, ans4});
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif
  cin >> h >> w;
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      cin >> g[i][j].ff;
    }
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      cin >> g[i][j].ss;
    }
  }
  memset(dp, -1, sizeof dp);
  cout << get_ans(1, 1, 0, 0, 0) << endl;
  return 0;
}