prithivMLmods/Coder-Stat huggingface dataset:
Problem p02733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using ll = long long;

using namespace std;

int rui[20][1010] = {{0}};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      char c;
      cin >> c;
      rui[i][j] = c - '0';
    }
  }
  for (int i = 0; i <= h; i++) {
    for (int j = 0; j < w; j++) {
      rui[i][j + 1] += rui[i][j];
    }
  }
  for (int j = 0; j <= w; j++) {
    for (int i = 0; i < h; i++) {
      rui[i + 1][j] += rui[i][j];
    }
  }
  int lim = (1 << (h - 1));
  int ans = h + w;
  for (int b = 0; b < lim; b++) {
    vector<int> ends;
    for (int i = 0; i < h - 1; i++)
      if (b & (1 << i))
        ends.push_back(i);
    ends.push_back(h - 1);
    int sj = 0, cnt = ends.size() - 1;
    for (int j = 0; j < w; j++) {
      int si = 0;
      for (auto &ei : ends) {
        int val =
            rui[ei + 1][j + 1] - rui[si][j + 1] - rui[ei + 1][sj] + rui[si][sj];
        cerr << ei + 1 << ' ' << j + 1 << ' ' << si << ' ' << sj << '\n';
        if (val > k) {
          if (sj == j)
            cnt = h + w;
          sj = j;
          cnt++;
          break;
        }
        si = ei + 1;
      }
    }
    ans = min(ans, cnt);
    // cerr << cnt << ' ' << b << '\n';
  }
  cout << ans << '\n';
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using ll = long long;

using namespace std;

int rui[20][1010] = {{0}};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      char c;
      cin >> c;
      rui[i][j] = c - '0';
    }
  }
  for (int i = 0; i <= h; i++) {
    for (int j = 0; j < w; j++) {
      rui[i][j + 1] += rui[i][j];
    }
  }
  for (int j = 0; j <= w; j++) {
    for (int i = 0; i < h; i++) {
      rui[i + 1][j] += rui[i][j];
    }
  }
  int lim = (1 << (h - 1));
  int ans = h + w;
  for (int b = 0; b < lim; b++) {
    vector<int> ends;
    for (int i = 0; i < h - 1; i++)
      if (b & (1 << i))
        ends.push_back(i);
    ends.push_back(h - 1);
    int sj = 0, cnt = ends.size() - 1;
    for (int j = 0; j < w; j++) {
      int si = 0;
      for (auto &ei : ends) {
        int val =
            rui[ei + 1][j + 1] - rui[si][j + 1] - rui[ei + 1][sj] + rui[si][sj];
        if (val > k) {
          if (sj == j)
            cnt = h + w;
          sj = j;
          cnt++;
          break;
        }
        si = ei + 1;
      }
    }
    ans = min(ans, cnt);
    // cerr << cnt << ' ' << b << '\n';
  }
  cout << ans << '\n';
  return 0;
}
Problem p02733 (Python)

Buggy submission (status: Time Limit Exceeded):
from itertools import combinations, chain

H, W, K = map(int, input().split())
Sss = []
for _ in range(H):
    Sss.append(list(map(int, input())))


def h_divide(Sss, div_is):
    result = []
    for s, e in zip(chain([0], div_is), chain(div_is, [len(Sss)])):
        result.append(Sss[s:e])
    return result


def _v_end(Sss, start, K):
    count = 0
    for i in range(start, len(Sss[0])):
        s = sum(map(lambda Ss: Ss[i], Sss))
        if count + s > K:
            return i - 1
        count += s
    return len(Sss[0]) - 1


def v_end(Ssss, start, K):
    result = -1
    for Sss in Ssss:
        end = _v_end(Sss, start, K)
        if end < start:
            return -1
        if result < 0 or result > end:
            result = end
    return result


def v_div_count(Ssss, K):
    start, result = 0, 0
    while True:
        end = v_end(Ssss, start, K)
        if end < 0:
            return -1
        if end == len(Ssss[0][0]) - 1:
            break
        result += 1
        start = end + 1
    return result


h_cands = list(range(1, len(Sss)))
ans = -1
for n in range(len(h_cands) + 1):
    for comb in combinations(h_cands, n):
        Ssss = h_divide(Sss, list(comb))
        cnt = v_div_count(Ssss, K)
        if cnt < 0:
            continue
        if ans < 0 or ans > n + cnt:
            ans = n + cnt
print(ans)


Fixed submission:
from itertools import combinations, chain

H, W, K = map(int, input().split())
Sss = []
for _ in range(H):
    Sss.append(list(map(int, input())))


def h_divide(Sss, div_is):
    result = []
    for s, e in zip(chain([0], div_is), chain(div_is, [len(Sss)])):
        result.append(Sss[s:e])
    return result


def _v_end(Sss, start, K):
    count = 0
    for i in range(start, len(Sss[0])):
        s = sum(map(lambda Ss: Ss[i], Sss))
        if count + s > K:
            return i - 1
        count += s
    return len(Sss[0]) - 1


def v_end(Ssss, start, K):
    result = -1
    for Sss in Ssss:
        end = _v_end(Sss, start, K)
        if end < start:
            return -1
        if result < 0 or result > end:
            result = end
    return result


def v_div_count(Ssss, K):
    start, result = 0, 0
    while True:
        end = v_end(Ssss, start, K)
        if end < 0:
            return -1
        if end == len(Ssss[0][0]) - 1:
            break
        result += 1
        start = end + 1
    return result


h_cands = list(range(1, len(Sss)))
ans = -1
for n in range(len(h_cands) + 1):
    for comb in combinations(h_cands, n):
        if ans > 0 and n >= ans:
            continue
        Ssss = h_divide(Sss, list(comb))
        cnt = v_div_count(Ssss, K)
        if cnt < 0:
            continue
        if ans < 0 or ans > n + cnt:
            ans = n + cnt
