prithivMLmods/Coder-Stat huggingface dataset:
Problem p02839 (C++)

Buggy submission (status: Runtime Error):

// problem: "https://atcoder.jp/contests/abc147/tasks/abc147_e"

#include <bits/stdc++.h>
#define p 6401

using namespace std;

int minD(vector<vector<int>> &A, vector<vector<int>> &B, int h, int w) {
  vector<vector<int>> D(h, vector<int>(w, 0));
  int i, j;
  for (i = 0; i != h; i++)
    for (j = 0; j != w; j++)
      D[i][j] = abs(A[i][j] - B[i][j]);

  vector<vector<vector<bool>>> V(
      h, vector<vector<bool>>(w, vector<bool>(p, false)));
  int k;

  V[0][0][D[0][0]] = true;
  for (i = 0; i != h; i++) {
    for (j = 0; j != w; j++) {
      if (i - 1 >= 0) {
        for (k = 0; k != p; k++) {
          if (V[i - 1][j][k]) {
            V[i][j][abs(D[i][j] - k)] = true;
            V[i][j][abs(D[i][j] + k)] = true;
          }
        }
      }

      if (j - 1 >= 0) {
        for (k = 0; k != p; k++) {
          if (V[i][j - 1][k]) {
            V[i][j][abs(D[i][j] - k)] = true;
            V[i][j][abs(D[i][j] + k)] = true;
          }
        }
      }
    }
  }

  for (k = 0; k != p; k++)
    if (V[h - 1][w - 1][k])
      break;

  return k;
}

int main() {
  int h, w;
  cin >> h >> w;

  int i, j;
  vector<vector<int>> A(h, vector<int>(w, 0));
  for (i = 0; i != h; i++) {
    for (j = 0; j != w; j++) {
      int x;
      cin >> x;
      A[i][j] = x;
    }
  }

  vector<vector<int>> B(h, vector<int>(w, 0));
  for (i = 0; i != h; i++) {
    for (j = 0; j != w; j++) {
      int x;
      cin >> x;
      B[i][j] = x;
    }
  }

  cout << minD(A, B, h, w); // define

  return 0;
}


Fixed submission:

// problem: "https://atcoder.jp/contests/abc147/tasks/abc147_e"

#include <bits/stdc++.h>
#define p 6401 * 2

using namespace std;

int minD(vector<vector<int>> &A, vector<vector<int>> &B, int h, int w) {
  vector<vector<int>> D(h, vector<int>(w, 0));
  int i, j;
  for (i = 0; i != h; i++)
    for (j = 0; j != w; j++)
      D[i][j] = abs(A[i][j] - B[i][j]);

  vector<vector<vector<bool>>> V(
      h, vector<vector<bool>>(w, vector<bool>(p, false)));
  int k;

  V[0][0][D[0][0]] = true;
  for (i = 0; i != h; i++) {
    for (j = 0; j != w; j++) {
      if (i - 1 >= 0) {
        for (k = 0; k != p; k++) {
          if (V[i - 1][j][k]) {
            V[i][j][abs(D[i][j] - k)] = true;
            V[i][j][abs(D[i][j] + k)] = true;
          }
        }
      }

      if (j - 1 >= 0) {
        for (k = 0; k != p; k++) {
          if (V[i][j - 1][k]) {
            V[i][j][abs(D[i][j] - k)] = true;
            V[i][j][abs(D[i][j] + k)] = true;
          }
        }
      }
    }
  }

  for (k = 0; k != p; k++)
    if (V[h - 1][w - 1][k])
      break;

  return k;
}

int main() {
  int h, w;
  cin >> h >> w;

  int i, j;
  vector<vector<int>> A(h, vector<int>(w, 0));
  for (i = 0; i != h; i++) {
    for (j = 0; j != w; j++) {
      int x;
      cin >> x;
      A[i][j] = x;
    }
  }

  vector<vector<int>> B(h, vector<int>(w, 0));
  for (i = 0; i != h; i++) {
    for (j = 0; j != w; j++) {
      int x;
      cin >> x;
      B[i][j] = x;
    }
  }

  cout << minD(A, B, h, w); // define

  return 0;
}
Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 85
#define maxSum (2 * N * N)

using namespace std;

const int del = maxSum + 105;

int n, m;
int a[N][N];
bool f[2][N][maxSum * 2 + 500];

