Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define pow(x, y) modpow(x, y)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)

template <class T = int> T in() {
  T x;
  cin >> x;
  return (x);
}

/* modとる */
inline ll modu(ll a) { return (a % mod + mod) % mod; }

/* modつき二分累乗法 */
ll modpow(ll a, ll b) {
  ll res = 1;
  for (a = modu(a); b; a = modu(a * a), b >>= 1) {
    if (b & 1)
      res = modu(res * a);
  }
  return res;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int H = in(), W = in(), K = in();
  vector<vector<int>> S(H + 1, vector<int>(W + 1));
  REP(i, H + 1) {
    REP(l, W + 1) {
      if (i == 0) {
        S[i][l] = 0;
      } else if (l == 0) {
        S[i][l] = 0;
      } else {
        S[i][l] = S[i - 1][l] + S[i][l - 1] - S[i - 1][l - 1] +
                  (in<char>() == '1' ? 1 : 0);
      }
    }
  }
  int answer = INT32_MAX - 50;
  vector<int> where;
  REP(i, modpow(2, H - 1)) {
    int tmpe = i;
    where.assign(1, false);
    int counn = 0;
    while (tmpe != 0) {
      if (tmpe % 2 == 1)
        where.push_back(counn + 1);
      counn++;
      tmpe = tmpe >> 1;
    }
    where.push_back(H);
    int point = 0;
    int beforecut = 0;
    REP(i, W) {
      bool out = 0;
      REP(l, where.size()) {
        if (S[where[l + 1]][i + 1] - S[where[l]][i + 1] -
                S[where[l + 1]][beforecut] + S[where[l]][beforecut] >
            K) {
          if (beforecut == i) {
            out = 1;
            break;
          }
          beforecut = i;
          point++;
          break;
        }
      }
      if (out) {
        point = INT32_MAX - 50000;
        break;
      }
    }
    int wheresize = where.size();
    answer = min(answer, point + wheresize - 2);
  }
  cout << answer << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define pow(x, y) modpow(x, y)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)

template <class T = int> T in() {
  T x;
  cin >> x;
  return (x);
}

/* modとる */
inline ll modu(ll a) { return (a % mod + mod) % mod; }

/* modつき二分累乗法 */
ll modpow(ll a, ll b) {
  ll res = 1;
  for (a = modu(a); b; a = modu(a * a), b >>= 1) {
    if (b & 1)
      res = modu(res * a);
  }
  return res;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int H = in(), W = in(), K = in();
  vector<vector<int>> S(H + 1, vector<int>(W + 1));
  REP(i, H + 1) {
    REP(l, W + 1) {
      if (i == 0) {
        S[i][l] = 0;
      } else if (l == 0) {
        S[i][l] = 0;
      } else {
        S[i][l] = S[i - 1][l] + S[i][l - 1] - S[i - 1][l - 1] +
                  (in<char>() == '1' ? 1 : 0);
      }
    }
  }
  int answer = INT32_MAX - 50;
  vector<int> where;
  REP(i, modpow(2, H - 1)) {
    int tmpe = i;
    where.assign(1, false);
    int counn = 0;
    while (tmpe != 0) {
      if (tmpe % 2 == 1)
        where.push_back(counn + 1);
      counn++;
      tmpe = tmpe >> 1;
    }
    where.push_back(H);
    int point = 0;
    int beforecut = 0;
    REP(i, W) {
      bool out = 0;
      REP(l, where.size() - 1) {
        if (S[where[l + 1]][i + 1] - S[where[l]][i + 1] -
                S[where[l + 1]][beforecut] + S[where[l]][beforecut] >
            K) {
          if (beforecut == i) {
            out = 1;
            break;
          }
          beforecut = i;
          point++;
          break;
        }
      }
      if (out) {
        point = INT32_MAX - 50000;
        break;
      }
    }
    int wheresize = where.size();
    answer = min(answer, point + wheresize - 2);
  }
  cout << answer << "\n";
}