print(ans)
Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  for (int i = 0; i < h; ++i) {
    cin >> s[i];
  }
  int ret = 1e8;
  for (int i = 0; i < 1 << (h - 1); ++i) {
    int temp = __builtin_popcount(i); // holizontal cut
    vector<int> white(h, 0);
    bool ng = false;
    bool cut = false;
    for (int j = 0; j < w; ++j) {
      int row = 0;
      for (int l = 0; l < h; ++l) {
        if (s[l][j] == '1') {
          white[row]++;
          if (white[row] > k) {
            cut = true;
          }
        }
        if (1 << l & i) {
          row++;
        }
      }
      if (ng)
        break;
      if (cut) { // vertical cut
        temp++;
        cut = false;
        white.assign(h, 0);
        for (int l = 0; l < h; ++l) {
          if (s[l][j] == '1') {
            white[row]++;
            if (white[row] > k)
              ng = true;
          }
          if (1 << l & i) {
            row++;
          }
        }
      }
      if (ng)
        break;
    }
    if (!ng)
      chmin(ret, temp);
  }
  cout << ret << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  for (int i = 0; i < h; ++i) {
    cin >> s[i];
  }
  int ret = 1e8;
  for (int i = 0; i < 1 << (h - 1); ++i) {
    int temp = __builtin_popcount(i); // holizontal cut
    vector<int> white(h, 0);
    bool ng = false;
    bool cut = false;
    for (int j = 0; j < w; ++j) {
      int row = 0;
      for (int l = 0; l < h; ++l) {
        if (s[l][j] == '1') {
          white[row]++;
          if (white[row] > k) {
            cut = true;
          }
        }
        if (1 << l & i) {
          row++;
        }
      }
      if (ng)
        break;
      if (cut) { // vertical cut
        temp++;
        cut = false;
        white.assign(h, 0);
        row = 0;
        for (int l = 0; l < h; ++l) {
          if (s[l][j] == '1') {
            white[row]++;
            if (white[row] > k)
              ng = true;
          }
          if (1 << l & i) {
            row++;
          }
        }
      }
      if (ng)
        break;
    }
    if (!ng)
      chmin(ret, temp);
  }
  cout << ret << "\n";
  return 0;
}
Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define eb emplace_back
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, T b) { a = min(a, b); }
template <class T> void chmax(T &a, T b) { a = max(a, b); }
void ok() { cerr << "ok" << endl; }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> c(h);
  for (int i = 0; i < h; i++) {
    cin >> c[i];
  }
  int res = INF;
  for (int i = 0; i < (1 << (h - 1)); i++) {
    vector<int> co(h);
    int s = __builtin_popcount(i);
    bool ff = true;
    for (int j = 0; j < w; j++) {
      bool f = false;
      int id = 0;
      for (int l = 0; l < h; l++) {
        co[id] += (c[l][j] == '1');
        if (co[id] > k) {
          s++;
          for (int m = 0; m < h; m++) {
            co[m] = 0;
          }
          f = true;
          break;
        }
        if ((i >> l) & 1) {
          id++;
        }
      }
      if (f) {
        for (int l = 0; l < h; l++) {
          co[id] += (c[l][j] == '1');
          if (co[id] > k) {
            ff = false;
            break;
          }
          if ((i >> l) & 1) {
            id++;
          }
        }
      }
    }
    if (ff) {
      chmin(res, s);
    }
  }
  cout << res << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define eb emplace_back
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, T b) { a = min(a, b); }
template <class T> void chmax(T &a, T b) { a = max(a, b); }
void ok() { cerr << "ok" << endl; }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> c(h);
  for (int i = 0; i < h; i++) {
    cin >> c[i];
  }
  int res = INF;
  for (int i = 0; i < (1 << (h - 1)); i++) {
    vector<int> co(h);
    int s = __builtin_popcount(i);
    bool ff = true;
    for (int j = 0; j < w; j++) {
      bool f = false;
      int id = 0;
      for (int l = 0; l < h; l++) {
        co[id] += (c[l][j] == '1');
        if (co[id] > k) {
          s++;
          for (int m = 0; m < h; m++) {
            co[m] = 0;
          }
          f = true;
          break;
        }
        if ((i >> l) & 1) {
          id++;
        }
      }
      if (f) {
        id = 0;
        for (int l = 0; l < h; l++) {
          co[id] += (c[l][j] == '1');
          if (co[id] > k) {
            ff = false;
            break;
          }
          if ((i >> l) & 1) {
            id++;
          }
        }
      }
    }
    if (ff) {
      chmin(res, s);
    }
  }
  cout << res << '\n';
}
Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s[i];

  int answer = h + w;
  for (int mask = 0; mask < (1 << (h - 1)); mask++) {
    int block = __builtin_popcount(mask) + 1;
    int cut = block - 1;
    vector<int> count(block, 0);
    for (int i = 0; i < w; i++) {
      int block_num = 0;
      vector<int> count_new(block, 0);
      for (int j = 0; j < h; j++) {
        count_new[block_num] += s[j][i] - '0';
        block_num += mask & (1 << j);
      }
      bool possible = true;
      for (int j = 0; j < block; j++) {
        if (count_new[j] > k) {
          possible = false;
          break;
        }
      }
      if (!possible) {
        cut = h + w;
        break;
      }
      bool pass = true;
      for (int j = 0; j < block; j++) {
        if (count[j] + count_new[j] > k) {
          pass = false;
          break;
        }
      }
      if (pass) {
        for (int j = 0; j < block; j++)
          count[j] += count_new[j];
      } else {
        if (i == 0) {
          cut = h + w;
          break;
        }
        count = count_new;
        cut += 1;
      }
    }
    answer = min(answer, cut);
  }
  cout << answer << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s[i];

  int answer = h + w;
  for (int mask = 0; mask < (1 << (h - 1)); mask++) {
    int block = __builtin_popcount(mask) + 1;
    int cut = block - 1;
    vector<int> count(block, 0);
    for (int i = 0; i < w; i++) {
      int block_num = 0;
      vector<int> count_new(block, 0);
      for (int j = 0; j < h; j++) {
        count_new[block_num] += s[j][i] - '0';
        block_num += (mask & 1 << j) > 0 ? 1 : 0;
      }
      bool possible = true;
      for (int j = 0; j < block; j++) {
        if (count_new[j] > k) {
          possible = false;
          break;
        }
      }
      if (!possible) {
        cut = h + w;
        break;
      }
      bool pass = true;
      for (int j = 0; j < block; j++) {
        if (count[j] + count_new[j] > k) {
          pass = false;
          break;
        }
      }
      if (pass) {
        for (int j = 0; j < block; j++)
          count[j] += count_new[j];
      } else {
        if (i == 0) {
          cut = h + w;
          break;
        }
        count = count_new;
        cut += 1;
      }
    }
    answer = min(answer, cut);
  }
  cout << answer << endl;
  return 0;
}
Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n, m, k;
char grid[11][110];
int dp[11][110];
int ans;