int main() {
  cin >> n >> m;

  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      cin >> a[i][j];
  for (int i = 1; i <= n; ++i)
    for (int j = 1, d; j <= m; ++j)
      cin >> d, a[i][j] -= d;

  int u = 0, v = 1;
  f[0][1][a[1][1] + del] = true;
  for (int i = 2; i < n + m; ++i) {
    memset(f[v], 0, sizeof f[v]);
    for (int x = 1, y; x <= n; ++x) {
      y = i - x;
      if (y < 1 || y > m)
        continue;
      if (x + 1 <= n) {
        for (int o = -maxSum; o <= maxSum; ++o)
          if (f[u][x][o + del]) {
            f[v][x + 1][o + a[x + 1][y] + del] = true;
            f[v][x + 1][o - a[x + y][y] + del] = true;
          }
      }
      if (y + 1 <= m) {
        for (int o = -maxSum; o <= maxSum; ++o)
          if (f[u][x][o + del]) {
            f[v][x][o + a[x][y + 1] + del] = true;
            f[v][x][o - a[x][y + 1] + del] = true;
          }
      }
    }
    swap(u, v);
  }

  int ans = INT_MAX;
  for (int i = -maxSum; i <= maxSum; ++i)
    if (f[u][n][i + del] && abs(i) < ans)
      ans = abs(i);

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 85
#define maxSum (2 * N * N)

using namespace std;

const int del = maxSum + 105;

int n, m;
int a[N][N];
bool f[2][N][maxSum * 2 + 500];

int main() {
  cin >> n >> m;

  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      cin >> a[i][j];
  for (int i = 1; i <= n; ++i)
    for (int j = 1, d; j <= m; ++j)
      cin >> d, a[i][j] -= d;

  int u = 0, v = 1;
  f[0][1][a[1][1] + del] = true;
  for (int i = 2; i < n + m; ++i) {
    memset(f[v], 0, sizeof f[v]);
    for (int x = 1, y; x <= n; ++x) {
      y = i - x;
      if (y < 1 || y > m)
        continue;
      if (x + 1 <= n) {
        for (int o = -maxSum; o <= maxSum; ++o)
          if (f[u][x][o + del]) {
            f[v][x + 1][o + a[x + 1][y] + del] = true;
            f[v][x + 1][o - a[x + 1][y] + del] = true;
          }
      }
      if (y + 1 <= m) {
        for (int o = -maxSum; o <= maxSum; ++o)
          if (f[u][x][o + del]) {
            f[v][x][o + a[x][y + 1] + del] = true;
            f[v][x][o - a[x][y + 1] + del] = true;
          }
      }
    }
    swap(u, v);
  }

  int ans = INT_MAX;
  for (int i = -maxSum; i <= maxSum; ++i)
    if (f[u][n][i + del] && abs(i) < ans)
      ans = abs(i);

  cout << ans << endl;
  return 0;
}
Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

const int INF = 1e9;
const int MAXC = 80;

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int>> a(h, vector<int>(w));
  vector<vector<int>> b(h, vector<int>(w));

  rep(i, h) { rep(j, w) cin >> a[i][j]; }
  rep(i, h) { rep(j, w) cin >> b[i][j]; }

  vector<vector<int>> c(h, vector<int>(w));
  rep(i, h) rep(j, w) c[i][j] = abs(a[i][j] - b[i][j]);

  int MAX = MAXC * h * w;
  bool dp[h][w][MAX];
  rep(i, h) rep(j, w) rep(k, MAX) dp[i][j][k] = false;
  dp[0][0][c[0][0]] = true;

  rep(i, h) {
    rep(j, w) {
      rep(k, MAX) {
        int n = c[i][j];
        if (i - 1 >= 0 && abs(k - n) < MAX)
          dp[i][j][abs(k - n)] |= dp[i - 1][j][k];
        if (j - 1 >= 0 && abs(k - n) < MAX)
          dp[i][j][abs(k - n)] |= dp[i][j - 1][k];
        if (i - 1 >= 0 && k + n < MAX)
          dp[i][j][k + n] |= dp[i - 1][j][k];
        if (j - 1 >= 0 && k + n < MAX)
          dp[i][j][k + n] |= dp[i][j - 1][k];
      }
    }
  }

  rep(k, MAX) {
    if (dp[h - 1][w - 1][k]) {
      cout << k << endl;
      return 0;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

const int INF = 1e9;
const int MAXC = 80;

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int>> a(h, vector<int>(w));
  vector<vector<int>> b(h, vector<int>(w));

  rep(i, h) { rep(j, w) cin >> a[i][j]; }
  rep(i, h) { rep(j, w) cin >> b[i][j]; }

  vector<vector<int>> c(h, vector<int>(w));
  rep(i, h) rep(j, w) c[i][j] = abs(a[i][j] - b[i][j]);

  int MAX = MAXC * (h + w);
  bool dp[h][w][MAX];
  rep(i, h) rep(j, w) rep(k, MAX) dp[i][j][k] = false;
  dp[0][0][c[0][0]] = true;

  rep(i, h) {
    rep(j, w) {
      rep(k, MAX) {
        int n = c[i][j];
        if (i - 1 >= 0 && abs(k - n) < MAX)
          dp[i][j][abs(k - n)] |= dp[i - 1][j][k];
        if (j - 1 >= 0 && abs(k - n) < MAX)
          dp[i][j][abs(k - n)] |= dp[i][j - 1][k];
        if (i - 1 >= 0 && k + n < MAX)
          dp[i][j][k + n] |= dp[i - 1][j][k];
        if (j - 1 >= 0 && k + n < MAX)
          dp[i][j][k + n] |= dp[i][j - 1][k];
      }
    }
  }

  rep(k, MAX) {
    if (dp[h - 1][w - 1][k]) {
      cout << k << endl;
      return 0;
    }
  }
}
Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int N = 83;
const int M = 6405;
#define ll long long

int n, m, DP[M][N][N], b[2 * N][N], a[N][N];

void in() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n * 2; i++)
    for (int j = 1; j <= m; j++)
      scanf("%d", &b[i][j]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      a[i][j] = abs(b[i][j] - b[i + n][j]);
}

void _DP() {
  DP[0][1][1] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      for (int l = 0; l <= 6400; l++) {
        if (!DP[l][i][j])
          continue;
        DP[l + a[i][j]][i + 1][j] = DP[abs(l - a[i][j])][i + 1][j] =
            DP[l + a[i][j]][i][j + 1] = DP[abs(l - a[i][j])][i][j + 1] = 1;
      }
}

void print() {
  for (int i = 0; i <= 6400; i++)
    if (DP[i][n + 1][m] || DP[i][n][m + 1]) {
      cout << i;
      break;
    }
}

int main() {
  in();
  _DP();
  print();
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int N = 100;
const int M = 8000;
#define ll long long

int n, m, DP[M][N][N], b[2 * N][N], a[N][N];

void in() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n * 2; i++)
    for (int j = 1; j <= m; j++)
      scanf("%d", &b[i][j]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      a[i][j] = abs(b[i][j] - b[i + n][j]);
}

void _DP() {
  DP[0][1][1] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      for (int l = 0; l <= 6400; l++) {
        if (!DP[l][i][j])
          continue;
        DP[l + a[i][j]][i + 1][j] = DP[abs(l - a[i][j])][i + 1][j] =
            DP[l + a[i][j]][i][j + 1] = DP[abs(l - a[i][j])][i][j + 1] = 1;
      }
}

void print() {
  for (int i = 0; i <= 6400; i++)
    if (DP[i][n + 1][m] || DP[i][n][m + 1]) {
      cout << i;
      break;
    }
}

int main() {
  in();
  _DP();
  print();
}
Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int MX = 13000, N = 80;
bitset<2 * MX> pos[N][N];
int a[N][N], b[N][N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> b[i][j];

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int d = abs(a[i][j] - b[i][j]);
      if (i == 1 && j == 1)
        pos[i][j][MX + d] = 1;
      if (i > 1)
        pos[i][j] |= (pos[i - 1][j] << d) | (pos[i - 1][j] >> d);
      if (j > 1)
        pos[i][j] |= (pos[i][j - 1] << d) | (pos[i][j - 1] >> d);

      //            cout<<i<<" "<<j<<" "<<d<<" ---> ";
      //            for (int x=0; x<2*MX; x++)
      //                if (pos[i][j][x])   cout<<x-MX<<" "; cout<<endl;
    }
  }

  for (int i = 0; i < MX; i++) {
    if (pos[n][m][MX + i] || pos[n][m][MX - i]) {
      cout << i << endl;
      break;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int MX = 13000, N = 85;
bitset<2 * MX> pos[N][N];
int a[N][N], b[N][N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> b[i][j];

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int d = abs(a[i][j] - b[i][j]);
      if (i == 1 && j == 1)
        pos[i][j][MX + d] = 1;
      if (i > 1)
        pos[i][j] |= (pos[i - 1][j] << d) | (pos[i - 1][j] >> d);
      if (j > 1)
        pos[i][j] |= (pos[i][j - 1] << d) | (pos[i][j - 1] >> d);

      //            cout<<i<<" "<<j<<" "<<d<<" ---> ";
      //            for (int x=0; x<2*MX; x++)
      //                if (pos[i][j][x])   cout<<x-MX<<" "; cout<<endl;
    }
  }

  for (int i = 0; i < MX; i++) {
    if (pos[n][m][MX + i] || pos[n][m][MX - i]) {
      cout << i << endl;
      break;
    }
  }
}
Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  int a[h][w], b[h][w];
  int diff[h][w];
  for (int x = 0; x < h; x++) {
    for (int y = 0; y < w; y++) {
      cin >> a[x][y];
    }
  }
  for (int x = 0; x < h; x++) {
    for (int y = 0; y < w; y++) {
      cin >> b[x][y];
    }
  }
  for (int x = 0; x < h; x++) {
    for (int y = 0; y < w; y++) {
      diff[x][y] = abs(a[x][y] - b[x][y]);
    }
  }
  int x = 0, y = 0;
  int base = 80 * 80;
  int lim = base * 2;
  int dp[h][w][lim + 1];
  for (int x = 0; x < h; x++) {
    for (int y = 0; y < w; y++) {
      for (int d = 0; d <= lim; d++) {
        dp[x][y][d] = 0;
      }
    }
  }
  dp[0][0][diff[0][0]] = 1;
  for (int x = 0; x < h; x++) {
    for (int y = 0; y < w; y++) {
      for (int d = 0; d <= lim; d++) {
        if (x != 0) {
          dp[x][y][d] = max(dp[x][y][d], dp[x - 1][y][abs(d - diff[x][y])]);
          if (d + diff[x][y] <= lim) {
            dp[x][y][d] = max(dp[x][y][d], dp[x - 1][y][d + diff[x][y]]);
          }
        }
        if (y != 0) {
          dp[x][y][d] = max(dp[x][y][d], dp[x][y - 1][abs(d - diff[x][y])]);
          if (d + diff[x][y] <= lim) {
            dp[x][y][d] = max(dp[x][y][d], dp[x][y - 1][d + diff[x][y]]);
          }
        }
      }
    }
  }
  for (int d = 0; d <= base; d++) {
    if (dp[h - 1][w - 1][d] == 1) {
      cout << d << endl;
      return 0;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  int a[h][w], b[h][w];
  int diff[h][w];
  for (int x = 0; x < h; x++) {
    for (int y = 0; y < w; y++) {
      cin >> a[x][y];
    }
  }
  for (int x = 0; x < h; x++) {
    for (int y = 0; y < w; y++) {
      cin >> b[x][y];
    }
  }
  for (int x = 0; x < h; x++) {
    for (int y = 0; y < w; y++) {
      diff[x][y] = abs(a[x][y] - b[x][y]);
    }
  }
  int x = 0, y = 0;
  int base = 80 * 80;
  int lim = base * 2;
  vector<vector<vector<int>>> dp(
      h, vector<vector<int>>(w, vector<int>(lim + 1, 0)));
  dp[0][0][diff[0][0]] = 1;
  for (int x = 0; x < h; x++) {
    for (int y = 0; y < w; y++) {
      for (int d = 0; d <= lim; d++) {
        if (x != 0) {
          dp[x][y][d] = max(dp[x][y][d], dp[x - 1][y][abs(d - diff[x][y])]);
          if (d + diff[x][y] <= lim) {
            dp[x][y][d] = max(dp[x][y][d], dp[x - 1][y][d + diff[x][y]]);
          }
        }
        if (y != 0) {
          dp[x][y][d] = max(dp[x][y][d], dp[x][y - 1][abs(d - diff[x][y])]);
          if (d + diff[x][y] <= lim) {
            dp[x][y][d] = max(dp[x][y][d], dp[x][y - 1][d + diff[x][y]]);
          }
        }
      }
    }
  }
  for (int d = 0; d <= base; d++) {
    if (dp[h - 1][w - 1][d] == 1) {
      cout << d << endl;
      return 0;
    }
  }
}
Problem p02839 (C++)

Buggy submission (status: Runtime Error):
/**
 * Created by hiramekun at 20:44 on 2019-12-08.
 */
#include <bits/stdc++.h>
#pragma optimize("", off)
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

int main() {
  ll h, w;
  cin >> h >> w;
  vector<bool> v;
  v[0];
  vector<int> v2;
  v2[0];
  int a[h][w], b[h][w];
  rep(i, h) rep(j, w) cin >> a[i][j];
  rep(i, h) rep(j, w) cin >> b[i][j];

  ll max_diff = 2 * 80 * 160;
  bool dp[h][w][max_diff];
  rep(i, h) rep(j, w) rep(l, max_diff) dp[i][j][l] = false;

  ll mid = max_diff / 2;
  dp[0][0][b[0][0] - a[0][0] + mid] = true;
  dp[0][0][-b[0][0] + a[0][0] + mid] = true;

  rep(i, h) {
    rep(j, w) {
      rep(l, max_diff) {
        if (i + 1 <= h - 1) {
          assert(l - b[i + 1][j] + a[i + 1][j] >= 0);
          assert(l + b[i + 1][j] - a[i + 1][j] >= 0);
          dp[i + 1][j][l + b[i + 1][j] - a[i + 1][j]] |= dp[i][j][l];
          dp[i + 1][j][l - b[i + 1][j] + a[i + 1][j]] |= dp[i][j][l];
        }
        if (j + 1 <= w - 1) {
          dp[i][j + 1][l + b[i][j + 1] - a[i][j + 1]] |= dp[i][j][l];
          dp[i][j + 1][l - b[i][j + 1] + a[i][j + 1]] |= dp[i][j][l];
        }
      }
    }
  }
  ll ans = 1e18;
  rep(i, max_diff) {
    if (dp[h - 1][w - 1][i])
      ans = min(ans, abs(i - mid));
  }
  cout << ans << '\n';
}


Fixed submission:
/**
 * Created by hiramekun at 20:44 on 2019-12-08.
 */
#include <bits/stdc++.h>
#pragma optimize("", off)
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

int main() {
  ll h, w;
  cin >> h >> w;
  vector<bool> v;
  v[0];
  vector<int> v2;
  v2[0];
  int a[h][w], b[h][w];
  rep(i, h) rep(j, w) cin >> a[i][j];
  rep(i, h) rep(j, w) cin >> b[i][j];

  ll max_diff = 2 * 80 * 160;
  bool dp[h][w][max_diff];
  rep(i, h) rep(j, w) rep(l, max_diff) dp[i][j][l] = false;

  ll mid = max_diff / 2;
  dp[0][0][b[0][0] - a[0][0] + mid] = true;
  dp[0][0][-b[0][0] + a[0][0] + mid] = true;

  rep(i, h) {
    rep(j, w) {
      rep(l, max_diff) {
        if (i + 1 <= h - 1) {

          dp[i + 1][j][l + b[i + 1][j] - a[i + 1][j]] |= dp[i][j][l];
          dp[i + 1][j][l - b[i + 1][j] + a[i + 1][j]] |= dp[i][j][l];
        }
        if (j + 1 <= w - 1) {
          dp[i][j + 1][l + b[i][j + 1] - a[i][j + 1]] |= dp[i][j][l];
          dp[i][j + 1][l - b[i][j + 1] + a[i][j + 1]] |= dp[i][j][l];
        }
      }
    }
  }
  ll ans = 1e18;
  rep(i, max_diff) {
    if (dp[h - 1][w - 1][i])
      ans = min(ans, abs(i - mid));
  }
  cout << ans << '\n';
}
Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using namespace std;

int h, w;
int ma[80][80], mb[80][80], ch[80][80];
bool ans[80][80][16000];

int main() {
  cin >> h >> w;
  rep(i, h) rep(j, w) cin >> ma[i][j];
  rep(i, h) rep(j, w) cin >> mb[i][j];
  rep(i, h) rep(j, w) ch[i][j] = abs(ma[i][j] - mb[i][j]);

  ans[0][0][ch[0][0]] = true;

  rep(i, h) rep(j, w) rep(k, 640000) {
    if (ans[i][j][k]) {
      if (i + 1 < h) {
        ans[i + 1][j][k + ch[i + 1][j]] = true;
        ans[i + 1][j][abs(k - ch[i + 1][j])] = true;
      }
      if (j + 1 < w) {
        ans[i][j + 1][k + ch[i][j + 1]] = true;
        ans[i][j + 1][abs(k - ch[i][j + 1])] = true;
      }
    }
  }
  rep(k, 640000) if (ans[h - 1][w - 1][k]) {
    cout << k << endl;
    return 0;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using namespace std;

int h, w;
int ma[80][80], mb[80][80], ch[80][80];
bool ans[80][80][16000];

int main() {
  cin >> h >> w;
  rep(i, h) rep(j, w) cin >> ma[i][j];
  rep(i, h) rep(j, w) cin >> mb[i][j];
  rep(i, h) rep(j, w) ch[i][j] = abs(ma[i][j] - mb[i][j]);

  ans[0][0][ch[0][0]] = true;

  rep(i, h) rep(j, w) rep(k, 16000) {
    if (ans[i][j][k]) {
      if (i + 1 < h) {
        ans[i + 1][j][k + ch[i + 1][j]] = true;
        ans[i + 1][j][abs(k - ch[i + 1][j])] = true;
      }
      if (j + 1 < w) {
        ans[i][j + 1][k + ch[i][j + 1]] = true;
        ans[i][j + 1][abs(k - ch[i][j + 1])] = true;
      }
    }
  }
  rep(k, 640000) if (ans[h - 1][w - 1][k]) {
    cout << k << endl;
    return 0;
  }
}
Problem p02839 (C++)

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

typedef long long ll;
typedef vector<ll> v1;
typedef vector<v1> v2;

typedef vector<vector<ll>> graph;

const ll INF = 1ll << 50;
const ll mod = 1000000007;

ll h, w;
ll a[81][81];
ll b[81][81];
ll offset = 81 * 160;
ll dp[81][81][81 * 160 * 2];
ll ans = INF;

int main() {
  cin >> h >> w;
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      cin >> b[i][j];
    }
  }
  dp[0][0][(a[0][0] - b[0][0]) + offset] = 1;
  dp[0][0][(b[0][0] - a[0][0]) + offset] = 1;

  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      if (i + 1 < h) {
        for (ll k = 0; k < 81 * 160 * 2; k++) {
          if (dp[i][j][k] == 1) {
            dp[i + 1][j][k + (a[i + 1][j] - b[i + 1][j])] = 1;
            dp[i + 1][j][k + (b[i + 1][j] - a[i + 1][j])] = 1;
          }
        }
      }
      if (j + 1 < w) {
        for (ll k = 0; k < 81 * 160 * 2; k++) {
          if (dp[i][j][k] == 1) {
            dp[i][j + 1][k + (a[i][j + 1] - b[i][j + 1])] = 1;
            dp[i][j + 1][k + (b[i][j + 1] - a[i][j + 1])] = 1;
          }
        }
      }
    }
  }

  // for(ll i = 0;i < h;i++){
  //     for(ll j = 0;j < w;j++){
  //         cerr << dp[i][j][offset+0];
  //     }
  //     cerr << endl;
  // }
  for (ll k = 0; k < 81 * 160 * 2; k++) {
    if (dp[h - 1][w - 1][k]) {
      ans = min(ans, abs(k - offset));
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> v1;
typedef vector<v1> v2;

typedef vector<vector<ll>> graph;

const ll INF = 1ll << 50;
const ll mod = 1000000007;

ll h, w;
ll a[81][81];
ll b[81][81];
ll offset = 81 * 160;
char dp[81][81][81 * 160 * 2];
ll ans = INF;

int main() {
  cin >> h >> w;
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      cin >> b[i][j];
    }
  }
  dp[0][0][(a[0][0] - b[0][0]) + offset] = 1;
  dp[0][0][(b[0][0] - a[0][0]) + offset] = 1;

  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      if (i + 1 < h) {
        for (ll k = 0; k < 81 * 160 * 2; k++) {
          if (dp[i][j][k] == 1) {
            dp[i + 1][j][k + (a[i + 1][j] - b[i + 1][j])] = 1;
            dp[i + 1][j][k + (b[i + 1][j] - a[i + 1][j])] = 1;
          }
        }
      }
      if (j + 1 < w) {
        for (ll k = 0; k < 81 * 160 * 2; k++) {
          if (dp[i][j][k] == 1) {
            dp[i][j + 1][k + (a[i][j + 1] - b[i][j + 1])] = 1;
            dp[i][j + 1][k + (b[i][j + 1] - a[i][j + 1])] = 1;
          }
        }
      }
    }
  }

  // for(ll i = 0;i < h;i++){
  //     for(ll j = 0;j < w;j++){
  //         cerr << dp[i][j][offset+0];
  //     }
  //     cerr << endl;
  // }
  for (ll k = 0; k < 81 * 160 * 2; k++) {
    if (dp[h - 1][w - 1][k]) {
      ans = min(ans, abs(k - offset));
    }
  }
  cout << ans << endl;
}
Problem p02839 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define endl enjoy_codeforces
using lint = long long;
int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  lint h, w;
  std::cin >> h >> w;
  std::vector<std::vector<lint>> a(h, std::vector<lint>(w));
  for (lint i = 0; i < h; i++) {
    for (lint j = 0; j < w; j++) {
      std::cin >> a.at(i).at(j);
    }
  }
  for (lint i = 0; i < h; i++) {
    for (lint j = 0; j < w; j++) {
      lint x;
      std::cin >> x;
      a.at(i).at(j) = std::abs(a.at(i).at(j) - x);
    }
  }
  lint max = 20000;
  std::vector<std::vector<std::vector<lint>>> dp(
      h, std::vector<std::vector<lint>>(w, std::vector<lint>(max + 1, false)));
  dp.at(0).at(0).at(a.at(0).at(0)) = true;
  for (lint i = 0; i < h; i++) {
    for (lint j = 0; j < w; j++) {
      lint x = a.at(i).at(j);
      if (i) {
        for (lint k = 0; k <= max; k++) {
          dp.at(i).at(j).at(k) |= dp.at(i - 1).at(j).at(std::abs(k - x)) ||
                                  k + x <= max && dp.at(i - 1).at(j).at(k + x);
        }
      }
      if (j) {
        for (lint k = 0; k <= max; k++) {
          dp.at(i).at(j).at(k) |= dp.at(i).at(j - 1).at(std::abs(k - x)) ||
                                  k + x <= max && dp.at(i).at(j - 1).at(k + x);
        }
      }
    }
  }
  for (int i = 0;; i++) {
    if (dp.at(h - 1).at(w - 1).at(i)) {
      std::cout << i << '\n';
      return 0;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define endl enjoy_codeforces
using lint = long long;
int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  lint h, w;
  std::cin >> h >> w;
  std::vector<std::vector<lint>> a(h, std::vector<lint>(w));
  for (lint i = 0; i < h; i++) {
    for (lint j = 0; j < w; j++) {
      std::cin >> a.at(i).at(j);
    }
  }
  for (lint i = 0; i < h; i++) {
    for (lint j = 0; j < w; j++) {
      lint x;
      std::cin >> x;
      a.at(i).at(j) = std::abs(a.at(i).at(j) - x);
    }
  }
  lint max = 80 * 80 * 2 + 100;
  std::vector<std::vector<std::vector<lint>>> dp(
      h, std::vector<std::vector<lint>>(w, std::vector<lint>(max + 1, false)));
  dp.at(0).at(0).at(a.at(0).at(0)) = true;
  for (lint i = 0; i < h; i++) {
    for (lint j = 0; j < w; j++) {
      lint x = a.at(i).at(j);
      if (i) {
        for (lint k = 0; k <= max; k++) {
          dp.at(i).at(j).at(k) |= dp.at(i - 1).at(j).at(std::abs(k - x)) ||
                                  k + x <= max && dp.at(i - 1).at(j).at(k + x);
        }
      }
      if (j) {
        for (lint k = 0; k <= max; k++) {
          dp.at(i).at(j).at(k) |= dp.at(i).at(j - 1).at(std::abs(k - x)) ||
                                  k + x <= max && dp.at(i).at(j - 1).at(k + x);
        }
      }
    }
  }
  for (int i = 0;; i++) {
    if (dp.at(h - 1).at(w - 1).at(i)) {
      std::cout << i << '\n';
      return 0;
    }
  }
}
Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int a[85][85], b[85][85];
bool dp[85][85][12800 + 12800 + 1];
int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  int mid = 128000;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      scanf("%d", &a[i][j]);
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      scanf("%d", &b[i][j]);
  dp[1][1][mid + a[1][1] - b[1][1]] = 1;
  dp[1][1][mid + b[1][1] - a[1][1]] = 1;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (i == 1 && j == 1)
        continue;
      if (i > 1) {
        for (int k = 0; k <= mid + mid; k++) {
          if (!dp[i - 1][j][k])
            continue;
          dp[i][j][k + a[i][j] - b[i][j]] = 1;
          dp[i][j][k + b[i][j] - a[i][j]] = 1;
        }
      }
      if (j > 1) {
        for (int k = 0; k <= mid + mid; k++) {
          if (!dp[i][j - 1][k])
            continue;
          dp[i][j][k + a[i][j] - b[i][j]] = 1;
          dp[i][j][k + b[i][j] - a[i][j]] = 1;
        }
      }
    }
  }
  int ans = 1e9;
  for (int k = 0; k <= mid + mid; k++)
    if (dp[h][w][k])
      ans = min(ans, abs(k - mid));
  printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[85][85], b[85][85];
