Problem p02733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vc = vector<char>;
using vvc = vector<vc>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpli = vector<pli>;
using vpll = vector<pll>;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, s, n) for (int i = (s); i < (int)n; i++)
int INF = 1e9 + 7;

vvi side;
int H, W, K;

void dfs(vi v, int i) {
  if (i == H) {
    v.push_back(H);
    side.push_back(v);
    return;
  }
  dfs(v, i + 1);
  v.push_back(i);
  dfs(v, i + 1);
}

int main() {
  cin >> H >> W >> K;
  vvc S(H, vc(W));
  rep(h, H) { rep(w, W) cin >> S[h][w]; }

  vector<vvi> sum(H, vvi(H, vi(W + 1)));
  rep(h1, H) {
    rep2(h2, h1, H) {
      rep(w, W) {
        int count = 0;
        rep2(h, h1, h2 + 1) count += S[h][w] - '0';
        sum[h1][h2][w + 1] = sum[h1][h2][w] + count;
      }
    }
  }

  dfs({0}, 1);
  int ans = INF;
  rep(i, side.size()) {
    int count = side[i].size() - 2, l = 0;
    while (true) {
      bool flag = true;
      int r = l;
      while (flag && r < W) {
        r++;
        rep(j, side[i].size() - 1) flag &=
            sum[side[i][j]][side[i][j + 1] - 1][r] -
                sum[side[i][j]][side[i][j + 1] - 1][l] <=
            K;
      }
      l = r - 1;
      if (!flag)
        count++;
      if (r == W)
        break;
    }
    ans = min(ans, count);
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vc = vector<char>;
using vvc = vector<vc>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpli = vector<pli>;
using vpll = vector<pll>;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, s, n) for (int i = (s); i < (int)n; i++)
int INF = 1e9 + 7;

vvi side;
int H, W, K;

void dfs(vi v, int i) {
  if (i == H) {
    v.push_back(H);
    side.push_back(v);
    return;
  }
  dfs(v, i + 1);
  v.push_back(i);
  dfs(v, i + 1);
}

int main() {
  cin >> H >> W >> K;
  vvc S(H, vc(W));
  rep(h, H) { rep(w, W) cin >> S[h][w]; }

  vector<vvi> sum(H, vvi(H, vi(W + 1)));
  rep(h1, H) {
    rep2(h2, h1, H) {
      rep(w, W) {
        int count = 0;
        rep2(h, h1, h2 + 1) count += S[h][w] - '0';
        sum[h1][h2][w + 1] = sum[h1][h2][w] + count;
      }
    }
  }

  dfs({0}, 1);
  int ans = INF;
  rep(i, side.size()) {
    int count = side[i].size() - 2, l = 0;
    while (true) {
      bool flag = true;
      int r = l;
      while (flag && r < W) {
        r++;
        rep(j, side[i].size() - 1) flag &=
            sum[side[i][j]][side[i][j + 1] - 1][r] -
                sum[side[i][j]][side[i][j + 1] - 1][l] <=
            K;
      }
      if (r == l + 1) {
        count = INF;
        break;
      }
      l = r - 1;
      if (!flag)
        count++;
      if (r == W)
        break;
    }
    ans = min(ans, count);
  }

  cout << ans << endl;
}