bool check(int l, int r, int a, int b) {
  return dp[a][b] - dp[l - 1][b] - dp[a][r - 1] + dp[l - 1][r - 1] <= k;
}

vector<int> pos = {1};

bool testC(int last, int j) {
  bool fl = true;
  for (int i = 0; i < pos.size() - 1; i++) {
    int r1 = pos[i];
    int r2 = pos[i + 1] - 1;
    if (!check(r1, last, r2, j))
      return false;
  }
  return true;
}

void gen(int cur) {
  if (cur == n + 1) {
    pos.push_back(n + 1);
    int cnt = 0, last = 1;
    bool ok = true;
    for (int j = 1; j <= m and ok; j++) {
      if (testC(last, j))
        continue;
      if (j - 1 >= last and testC(last, j - 1)) {
        cnt++;
        last = j;
        continue;
      }
      ok = false;
    }
    if (ok)
      ans = min(ans, (int)pos.size() - 2 + cnt);
    pos.pop_back();
    return;
  }
  gen(cur + 1);
  pos.push_back(cur);
  gen(cur + 1);
  pos.pop_back();
}

int32_t main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> grid[i][j];

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      dp[i][j] =
          dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + (grid[i][j] == '1');

  ans = (n - 1) * (m - 1);
  gen(2);
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int n, m, k;
char grid[11][1010];
int dp[11][1010];
int ans;

bool check(int l, int r, int a, int b) {
  return dp[a][b] - dp[l - 1][b] - dp[a][r - 1] + dp[l - 1][r - 1] <= k;
}

vector<int> pos = {1};

bool testC(int last, int j) {
  bool fl = true;
  for (int i = 0; i < pos.size() - 1; i++) {
    int r1 = pos[i];
    int r2 = pos[i + 1] - 1;
    if (!check(r1, last, r2, j))
      return false;
  }
  return true;
}

void gen(int cur) {
  if (cur == n + 1) {
    pos.push_back(n + 1);
    int cnt = 0, last = 1;
    bool ok = true;
    for (int j = 1; j <= m and ok; j++) {
      if (testC(last, j))
        continue;
      if (j - 1 >= last and testC(last, j - 1)) {
        cnt++;
        last = j;
        continue;
      }
      ok = false;
    }
    if (ok)
      ans = min(ans, (int)pos.size() - 2 + cnt);
    pos.pop_back();
    return;
  }
  gen(cur + 1);
  pos.push_back(cur);
  gen(cur + 1);
  pos.pop_back();
}

