Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// #define int long long
typedef int ll;
typedef long double ld;
const ll N = 200005;
char en = '\n';
ll inf = 1e9;
ll mod = 1e9 + 7;
ll power(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll h, w, k;
  cin >> h >> w >> k;

  char tmp[h + 5][w + 5];

  ll arr[h + 5][w + 5];
  memset(arr, 0, sizeof(arr));
  for (ll i = 1; i <= h; i++) {
    cin >> (tmp[i] + 1);
    for (ll j = 1; j <= w; j++)
      arr[i][j] = tmp[i][j] - '0';
  }

  ll pre[h + 5][w + 5];

  memset(pre, 0, sizeof(pre));
  for (ll i = 1; i <= h; i++) {
    for (ll j = 1; j <= w; j++) {
      pre[i][j] = arr[i][j] + pre[i - 1][j];
    }
  }

  ll ans = inf;

  for (ll mask = 0; mask < (1 << (h - 1)); mask++) {
    vector<ll> cuts;
    for (ll j = 0; j < mask; j++) {
      if ((mask >> j) & 1) {
        cuts.push_back(j + 1);
      }
    }

    ll prev1 = 0;
    vector<vector<ll>> arr2;
    bool bad = false;
    for (ll &x : cuts) {
      vector<ll> tmp(w + 1, 0);
      for (ll j = 1; j <= w; j++) {
        tmp[j] = pre[x][j] - pre[prev1][j];
        if (tmp[j] > k)
          bad = true;
      }
      prev1 = x;
      arr2.push_back(tmp);
    }
    vector<ll> tmp(w + 1, 0);
    for (ll j = 1; j <= w; j++) {
      tmp[j] = pre[h][j] - pre[prev1][j];
      if (tmp[j] > k)
        bad = true;
    }
    if (bad)
      continue;
    arr2.push_back(tmp);

    ll res = cuts.size();
    ll sum1[10];
    memset(sum1, 0, sizeof(sum1));
    for (ll j = 1; j <= w; j++) {

      bool bad = false;
      for (ll i = 0; i < arr2.size(); i++) {
        sum1[i] += arr2[i][j];
        if (sum1[i] > k) {
          bad = true;
        }
      }
      if (bad) {
        res++;
        j--;
        memset(sum1, 0, sizeof(sum1));
      }
    }
    ans = min(ans, res);
  }
  cout << ans << en;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// #define int long long
typedef int ll;
typedef long double ld;
const ll N = 200005;
char en = '\n';
ll inf = 1e9;
ll mod = 1e9 + 7;
ll power(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll h, w, k;
  cin >> h >> w >> k;

  char tmp[h + 5][w + 5];

  ll arr[h + 5][w + 5];
  memset(arr, 0, sizeof(arr));
  for (ll i = 1; i <= h; i++) {
    cin >> (tmp[i] + 1);
    for (ll j = 1; j <= w; j++)
      arr[i][j] = tmp[i][j] - '0';
  }

  ll pre[h + 5][w + 5];

  memset(pre, 0, sizeof(pre));
  for (ll i = 1; i <= h; i++) {
    for (ll j = 1; j <= w; j++) {
      pre[i][j] = arr[i][j] + pre[i - 1][j];
    }
  }

  ll ans = inf;

  for (ll mask = 0; mask < (1 << (h - 1)); mask++) {
    vector<ll> cuts;
    for (ll j = 0; j < (h - 1); j++) {
      if ((mask >> j) & 1) {
        cuts.push_back(j + 1);
      }
    }

    ll prev1 = 0;
    vector<vector<ll>> arr2;
    bool bad = false;
    for (ll &x : cuts) {
      vector<ll> tmp(w + 1, 0);
      for (ll j = 1; j <= w; j++) {
        tmp[j] = pre[x][j] - pre[prev1][j];
        if (tmp[j] > k)
          bad = true;
      }
      prev1 = x;
      arr2.push_back(tmp);
    }
    vector<ll> tmp(w + 1, 0);
    for (ll j = 1; j <= w; j++) {
      tmp[j] = pre[h][j] - pre[prev1][j];
      if (tmp[j] > k)
        bad = true;
    }
    if (bad)
      continue;
    arr2.push_back(tmp);

    ll res = cuts.size();
    ll sum1[10];
    memset(sum1, 0, sizeof(sum1));
    for (ll j = 1; j <= w; j++) {

      bool bad = false;
      for (ll i = 0; i < arr2.size(); i++) {
        sum1[i] += arr2[i][j];
        if (sum1[i] > k) {
          bad = true;
        }
      }
      if (bad) {
        res++;
        j--;
        memset(sum1, 0, sizeof(sum1));
      }
    }
    ans = min(ans, res);
  }
  cout << ans << en;

  return 0;
}