Problem p02733 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define MAX 500005
#define INF 0x3f3f3f3f
using namespace std;

template <typename T> void read(T &n) {
  n = 0;
  T f = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-')
    c = getchar();
  if (c == '-')
    f = -1, c = getchar();
  while (isdigit(c))
    n = n * 10 + c - '0', c = getchar();
  n *= f;
}
template <typename T> void write(T n) {
  if (n < 0)
    putchar('-'), n = -n;
  if (n > 9)
    write(n / 10);
  putchar(n % 10 + '0');
}

int n, m, k;
int s[15][1005], a[15][1005], t[15];
vector<int> v;

int solve() {
  int res = 0;
  for (int i = 1; i <= m; i++) {
    int flag = 0;
    for (int j = 1; j < v.size(); j++) {
      if (s[i][v[j]] - s[i][v[j - 1]] > k)
        return INF;
      t[j] += s[i][v[j]] - s[i][v[j - 1]];
      if (t[j] > k)
        flag = 1;
    }
    if (flag) {
      res++;
      for (int j = 1; j < v.size(); j++) {
        t[j] = s[i][v[j]] - s[i][v[j - 1]];
      }
    }
  }
  return res;
}

int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%1d", &a[i][j]);
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      s[i][j] = s[i][j - 1] + a[j][i];
    }
  }
  int ans = INF;
  for (int i = 0; i < (1 << n - 1); i++) {
    v.clear();
    v.push_back(0);
    int cnt = 0;
    for (int j = 0; j < n; j++) {
      if ((i >> j) & 1)
        v.push_back(j + 1), cnt++;
    }
    v.push_back(n);
    for (int i = 1; i < v.size(); i++)
      t[i] = 0;
    cnt += solve();
    ans = min(ans, cnt);
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define MAX 500005
#define INF 0x3f3f3f3f
using namespace std;

template <typename T> void read(T &n) {
  n = 0;
  T f = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-')
    c = getchar();
  if (c == '-')
    f = -1, c = getchar();
  while (isdigit(c))
    n = n * 10 + c - '0', c = getchar();
  n *= f;
}
template <typename T> void write(T n) {
  if (n < 0)
    putchar('-'), n = -n;
  if (n > 9)
    write(n / 10);
  putchar(n % 10 + '0');
}

int n, m, k;
int s[1005][15], a[15][1005], t[15];
vector<int> v;

int solve() {
  int res = 0;
  for (int i = 1; i <= m; i++) {
    int flag = 0;
    for (int j = 1; j < v.size(); j++) {
      if (s[i][v[j]] - s[i][v[j - 1]] > k)
        return INF;
      t[j] += s[i][v[j]] - s[i][v[j - 1]];
      if (t[j] > k)
        flag = 1;
    }
    if (flag) {
      res++;
      for (int j = 1; j < v.size(); j++) {
        t[j] = s[i][v[j]] - s[i][v[j - 1]];
      }
    }
  }
  return res;
}

int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%1d", &a[i][j]);
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      s[i][j] = s[i][j - 1] + a[j][i];
    }
  }
  int ans = INF;
  for (int i = 0; i < (1 << n - 1); i++) {
    v.clear();
    v.push_back(0);
    int cnt = 0;
    for (int j = 0; j < n; j++) {
      if ((i >> j) & 1)
        v.push_back(j + 1), cnt++;
    }
    v.push_back(n);
    for (int i = 1; i < v.size(); i++)
      t[i] = 0;
    cnt += solve();
    ans = min(ans, cnt);
  }
  cout << ans << endl;

  return 0;
}