int32_t main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> grid[i][j];

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      dp[i][j] =
          dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + (grid[i][j] == '1');

  ans = (n - 1) * (m - 1);
  gen(2);
  cout << ans << endl;

  return 0;
}
Problem p02733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
typedef long long ll;
const int inf = 1001001001;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> d(h, vector<int>(w, 0));
  rep(i, h) rep(j, w) {
    int t = 0;
    if (s[i][j] == '1')
      t = 1;
    if (j == 0)
      d[i][j] = t;
    else
      d[i][j] = d[i][j - 1] + t;
  }
  rep(i, h - 1) rep(j, w) { d[i + 1][j] += d[i][j]; }
  ll ans = 1001001001;
  for (int bit = 0; bit < 1 << (h - 1); bit++) {
    ll tmp = __builtin_popcount(bit);
    int r = 0;
    int l = 0;
    while (r < w) {
      int t = 0;
      int b = 0;
      bool flg = false;
      while (t < h) {
        while (((bit >> b) & 1) == 0 && b < h - 1)
          b++;
        int x = 0;
        if (t == 0) {
          if (l == 0) {
            x = d[b][r];
          } else {
            x = d[b][r] - d[b][l - 1];
          }
        } else {
          if (l == 0) {
            x = d[b][r] - d[t - 1][r];
          } else {
            x = d[b][r] - d[t - 1][r] - d[b][l - 1] + d[t - 1][l - 1];
          }
        }
        if (x > k) {
          flg = true;
          break;
        }
        t = b + 1;
        b = t;
      }
      if (flg) {
        tmp++;
        l = r;
      } else {
        r++;
      }
    }
    ans = min(ans, tmp);
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
typedef long long ll;
const int inf = 1001001001;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> d(h, vector<int>(w, 0));
  rep(i, h) rep(j, w) {
    int t = 0;
    if (s[i][j] == '1')
      t = 1;
    if (j == 0)
      d[i][j] = t;
    else
      d[i][j] = d[i][j - 1] + t;
  }
  rep(i, h - 1) rep(j, w) { d[i + 1][j] += d[i][j]; }
  ll ans = 1001001001;
  for (int bit = 0; bit < 1 << (h - 1); bit++) {
    ll tmp = __builtin_popcount(bit);
    int r = 0;
    int l = 0;
    while (r < w) {
      int t = 0;
      int b = 0;
      bool flg = false;
      while (t < h) {
        while (((bit >> b) & 1) == 0 && b < h - 1)
          b++;
        int x = 0;
        if (t == 0) {
          if (l == 0) {
            x = d[b][r];
          } else {
            x = d[b][r] - d[b][l - 1];
          }
        } else {
          if (l == 0) {
            x = d[b][r] - d[t - 1][r];
          } else {
            x = d[b][r] - d[t - 1][r] - d[b][l - 1] + d[t - 1][l - 1];
          }
        }
        if (x > k) {
          flg = true;
          break;
        }
        t = b + 1;
        b = t;
      }
      if (flg) {
        tmp++;
        if (r == l) {
          tmp = 1001001001;
          break;
        }
        l = r;
      } else {
        r++;
      }
    }
    ans = min(ans, tmp);
  }
  cout << ans << endl;

  return 0;
}
Problem p02733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int H, W, K;
  scanf("%d %d %d", &H, &W, &K);
  vector<vector<char>> a(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      cin >> a[i][j];
  }

  int ans = 1e9;
  for (int bit = 0; bit < (1 << (H - 1)); bit++) {
    int num = __builtin_popcount(bit);
    vector<int> data(num + 1, 0);
    int ans1 = 0;
    bool check = true;
    for (int i = 0; i < W; i++) {
      int cnt = 0;
      for (int j = 0; j < H; j++) {
        if (a[j][i] == '1')
          data[cnt]++;
        if (bit & (1 << j))
          cnt++;
      }
      if (*max_element(data.begin(), data.end()) > K)
        check = false;
      for (int j = 0; j <= num; j++)
        data[j] = 0;
    }
    if (!check)
      ans1 = 1e9;

    for (int i = 0; i < W; i++) {
      int cnt = 0;
      for (int j = 0; j < H; j++) {
        if (a[j][i] == '1')
          data[cnt]++;
        if (bit & (1 << j))
          cnt++;
      }
      if (*max_element(data.begin(), data.end()) > K) {
        ans1++;
        for (int j = 0; j <= num; j++)
          data[j] = 0;
        --i;
      }
    }
    ans = min(ans, ans1 + num);
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int H, W, K;
  scanf("%d %d %d", &H, &W, &K);
  vector<vector<char>> a(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      cin >> a[i][j];
  }

  int ans = 1e9;
  for (int bit = 0; bit < (1 << (H - 1)); bit++) {
    int num = __builtin_popcount(bit);
    vector<int> data(num + 1, 0);
    int ans1 = 0;
    bool check = true;
    for (int i = 0; i < W; i++) {
      int cnt = 0;
      for (int j = 0; j < H; j++) {
        if (a[j][i] == '1')
          data[cnt]++;
        if (bit & (1 << j))
          cnt++;
      }
      if (*max_element(data.begin(), data.end()) > K)
        check = false;
      for (int j = 0; j <= num; j++)
        data[j] = 0;
    }
    if (!check)
      continue;

    for (int i = 0; i < W; i++) {
      int cnt = 0;
      for (int j = 0; j < H; j++) {
        if (a[j][i] == '1')
          data[cnt]++;
        if (bit & (1 << j))
          cnt++;
      }
      if (*max_element(data.begin(), data.end()) > K) {
        ans1++;
        for (int j = 0; j <= num; j++)
          data[j] = 0;
        --i;
      }
    }
    ans = min(ans, ans1 + num);
  }
  printf("%d\n", ans);
}
Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
int H, W, K;
vector<string> s;

template <typename T> void print_vec(vector<T> &v) {
  for (auto i : v)
    cout << i << " ";
  cout << "\n";
}

vector<int> cnt_whi(vector<int> &a, int w, int I) {
  int idx = 0;
  vector<int> res(__builtin_popcount(I) + 1, 0);
  for (int i = 0; i < H; i++) {
    if (s[i][w] == '1')
      a[idx]++, res[idx]++;
    if ((I & (1 << i)) != 0)
      idx++;
  }
  return res;
}

int op_count(int I) {
  int n = __builtin_popcount(I);
  int res = n;
  vector<int> cnt(n + 1, 0);
  for (int i = 0; i < W; i++) {
    auto v = cnt_whi(cnt, i, I);
    // print_vec(cnt);
    for (auto it : v)
      if (it >= K)
        return 1 << 29;
    bool d = false;
    for (auto it : cnt)
      if (it > K)
        d = true;
    if (d) {
      res++;
      swap(v, cnt);
    }
  }
  return res;
}

int main() {
  cin >> H >> W >> K;
  s.resize(H);
  for (int i = 0; i < H; i++)
    cin >> s[i];
  int ans = 1 << 29;
  for (int i = 0; i < (1 << (H - 1)); i++) {
    int r = op_count(i);
    ans = min(ans, r);
  }
  assert(ans != 1 << 29);
  cout << ans << endl;
}


Fixed submission:
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
int H, W, K;
vector<string> s;

template <typename T> void print_vec(vector<T> &v) {
  for (auto i : v)
    cout << i << " ";
  cout << "\n";
}

vector<int> cnt_whi(vector<int> &a, int w, int I) {
  int idx = 0;
  vector<int> res(__builtin_popcount(I) + 1, 0);
  for (int i = 0; i < H; i++) {
    if (s[i][w] == '1')
      a[idx]++, res[idx]++;
    if ((I & (1 << i)) != 0)
      idx++;
  }
  return res;
}

