Problem p02733 (C++)

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

using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(vr) vr.begin(), vr.end()
#define pi pair<int, int>

typedef long long ll;
namespace myrand {
mt19937 mt(chrono::system_clock::now().time_since_epoch() /
           chrono::microseconds(1));
ll Int(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(mt); }
} // namespace myrand
using namespace myrand;
auto ss = chrono::system_clock::now();
double remain_time() {
  auto en = chrono::system_clock::now();
  return (chrono::duration_cast<chrono::nanoseconds>(en - ss).count() * 1e-9);
}

int a[15][1005], s[15][1005];
int bitmask[15];
int kqtemp, res, H, W, k;
vector<int> bitt;

int sum(int x, int y, int i, int j) {
  return (s[x][y] + s[i - 1][j - 1] - s[i - 1][y] - s[x][j - 1]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt","r",stdin);
  cin >> H >> W >> k;
  for (int i = 1; i <= H; i++) {
    string s;
    cin >> s;
    s = '@' + s;
    for (int j = 1; j <= W; j++)
      a[i][j] = s[j] - '0';
  }
  res = (H - 1) + (W - 1);
  bitmask[0] = 1;
  for (int i = 1; i <= H + 1; i++)
    bitmask[i] = bitmask[i - 1] * 2;
  for (int i = 1; i <= H; i++)
    for (int j = 1; j <= W; j++)
      s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
  for (int i = bitmask[H - 1] - 1; i >= 0; i--) {
    bitt.clear();
    bitt.push_back(0);
    for (int j = 0; j <= H - 2; j++) {
      if ((i >> j) & 1 == 1)
        bitt.push_back(j + 1);
    }
    int j1 = W;
    kqtemp = __builtin_popcount(i);
    bitt.push_back(H);
    while (j1 >= 1) {
      int temp = j1;
      bool flag = true;
      while (temp >= 1) {
        for (int bit = bitt.size() - 2; bit >= 0; bit--) {
          if (bitt[bit] == -1)
            continue;
          int cucu = sum(bitt[bit + 1], j1, bitt[bit] + 1, temp);
          if (sum(bitt[bit + 1], j1, bitt[bit] + 1, temp) > k) {
            flag = false;
            break;
          }
        }
        if (!flag)
          break;
        else
          temp--;
      }
      if (flag == false)
        kqtemp++;
      if (flag == true && temp <= 1) {
        j1 = 0;
        break;
      }
      j1 = temp;
    }
    res = min(res, kqtemp);
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(vr) vr.begin(), vr.end()
#define pi pair<int, int>

typedef long long ll;
namespace myrand {
mt19937 mt(chrono::system_clock::now().time_since_epoch() /
           chrono::microseconds(1));
ll Int(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(mt); }
} // namespace myrand
using namespace myrand;
auto ss = chrono::system_clock::now();
double remain_time() {
  auto en = chrono::system_clock::now();
  return (chrono::duration_cast<chrono::nanoseconds>(en - ss).count() * 1e-9);
}

int a[15][1005], s[15][1005];
int bitmask[15];
int kqtemp, res, H, W, k;
vector<int> bitt;

int sum(int x, int y, int i, int j) {
  return (s[x][y] + s[i - 1][j - 1] - s[i - 1][y] - s[x][j - 1]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt","r",stdin);
  cin >> H >> W >> k;
  for (int i = 1; i <= H; i++) {
    string s;
    cin >> s;
    s = '@' + s;
    for (int j = 1; j <= W; j++)
      a[i][j] = s[j] - '0';
  }
  res = (H - 1) + (W - 1);
  bitmask[0] = 1;
  for (int i = 1; i <= H + 1; i++)
    bitmask[i] = bitmask[i - 1] * 2;
  for (int i = 1; i <= H; i++)
    for (int j = 1; j <= W; j++)
      s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
  for (int i = bitmask[H - 1] - 1; i >= 0; i--) {
    bitt.clear();
    bitt.push_back(0);
    for (int j = 0; j <= H - 2; j++) {
      if ((i >> j) & 1 == 1)
        bitt.push_back(j + 1);
    }
    int j1 = W;
    kqtemp = __builtin_popcount(i);
    bitt.push_back(H);
    while (j1 >= 1) {
      int temp = j1;
      bool flag = true;
      while (temp >= 1) {
        for (int bit = bitt.size() - 2; bit >= 0; bit--) {
          if (bitt[bit] == -1)
            continue;
          int cucu = sum(bitt[bit + 1], j1, bitt[bit] + 1, temp);
          if (sum(bitt[bit + 1], j1, bitt[bit] + 1, temp) > k) {
            flag = false;
            break;
          }
        }
        if (!flag)
          break;
        else
          temp--;
      }
      if (flag == false)
        kqtemp++;
      if (flag == true && temp <= 1) {
        j1 = 0;
        break;
      }
      if (j1 == temp && flag == false) {
        kqtemp = 1000000;
        break;
      }
      j1 = temp;
    }
    res = min(res, kqtemp);
  }
  cout << res << endl;
  return 0;
}