bool dp[85][85][12800 + 12800 + 1];
int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  int mid = 12800;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      scanf("%d", &a[i][j]);
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      scanf("%d", &b[i][j]);
  dp[1][1][mid + a[1][1] - b[1][1]] = 1;
  dp[1][1][mid + b[1][1] - a[1][1]] = 1;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (i == 1 && j == 1)
        continue;
      if (i > 1) {
        for (int k = 0; k <= mid + mid; k++) {
          if (!dp[i - 1][j][k])
            continue;
          dp[i][j][k + a[i][j] - b[i][j]] = 1;
          dp[i][j][k + b[i][j] - a[i][j]] = 1;
        }
      }
      if (j > 1) {
        for (int k = 0; k <= mid + mid; k++) {
          if (!dp[i][j - 1][k])
            continue;
          dp[i][j][k + a[i][j] - b[i][j]] = 1;
          dp[i][j][k + b[i][j] - a[i][j]] = 1;
        }
      }
    }
  }
  int ans = 1e9;
  for (int k = 0; k <= mid + mid; k++)
    if (dp[h][w][k])
      ans = min(ans, abs(k - mid));
  printf("%d\n", ans);
}
Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

const int N = 67;
const int M = 6400;
int n, m, A[N][N], B[N][N];
std::bitset<M * 2 + 1> dp[N][N];

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      scanf("%d", A[i] + j);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      scanf("%d", B[i] + j);
  dp[0][1].set(M);
  dp[1][0].set(M);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int d = std::abs(A[i][j] - B[i][j]);
      dp[i][j] |= (dp[i - 1][j] << d) | (dp[i - 1][j] >> d);
      dp[i][j] |= (dp[i][j - 1] << d) | (dp[i][j - 1] >> d);
    }
  int ans = 1e9;
  for (int i = 0; i <= 2 * M; i++)
    if (dp[n][m].test(i))
      ans = std::min(ans, std::abs(i - M));
  printf("%d\n", ans);
  return 0;
}

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