int op_count(int I) {
  int n = __builtin_popcount(I);
  int res = n;
  vector<int> cnt(n + 1, 0);
  for (int i = 0; i < W; i++) {
    auto v = cnt_whi(cnt, i, I);
    // print_vec(cnt);
    for (auto it : v)
      if (it > K)
        return 1 << 29;
    bool d = false;
    for (auto it : cnt)
      if (it > K)
        d = true;
    if (d) {
      res++;
      swap(v, cnt);
    }
  }
  return res;
}

int main() {
  cin >> H >> W >> K;
  s.resize(H);
  for (int i = 0; i < H; i++)
    cin >> s[i];
  int ans = 1 << 29;
  for (int i = 0; i < (1 << (H - 1)); i++) {
    int r = op_count(i);
    ans = min(ans, r);
  }
  assert(ans != 1 << 29);
  cout << ans << endl;
}
Problem p02733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<vector<int>> V(H + 1, vector<int>(W + 1, 0));
  for (int i = 1; i <= H; i++) {
    string S;
    cin >> S;
    for (int j = 1; j <= W; j++) {
      if (S[j - 1] == '1')
        V[i][j]++;
      V[i][j] += V[i][j - 1];
    }
  }

  int ans = H + W;
  for (int tmp = 0; tmp < (1 << (H - 1)); tmp++) {
    bitset<9> B(tmp);
    int N = B.count(), lastcut = 0;
    for (int i = 1; i <= W; i++) {
      int white = 0;
      for (int j = 1; j <= H; j++) {
        white += V[j][i] - V[j][lastcut];
        if (white > K) {
          if (lastcut == i - 1)
            N += 10000;
          N++;
          lastcut = i - 1;
          i--;
          break;
        }
        if (j != H)
          if (B.test(j - 1))
            white = 0;
      }
    }
    ans = min(ans, N);
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<vector<int>> V(H + 1, vector<int>(W + 1, 0));
  for (int i = 1; i <= H; i++) {
    string S;
    cin >> S;
    for (int j = 1; j <= W; j++) {
      if (S[j - 1] == '1')
        V[i][j]++;
      V[i][j] += V[i][j - 1];
    }
  }

  int ans = H + W;
  for (int tmp = 0; tmp < (1 << (H - 1)); tmp++) {
    bitset<9> B(tmp);
    int N = B.count(), lastcut = 0;
    for (int i = 1; i <= W; i++) {
      int white = 0;
      for (int j = 1; j <= H; j++) {
        white += V[j][i] - V[j][lastcut];
        if (white > K) {
          if (lastcut == i - 1)
            N += 10000;
          N++;
          lastcut = i - 1;
          i--;
          break;
        }
        if (j != H)
          if (B.test(j - 1))
            white = 0;
      }
      if (N > ans)
        break;
    }
    ans = min(ans, N);
  }

  cout << ans << endl;
  return 0;
}
Problem p02733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 1000007
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
#define N 1010
using namespace std;
ll rint() {
  ll ret = 0, uu = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-')
    c = getchar();
  if (c == '-')
    uu = -1, c = getchar();
  while (isdigit(c))
    ret = ret * 10 + (c - '0'), c = getchar();
  return ret * uu;
}
int n, m, k, nm[11], num[11], ans = INF, tmp, tp;
char grid[11][N];
int main() {
  int i, j, l, lst;
  n = rint();
  m = rint();
  k = rint();
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      scanf(" %c", &grid[i][j]);
    }
  }
  for (i = 0; i < (1 << (n - 1)); i++) {
    memset(nm, 0, sizeof(nm));
    for (j = 0; j + 1 < n; j++) {
      if (i & (1 << j)) {
        nm[j + 1] = nm[j] + 1;
      } else {
        nm[j + 1] = nm[j];
      }
    }
    tmp = 0;
    tp = __builtin_popcount(i);
    memset(num, 0, sizeof(num));
    for (j = m - 1; j >= 0; j--) {
      if (tmp + tp >= ans) {
        break;
      }
      for (l = 0; l < n; l++) {
        if (grid[l][j] == '1') {
          num[nm[l]]++;
        }
        if (num[nm[l]] > k) {
          l = -1;
          break;
        }
      }
      if (l == -1) {
        tmp++;
        j++;
        memset(num, 0, sizeof(num));
      }
    }
    ans = min(ans, tmp + tp);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 1000007
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
#define N 1010
using namespace std;
ll rint() {
  ll ret = 0, uu = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-')
    c = getchar();
  if (c == '-')
    uu = -1, c = getchar();
  while (isdigit(c))
    ret = ret * 10 + (c - '0'), c = getchar();
  return ret * uu;
}
int n, m, k, nm[11], num[11], ans = INF, tmp, tp;
char grid[11][N];
int main() {
  int i, j, l, lst;
  n = rint();
  m = rint();
  k = rint();
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      scanf(" %c", &grid[i][j]);
    }
  }
  for (i = (1 << n) - 1; i >= 0; i--) {
    memset(nm, 0, sizeof(nm));
    for (j = 0; j + 1 < n; j++) {
      if (i & (1 << j)) {
        nm[j + 1] = nm[j] + 1;
      } else {
        nm[j + 1] = nm[j];
      }
    }
    tmp = 0;
    tp = __builtin_popcount(i);
    memset(num, 0, sizeof(num));
    for (j = m - 1; j >= 0; j--) {
      if (tmp + tp >= ans) {
        break;
      }
      for (l = 0; l < n; l++) {
        if (grid[l][j] == '1') {
          num[nm[l]]++;
        }
        if (num[nm[l]] > k) {
          l = -1;
          break;
        }
      }
      if (l == -1) {
        tmp++;
        j++;
        memset(num, 0, sizeof(num));
      }
    }
    ans = min(ans, tmp + tp);
  }
  cout << ans << endl;
  return 0;
}
Problem p02733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
using ll = long long;

