Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // minmax, sort, swap
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>     // printf, scanf
#include <deque>      // deque
#include <functional> // function<void(int)>
#include <iomanip>    // cout<<setprecision(n)
#include <iostream>   // cin, cout, cerr
#include <map>        // key-value pairs sorted by keys
#include <numeric>    // iota
#include <queue>      // queue, priority_queue
#include <set>        // set
#include <string>     // string, stoi, to_string
#include <vector>     // vector

#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif

#define int long long // at least int64 > 9*10^18
#define EL '\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
/* libraries */

signed main() {
  int h, w, k;
  std::cin >> h >> w >> k;
  std::vector<std::string> g(h);
  rep(i, h) std::cin >> g[i];
  const int INF = 1e9;
  int min = INF;
  rep(c, 1 << (h - 1)) {
    int count = __builtin_popcount(c);
    int bc = count;
    std::vector<int> ac(bc + 1, 0);
    rep(i, w) {
      int j = 0;
      std::vector<int> ad(bc + 1, 0);
      rep(xx, h) {
        ad[j] += (g[xx][i] == '1');
        if (c >> xx & 1)
          j++;
      }
      int brk = false;
      rep(xx, bc + 1) {
        if (ad[xx] > k) {
          goto impossible;
        }
        if (!brk && ac[xx] + ad[xx] > k) {
          if (i != 0)
            count++;
          ac = ad;
          brk = true;
        }
      }
      if (!brk) {
        rep(xx, count + 1) ac[xx] += ad[xx];
      }
    }
    min = std::min(min, count);
  impossible:;
  }
  print(min);

  return 0;
}


Fixed submission:
#include <algorithm> // minmax, sort, swap
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>     // printf, scanf
#include <deque>      // deque
#include <functional> // function<void(int)>
#include <iomanip>    // cout<<setprecision(n)
#include <iostream>   // cin, cout, cerr
#include <map>        // key-value pairs sorted by keys
#include <numeric>    // iota
#include <queue>      // queue, priority_queue
#include <set>        // set
#include <string>     // string, stoi, to_string
#include <vector>     // vector

#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif

#define int long long // at least int64 > 9*10^18
#define EL '\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
/* libraries */

signed main() {
  int h, w, k;
  std::cin >> h >> w >> k;
  std::vector<std::string> g(h);
  rep(i, h) std::cin >> g[i];
  const int INF = 1e9;
  int min = INF;
  rep(c, 1 << (h - 1)) {
    int count = __builtin_popcount(c);
    int bc = count;
    std::vector<int> ac(bc + 1, 0);
    rep(i, w) {
      int j = 0;
      std::vector<int> ad(bc + 1, 0);
      rep(xx, h) {
        ad[j] += (g[xx][i] == '1');
        if (c >> xx & 1)
          j++;
      }
      int brk = false;
      rep(xx, bc + 1) {
        if (ad[xx] > k) {
          goto impossible;
        }
        if (!brk && ac[xx] + ad[xx] > k) {
          if (i != 0)
            count++;
          ac = ad;
          brk = true;
        }
      }
      if (!brk) {
        rep(xx, bc + 1) ac[xx] += ad[xx];
      }
    }
    min = std::min(min, count);
  impossible:;
  }
  print(min);

  return 0;
}