const int N = 88;
const int M = 6400;
int n, m, A[N][N], B[N][N];
std::bitset<M * 2 + 1> dp[N][N];

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      scanf("%d", A[i] + j);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      scanf("%d", B[i] + j);
  dp[0][1].set(M);
  dp[1][0].set(M);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int d = std::abs(A[i][j] - B[i][j]);
      dp[i][j] |= (dp[i - 1][j] << d) | (dp[i - 1][j] >> d);
      dp[i][j] |= (dp[i][j - 1] << d) | (dp[i][j - 1] >> d);
    }
  int ans = 1e9;
  for (int i = 0; i <= 2 * M; i++)
    if (dp[n][m].test(i))
      ans = std::min(ans, std::abs(i - M));
  printf("%d\n", ans);
  return 0;
}
Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int ac = 83, mod = 1e9;
bool A[ac][ac][ac * ac * 2];
ll B[ac][ac];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> B[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int x;
      cin >> x;
      B[i][j] -= x;
    }
  A[0][1][ac * ac] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      for (int k = max(0ll, B[i][j]); k < ac * ac * 2 - max(0ll, B[i][j]);
           k++) {
        cerr << k - B[i][j] << ' ';
        if (A[i - 1][j][k - B[i][j]])
          A[i][j][k] = 1;
        if (A[i - 1][j][k + B[i][j]])
          A[i][j][k] = 1;
        if (A[i][j - 1][k - B[i][j]])
          A[i][j][k] = 1;
        if (A[i][j - 1][k + B[i][j]])
          A[i][j][k] = 1;
      }
    }
  int b = 1e9;
  for (int i = 0; i < ac * ac * 2; i++)
    if (A[n][m][i])
      b = min(b, abs(i - ac * ac));
  cout << b << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int ac = 83, mod = 1e9;
