Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)

using namespace std;

llint h, w, k;
char c[15][1005];
llint sum[15], nsum[15];
// llint sum[15][1005];

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

  cin >> h >> w >> k;
  for (int y = 0; y < h; y++) {
    for (int x = 1; x <= w; x++) {
      cin >> c[x][y];
    }
  }

  /*for(int y = 1; y <= h; y++){
          for(int x = 1; x <= w; x++){
                  sum[x][y] = sum[x-1][y] + sum[x][y-1] - sum[x-1][y-1] +
  (c[x][y]-'0');
          }
  }*/

  llint H = 1 << (h - 1), ans = inf;
  for (int i = 0; i < H; i++) {
    llint cnt = 0;
    for (int y = 0; y < h - 1; y++) {
      if (i & (1 << y))
        cnt++;
    }
    for (int y = 0; y < h; y++)
      sum[y] = 0;
    for (int x = 1; x <= w; x++) {
      llint p = 0;
      for (int y = 0; y < h; y++)
        nsum[y] = 0;
      for (int y = 0; y < h; y++) {
        nsum[p] += c[x][y] - '0';
        if (i & (1 << y))
          p = y + 1;
      }
      bool flag = true;
      for (int y = 0; y < h; y++) {
        if (nsum[y] > k)
          goto end;
        if (sum[y] + nsum[y] > k)
          flag = false;
      }
      if (flag) {
        for (int y = 0; y < h; y++)
          sum[y] += nsum[y];
      } else {
        for (int y = 0; y < h; y++)
          sum[y] = nsum[y];
        cnt++;
      }
    }
    ans = min(ans, cnt);
  end:;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)

using namespace std;

llint h, w, k;
char c[1005][15];
llint sum[15], nsum[15];
// llint sum[15][1005];

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

  cin >> h >> w >> k;
  for (int y = 0; y < h; y++) {
    for (int x = 1; x <= w; x++) {
      cin >> c[x][y];
    }
  }

  /*for(int y = 1; y <= h; y++){
          for(int x = 1; x <= w; x++){
                  sum[x][y] = sum[x-1][y] + sum[x][y-1] - sum[x-1][y-1] +
  (c[x][y]-'0');
          }
  }*/

  llint H = 1 << (h - 1), ans = inf;
  for (int i = 0; i < H; i++) {
    llint cnt = 0;
    for (int y = 0; y < h - 1; y++) {
      if (i & (1 << y))
        cnt++;
    }
    for (int y = 0; y < h; y++)
      sum[y] = 0;
    for (int x = 1; x <= w; x++) {
      llint p = 0;
      for (int y = 0; y < h; y++)
        nsum[y] = 0;
      for (int y = 0; y < h; y++) {
        nsum[p] += c[x][y] - '0';
        if (i & (1 << y))
          p = y + 1;
      }
      bool flag = true;
      for (int y = 0; y < h; y++) {
        if (nsum[y] > k)
          goto end;
        if (sum[y] + nsum[y] > k)
          flag = false;
      }
      if (flag) {
        for (int y = 0; y < h; y++)
          sum[y] += nsum[y];
      } else {
        for (int y = 0; y < h; y++)
          sum[y] = nsum[y];
        cnt++;
      }
    }
    ans = min(ans, cnt);
  end:;
  }
  cout << ans << endl;

  return 0;
}