Problem p02733 (C++)

Buggy submission (status: Runtime Error):
/*
Author- Aasav Badera
Time- Tue Mar 24 21:33:15 2020
*/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mii map<ll, ll>
#define dbg(x)                                                                 \
  { cerr << #x << ": " << x << endl; }
#define dbg2(x, y)                                                             \
  { cerr << #x << ": " << x << " , " << #y << ": " << y << endl; }
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define pii pair<ll, ll>
#define ppb pop_back
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define si set<ll>
#define all(a) (a).begin(), (a).end()
#define sz(x) (ll) x.size()
#define hell 1000000007
#define mp make_pair
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define inf 1e18L + 1
#define endl '\n'
using namespace std;
#define N 300005
ll powe(ll x, ll y) {
  ll res = 1;

  x = x;

  while (y > 0) {
    if (y & 1)
      res = (res * x);

    y = y >> 1;
    x = (x * x);
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ll tests = 1;
  // cin >> tests;
  while (tests--) {
    ll h, w, k;
    cin >> h >> w >> k;
    string s[h];
    rep(i, 0, h) cin >> s[i];
    ll dp[h + 1][w + 1];
    rep(i, 0, h + 1) dp[i][0] = 0;
    rep(i, 0, w + 1) dp[0][i] = 0;
    rep(i, 1, h + 1) {
      rep(j, 1, w + 1) {
        dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] +
                   s[i - 1][j - 1] - '0';
      }
    }
    // rep(i, 0, h)
    // {
    // 	rep(j, 0 ,w)
    // 	{
    // 		cout << dp[i][j] << " ";
    // 	}
    // 	cout << endl;
    // }
    ll ans = inf;
    rep(i, 0, powe(2, h - 1)) {
      ll ans1 = 0;
      vi v;
      ll j = 1, z = 1;
      v.pb(0);
      while (j <= 2 * i) {
        if (j & i)
          v.pb(z);
        j *= 2;
        z++;
      }
      v.pb(h);
      ll prevmove = 0;
      rep(width, 1, w + 1) {
        rep(division, 1, sz(v)) {
          if (dp[v[division]][width] - dp[v[division - 1]][width] -
                  dp[v[division]][width - 1] + dp[v[division - 1]][width - 1] >
              k) {
            ans1 = inf;
            break;
          }
          if (dp[v[division]][width] - dp[v[division - 1]][width] -
                  dp[v[division]][prevmove] + dp[v[division - 1]][prevmove] >
              k) {
            ans1++;
            prevmove = width - 1;
            break;
          }
        }
      }
      ans1 += sz(v) - 2;
      ans = min(ans1, ans);
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
/*
Author- Aasav Badera
Time- Tue Mar 24 21:33:15 2020
*/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mii map<ll, ll>
#define dbg(x)                                                                 \
  { cerr << #x << ": " << x << endl; }
#define dbg2(x, y)                                                             \
  { cerr << #x << ": " << x << " , " << #y << ": " << y << endl; }
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define pii pair<ll, ll>
#define ppb pop_back
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define si set<ll>
#define all(a) (a).begin(), (a).end()
#define sz(x) (ll) x.size()
#define hell 1000000007
#define mp make_pair
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define inf 1e18L + 1
#define endl '\n'
using namespace std;
#define N 300005
ll powe(ll x, ll y) {
  ll res = 1;

  x = x;

  while (y > 0) {
    if (y & 1)
      res = (res * x);

    y = y >> 1;
    x = (x * x);
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // #ifndef ONLINE_JUDGE
  // // for getting input from input.txt
  // freopen("input.txt", "r", stdin);
  // // for writing output to output.txt
  // freopen("output.txt", "w", stdout);
  // #endif
  ll tests = 1;
  // cin >> tests;
  while (tests--) {
    ll h, w, k;
    cin >> h >> w >> k;
    string s[h];
    rep(i, 0, h) cin >> s[i];
    ll dp[h + 1][w + 1];
    rep(i, 0, h + 1) dp[i][0] = 0;
    rep(i, 0, w + 1) dp[0][i] = 0;
    rep(i, 1, h + 1) {
      rep(j, 1, w + 1) {
        dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] +
                   s[i - 1][j - 1] - '0';
      }
    }
    // rep(i, 0, h)
    // {
    // 	rep(j, 0 ,w)
    // 	{
    // 		cout << dp[i][j] << " ";
    // 	}
    // 	cout << endl;
    // }
    ll ans = inf;
    rep(i, 0, powe(2, h - 1)) {
      ll ans1 = 0;
      vi v;
      ll j = 1, z = 1;
      v.pb(0);
      while (j <= 2 * i) {
        if (j & i)
          v.pb(z);
        j *= 2;
        z++;
      }
      v.pb(h);
      ll prevmove = 0;
      rep(width, 1, w + 1) {
        rep(division, 1, sz(v)) {
          if (dp[v[division]][width] - dp[v[division - 1]][width] -
                  dp[v[division]][width - 1] + dp[v[division - 1]][width - 1] >
              k) {
            ans1 = inf;
            break;
          }
          if (dp[v[division]][width] - dp[v[division - 1]][width] -
                  dp[v[division]][prevmove] + dp[v[division - 1]][prevmove] >
              k) {
            ans1++;
            prevmove = width - 1;
            break;
          }
        }
      }
      ans1 += sz(v) - 2;
      ans = min(ans1, ans);
    }
    cout << ans << endl;
  }
  return 0;
}