Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int H, W;
vector<vector<int>> A, B, C;

using mybit = bitset<12800>;

int main() {
  int ans = 0;
  cin >> H;
  cin >> W;
  A = B = C = vector<vector<int>>(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];
      C[i][j] = abs(A[i][j] - B[i][j]);
    }
  }

  auto sum_list = vector<vector<mybit>>(H, vector<mybit>(W));

  sum_list[0][0].set(C[0][0]);

  for (int i = 1; i < H + W; i++) {
    for (int j = max(i - H, 0); j <= min(i, H - 1); j++) {
      int x = i - j;
      int y = j;

      if (x > 0) {
        mybit left = sum_list[y][x - 1] << C[y][x];
        mybit right = sum_list[y][x - 1] >> C[y][x];
        string os = sum_list[y][x - 1].to_string();
        reverse(os.begin(), os.end());
        os.erase(C[y][x]);
        mybit over(os);
        over <<= 1;

        sum_list[y][x] |= left | right | over;
      }
      if (y > 0) {
        mybit left = sum_list[y - 1][x] << C[y][x];
        mybit right = sum_list[y - 1][x] >> C[y][x];
        string os = sum_list[y - 1][x].to_string();
        reverse(os.begin(), os.end());
        os.erase(C[y][x]);
        mybit over(os);
        over <<= 1;

        sum_list[y][x] |= left | right | over;
      }
    }
  }

  while (!sum_list[H - 1][W - 1].test(ans)) {
    ans++;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int H, W;
vector<vector<int>> A, B, C;

using mybit = bitset<12800>;

int main() {
  int ans = 0;
  cin >> H;
  cin >> W;
  A = B = C = vector<vector<int>>(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];
      C[i][j] = abs(A[i][j] - B[i][j]);
    }
  }

  auto sum_list = vector<vector<mybit>>(H, vector<mybit>(W));

  sum_list[0][0].set(C[0][0]);

  for (int i = 1; i < H + W; i++) {
    for (int x = min(i, W - 1); x >= 0; x--) {
      int y = i - x;
      if (y >= H) {
        break;
      }

      if (x > 0) {
        mybit left = sum_list[y][x - 1] << C[y][x];
        mybit right = sum_list[y][x - 1] >> C[y][x];
        string os = sum_list[y][x - 1].to_string();
        reverse(os.begin(), os.end());
        os.erase(C[y][x]);
        mybit over(os);
        over <<= 1;

        sum_list[y][x] |= left | right | over;
      }
      if (y > 0) {
        mybit left = sum_list[y - 1][x] << C[y][x];
        mybit right = sum_list[y - 1][x] >> C[y][x];
        string os = sum_list[y - 1][x].to_string();
        reverse(os.begin(), os.end());
        os.erase(C[y][x]);
        mybit over(os);
        over <<= 1;

        sum_list[y][x] |= left | right | over;
      }
    }
  }

  while (!sum_list[H - 1][W - 1].test(ans)) {
    ans++;
  }

  cout << ans << endl;

  return 0;
}