Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

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

  int H, W, K;
  cin >> H >> W >> K;
  vector<string> S(H);
  rep(i, H) cin >> S[i];

  int ans = MOD;
  for (int bit = 0; bit < (1 << (H - 1)); bit++) {
    int Z = __builtin_popcount(bit);
    vector<int> line;
    line.push_back(0);
    for (int i = 0; i < H - 1; i++)
      if (bit & (1 << i))
        line.push_back(i + 1);

    vector<int> trans(Z + 1, 0);
    int now = 0;
    for (int i = 0; i < H; i++) {
      if (now + 1 < line.size() && line[now + 1] == i)
        now++;
      trans[i] = now;
    }

    vector<int> num(Z + 1, 0);
    bool ng = false;
    int res = 0;
    for (int j = 0; j < W; j++) {
      vector<int> nxt(Z + 1, 0);
      for (int i = 0; i < H; i++)
        if (S[i][j] == '1')
          nxt[trans[i]]++;
      for (int i = 0; i < nxt.size(); i++)
        if (nxt[i] > K)
          ng = true;
      if (ng)
        break;

      bool over = false;
      for (int i = 0; i < num.size(); i++)
        if (num[i] + nxt[i] > K)
          over = true;
      if (over) {
        res++;
        num = nxt;
      } else {
        for (int i = 0; i < num.size(); i++)
          num[i] += nxt[i];
      }
    }

    if (ng)
      continue;
    chmin(ans, res + Z);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

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

  int H, W, K;
  cin >> H >> W >> K;
  vector<string> S(H);
  rep(i, H) cin >> S[i];

  int ans = MOD;
  for (int bit = 0; bit < (1 << (H - 1)); bit++) {
    int Z = __builtin_popcount(bit);
    vector<int> line;
    line.push_back(0);
    for (int i = 0; i < H - 1; i++)
      if (bit & (1 << i))
        line.push_back(i + 1);

    vector<int> trans(H, 0);
    int now = 0;
    for (int i = 0; i < H; i++) {
      if (now + 1 < line.size() && line[now + 1] == i)
        now++;
      trans[i] = now;
    }

    vector<int> num(Z + 1, 0);
    bool ng = false;
    int res = 0;
    for (int j = 0; j < W; j++) {
      vector<int> nxt(Z + 1, 0);
      for (int i = 0; i < H; i++)
        if (S[i][j] == '1')
          nxt[trans[i]]++;
      for (int i = 0; i < nxt.size(); i++)
        if (nxt[i] > K)
          ng = true;
      if (ng)
        break;

      bool over = false;
      for (int i = 0; i < num.size(); i++)
        if (num[i] + nxt[i] > K)
          over = true;
      if (over) {
        res++;
        num = nxt;
      } else {
        for (int i = 0; i < num.size(); i++)
          num[i] += nxt[i];
      }
    }

    if (ng)
      continue;
    chmin(ans, res + Z);
  }

  cout << ans << endl;

  return 0;
}