int main() {
  int mp[13][1005] = {};
  int h, w, k;
  cin >> h >> w >> k;
  rep(i, h) rep(j, w) scanf("%1d", &mp[i + 1][j + 1]);
  rep(i, h - 1) rep(j, w) mp[i + 2][j + 1] += mp[i + 1][j + 1];
  rep(j, w - 1) rep(i, h) mp[i + 1][j + 2] += mp[i + 1][j + 1];
  int ans = 1e9;

  for (int i = 0; i < (1 << (h - 1)); i++) {
    int cut = 0;
    rep(x, h - 1) if (i >> x & 1) cut++;
    int afterH = 0;
    int afterW = 0;
    bool small = true;
    int now = 1;
    while (now <= w) {
      int c;
      afterH = 0;
      small = true;
      for (int b = 0; b < (h - 1); b++) {
        if (i >> b & 1) {
          c = mp[b + 1][now] - mp[b + 1][afterW] - mp[afterH][now] +
              mp[afterH][afterW];
          if (c > k)
            small = false;
          afterH = b + 1;
        }
      }
      c = mp[h][now] - mp[h][afterW] - mp[afterH][now] + mp[afterH][afterW];
      if (c > k)
        small = false;
      if (small == false) {
        cut++;
        afterW = now - 1;
        now--;
      }
      now++;
    }
    ans = min(ans, cut);
  }
  cout << ans << endl;
  return 0;
}
//......mp[ah][aw] ..............mp[ah][now]

//......mp[b+1][aw]..............mp[b+1][now]


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
using ll = long long;

int main() {
  int mp[13][1005] = {};
  int h, w, k;
  cin >> h >> w >> k;
  rep(i, h) rep(j, w) scanf("%1d", &mp[i + 1][j + 1]);
  rep(i, h - 1) rep(j, w) mp[i + 2][j + 1] += mp[i + 1][j + 1];
  rep(j, w - 1) rep(i, h) mp[i + 1][j + 2] += mp[i + 1][j + 1];
  int ans = 1e9;

  for (int i = 0; i < (1 << (h - 1)); i++) {
    int cut = 0;
    rep(x, h - 1) if (i >> x & 1) cut++;
    int afterH = 0;
    int afterW = 0;
    bool small = true;
    int now = 1;
    while (now <= w) {
      int c;
      afterH = 0;
      small = true;
      for (int b = 0; b < (h - 1); b++) {
        if (i >> b & 1) {
          c = mp[b + 1][now] - mp[b + 1][afterW] - mp[afterH][now] +
              mp[afterH][afterW];
          if (c > k)
            small = false;
          afterH = b + 1;
        }
      }
      c = mp[h][now] - mp[h][afterW] - mp[afterH][now] + mp[afterH][afterW];
      if (c > k)
        small = false;
      if (small == false) {
        if (afterW == now - 1) {
          cut = 1e9;
          now++;
        }
        cut++;
        afterW = now - 1;
        now--;
      }
      now++;
    }
    ans = min(ans, cut);
  }
  cout << ans << endl;
  return 0;
}
//......mp[ah][aw] ..............mp[ah][now]

//......mp[b+1][aw]..............mp[b+1][now]
Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, N) for (int i = 0; i < N; i++)
#define rep1(i, N) for (int i = 1; i <= N; i++)
const int intinf = numeric_limits<int>::max();

using ll = long long int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define MOD 1000000007

void solve() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> S(H);
  rep(i, H) cin >> S[i];

  vector<vector<int>> s(H, vector<int>(W));
  rep(i, H) rep(j, W) s[i][j] = S[i][j] - '0';

  int ans = (H - 1) * (W - 1);
  rep(a, 1 << (H - 1)) {
    int ncut = __builtin_popcount(a);
    int ans_a = ncut;
    int h = ncut + 1;

    vector<vector<int>> bigs(h, vector<int>(W, 0));
    int ii = 0;
    rep(i, H) {
      rep(j, W + 1) bigs[ii][j] += s[i][j];
      if (a & (1 << i))
        ii++;
    }

    bool is_impossible = false;
    vector<int> bigk(h, 0);
    rep(j, W) {
      bool is_ok = true;
      rep(ii, h) {
        if (bigs[ii][j] > K) {
          is_impossible = true;
          break;
        }
        if (bigk[ii] + bigs[ii][j] > K) {
          is_ok = false;
          break;
        }
      }

      if (is_impossible)
        continue;

      if (!is_ok) {
        ans_a++;
        bigk.clear();
        bigk.resize(h, 0);
      }
      rep(ii, h) bigk[ii] += bigs[ii][j];
    }
    if (is_impossible)
      continue;

    ans = min(ans, ans_a);
  }
  cout << ans << endl;
}

int main() {
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, N) for (int i = 0; i < N; i++)
#define rep1(i, N) for (int i = 1; i <= N; i++)
const int intinf = numeric_limits<int>::max();

using ll = long long int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define MOD 1000000007

void solve() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> S(H);
  rep(i, H) cin >> S[i];

  vector<vector<int>> s(H, vector<int>(W));
  rep(i, H) rep(j, W) s[i][j] = S[i][j] - '0';

  int ans = (H - 1) * (W - 1);
  rep(a, 1 << (H - 1)) {
    int ncut = __builtin_popcount(a);
    int ans_a = ncut;
    int h = ncut + 1;

    vector<vector<int>> bigs(h, vector<int>(W, 0));
    int ii = 0;
    rep(i, H) {
      rep(j, W) bigs[ii][j] += s[i][j];
      if (a & (1 << i))
        ii++;
    }

    bool is_impossible = false;
    vector<int> bigk(h, 0);
    rep(j, W) {
      bool is_ok = true;
      rep(ii, h) {
        if (bigs[ii][j] > K) {
          is_impossible = true;
          break;
        }
        if (bigk[ii] + bigs[ii][j] > K) {
          is_ok = false;
          break;
        }
      }

      if (is_impossible)
        continue;

      if (!is_ok) {
        ans_a++;
        bigk.clear();
        bigk.resize(h, 0);
      }
      rep(ii, h) bigk[ii] += bigs[ii][j];
    }
    if (is_impossible)
      continue;

    ans = min(ans, ans_a);
  }
  cout << ans << endl;
}