bool A[ac][ac][ac * ac * 2];
ll B[ac][ac];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> B[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int x;
      cin >> x;
      B[i][j] -= x;
    }
  A[0][1][ac * ac] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      for (int k = max(0ll, B[i][j]); k < ac * ac * 2 - max(0ll, B[i][j]);
           k++) {
        if (A[i - 1][j][k - B[i][j]])
          A[i][j][k] = 1;
        if (A[i - 1][j][k + B[i][j]])
          A[i][j][k] = 1;
        if (A[i][j - 1][k - B[i][j]])
          A[i][j][k] = 1;
        if (A[i][j - 1][k + B[i][j]])
          A[i][j][k] = 1;
      }
    }
  int b = 1e9;
  for (int i = 0; i < ac * ac * 2; i++)
    if (A[n][m][i])
      b = min(b, abs(i - ac * ac));
  cout << b << '\n';
}
Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int H, W;
  cin >> H >> W;
  vector<vector<int>> A(H, vector<int>(W)), B(H, vector<int>(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];
    }
  }
  vector<vector<vector<int>>> dp(
      H, vector<vector<int>>(W, vector<int>((80 + 80) * 80 + 1)));
  dp[0][0][A[0][0] - B[0][0]] = true;
  dp[0][0][-A[0][0] + B[0][0]] = true;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int k = 0; k <= (80 + 80) * 80; k++) {
        if (i + 1 < H) {
          dp[i + 1][j][k] |= dp[i][j][k + abs(A[i + 1][j] - B[i + 1][j])];
          dp[i + 1][j][k] |= dp[i][j][abs(k - abs(A[i + 1][j] - B[i + 1][j]))];
        }
        if (j + 1 < W) {
          dp[i][j + 1][k] |= dp[i][j][k + abs(A[i][j + 1] - B[i][j + 1])];
          dp[i][j + 1][k] |= dp[i][j][abs(k - abs(A[i][j + 1] - B[i][j + 1]))];
        }
      }
    }
  }
  for (int k = 0; k <= (80 + 80) * 80; k++) {
    if (dp[H - 1][W - 1][k]) {
      cout << k;
      break;
    }
  }
  return 0;
}


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

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int H, W;
  cin >> H >> W;
  vector<vector<int>> A(H, vector<int>(W)), B(H, vector<int>(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];
    }
  }
  vector<vector<vector<int>>> dp(
      H, vector<vector<int>>(W, vector<int>((80 + 80) * 80 + 1)));
  dp[0][0][abs(A[0][0] - B[0][0])] = true;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int k = 0; k <= (80 + 80) * 80; k++) {
        if (i + 1 < H) {
          dp[i + 1][j][k] |= dp[i][j][k + abs(A[i + 1][j] - B[i + 1][j])];
          dp[i + 1][j][k] |= dp[i][j][abs(k - abs(A[i + 1][j] - B[i + 1][j]))];
        }
        if (j + 1 < W) {
          dp[i][j + 1][k] |= dp[i][j][k + abs(A[i][j + 1] - B[i][j + 1])];
          dp[i][j + 1][k] |= dp[i][j][abs(k - abs(A[i][j + 1] - B[i][j + 1]))];
        }
      }
    }
  }
  for (int k = 0; k <= (80 + 80) * 80; k++) {
    if (dp[H - 1][W - 1][k]) {
      cout << k;
      break;
    }
  }
  return 0;
}
Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int base = 80 * 80;
  int H, W;
  cin >> H >> W;
  pair<int, int> nums[H][W];
  rep(i, H) rep(j, W) cin >> nums[i][j].first;
  rep(i, H) rep(j, W) cin >> nums[i][j].second;
  vector<bool> dp[H][W];
  rep(i, H) rep(j, W) dp[i][j].resize(base * 2 + 1, false);

  dp[0][0][base + nums[0][0].first - nums[0][0].second] = true;
  dp[0][0][base + nums[0][0].second - nums[0][0].first] = true;
  int sz = base * 2;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (i + 1 < H) {
        int fst = nums[i + 1][j].first, snd = nums[i + 1][j].second;
        for (int k = 0; k <= sz; k++) {
          if (dp[i][j][k]) {
            dp[i + 1][j][k + (fst - snd)] = true;
            dp[i + 1][j][k + (snd - fst)] = true;
          }
        }
      }
      if (j + 1 < W) {
        int fst = nums[i][j + 1].first, snd = nums[i][j + 1].second;
        for (int k = 0; k <= sz; k++) {
          if (dp[i][j][k]) {
            dp[i][j + 1][k + (fst - snd)] = true;
            dp[i][j + 1][k + (snd - fst)] = true;
          }
        }
      }
    }
  }
  int ans = mod;
  for (int i = 0; i <= sz; i++) {
    if (dp[H - 1][W - 1][i])
      ans = min(ans, abs(i - base));
  }
  cout << ans << endl;
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int base = 80 * 160;
  int H, W;
  cin >> H >> W;
  pair<int, int> nums[H][W];
  rep(i, H) rep(j, W) cin >> nums[i][j].first;
  rep(i, H) rep(j, W) cin >> nums[i][j].second;
  vector<bool> dp[H][W];
  rep(i, H) rep(j, W) dp[i][j].resize(base * 2 + 1, false);

  dp[0][0][base + nums[0][0].first - nums[0][0].second] = true;
  dp[0][0][base + nums[0][0].second - nums[0][0].first] = true;
  int sz = base * 2;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (i + 1 < H) {
        int fst = nums[i + 1][j].first, snd = nums[i + 1][j].second;
        for (int k = 0; k <= sz; k++) {
          if (dp[i][j][k]) {
            dp[i + 1][j][k + (fst - snd)] = true;
            dp[i + 1][j][k + (snd - fst)] = true;
          }
        }
      }
      if (j + 1 < W) {
        int fst = nums[i][j + 1].first, snd = nums[i][j + 1].second;
        for (int k = 0; k <= sz; k++) {
          if (dp[i][j][k]) {
            dp[i][j + 1][k + (fst - snd)] = true;
            dp[i][j + 1][k + (snd - fst)] = true;
          }
        }
      }
    }
  }
  int ans = mod;
  for (int i = 0; i <= sz; i++) {
    if (dp[H - 1][W - 1][i])
      ans = min(ans, abs(i - base));
  }
  cout << ans << endl;
  return 0;
}
Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
const long long INF = 1e17;

