Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pp pair<ll, ll>
typedef tree<pp, null_type, less<pp>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define ld long double
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (ll i = a; i < b; i += 1)
#define sz(v) (ll)(v.size())
#define hell (ll)1000000007
#define slld(x) scanf("%lld", &x)
ll t = 1;
const ll inf = hell, N = 305;
inline ll mult(ll x, ll y) { return (x * y) % hell; }
inline ll powMod(ll base, ll expo) {
  ll ans = 1;
  while (expo) {
    if (expo & 1)
      ans = mult(ans, base);
    base = mult(base, base);
    expo >>= 1;
  }
  return ans;
}
void solve() {
  ll h, w, k, f, res = inf, ans;
  cin >> h >> w >> k;
  char c;
  ll a[h + 1][w + 1];
  rep(i, 1, h + 1) {
    rep(j, 1, w + 1) {
      cin >> c;
      a[i][j] = c - '0';
    }
  }
  map<ll, vector<ll>> mapa;
  rep(mask, 0, (1 << (h - 1))) {
    ans = 0;
    mapa.clear();
    rep(i, 1, h + 1) {
      mapa[ans].pb(i);
      if ((mask >> (i - 1)) & 1)
        ans++;
    }
    vector<ll> cnt(ans + 1, 0);
    rep(i, 1, w + 1) {
      f = 1;
      vector<ll> tmp(ans + 1, 0);
      for (auto x : mapa) {
        for (auto y : x.se)
          tmp[x.fi] += a[y][i];
        if (cnt[x.fi] + tmp[x.fi] > k)
          f = 0;
      }
      for (auto x : mapa) {
        f == 1 ? cnt[x.fi] += tmp[x.fi] : cnt[x.fi] = tmp[x.fi];
        if (cnt[x.fi] > k)
          ans = inf;
      }
      if (f == 0)
        ans++;
    }
    res = min(res, ans);
  }
  cout << res << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // cin >> t ;
  while (t--) {
    solve();
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pp pair<ll, ll>
typedef tree<pp, null_type, less<pp>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define ld long double
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (ll i = a; i < b; i += 1)
#define sz(v) (ll)(v.size())
#define hell (ll)1000000007
#define slld(x) scanf("%lld", &x)
ll t = 1;
const ll inf = hell, N = 305;
inline ll mult(ll x, ll y) { return (x * y) % hell; }
inline ll powMod(ll base, ll expo) {
  ll ans = 1;
  while (expo) {
    if (expo & 1)
      ans = mult(ans, base);
    base = mult(base, base);
    expo >>= 1;
  }
  return ans;
}
void solve() {
  ll h, w, k, f, res = inf, ans;
  cin >> h >> w >> k;
  char c;
  ll a[h + 1][w + 1];
  rep(i, 1, h + 1) {
    rep(j, 1, w + 1) {
      cin >> c;
      a[i][j] = c - '0';
    }
  }
  map<ll, vector<ll>> mapa;
  rep(mask, 0, (1 << (h - 1))) {
    ans = 0;
    mapa.clear();
    rep(i, 1, h + 1) {
      mapa[ans].pb(i);
      if ((mask >> (i - 1)) & 1)
        ans++;
    }
    vector<ll> cnt(ans + 1, 0);
    rep(i, 1, w + 1) {
      f = 1;
      if (ans >= inf)
        continue;
      vector<ll> tmp(ans + 1, 0);
      for (auto x : mapa) {
        for (auto y : x.se)
          tmp[x.fi] += a[y][i];
        if (cnt[x.fi] + tmp[x.fi] > k)
          f = 0;
      }
      for (auto x : mapa) {
        f == 1 ? cnt[x.fi] += tmp[x.fi] : cnt[x.fi] = tmp[x.fi];
        if (cnt[x.fi] > k)
          ans = inf;
      }
      if (f == 0)
        ans++;
    }
    res = min(res, ans);
  }
  cout << res << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // cin >> t ;
  while (t--) {
    solve();
  }
  return 0;
}