Problem p02733 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
char **Sdata;
int H, W, K;

int MaxVerticalBar(int offset, vector<int> RegionEdge, int BarNumber) {
  int MAX = offset;
  vector<int> Chocolates(BarNumber + 1, 0);
  int ChocoEnd = 0;

  for (MAX = offset; MAX < W; MAX++) {
    for (int i = 0; i <= BarNumber; i++)
      for (int i2 = RegionEdge[i]; i2 < RegionEdge[i + 1]; i2++) {
        Chocolates[i] += (int)(Sdata[i2][MAX] - '0');
        if (Chocolates[i] > K) {
          if (MAX != 0)
            return MAX - 1;
          else
            return 0;
        }
      }
  }
  return MAX;
};

int main(void) {
  cin >> H >> W >> K;

  Sdata = new char *[H];
  for (int i = 0; i < H; i++) {
    Sdata[i] = new char[W];
    for (int j = 0; j < W; j++)
      cin >> Sdata[i][j];
  }
  int BarNumber = 0;
  vector<int> RegionEdge(H + 1, 0);
  int WorldRecord = H + W;
  for (int UnderBar = 0; UnderBar < (1 << (H - 1)); UnderBar++) {
    BarNumber = 0;
    for (int i = 0; i < H; i++) {
      if (((UnderBar >> i) & 1) != 0) {
        RegionEdge[BarNumber + 1] = i + 1;
        BarNumber++;
      }
    }
    RegionEdge[BarNumber + 1] = H;

    if (BarNumber < WorldRecord) {
      int answer = BarNumber;
      for (int offset = 0; offset < W; offset++) {
        int VerticalBar;
        VerticalBar = MaxVerticalBar(offset, RegionEdge, BarNumber);
        if (VerticalBar != W)
          answer++;
        offset = VerticalBar;
      }

      if (WorldRecord > answer)
        WorldRecord = answer;
    }
  }

  cout << WorldRecord << endl;

  for (int i = 0; i < H; i++)
    delete[] Sdata[i];
  delete[] Sdata;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
char **Sdata;
int H, W, K;

int MaxVerticalBar(int offset, vector<int> RegionEdge, int BarNumber) {
  int MAX = offset;
  vector<int> Chocolates(BarNumber + 1, 0);
  int ChocoEnd = 0;

  for (MAX = offset; MAX < W; MAX++) {
    for (int i = 0; i <= BarNumber; i++)
      for (int i2 = RegionEdge[i]; i2 < RegionEdge[i + 1]; i2++) {
        Chocolates[i] += (int)(Sdata[i2][MAX] - '0');
        if (Chocolates[i] > K) {
          if (MAX != 0)
            return MAX - 1;
          else
            return 0;
        }
      }
  }
  return MAX;
};

int main(void) {
  cin >> H >> W >> K;

  Sdata = new char *[H];
  for (int i = 0; i < H; i++) {
    Sdata[i] = new char[W];
    for (int j = 0; j < W; j++)
      cin >> Sdata[i][j];
  }
  int BarNumber = 0;
  vector<int> RegionEdge(H + 1, 0);
  int WorldRecord = H + W;
  for (int UnderBar = 0; UnderBar < (1 << (H - 1)); UnderBar++) {
    BarNumber = 0;
    for (int i = 0; i < H; i++) {
      if (((UnderBar >> i) & 1) != 0) {
        RegionEdge[BarNumber + 1] = i + 1;
        BarNumber++;
      }
    }
    RegionEdge[BarNumber + 1] = H;

    if (BarNumber < WorldRecord) {
      int answer = BarNumber;
      for (int offset = 0; offset < W; offset++) {
        int VerticalBar;
        VerticalBar = MaxVerticalBar(offset, RegionEdge, BarNumber);
        if (VerticalBar != W)
          answer++;
        if (WorldRecord <= answer)
          break;
        offset = VerticalBar;
      }

      if (WorldRecord > answer)
        WorldRecord = answer;
    }
  }

  cout << WorldRecord << endl;

  for (int i = 0; i < H; i++)
    delete[] Sdata[i];
  delete[] Sdata;
}