int main() {
  solve();
  return 0;
}
Problem p02733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  for (auto &&e : s) {
    cin >> e;
  }
  int res = 1e9;
  for (int bt = 0; bt < 1 << (h - 1); ++bt) {
    vector<int> p{0};
    for (int i = 1; i < h; ++i) {
      if (bt >> (i - 1) & 1) {
        p.push_back(i);
      }
    }
    p.push_back(h);
    int n = p.size() - 1;
    int cur = n - 1;
    int l = 0, r = 0;
    while (l < w) {
      vector<int> c(n);
      while (r < w) {
        for (int t = 0; t < n; ++t) {
          for (int i = p[t]; i < p[t + 1]; ++i) {
            c[t] += s[i][r] == '1';
          }
        }
        if (*max_element(begin(c), end(c)) > k) {
          break;
        }
        ++r;
      }
      l = r;
      cur += l < w;
    }
    res = min(res, cur);
  }
  cout << res << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  for (auto &&e : s) {
    cin >> e;
  }
  int res = 1e9;
  for (int bt = 0; bt < 1 << (h - 1); ++bt) {
    vector<int> p{0};
    for (int i = 1; i < h; ++i) {
      if (bt >> (i - 1) & 1) {
        p.push_back(i);
      }
    }
    p.push_back(h);
    int n = p.size() - 1;
    int cur = n - 1;
    int l = 0, r = 0;
    while (l < w) {
      vector<int> c(n);
      while (r < w) {
        for (int t = 0; t < n; ++t) {
          for (int i = p[t]; i < p[t + 1]; ++i) {
            c[t] += s[i][r] == '1';
          }
        }
        if (*max_element(begin(c), end(c)) > k) {
          break;
        }
        ++r;
      }
      if (l == r) {
        cur = 1e9;
        break;
      }
      l = r;
      cur += l < w;
    }
    res = min(res, cur);
  }
  cout << res << '\n';
}
Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define INF 1 << 30

using namespace std;
typedef long long ll;
typedef pair<int, int> pp;

int Cut(int n, int k, vector<string> &s) {
  int i, j, c = 0, ans = 0;
  vector<vector<int>> d(s.size(), vector<int>(s[0].size(), 0));
  vector<int> a(s.size());
  rep(i, s.size()) {
    a[i] = ans;
    if (n & (1 << i))
      ans++;
  }
  rep(j, s[0].size()) {
    rep(i, s.size()) {
      if (s[i][j] == '1') {
        if (d[a[i]][c] == k) {
          c++;
          j--;
          break;
        }
        d[a[i]][c]++;
      }
    }
  }
  return ans + c;
}

