Problem p02839 (C++)

Buggy submission (status: Runtime Error):
//    https://youtu.be/telPDj1VQjQ

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

typedef long long ll;
typedef long double ld;
typedef long long unsigned llu;
typedef pair<long long, long long> pll;
const long long inf = 2000000000000000000LL; // 2e18
#define pi acos(-1.0)
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define shesh "\n"
#define ff first
#define ss second
#define pb push_back
#define bp pop_back
#define pf push_front
#define fp pop_front
#define ub upper_bound
#define lb lower_bound
#define all(x) x.begin(), x.end()
#define debug(x) cout << "debug " << x << "\n"
#define mest(a, b) memset(a, b, sizeof(a))

int main() {
  // fast;
  ll t, n, m, i, j, k, temp, flag, ans, h, w;
  // cin>>h>>w;
  scanf("%lld%lld", &h, &w);
  ll a[h][w];
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      // cin>>a[i][j];
      scanf("%lld", &a[i][j]);
    }
  }
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      // cin>>b[i][j];
      scanf("%lld", &temp);
      a[i][j] = abs(a[i][j] - temp);
    }
  }
  vector<ll> s[h][w];
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (i + j == 0) {
        s[i][j].pb(a[i][j]);
        // s[i][j].insert(-1*c[i][j]);
        continue;
      }
      // cout<<i<<' '<<j<<' ';
      bool b[10010] = {};
      if (i - 1 >= 0) {
        for (auto it = s[i - 1][j].begin(); it != s[i - 1][j].end(); it++) {
          temp = *it;
          temp -= a[i][j];
          temp = abs(temp);
          // cout<<temp<<' ';
          if (!b[temp]) {
            s[i][j].pb(temp);
            b[temp] = true;
          }
          // s[i][j].insert(temp);
          temp = *it;
          temp += a[i][j];
          // cout<<temp<<' ';
          if (!b[temp]) {
            s[i][j].pb(temp);
            b[temp] = true;
          }
          // s[i][j].insert(temp);
        }
      }
      if (j - 1 >= 0) {
        for (auto it = s[i][j - 1].begin(); it != s[i][j - 1].end(); it++) {
          temp = *it;
          temp -= a[i][j];
          temp = abs(temp);
          // cout<<temp<<' ';
          if (!b[temp]) {
            s[i][j].pb(temp);
            b[temp] = true;
          }
          // s[i][j].insert(temp);
          temp = *it;
          temp += a[i][j];
          // cout<<temp<<' ';
          if (!b[temp]) {
            s[i][j].pb(temp);
            b[temp] = true;
          }
          // s[i][j].insert(temp);
        }
      }
      // cout<<endl;
    }
  }
  temp = (80 * 80) + 1;
  for (auto it = s[h - 1][w - 1].begin(); it != s[h - 1][w - 1].end(); it++)
    temp = min(temp, abs(*it));
  printf("%lld", temp);
  return 0;
}


Fixed submission:
//    https://youtu.be/telPDj1VQjQ

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

typedef long long ll;
typedef long double ld;
typedef long long unsigned llu;
typedef pair<long long, long long> pll;
const long long inf = 2000000000000000000LL; // 2e18
#define pi acos(-1.0)
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define shesh "\n"
#define ff first
#define ss second
#define pb push_back
#define bp pop_back
#define pf push_front
#define fp pop_front
#define ub upper_bound
#define lb lower_bound
#define all(x) x.begin(), x.end()
#define debug(x) cout << "debug " << x << "\n"
#define mest(a, b) memset(a, b, sizeof(a))

int main() {
  // fast;
  ll t, n, m, i, j, k, temp, flag, ans, h, w;
  // cin>>h>>w;
  scanf("%lld%lld", &h, &w);
  ll a[h][w];
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      // cin>>a[i][j];
      scanf("%lld", &a[i][j]);
    }
  }
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      // cin>>b[i][j];
      scanf("%lld", &temp);
      a[i][j] = abs(a[i][j] - temp);
    }
  }
  vector<ll> s[h][w];
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (i + j == 0) {
        s[i][j].pb(a[i][j]);
        // s[i][j].insert(-1*c[i][j]);
        continue;
      }
      // cout<<i<<' '<<j<<' ';
      bool b[20000] = {};
      if (i - 1 >= 0) {
        for (auto it = s[i - 1][j].begin(); it != s[i - 1][j].end(); it++) {
          temp = *it;
          temp -= a[i][j];
          temp = abs(temp);
          // cout<<temp<<' ';
          if (!b[temp]) {
            s[i][j].pb(temp);
            b[temp] = true;
          }
          // s[i][j].insert(temp);
          temp = *it;
          temp += a[i][j];
          // cout<<temp<<' ';
          if (!b[temp]) {
            s[i][j].pb(temp);
            b[temp] = true;
          }
          // s[i][j].insert(temp);
        }
      }
      if (j - 1 >= 0) {
        for (auto it = s[i][j - 1].begin(); it != s[i][j - 1].end(); it++) {
          temp = *it;
          temp -= a[i][j];
          temp = abs(temp);
          // cout<<temp<<' ';
          if (!b[temp]) {
            s[i][j].pb(temp);
            b[temp] = true;
          }
          // s[i][j].insert(temp);
          temp = *it;
          temp += a[i][j];
          // cout<<temp<<' ';
          if (!b[temp]) {
            s[i][j].pb(temp);
            b[temp] = true;
          }
          // s[i][j].insert(temp);
        }
      }
      // cout<<endl;
    }
  }
  temp = (80 * 80) + 1;
  for (auto it = s[h - 1][w - 1].begin(); it != s[h - 1][w - 1].end(); it++)
    temp = min(temp, abs(*it));
  printf("%lld", temp);
  return 0;
}