Problem p02839 (C++)

Buggy submission (status: Runtime Error):
// pragma
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

#define YOU using
#define DONT namespace
#define SAY std

YOU DONT SAY;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define REPP(i, a, b, d) for (int i = a; i <= b; i += d)
#define REP(i, a, b) REPP(i, a, b, 1)
#define REVV(i, a, b, d) for (int i = a; i >= b; i -= d)
#define REV(i, a, b) REVV(i, a, b, 1)

#define FOR(i, a) for (int i = 0; i < a; i++)
#define FORD(i, a) for (int i = (int)a - 1; i >= 0; i--)

#define pb push_back
#define F first
#define S second

const int OO = 1e9;
const ll INF = 1e18;

const int irand(int lo, int hi) {
  return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo;
}

const ll lrand(ll lo, ll hi) {
  return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo;
}

#define getc getchar
template <typename T> T getnum() {
  int sign = 1;
  T ret = 0;
  char c;

  do {
    c = getc();
  } while (c == ' ' || c == '\n');
  if (c == '-')
    sign = -1;
  else
    ret = c - '0';
  while (1) {
    c = getc();
    if (c < '0' || c > '9')
      break;
    ret = 10 * ret + c - '0';
  }
  return sign * ret;
}

inline void ini(int &x) { x = getnum<int>(); }

inline void scani(int &x) { scanf("%d", &x); }

// end of macro

const int N = 5;

int h, w;
int arr[N][N];

bitset<4 * N * N + 5> btt[N][N]; // offset by 2*N*N
const int OFFSET = 2 * N * N;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#define endl '\n'
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(10);

  cin >> h >> w;
  FOR(i, h) {
    FOR(j, w) { cin >> arr[i][j]; }
  }
  FOR(i, h) {
    FOR(j, w) {
      int x;
      cin >> x;
      arr[i][j] -= x;
      arr[i][j] = abs(arr[i][j]);
    }
  }

  btt[0][0][arr[0][0] + OFFSET] = 1;

  FOR(i, h) {
    FOR(j, w) {
      if (i + 1 < h) {
        auto curr = btt[i][j];
        curr = (curr >> arr[i + 1][j]) | (curr << arr[i + 1][j]);

        btt[i + 1][j] |= curr;
      }
      if (j + 1 < w) {
        auto curr = btt[i][j];
        curr = (curr >> arr[i][j + 1]) | (curr << arr[i][j + 1]);

        btt[i][j + 1] |= curr;
      }

      //			printf("%d %d\n",i,j);
      //			FOR(k,4*N*N+5){
      //				if(btt[i][j][k] == 1)printf("%d ",k -
      //OFFSET); 			}printf("\n");
    }
  }

  int ans = OO;
  //	cout << btt[h-1][w-1] << endl;
  FOR(i, 4 * N * N + 5) {
    if (btt[h - 1][w - 1][i] == 1) {
      //			printf("%d\n",i - OFFSET);
      ans = min(ans, abs(i - OFFSET));
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
// pragma
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

#define YOU using
#define DONT namespace
#define SAY std

YOU DONT SAY;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define REPP(i, a, b, d) for (int i = a; i <= b; i += d)
#define REP(i, a, b) REPP(i, a, b, 1)
#define REVV(i, a, b, d) for (int i = a; i >= b; i -= d)
#define REV(i, a, b) REVV(i, a, b, 1)

#define FOR(i, a) for (int i = 0; i < a; i++)
#define FORD(i, a) for (int i = (int)a - 1; i >= 0; i--)

#define pb push_back
#define F first
#define S second

const int OO = 1e9;
const ll INF = 1e18;

const int irand(int lo, int hi) {
  return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo;
}

const ll lrand(ll lo, ll hi) {
  return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo;
}

#define getc getchar
template <typename T> T getnum() {
  int sign = 1;
  T ret = 0;
  char c;

  do {
    c = getc();
  } while (c == ' ' || c == '\n');
  if (c == '-')
    sign = -1;
  else
    ret = c - '0';
  while (1) {
    c = getc();
    if (c < '0' || c > '9')
      break;
    ret = 10 * ret + c - '0';
  }
  return sign * ret;
}

inline void ini(int &x) { x = getnum<int>(); }

inline void scani(int &x) { scanf("%d", &x); }

// end of macro

const int N = 80;

int h, w;
int arr[N][N];

bitset<4 * N * N + 5> btt[N][N]; // offset by 2*N*N
const int OFFSET = 2 * N * N;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#define endl '\n'
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(10);

  cin >> h >> w;
  FOR(i, h) {
    FOR(j, w) { cin >> arr[i][j]; }
  }
  FOR(i, h) {
    FOR(j, w) {
      int x;
      cin >> x;
      arr[i][j] -= x;
      arr[i][j] = abs(arr[i][j]);
    }
  }

  btt[0][0][arr[0][0] + OFFSET] = 1;

  FOR(i, h) {
    FOR(j, w) {
      if (i + 1 < h) {
        auto curr = btt[i][j];
        curr = (curr >> arr[i + 1][j]) | (curr << arr[i + 1][j]);

        btt[i + 1][j] |= curr;
      }
      if (j + 1 < w) {
        auto curr = btt[i][j];
        curr = (curr >> arr[i][j + 1]) | (curr << arr[i][j + 1]);

        btt[i][j + 1] |= curr;
      }

      //			printf("%d %d\n",i,j);
      //			FOR(k,4*N*N+5){
      //				if(btt[i][j][k] == 1)printf("%d ",k -
      //OFFSET); 			}printf("\n");
    }
  }

  int ans = OO;
  //	cout << btt[h-1][w-1] << endl;
  FOR(i, 4 * N * N + 5) {
    if (btt[h - 1][w - 1][i] == 1) {
      //			printf("%d\n",i - OFFSET);
      ans = min(ans, abs(i - OFFSET));
    }
  }

  cout << ans << endl;

  return 0;
}