Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

int main() {

  int h, w, kk;
  cin >> h >> w >> kk;
  vector<vector<int>> v(h, vector<int>(w, 0));

  rep(i, h) {
    string s;
    cin >> s;
    int ssum = 0;
    rep(j, s.size()) {
      v[i][j] = (s[j] == '1' ? 1 : 0);
      if (s[j] == '1') {
        ssum++;
      }
    }
  }

  ll zen = pow(2, h - 1) + 1;
  ll ans = MOD;

  for (int i = 0; i < zen; i++) {

    ll k = i;
    vector<int> q;
    q.push_back(0);
    for (int j = 0; j < h - 1; j++) {
      if ((k >> j) & 1) {
        q.push_back(j + 1);
      }
    }

    q.push_back(h);
    bool ff = false;
    vector<int> rui(q.size() - 1, 0);
    vector<bool> line(w, false);
    ll ssum = 0;

    for (int j = 0; j < w; j++) {
      vector<int> srui(q.size() - 1, 0);

      for (int jj = 0; jj < q.size() - 1; jj++) {
        int ksum = 0;
        for (int t = q[jj]; t < q[jj + 1]; t++) {
          if (v[t][j]) {
            srui[jj]++;
          }
        }
        if (srui[jj] > kk) {
          ff = true;
          break;
        }
      }
      if (ff)
        break;
      bool f = false;
      rep(jk, q.size()) {
        if (srui[jk] + rui[jk] > kk) {
          f = true;
        } else {
          rui[jk] += srui[jk];
        }
      }
      if (f) {
        ssum++;
        rui = srui;
      }
    }

    if (!ff) {
      ssum += q.size() - 2;
      ans = min(ans, ssum);
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

int main() {

  int h, w, kk;
  cin >> h >> w >> kk;
  vector<vector<int>> v(h, vector<int>(w, 0));

  rep(i, h) {
    string s;
    cin >> s;
    int ssum = 0;
    rep(j, s.size()) {
      v[i][j] = (s[j] == '1' ? 1 : 0);
      if (s[j] == '1') {
        ssum++;
      }
    }
  }

  ll zen = pow(2, h - 1) + 1;
  ll ans = MOD;

  for (int i = 0; i < zen; i++) {

    ll k = i;
    vector<int> q;
    q.push_back(0);
    for (int j = 0; j < h - 1; j++) {
      if ((k >> j) & 1) {
        q.push_back(j + 1);
      }
    }

    q.push_back(h);
    bool ff = false;
    vector<int> rui(q.size() - 1, 0);
    vector<bool> line(w, false);
    ll ssum = 0;

    for (int j = 0; j < w; j++) {
      vector<int> srui(q.size() - 1, 0);

      for (int jj = 0; jj < q.size() - 1; jj++) {
        int ksum = 0;
        for (int t = q[jj]; t < q[jj + 1]; t++) {
          if (v[t][j]) {
            srui[jj]++;
          }
        }
        if (srui[jj] > kk) {
          ff = true;
          break;
        }
      }
      if (ff)
        break;
      bool f = false;
      rep(jk, q.size() - 1) {
        if (srui[jk] + rui[jk] > kk) {
          f = true;
        } else {
          rui[jk] += srui[jk];
        }
      }
      if (f) {
        ssum++;
        rui = srui;
      }
    }

    if (!ff) {
      ssum += q.size() - 2;
      ans = min(ans, ssum);
    }
  }
  cout << ans << endl;

  return 0;
}