Problem p03147 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

const vector<vector<int>> DivideGroup(const vector<int> origGroup) {
  vector<vector<int>> divideds;
  vector<int> divided;
  for (const int member : origGroup) {
    if (member == 0) {
      if (!divided.empty()) {
        divideds.push_back(divided);
        divided.clear();
      }
    } else {
      divided.push_back(member);
    }
  }
  if (!divided.empty()) {
    divideds.push_back(divided);
  }
  return divideds;
}

int main() {
  int N;
  cin >> N;
  vector<int> heights(N);
  for (int i = 0; i < N; ++i) {
    cin >> heights[i];
  }

  vector<vector<int>> groups{heights};
  int num = 0;
  for (;;) {
    if (groups.empty()) {
      break;
    }

    // 0で各グループを分割
    vector<vector<int>> dividedGroups;
    for (auto itr = groups.begin(); itr != groups.end();) {
      const auto divideds = DivideGroup(*itr);
      if (!divideds.empty()) {
        dividedGroups.insert(dividedGroups.end(), divideds.begin(),
                             divideds.end());
        itr = groups.erase(itr);
      } else {
        ++itr;
      }
    }
    groups.insert(groups.end(), dividedGroups.begin(), dividedGroups.end());

    // 各グループごとに１ずつ減らす
    for (auto &group : groups) {
      for (int &member : group) {
        --member;
      }
      ++num;
    }

    // グループ内がゼロのグループは削除
    for (auto itr = groups.begin(); itr != groups.end();) {
      if (accumulate(itr->begin(), itr->end(), 0) == 0) {
        itr = groups.erase(itr);
      } else {
        ++itr;
      }
    }
  }
  cout << num << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

const vector<vector<int>> DivideGroup(const vector<int> origGroup) {
  vector<vector<int>> divideds;
  vector<int> divided;
  for (const int member : origGroup) {
    if (member == 0) {
      if (!divided.empty()) {
        divideds.push_back(divided);
        divided.clear();
      }
    } else {
      divided.push_back(member);
    }
  }
  if (!divided.empty()) {
    divideds.push_back(divided);
  }
  return divideds;
}

int main() {
  int N;
  cin >> N;
  vector<int> heights(N);
  for (int i = 0; i < N; ++i) {
    cin >> heights[i];
  }
  if (accumulate(heights.begin(), heights.end(), 0) == 0) {
    cout << 0 << endl;
    return 0;
  }

  vector<vector<int>> groups{heights};
  int num = 0;
  for (;;) {
    if (groups.empty()) {
      break;
    }

    // 0で各グループを分割
    vector<vector<int>> dividedGroups;
    for (auto itr = groups.begin(); itr != groups.end();) {
      const auto divideds = DivideGroup(*itr);
      if (!divideds.empty()) {
        dividedGroups.insert(dividedGroups.end(), divideds.begin(),
                             divideds.end());
        itr = groups.erase(itr);
      } else {
        ++itr;
      }
    }
    groups.insert(groups.end(), dividedGroups.begin(), dividedGroups.end());

    // 各グループごとに１ずつ減らす
    for (auto &group : groups) {
      for (int &member : group) {
        --member;
      }
      ++num;
    }

    // グループ内がゼロのグループは削除
    for (auto itr = groups.begin(); itr != groups.end();) {
      if (accumulate(itr->begin(), itr->end(), 0) == 0) {
        itr = groups.erase(itr);
      } else {
        ++itr;
      }
    }
  }
  cout << num << endl;
}