int main() {
  ll H, W;
  cin >> H >> W;
  vector<vector<ll>> a(H, vector<ll>(W));
  vector<vector<ll>> b(H, vector<ll>(W));
  vector<vector<ll>> c(H, vector<ll>(W));
  rep(i, H) rep(j, W) cin >> a[i][j];
  rep(i, H) rep(j, W) cin >> b[i][j];
  rep(i, H) rep(j, W) c[i][j] = b[i][j] - a[i][j];
  vector<vector<vector<bool>>> dp(
      H, vector<vector<bool>>(W, vector<bool>(82 * (H + W), false)));
  dp[0][0][abs(c[0][0])] = true;

  rep(i, H) rep(j, W) rep(k, 82 * (H + W)) {
    if (i < H - 1 && dp[i][j][k]) {
      dp[i + 1][j][k + abs(c[i + 1][j])] = true;
      dp[i + 1][j][abs(k - abs(c[i + 1][j]))] = true;
    }
    if (i < W - 1 && dp[i][j][k]) {
      dp[i][j + 1][k + abs(c[i][j + 1])] = true;
      dp[i][j + 1][abs(k - abs(c[i][j + 1]))] = true;
    }
  }

  ll ans = INF;
  rep(i, 82 * (H + W)) {
    if (dp[H - 1][W - 1][i])
      ans = i < ans ? i : ans;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
const long long INF = 1e17;

int main() {
  ll H, W;
  cin >> H >> W;
  vector<vector<ll>> a(H, vector<ll>(W));
  vector<vector<ll>> b(H, vector<ll>(W));
  vector<vector<ll>> c(H, vector<ll>(W));
  rep(i, H) rep(j, W) cin >> a[i][j];
  rep(i, H) rep(j, W) cin >> b[i][j];
  rep(i, H) rep(j, W) c[i][j] = b[i][j] - a[i][j];
  vector<vector<vector<bool>>> dp(
      H, vector<vector<bool>>(W, vector<bool>(82 * (H + W), false)));
  dp[0][0][abs(c[0][0])] = true;

  rep(i, H) rep(j, W) rep(k, 82 * (H + W)) {
    if (i < H - 1 && dp[i][j][k]) {
      dp[i + 1][j][k + abs(c[i + 1][j])] = true;
      dp[i + 1][j][abs(k - abs(c[i + 1][j]))] = true;
    }
    if (j < W - 1 && dp[i][j][k]) {
      dp[i][j + 1][k + abs(c[i][j + 1])] = true;
      dp[i][j + 1][abs(k - abs(c[i][j + 1]))] = true;
    }
  }

  ll ans = INF;
  rep(i, 82 * (H + W)) {
    if (dp[H - 1][W - 1][i])
      ans = i < ans ? i : ans;
  }
  cout << ans << endl;

  return 0;
}
Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define x first
#define y second
#define fastio ios_base::sync_with_stdio(0), cin.tie(0)
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;

int n, m;
int a[80][80];
int b[80][80];
bool cache[80][80][12805];

const int dx[] = {1, 0};
const int dy[] = {0, 1};

bool f(int x, int y, int k) {
  cache[x][y][k] = true;

  for (int d = 0; d < 2; d++) {
    int nx = x + dx[d], ny = y + dy[d];
    if (!(0 <= nx && nx < n && 0 <= ny && ny < m))
      continue;
    int t = abs(a[nx][ny] - b[nx][ny]);
    f(nx, ny, k + t);
    f(nx, ny, abs(k - t));
  }
}

int main() {
  fastio;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> a[i][j];

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> b[i][j];

  f(0, 0, abs(a[0][0] - b[0][0]));

  for (int i = 0; i <= 12800; i++)
    if (cache[n - 1][m - 1][i]) {
      cout << i << '\n';
      return 0;
    }
}

Fixed submission:
#include <bits/stdc++.h>
#define x first
#define y second
#define fastio ios_base::sync_with_stdio(0), cin.tie(0)
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;

int n, m;
int a[80][80];
int b[80][80];
bool cache[80][80][12805];

const int dx[] = {1, 0};
const int dy[] = {0, 1};

void f(int x, int y, int k) {
  if (cache[x][y][k])
    return;
  cache[x][y][k] = true;

  for (int d = 0; d < 2; d++) {
    int nx = x + dx[d], ny = y + dy[d];
    if (!(0 <= nx && nx < n && 0 <= ny && ny < m))
      continue;
    int t = abs(a[nx][ny] - b[nx][ny]);
    f(nx, ny, k + t);
    f(nx, ny, abs(k - t));
  }
}

int main() {
  fastio;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> a[i][j];

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> b[i][j];

  f(0, 0, abs(a[0][0] - b[0][0]));

  for (int i = 0; i <= 12800; i++)
    if (cache[n - 1][m - 1][i]) {
      cout << i << '\n';
      return 0;
    }
}
Problem p02839 (Python)

Buggy submission (status: Runtime Error):
H, W = map(int, input().split())
M = (H + W) * 80

A = [tuple(map(int, input().split())) for _ in range(H)]
B = [tuple(map(int, input().split())) for _ in range(H)]

dp = [[0] * W for _ in range(H)]
dp[0][0] = 1 << (M - abs(A[0][0] - B[0][0]))

for h in range(H):
    for w, (a, b) in enumerate(zip(A[h], B[h])):
        d = abs(a - b)
        if d == 0:
            continue
        mask = 0
        if h > 0:
            mask |= dp[h - 1][w] << d
            mask |= dp[h - 1][w] >> d
        if w > 0:
            mask |= dp[h][w - 1] << d
            mask |= dp[h][w - 1] >> d
        dp[h][w] |= mask

state = dp[-1][-1]
nums = []
for digit, b in enumerate(bin(state)[2:][::-1]):
    if b == "1":
        nums.append(digit - M)
ans = min(abs(n) for n in nums)
print(ans)


Fixed submission:
H, W = map(int, input().split())
M = (H + W) * 80

A = [tuple(map(int, input().split())) for _ in range(H)]
B = [tuple(map(int, input().split())) for _ in range(H)]

dp = [[0] * W for _ in range(H)]
dp[0][0] = 1 << (M - abs(A[0][0] - B[0][0]))

for h in range(H):
    for w, (a, b) in enumerate(zip(A[h], B[h])):
        d = abs(a - b)
        mask = 0
        if h > 0:
            mask |= dp[h - 1][w] << d
            mask |= dp[h - 1][w] >> d
        if w > 0:
            mask |= dp[h][w - 1] << d
            mask |= dp[h][w - 1] >> d
        dp[h][w] |= mask

state = dp[-1][-1]
nums = []
for digit, b in enumerate(bin(state)[2:][::-1]):
    if b == "1":
        nums.append(digit - M)
ans = min(abs(n) for n in nums)
print(ans)
Problem p02839 (Python)

Buggy submission (status: Time Limit Exceeded):
H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]

Z = sum(sum(a) for a in A) + sum(sum(b) for b in B) + 10

dp = [[0] * (W + 1) for _ in range(H + 1)]
dp[0][0] = 1 << Z

for h in range(H):
    for w in range(W):
        d = dp[h][w]
        k = abs(A[h][w] - B[h][w])

        dp[h + 1][w] |= (d << k) | (d >> k)
        dp[h][w + 1] |= (d << k) | (d >> k)

ans = 10**18
for a in (dp[H][W - 1], dp[H - 1][W]):
    for d in range(Z * 2 + 10):
        if a & (1 << d) != 0:
            ans = min(ans, abs(d - Z))
print(ans)


Fixed submission:
H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]