int main(void) {
  int num, i, h, w, k, ans = INF;
  cin >> h >> w >> k;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  rep(i, 1 << (h - 1)) { chmin(ans, Cut(i, k, s)); }
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define INF 1 << 30

using namespace std;
typedef long long ll;
typedef pair<int, int> pp;

int Cut(int n, int k, vector<string> &s) {
  int i, j, c = 0, ans = 0;
  vector<vector<int>> d(s.size(), vector<int>(s[0].size(), 0));
  vector<int> a(s.size());
  rep(i, s.size()) {
    a[i] = ans;
    if (n & (1 << i))
      ans++;
  }
  rep(j, s[0].size()) {
    rep(i, s.size()) {
      if (s[i][j] == '1') {
        if (c >= s[0].size())
          return INF;
        if (d[a[i]][c] == k) {
          c++;
          j--;
          break;
        }
        d[a[i]][c]++;
      }
    }
  }
  return ans + c;
}

int main(void) {
  int num, i, h, w, k, ans = INF;
  cin >> h >> w >> k;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  rep(i, 1 << (h - 1)) { chmin(ans, Cut(i, k, s)); }
  cout << ans << "\n";
  return 0;
}
Problem p02733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;
const int INF = 1001001001;
int sum[11][1005];

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  rep(i, h) { cin >> s[i]; }
  rep(i, h) rep(j, w) { sum[i][j + 1] = sum[i][j] + (s[i][j] - '0'); }

  int ans = INF;
  rep(i, 1 << (h - 1)) {
    int res = __builtin_popcount(i);
    int st = 0;
    int temp = 0;
    rep(j, w) rep(l, h) {
      temp += (sum[l][j + 1] - sum[l][st]);
      if (i >> l & 1 || l == h - 1) {
        if (temp > k) {
          st = j;
          res++;
          l = 0;
        }
        temp = 0;
      }
    }

    ans = min(ans, res);
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;
const int INF = 1001001001;
int sum[11][1005];

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  rep(i, h) { cin >> s[i]; }
  rep(i, h) rep(j, w) { sum[i][j + 1] = sum[i][j] + (s[i][j] - '0'); }

  int ans = INF;
  rep(i, 1 << (h - 1)) {
    int res = __builtin_popcount(i);
    int st = 0;
    int temp = 0;
    rep(j, w) rep(l, h) {
      temp += (sum[l][j + 1] - sum[l][st]);
      if (i >> l & 1 || l == h - 1) {
        if (temp > k) {
          if (st == j) {
            res += 10000;
            break;
          }
          st = j;
          res++;
          l = 0;
        }
        temp = 0;
      }
    }

    ans = min(ans, res);
  }

  cout << ans << endl;
  return 0;
}
Problem p02733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e9;

int DP[11][1001];
int H, W, K;
vector<string> SS;
int Min = 100000;

int dfs(string S) {
  if (S.size() == H - 1) {
    int hcnt = 0;
    vector<int> CH;
    for (int i = 0; i < S.size(); i++)
      if (S[i] == '1') {
        hcnt++;
        CH.push_back(i);
      }
    CH.push_back(H - 1);
    vector<int> WB(hcnt + 1), WB_(hcnt + 1);
    int wcnt = 0;
    // cout << S << " " << hcnt << endl;
    if (hcnt >= Min)
      return 0;

    for (int i = 0; i < W; i++) {
      int s = 0;

      for (int idx = 0; idx < hcnt + 1; idx++) {
        int x = CH[idx];
        for (int j = s; j <= x; j++) {
          if (SS[j][i] == '1')
            WB[idx]++;
        }
        s = x + 1;
      }

      for (int idx = 0; idx < hcnt + 1; idx++) {
        if (WB[idx] > K) {
          i--;
          wcnt++;
          WB = WB_;
          break;
        }
      }
    }
    Min = min(Min, wcnt + hcnt);
    // cout << S << " " << hcnt << " " << wcnt << endl;
    return 0;
  }
  string S_ = S + '0';
  dfs(S_);
  string S__ = S + '1';
  dfs(S__);
  return 0;
}

int main() {
  cin >> H >> W >> K;
  SS.resize(H);
  for (int i = 0; i < H; i++)
    cin >> SS[i];
  vector<int> CH(H), CW(W);
  dfs("");
  cout << Min << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e9;

int DP[11][1001];
int H, W, K;
vector<string> SS;
int Min = 100000;

int dfs(string S) {
  if (S.size() == H - 1) {
    int hcnt = 0;
    vector<int> CH;
    for (int i = 0; i < S.size(); i++)
      if (S[i] == '1') {
        hcnt++;
        CH.push_back(i);
      }
    CH.push_back(H - 1);
    vector<int> WB(hcnt + 1), WB_(hcnt + 1);
    int wcnt = 0;
    // cout << S << " " << hcnt << endl;
    if (hcnt >= Min)
      return 0;

    for (int i = 0; i < W; i++) {
      int s = 0;

      for (int idx = 0; idx < hcnt + 1; idx++) {
        int x = CH[idx];
        for (int j = s; j <= x; j++) {
          if (SS[j][i] == '1')
            WB[idx]++;
        }
        s = x + 1;
      }

      for (int idx = 0; idx < hcnt + 1; idx++) {
        if (WB[idx] > K) {
          i--;
          wcnt++;
          if (hcnt + wcnt >= Min)
            return 0;
          WB = WB_;
          break;
        }
      }
    }
    Min = min(Min, wcnt + hcnt);
    // cout << S << " " << hcnt << " " << wcnt << endl;
    return 0;
  }
  string S_ = S + '0';
  dfs(S_);
  string S__ = S + '1';
  dfs(S__);
  return 0;
}

int main() {
  cin >> H >> W >> K;
  SS.resize(H);
  for (int i = 0; i < H; i++)
    cin >> SS[i];
  vector<int> CH(H), CW(W);
  dfs("");
  cout << Min << endl;
}
Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 2e5 + 50;
const int mod = 1e9 + 7;
int s[15][15];
int mark[15];
int sum(int x1, int y1, int x2, int y2) {
  return s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1];
}
int main() {
  memset(s, 0, sizeof s);
  int h, w, k, mi = 1000000000;
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 1; i <= h; i++) {
    while (getchar() != '\n')
      ;
    for (int j = 1; j <= w; j++) {
      char x;
      scanf("%c", &x);
      s[i][j] = x - '0' + s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1];
    }
  }
  for (int i = 0; i < 1 << (h - 1); i++) {
    int cnt = i, y = 1, ans = 0;
    for (int j = 1; j < h; j++) {
      mark[j] = cnt % 2;
      if (mark[j] == 1)
        ans++;
      cnt /= 2;
    }
    for (int j = 1; j <= w; j++) {
      int x = 1;
      for (int c = 1; c <= h; c++) {
        if (sum(x, y, c, j) > k)
          goto loop;
        if (mark[c] == 1 || c == h) {
          if (sum(x, y, c, j + 1) > k) {
            y = j + 1;
            ans++;
          }
          x = c + 1;
        }
      }
    }
    mi = min(mi, ans);
  loop:;
  }
  printf("%d", mi);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 2e5 + 50;
const int mod = 1e9 + 7;
int s[15][1050];
int mark[15];
int sum(int x1, int y1, int x2, int y2) {
  return s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1];
}
int main() {
  memset(s, 0, sizeof s);
  int h, w, k, mi = 1000000000;
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 1; i <= h; i++) {
    while (getchar() != '\n')
      ;
    for (int j = 1; j <= w; j++) {
      char x;
      scanf("%c", &x);
      s[i][j] = x - '0' + s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1];
    }
  }
  for (int i = 0; i < 1 << (h - 1); i++) {
    int cnt = i, y = 1, ans = 0;
    for (int j = 1; j < h; j++) {
      mark[j] = cnt % 2;
      if (mark[j] == 1)
        ans++;
      cnt /= 2;
    }
    for (int j = 1; j <= w; j++) {
      int x = 1;
      for (int c = 1; c <= h; c++) {
        if (sum(x, y, c, j) > k)
          goto loop;
        if (mark[c] == 1 || c == h) {
          if (sum(x, y, c, j + 1) > k) {
            y = j + 1;
            ans++;
          }
          x = c + 1;
        }
      }
    }
    mi = min(mi, ans);
  loop:;
  }
  printf("%d", mi);
  return 0;
}