Problem p02733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);

  ll H, W, K;
  cin >> H >> W >> K;
  vector<string> S(H);
  Rep(i, H) { cin >> S[i]; }

  mat s(H, vec(W + 1, 0));
  Rep(i, H) {
    Rep(j, W) { s[i][j + 1] += s[i][j] + (S[i][j] == '1'); }
  }

  ll ans = inf;

  Rep(i, 1 << (H - 1)) {
    ll tmp = 0;
    Rep(j, H - 1) {
      if (i & (1 << j)) {
        tmp++;
      }
    }
    mat cnt(tmp + 1, vec(W + 1, 0));
    ll now = 0;
    Rep(j, H) {
      Rep(k, W + 1) {
        // cout << now << "\n";
        cnt[now][k] += s[j][k];
      }
      if (i & (1 << j)) {
        now++;
      }
    }

    // cout << i << "\n";
    // Rep (j, tmp+1) {
    //     Rep (k, W+1) {
    //         cout << cnt[j][k] << " \n"[k==W];
    //     }
    // }

    ll a = tmp + 1;
    ll left = 0;
    while (left < W) {
      ll right = inf;
      Rep(j, a) {
        auto e = upper_bound(cnt[j].begin(), cnt[j].end(), cnt[j][left] + K);
        chmin(right, e - cnt[j].begin());
      }
      // cout << i << " " << left << " " << right << "\n";
      if (right < W + 1) {
        tmp++;
        left = right - 1;
      } else if (right == W + 1) {
        left = right - 1;
      }
    }

    chmin(ans, tmp);
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);

  ll H, W, K;
  cin >> H >> W >> K;
  vector<string> S(H);
  Rep(i, H) { cin >> S[i]; }

  mat s(H, vec(W + 1, 0));
  Rep(i, H) {
    Rep(j, W) { s[i][j + 1] += s[i][j] + (S[i][j] == '1'); }
  }

  ll ans = inf;

  Rep(i, 1 << (H - 1)) {
    ll tmp = 0;
    Rep(j, H - 1) {
      if (i & (1 << j)) {
        tmp++;
      }
    }
    mat cnt(tmp + 1, vec(W + 1, 0));
    ll now = 0;
    Rep(j, H) {
      Rep(k, W + 1) {
        // cout << now << "\n";
        cnt[now][k] += s[j][k];
      }
      if (i & (1 << j)) {
        now++;
      }
    }

    // cout << i << "\n";
    // Rep (j, tmp+1) {
    //     Rep (k, W+1) {
    //         cout << cnt[j][k] << " \n"[k==W];
    //     }
    // }

    ll a = tmp + 1;
    ll left = 0;
    while (left < W) {
      ll right = inf;
      Rep(j, a) {
        auto e = upper_bound(cnt[j].begin(), cnt[j].end(), cnt[j][left] + K);
        chmin(right, e - cnt[j].begin());
      }
      // cout << i << " " << left << " " << right << "\n";
      if (left + 1 == right) {
        tmp = inf;
        break;
      }
      if (right < W + 1) {
        tmp++;
        left = right - 1;
      } else if (right == W + 1) {
        left = right - 1;
      }
    }

    chmin(ans, tmp);
  }

  cout << ans << endl;
}