Problem p02733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <string>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\n"
#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

/*
 for(i=0; i<N; i++)
   cin >> a[i];
*/

int main() {
  ll i, j, k;

  ll H, W, K;
  cin >> H >> W >> K;
  string s[11];
  ll sum[11][1111] = {};

  for (i = 1; i <= H; i++) {
    cin >> s[i];
    s[i] = "0" + s[i];
  }

  for (i = 1; i <= H; i++)
    for (j = 1; j <= W; j++)
      sum[i][j] = sum[i][j - 1] + s[i][j] - '0';
  for (j = 1; j <= W; j++)
    for (i = 1; i <= H; i++)
      sum[i][j] += sum[i - 1][j];

  ll ans = INF;
  for (ll S = 0; S < 1 << (H - 1); S++) {

    vector<ll> v;
    v.push_back(0);
    for (i = 0; i < H - 1; i++)
      if (S >> i & 1)
        v.push_back(i + 1);
    v.push_back(H);

    ll n = v.size() - 1;
    vector<ll> cnt[n];
    for (k = 0, i = 1; i < v.size(); k++, i++) {
      cnt[k].push_back(0);
      for (j = 1; j <= W; j++)
        cnt[k].push_back(sum[v[i]][j] - sum[v[i - 1]][j]);
      cnt[k].push_back(INF);
    }

    ll c = -1;
    ll k = 0;
    while (k < W) {
      ll t = W;
      for (i = 0; i < n; i++) {
        ll p = k + 1;
        while (cnt[i][k] + K >= cnt[i][p])
          p++;
        p--;
        chmin(t, p);
      }

      k = t;
      c++;
    }

    chmin(ans, (ll)v.size() - 2LL + c);
  }

  pt(ans);
}


Fixed submission:
#include <bits/stdc++.h>
#include <string>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\n"
#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)
#define moC(a, s, b) (a) = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

/*
 for(i=0; i<N; i++)
   cin >> a[i];
*/

int main() {
  ll i, j, k;

  ll H, W, K;
  cin >> H >> W >> K;
  string s[11];
  ll sum[11][1111] = {};

  for (i = 1; i <= H; i++) {
    cin >> s[i];
    s[i] = "0" + s[i];
  }

  for (i = 1; i <= H; i++)
    for (j = 1; j <= W; j++)
      sum[i][j] = sum[i][j - 1] + s[i][j] - '0';
  for (j = 1; j <= W; j++)
    for (i = 1; i <= H; i++)
      sum[i][j] += sum[i - 1][j];

  ll ans = INF;
  for (ll S = 0; S < 1 << (H - 1); S++) {

    vector<ll> v;
    v.push_back(0);
    for (i = 0; i < H - 1; i++)
      if (S >> i & 1)
        v.push_back(i + 1);
    v.push_back(H);

    ll n = v.size() - 1;
    vector<ll> cnt[n];
    for (k = 0, i = 1; i < v.size(); k++, i++) {
      cnt[k].push_back(0);
      for (j = 1; j <= W; j++)
        cnt[k].push_back(sum[v[i]][j] - sum[v[i - 1]][j]);
      cnt[k].push_back(INF);
    }

    ll c = -1;
    ll k = 0;
    while (k < W) {
      ll t = W;
      for (i = 0; i < n; i++) {
        ll p = k + 1;
        while (cnt[i][k] + K >= cnt[i][p])
          p++;
        p--;
        chmin(t, p);
      }

      if (k == t) {
        c = INF;
        break;
      }

      k = t;
      c++;
    }

    chmin(ans, (ll)v.size() - 2LL + c);
  }

  pt(ans);
}