Problem p00912 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int N, W;
int table[50005], ruisekiwa[50005];
bool check[50005];

bool one_line_check(int left,
                    int right) { // left～rightの単語を一行に詰め込めるか

  if (ruisekiwa[right] - ruisekiwa[left - 1] + (right - left) <= W) {
    return true;
  } else {
    return false;
  }
}

bool put_check(int left, int right, int limit) {

  if (right == N)
    return true; // 最終文字は、右端に詰めなくて良い

  if (ruisekiwa[right] - ruisekiwa[left - 1] + limit * (right - left) >= W) {
    return true;
  } else { // ★★各幅を全てlimitにしても、一行の文字数に足りない→少なくとも一か所、limit+1以上のスペースを入れなければならない!!
    return false;
  }
}

bool is_OK(int limit) {

  if (one_line_check(1, N))
    return true;

  check[0] = true;
  bool FLG;

  int left_start = 1;

  for (int right_id = 1; right_id <= N; right_id++) { // 右端のループ
    while (left_start <= right_id &&
               one_line_check(left_start, right_id) == false ||
           check[left_start - 1] == false)
      left_start++;
    FLG = false;
    for (int left_id = left_start; left_id <= right_id; left_id++) {
      if (check[left_id - 1] == false)
        continue;
      if (put_check(left_id, right_id, limit) == true) {
        FLG = true;
        break;
      } else {
        break;
      }
    }
    check[right_id] = FLG;
    if (check[right_id]) {
      if (right_id == N || one_line_check(right_id + 1, N) == true) {
        return true;
      }
    }
  }

  return false;
}

void func() {

  ruisekiwa[0] = 0;
  for (int i = 1; i <= N; i++) {
    scanf("%d", &table[i]);
    ruisekiwa[i] = table[i];
    ruisekiwa[i] += ruisekiwa[i - 1];
  }

  int left = 1, right = W, m = (left + right) / 2;
  int ans = W;

  while (left <= right) {
    if (is_OK(m)) {
      ans = m;
      right = m - 1;
    } else {
      left = m + 1;
    }
    m = (left + right) / 2;
  }

  printf("%d\\n", ans);
}

int main() {

  while (true) {
    scanf("%d %d", &W, &N);
    if (W == 0 && N == 0)
      break;

    func();
  }

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int N, W;
int table[50005], ruisekiwa[50005];
bool check[50005];

bool one_line_check(int left,
                    int right) { // left～rightの単語を一行に詰め込めるか

  if (ruisekiwa[right] - ruisekiwa[left - 1] + (right - left) <= W) {
    return true;
  } else {
    return false;
  }
}

bool put_check(int left, int right, int limit) {

  if (right == N)
    return true; // 最終文字は、右端に詰めなくて良い

  if (ruisekiwa[right] - ruisekiwa[left - 1] + limit * (right - left) >= W) {
    return true;
  } else { // ★★各幅を全てlimitにしても、一行の文字数に足りない→少なくとも一か所、limit+1以上のスペースを入れなければならない!!
    return false;
  }
}

bool is_OK(int limit) {

  if (one_line_check(1, N))
    return true;

  check[0] = true;
  bool FLG;

  int left_start = 1, next_start;

  for (int right_id = 1; right_id <= N; right_id++) { // 右端のループ
    while (one_line_check(left_start, right_id) == false ||
           check[left_start - 1] == false)
      left_start++;
    FLG = false;
    for (int left_id = left_start; left_id <= right_id; left_id++) {
      if (check[left_id - 1] == false)
        continue;
      if (put_check(left_id, right_id, limit) == true) {
        FLG = true;
        break;
      } else {
        break;
      }
    }
    check[right_id] = FLG;
    if (check[right_id]) {
      if (right_id == N || one_line_check(right_id + 1, N) == true) {
        return true;
      }
    }
  }

  return false;
}

void func() {

  ruisekiwa[0] = 0;
  for (int i = 1; i <= N; i++) {
    scanf("%d", &table[i]);
    ruisekiwa[i] = table[i];
    ruisekiwa[i] += ruisekiwa[i - 1];
  }

  int left = 1, right = W, m = (left + right) / 2;
  int ans = W;

  while (left <= right) {
    if (is_OK(m)) {
      ans = m;
      right = m - 1;
    } else {
      left = m + 1;
    }
    m = (left + right) / 2;
  }

  printf("%d\\n", ans);
}

int main() {

  while (true) {
    scanf("%d %d", &W, &N);
    if (W == 0 && N == 0)
      break;

    func();
  }

  return 0;
}