Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define MOD 1000000007
#define MAX 100005
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define mii map<int, int>
#define vi vector<int>
#define ff first
#define ss second
#define T                                                                      \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define umi unordered_map<int, int>
#define maxheap priority_queue<int>
#define minheap priority_queue<int, vector<int>, greater<int>>
#define inp(n) for (int i = 0; i < n; i++)
#define N 200005
#define mxN 2000005
#define all(v) v.begin(), v.end()
#define lb(v, val) (int)(lower_bound(v.begin(), v.end(), val) - v.begin())
#define ub(v, val) (int)(upper_bound(v.begin(), v.end(), val) - v.begin())
using namespace std;
using namespace std::chrono;

void solve() {
  int h, w, K;
  cin >> h >> w >> K;
  char a[h + 1][w + 1];
  inp(h) loop(j, 0, w) cin >> a[i][j];
  int output = INT_MAX;

  for (int mask = (1 << (h - 1)); mask < (1 << h); mask++) {
    bool possible = true;
    // cout << mask << ' ';
    int k, cut = 0;
    vi ans(10, 0), cur(10, 0);

    for (int j = 0; j < w; j++) {
      inp(10) cur[i] = 0;
      k = 0;
      for (int i = 0; i < h; i++) {
        if (a[i][j] == '1') {
          cur[k]++;
          ans[k]++;
        }
        if (mask & (1 << i)) {
          k++;
        }
      }
      for (int i = 0; i < k; i++) {
        if (cur[i] > K) {
          possible = false;
          break;
        }
      }
      for (int i = 0; i < k; i++) {
        if (ans[i] > K) {
          cut++;
          for (int x = 0; x < k; x++) {
            ans[x] = cur[x];
          }
          break;
        }
      }
      if (!possible)
        break;
    }
    if (possible)
      output = min(output, cut + __builtin_popcount(mask) - 1);
    // cout << cut << '\n';
  }
  cout << output;
}

signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define MOD 1000000007
#define MAX 100005
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define mii map<int, int>
#define vi vector<int>
#define ff first
#define ss second
#define T                                                                      \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define umi unordered_map<int, int>
#define maxheap priority_queue<int>
#define minheap priority_queue<int, vector<int>, greater<int>>
#define inp(n) for (int i = 0; i < n; i++)
#define N 200005
#define mxN 2000005
#define all(v) v.begin(), v.end()
#define lb(v, val) (int)(lower_bound(v.begin(), v.end(), val) - v.begin())
#define ub(v, val) (int)(upper_bound(v.begin(), v.end(), val) - v.begin())
using namespace std;
using namespace std::chrono;

void solve() {
  int h, w, K;
  cin >> h >> w >> K;
  char a[h + 1][w + 1];
  inp(h) loop(j, 0, w) cin >> a[i][j];
  int output = INT_MAX;

  for (int mask = (1 << (h - 1)); mask < (1 << h); mask++) {
    bool possible = true;
    // cout << mask << ' ';
    int k, cut = 0;
    vi ans(10, 0), cur(10, 0);

    for (int j = 0; j < w; j++) {
      inp(10) cur[i] = 0;
      k = 0;
      for (int i = 0; i < h; i++) {
        if (a[i][j] == '1') {
          cur[k]++;
          ans[k]++;
        }
        if (mask & (1 << i)) {
          k++;
        }
      }
      for (int i = 0; i < k; i++) {
        if (cur[i] > K) {
          possible = false;
          break;
        }
      }
      for (int i = 0; i < k; i++) {
        if (ans[i] > K) {
          cut++;
          for (int x = 0; x < k; x++) {
            ans[x] = cur[x];
          }
          break;
        }
      }
      if (!possible)
        break;
    }
    if (possible)
      output = min(output, cut + __builtin_popcount(mask) - 1);
    // cout << cut << '\n';
  }
  cout << output;
}

signed main() {
  /*#ifndef ONLINE_JUDGE
      freopen("input.txt","r",stdin);
      freopen("output.txt","w",stdout);
  #endif  */
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}