Z = 1000

dp = [[0] * (W + 1) for _ in range(H + 1)]
dp[0][0] = 1 << Z

for h in range(H):
    for w in range(W):
        d = dp[h][w]
        k = abs(A[h][w] - B[h][w])

        dp[h + 1][w] |= (d << k) | (d >> k)
        dp[h][w + 1] |= (d << k) | (d >> k)

ans = 10**18
for a in (dp[H][W - 1], dp[H - 1][W]):
    for d in range(Z * 2 + 10):
        if a & (1 << d) != 0:
            ans = min(ans, abs(d - Z))
print(ans)
Problem p02839 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    B_MAX = (80 + 80 - 1) * 80
    H, W = list(map(int, input().split(" ")))
    A = [list(map(int, input().split(" "))) for _ in range(H)]
    B = [list(map(int, input().split(" "))) for _ in range(H)]
    dp = [[[0 for _ in range(B_MAX + 1)] for _ in range(W)] for _ in range(H)]
    dp[0][0][abs(A[0][0] - B[0][0])] = 1
    max_b = abs(A[0][0] - B[0][0])
    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            d = abs(A[h][w] - B[h][w])
            max_b += d
            for b in range(min(B_MAX, max_b) + 1):
                v = 0
                if h > 0:
                    v = max(v, dp[h - 1][w][abs(b - d)])
                    if b + d <= B_MAX:
                        v = max(v, dp[h - 1][w][b + d])
                if w > 0:
                    v = max(v, dp[h][w - 1][abs(b - d)])
                    if b + d <= B_MAX:
                        v = max(v, dp[h][w - 1][b + d])
                dp[h][w][b] = v
    ans = 0
    while dp[H - 1][W - 1][ans] == 0:
        ans += 1
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    B_MAX = (80 + 80 - 1) * 80 // 2
    H, W = list(map(int, input().split(" ")))
    A = [list(map(int, input().split(" "))) for _ in range(H)]
    B = [list(map(int, input().split(" "))) for _ in range(H)]
    dp = [[[0 for _ in range(B_MAX + 1)] for _ in range(W)] for _ in range(H)]
    dp[0][0][abs(A[0][0] - B[0][0])] = 1
    max_b = abs(A[0][0] - B[0][0])
    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            d = abs(A[h][w] - B[h][w])
            max_b += d
            for b in range(min(B_MAX, max_b) + 1):
                v = 0
                if h > 0:
                    v = max(v, dp[h - 1][w][abs(b - d)])
                    if b + d <= B_MAX:
                        v = max(v, dp[h - 1][w][b + d])
                if w > 0:
                    v = max(v, dp[h][w - 1][abs(b - d)])
                    if b + d <= B_MAX:
                        v = max(v, dp[h][w - 1][b + d])
                dp[h][w][b] = v
    ans = 0
    while dp[H - 1][W - 1][ans] == 0:
        ans += 1
    print(ans)


if __name__ == "__main__":
    main()