alexjercan/bugnet huggingface dataset:
Problem p00180 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

const int inf = 999999;

int prim(int a[101][101], int n) {
  int r = 0, mcost[101];
  bool vis[101];

  fill(mcost, mcost + 101, inf);
  fill(vis, vis + 101, false);
  mcost[0] = 0;

  while (1) {
    int v = -1;

    for (int i = 0; i < n; i++) {
      if (!vis[i] && (v == -1 || mcost[v] > mcost[i])) {
        v = i;
      }
    }

    if (v == -1) {
      break;
    }
    vis[v] = true;
    r += mcost[v];

    for (int i = 0; i < n; i++) {
      mcost[i] = min(mcost[i], a[v][i]);
    }
  }

  return r;
}

int main() {
  int n, m, a[101][101], s, t, u;

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }

    fill(&a[0][0], &a[100][100], inf);
    for (int i = 0; i <= n; i++) {
      a[i][i] = 0;
    }

    while (m--) {
      cin >> s >> t >> u;
      a[s][t] = a[t][s] = u;
    }

    cout << prim(a, n) << endl;
  }

  return 0;
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

const int inf = 999999;

int prim(int a[101][101], int n) {
  int r = 0, mcost[101];
  bool vis[101];

  fill(mcost, mcost + 101, inf);
  fill(vis, vis + 101, false);
  mcost[0] = 0;

  while (1) {
    int v = -1;

    for (int i = 0; i < n; i++) {
      if (!vis[i] && (v == -1 || mcost[v] > mcost[i])) {
        v = i;
      }
    }

    if (v == -1) {
      break;
    }
    vis[v] = true;
    r += mcost[v];

    for (int i = 0; i < n; i++) {
      mcost[i] = min(mcost[i], a[v][i]);
    }
  }

  return r;
}

int main() {
  int n, m, a[101][101], s, t, u;

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }

    fill(&a[0][0], &a[101][101], inf);
    for (int i = 0; i <= n; i++) {
      a[i][i] = 0;
    }

    while (m--) {
      cin >> s >> t >> u;
      a[s][t] = a[t][s] = u;
    }

    cout << prim(a, n) << endl;
  }

  return 0;
}
Problem p00180 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

using namespace std;

// disjoint-set
int parent[100];

void init(int n) {
  for (int i = 0; i < n; i++)
    parent[i] = i;
}

int root(int a) {
  if (parent[a] == a)
    return a;
  return (parent[a] = root(parent[a]));
}

bool same_set_p(int a, int b) { return root(a) == root(b); }

void unite(int a, int b) { parent[root(a)] = root(b); }

// graph
class Edge {
public:
  int cost, a, b;
  Edge(int c = 0, int d = 0, int e = 0) : cost(c), a(d), b(e) {}
  bool operator<(const Edge &o) const { return cost < o.cost; }
};

// main
int n, m;
Edge graph[10000];

int main() {
  while (scanf("%d %d", &n, &m), n) {
    for (int i = 0; i < m; i++) {
      int a, b, cost;
      scanf("%d %d %d", &a, &b, &cost);
      graph[i] = Edge(cost, a, b);
    }

    sort(graph, graph + m);

    int answer = 0;
    init(n);

    for (int i = 0; i < m; i++) {
      Edge e = graph[i];
      if (!same_set_p(e.a, e.b)) {
        answer += e.cost;
        unite(e.a, e.b);
      }
    }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

// disjoint-set
int parent[100];

void init(int n) {
  for (int i = 0; i < n; i++)
    parent[i] = i;
}

int root(int a) {
  if (parent[a] == a)
    return a;
  return (parent[a] = root(parent[a]));
}

bool same_set_p(int a, int b) { return root(a) == root(b); }

void unite(int a, int b) { parent[root(a)] = root(b); }

// graph
class Edge {
public:
  int cost, a, b;
  Edge(int c = 0, int d = 0, int e = 0) : cost(c), a(d), b(e) {}
  bool operator<(const Edge &o) const { return cost < o.cost; }
};

// main
int n, m;
Edge graph[100];

int main() {
  while (scanf("%d %d", &n, &m), n) {
    for (int i = 0; i < m; i++) {
      int a, b, cost;
      scanf("%d %d %d", &a, &b, &cost);
      graph[i] = Edge(cost, a, b);
    }

    sort(graph, graph + m);

    int answer = 0;
    init(n);

    for (int i = 0; i < m; i++) {
      Edge e = graph[i];
      if (!same_set_p(e.a, e.b)) {
        answer += e.cost;
        unite(e.a, e.b);
      }
    }

    printf("%d\\n", answer);
  }
}
Problem p00180 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  int n, m;
  for (; cin >> n >> m, n;) {
    int edge[n][n];
    memset(edge, -1, sizeof(edge));
    for (int i = 0; i < m; i++) {
      int a, b, cost;
      cin >> a >> b >> cost;
      edge[a][b] = cost;
      edge[b][a] = cost;
    }
    bool closed[n];
    memset(closed, false, sizeof(closed));
    int cnt = n - 1;
    int ans = 0;
    closed[0] = true;
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        qu;
    for (int i = 0; i < n; i++) {
      if (edge[0][i] != -1)
        qu.push(make_pair(edge[0][i], i));
    }
    while (true) {
      int pos = qu.top().second;
      int cost = qu.top().first;
      //  cout<<"#"<<pos<<" "<<cost<<endl;
      qu.pop();
      if (closed[pos])
        continue;
      closed[pos] = true;
      ans += cost;
      cnt--;
      if (cnt <= 0)
        break;
      for (int i = 0; i < n; i++) {
        if (edge[pos][i] != -1)
          qu.push(make_pair(edge[pos][i], i));
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  int n, m;
  for (; cin >> n >> m, n;) {
    int edge[n][n];
    memset(edge, -1, sizeof(edge));
    for (int i = 0; i < m; i++) {
      int a, b, cost;
      cin >> a >> b >> cost;
      edge[a][b] = cost;
      edge[b][a] = cost;
    }
    bool closed[n];
    int cnt = n - 1;
    int ans = 0;
    memset(closed, false, sizeof(closed));
    closed[0] = true;
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        qu;
    for (int i = 0; i < m; i++) {
      if (edge[0][i] != -1)
        qu.push(make_pair(i, edge[0][i]));
    }
    while (true) {
      int pos = qu.top().first;
      int cost = qu.top().second;
      qu.pop();
      if (closed[pos])
        continue;
      closed[pos] = true;
      ans += cost;
      cnt--;
      if (cnt <= 0)
        break;
      for (int i = 0; i < m; i++) {
        if (!closed[i] && edge[pos][i] != -1)
          qu.push(make_pair(i, edge[pos][i]));
      }
    }
    cout << ans << endl;
  }
}
Problem p00180 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define TOSHI_MAX 100
#define COST_MAX 100000
using namespace std;

int prim(int data[TOSHI_MAX][TOSHI_MAX], int toshiNum) {
  bool visited[toshiNum];
  int cost[toshiNum];
  for (int i = 0; i < toshiNum; i++) {
    visited[i] = false;
    cost[i] = COST_MAX;
  }
  cost[0] = 0;
  while (1) {
    int min = COST_MAX;
    int p;
    for (int i = 0; i < toshiNum; i++) {
      if (!visited[i] && cost[i] < min) {
        min = cost[i];
        p = i;
      }
    }
    if (min == COST_MAX)
      break;

    visited[p] = true;
    for (int i = 0; i < toshiNum; i++) {
      if (!visited[i] && data[p][i] < cost[i]) {
        cost[i] = data[p][i];
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < toshiNum; i++) {
    if (cost[i] != COST_MAX)
      ans += cost[i];
  }

  return ans;
}

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    int data[TOSHI_MAX][TOSHI_MAX];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        data[i][j] = COST_MAX;
      }
    }

    for (int i = 0; i < m; i++) {
      int a, b, cost;
      cin >> a >> b >> cost;
      data[a][b] = data[b][a] = cost;
    }

    cout << prim(data, n) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define TOSHI_MAX 100
#define COST_MAX 100000
using namespace std;

int prim(int data[][TOSHI_MAX], int toshiNum) {
  bool visited[toshiNum];
  int cost[toshiNum];
  for (int i = 0; i < toshiNum; i++) {
    visited[i] = false;
    cost[i] = COST_MAX;
  }
  cost[0] = 0;
  while (1) {
    int min = COST_MAX;
    int p;
    for (int i = 0; i < toshiNum; i++) {
      if (!visited[i] && cost[i] < min) {
        min = cost[i];
        p = i;
      }
    }
    if (min == COST_MAX)
      break;

    visited[p] = true;
    for (int i = 0; i < toshiNum; i++) {
      if (!visited[i] && data[p][i] < cost[i]) {
        cost[i] = data[p][i];
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < toshiNum; i++) {
    if (cost[i] != COST_MAX)
      ans += cost[i];
  }

  return ans;
}

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    int data[TOSHI_MAX][TOSHI_MAX];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        data[i][j] = COST_MAX;
      }
    }

    for (int i = 0; i < m; i++) {
      int a, b, cost;
      cin >> a >> b >> cost;
      data[a][b] = data[b][a] = cost;
    }

    cout << prim(data, m) << endl;
  }

  return 0;
}
Problem p00181 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
int books[128];
int m, n;
bool check(int mid) {
  int now = 0; // 現在の添字
  int d = 0;   // 現在の棚
  int l = 0;   // 現在の棚の幅
  while (now < n && d < m) {
    if (l + books[now] <= mid) {
      l += books[now++];
    } else {
      d++;
      l = 0;
    }
  }
  if (d > m - 1)
    return false;
  else
    return true;
}
int main() {
  while (scanf("%d%d", &m, &n), (m | n)) {
    int sum = 0;
    int ma = -1000;
    for (int i = 0; i < n; i++) {
      scanf("%d", &books[i]);
      sum += books[i];
      ma = max(ma, books[i]);
    }

    int l = ma, r = sum;
    int mid = 0;
    for (int i = 0; i < 100; i++) {
      mid = (l + r) / 2;
      if (check(mid)) {
        r = mid;
      } else
        l = mid;
    }
    printf("%d\\n", r);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
int psum[128];
bool vis[1500001];
int m, n;
bool check(int mid) {
  int now = 0;
  int D = 0;
  int cnt = 0;
  bool f = false;
  while (true) {
    if (psum[now] < mid && !f)
      now++;
    else if (psum[now] == mid && !f) {
      f = true;
      cnt++;
      D = now - 1;
    } else if (f && psum[now] - psum[D] < mid)
      now++;
    else if (f && psum[now] - psum[D] == mid) {
      cnt++;
      D = now - 1;
    } else {
      f = true;
      cnt++;
      D = now - 1;
    }
    if (now == n)
      break;
  }

  if (cnt > m - 1)
    return false;
  else
    return true;
}
int main() {
  while (scanf("%d%d", &m, &n), (m | n)) {
    int books[128];
    int sum = 0;
    int ma = -1000;
    for (int i = 0; i < n; i++) {
      scanf("%d", &books[i]);
      sum += books[i];
      ma = max(ma, books[i]);
      if (i == 0)
        psum[i] = sum;
      else
        psum[i] = psum[i - 1] + books[i];
    }

    int l = ma, r = sum;
    int mid = 0;
    for (int i = 0; i < 100; i++) {
      mid = (l + r) / 2;
      if (check(mid)) {
        r = mid;
      } else
        l = mid;
    }
    printf("%d\\n", mid);
  }
  return 0;
}
Problem p00181 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int n, m;
int hon[100];
// 3 9 5 3 8 2 1 6 9 7 4

int doteki() {
  int d[101][30];
  int p[101];
  p[0] = hon[0];
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      d[i][j] = 1 << 29;
    }
  }
  for (int i = 1; i < m; i++) {
    p[i] = p[i - 1] + hon[i];
  }
  for (int i = 0; i < n; i++) {
    d[0][i] = p[0];
  }
  for (int i = 0; i < m; i++) {
    d[i][0] = p[i];
  }
  for (int i = 1; i < m; i++) {
    for (int j = 1; j < n; j++) {
      for (int k = 0; k < i; k++) {
        d[i][j] = min(max(d[k][j - 1], p[i] - p[k]), d[i][j]);
      }
    }
  }
  return d[m - 1][n - 1];
}
int nibun() {
  int R, L;
  R = L = 0;
  if (n >= m) {
    return *max_element(hon, hon + n);
  }
  for (int i = 0; i < m; i++) {
    L += hon[i];
  }
  while (1) {
    if (R == L) {
      return R;
    }
    double H = (double)(R + L) / 2;

    int dan = 0;
    int at = 0;
    for (int i = 0; i < m; i++) {
      if (at + hon[i] <= H) {
        at += hon[i];
      } else {
        at = 0;
        dan++;
        at += hon[i];
      }
    }

    if (dan < n) {
      L = H;
    } else {
      R = H + 0.5;
    }
  }
}

int main() {

  // 3 9 5 3 8 2 1 6 9 7 4
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < m; i++) {
      scanf("%d", &hon[i]);
    }

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

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main() {
  int R, L;
  int n, m;
  int hon[30];
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }
    R = L = 0;
    for (int i = 0; i < m; i++) {
      scanf("%d", &hon[i]);
      L += hon[i];
    }
    if (n >= m) {
      printf("%d\\n", *max_element(hon, hon + n));
      continue;
    }
    // 3 9 5 3 8 2 1 6 9 7 4
    while (1) {
      if (R == L) {
        break;
      }
      double H = (double)(R + L) / 2;

      int dan = 0;
      int at = 0;
      for (int i = 0; i < m; i++) {
        if (at + hon[i] <= H) {
          at += hon[i];
        } else {
          at = 0;
          dan++;
          at += hon[i];
        }
      }

      if (dan < n) {
        L = H;
      } else {
        R = H + 0.5;
      }

      printf("%d %d\\n", R, L);
    }
  }
}
Problem p00181 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

int n, m;
int DP[100][20];
int book[100];

int solve() {
  for (int i = 1; i < n; i++) {
    for (int j = 1; j < m; j++) {
      if (DP[i - 1][j - 1] <= book[i])
        DP[i][j] = book[i];
      else {
        int tmp = book[i];
        DP[i][j] = DP[i - 1][j - 1];
        for (int k = 1; k < i; k++) {
          tmp += book[i - k];
          if (DP[i - k - 1][j - 1] <= tmp) {
            if (tmp < DP[i][j])
              DP[i][j] = tmp;
            break;
          } else {
            if (DP[i - k - 1][j - 1] < DP[i][j])
              DP[i][j] = DP[i - k - 1][j - 1];
          }
        }
      }
    }
  }
  return DP[n - 1][m - 1];
}

int main() {
  while (true) {
    std::cin >> m >> n;
    if (!m && !n)
      return 0;
    for (int i = 0; i < n; i++) {
      std::cin >> book[i];
      if (i > 0) {
        DP[i][0] = book[i];
        DP[i][0] += DP[i - 1][0];
      } else {
        for (int k = 0; k < m; k++)
          DP[i][k] = book[i];
      }
    }

    std::cout << solve() << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int m, n, *b, ans;

int max(int *l, int size) {
  int tmp = l[0];
  for (int i = 1; i < size; i++)
    if (tmp < l[i])
      tmp = l[i];
  return tmp;
}

int min(int *l, int size) {
  int tmp = l[0];
  for (int i = 0; i < size; i++)
    if (tmp > l[i])
      tmp = l[i];
  return tmp;
}

bool bbb() {
  int j = 0;
  int a[m];
  for (int i = 0; i < m; i++) {
    a[i] = 0;
    for (; j < n; j++) {
      a[i] += b[j];
      if (a[i] > ans) {
        a[i] -= b[j];
        break;
      }
    }
  }
  if (j == n)
    return true;
  else
    return false;
}

void solve() {
  int plus = min(b, n);
  if (plus == 1)
    plus = 10;
  while (true) {
    if (bbb())
      return;
    ans += plus;
  }
}

int main() {
  while (true) {
    cin >> m >> n;
    if (!m && !n)
      return 0;
    b = new int[n];
    for (int i = 0; i < n; i++)
      cin >> b[i];
    ans = max(b, n);
    solve();
    cout << ans << endl;
    delete[] b;
  }
}
Problem p00181 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

typedef unsigned long long ull;

int m, n;
ull w[100];

bool b(ull W) {
  int need = 1;
  ull s = 0;
  for (int i = 0; i < n; i++) {
    if (w[i] > W) {
      return false;
    }

    if (s + w[i] > W) {
      need++;
      s = 0;
    }
    s += w[i];
  }
  return need <= m;
}

int main() {
  while (1) {
    scanf("%d", &m);
    scanf("%d", &n);
    if ((m | n) == 0)
      break;

    for (int i = 0; i < n; i++) {
      scanf("%llu", &w[i]);
    }

    ull l = 1;
    ull r = 1500000;
    while (r - l > 1) {
      ull mid = (r + l) / 2;
      if (b(mid))
        r = mid;
      else
        l = mid;
    }

    printf("%llu\\n", b(l) ? l : r);
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

typedef unsigned long long ull;

int m, n;
double avg;
ull books[100];
ull min_w = 150001;

void solve(ull max_w, int i, int depth) {
  if (depth <= m && i == n) {
    min_w = (max_w < min_w) ? max_w : min_w;
    return;
  }

  ull w = 0;
  for (int k = i; k < n; k++) {
    w += books[k];
    ull new_max_w = (max_w < w) ? w : max_w;
    solve(new_max_w, k + 1, depth + 1);
  }
}

int main() {
  while (1) {
    scanf("%d", &m);
    scanf("%d", &n);
    if (m == 0 && n == 0)
      break;

    for (int i = 0; i < n; i++) {
      scanf("%llu", &books[i]);
    }
    avg /= (float)n;

    min_w = 1500001;
    solve(0, 0, 0);
    printf("%llu\\n", min_w);
  }
}
Problem p00181 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <numeric>

#include <string.h>

#define INT_INF 2000000

int m, n;

int a[100 + 1];

int memo[20 + 1][100 + 1];

int search(int i, int j) {
  if (memo[i][j] >= 0)
    return memo[i][j];

  if (i == 1)
    return memo[i][j] = std::accumulate(a + j, a + n, 0);

  int am = INT_INF;

  for (int k = j, s = 0; k < n; s += a[k], k++)
    am = std::min(std::max(search(i - 1, k), s), am);

  return memo[i][j] = am;
}

int main(int argc, char **argv) {
  while (1) {
    std::cin >> m >> n;

    if (m == 0)
      break;

    for (int i = 0; i < n; i++)
      std::cin >> a[i];

    memset(memo, -1, sizeof(memo));

    std::cout << search(m, 0) << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>

#include <string.h>

#define INT_INF 2000000

int m, n;

int a[100 + 1];

int memo[20 + 1][100 + 1];

int search(int i, int j) {
  if (memo[i][j] >= 0)
    return memo[i][j];

  if (i == 1)
    return memo[i][j] = std::accumulate(a + j, a + n, 0);

  int am = INT_INF;

  for (int s = 0; j < n; s += a[j], j++)
    am = std::min(std::max(search(i - 1, j), s), am);

  return memo[i][j] = am;
}

int main(int argc, char **argv) {
  while (1) {
    std::cin >> m >> n;

    if (m == 0)
      break;

    for (int i = 0; i < n; i++)
      std::cin >> a[i];

    memset(memo, -1, sizeof(memo));

    std::cout << search(m, 0) << std::endl;
  }
}
Problem p00181 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <iostream>

using namespace std;

int jagde(int *book, int ix, int n, int m) {
  int sum;
  int j;

  sum = 0;
  j = 0;
  for (int i = 0; i < n; i++) {
    sum += book[i];
    if (sum > ix) {
      j++;
      i--;
      sum = 0;
    }
  }
  if (j >= m)
    return -1;

  ix--;

  sum = 0;
  j = 0;
  for (int i = 0; i < n; i++) {
    sum += book[i];
    if (book[i] > ix)
      return 0;
    if (sum > ix) {
      j++;
      i--;
      sum = 0;
    }
  }
  if (j >= m)
    return 0;
  return 1;
}

int answer(int m, int n, int *book) {
  int low, high;
  int sum[101];

  sum[0] = book[0];
  low = book[0];
  for (int i = 1; i < n; i++) {
    if (low < book[i])
      low = book[i];
    sum[i] = sum[i - 1] + book[i];
  }

  high = sum[n - 1];

  while (true) {
    int ix = (low + high) / 2;
    int answer = jagde(book, ix, n, m);

    if (answer == -1) {
      low = ix + 1;
    } else if (answer == 0) {
      return ix;
    } else if (answer == 1) {
      high = ix - 1;
    }
  }
}

int main(int argc, char const *argv[]) {
  int m, n;
  int book[101];

  while (cin >> m >> n && m != 0) {
    for (int i = 0; i < n; i++)
      cin >> book[i];

    cout << answer(m, n, book) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>

using namespace std;

int jagde(int *book, int ix, int n, int m) {
  int sum;
  int j;

  sum = 0;
  j = 0;
  for (int i = 0; i < n; i++) {
    sum += book[i];
    if (sum > ix) {
      j++;
      i--;
      sum = 0;
    }
  }
  if (j >= m)
    return -1;

  ix--;

  sum = 0;
  j = 0;
  for (int i = 0; i < n; i++) {
    sum += book[i];
    if (book[i] > ix)
      return 0;
    if (sum > ix) {
      j++;
      i--;
      sum = 0;
    }
  }
  if (j >= m)
    return 0;
  return 1;
}

int answer(int m, int n, int *book) {
  int low, high;
  int sum[101];

  sum[0] = book[0];
  low = book[0];
  for (int i = 1; i < n; i++) {
    if (low < book[i])
      low = book[i];
    sum[i] = sum[i - 1] + book[i];
  }

  high = sum[n - 1];

  while (true) {
    int ix = (low + high) / 2;
    int answer = jagde(book, ix, n, m);

    if (answer == -1) {
      low = ix;
    } else if (answer == 0) {
      return ix;
    } else if (answer == 1) {
      high = ix;
    }
  }
  return -1;
}

int main(int argc, char const *argv[]) {
  int m, n;
  int book[101];

  while (cin >> m >> n && m != 0) {
    for (int i = 0; i < n; i++)
      cin >> book[i];

    cout << answer(m, n, book) << endl;
  }
  return 0;
}
Problem p00181 (C++) — buggy submission, verdict: Time Limit Exceeded

// 2分探索したよ!!
#include <algorithm>
#include <iostream>
using namespace std;
#define INF (1 << 30)
int n, m, book[100];
bool basic(double x) {
  int index = 0, dan = 0, c = 0;
  while (index < n && dan < m) {
    if (c + book[index] <= x)
      c += book[index++];
    else
      c = 0, dan++;
  }
  return index == n && dan < m;
}
int main() {
  while (cin >> m >> n, m) {
    for (int i = 0; i < n; i++)
      cin >> book[i];
    int light = 0, left = INF;
    for (int i = 0; i < 100; i++) {
      int java = (light + left) / 2;
      basic(java) ? left = java : light = java;
    }
    cout << max(light, left) << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF (1 << 30)
int book[100], dp[20][100], m, n;
int solve(int index, int dan, int maxsize, int nowsize) {
  if (index == n)
    return maxsize;
  if (dp[dan][index] < maxsize)
    return dp[dan][index];
  int res = INF;
  res = min(res, solve(index + 1, dan, max(maxsize, nowsize + book[index]),
                       nowsize + book[index]));
  if (dan + 1 != m)
    res = min(
        res, solve(index + 1, dan + 1, max(maxsize, nowsize + book[index]), 0));
  return dp[dan][index] = res;
}
int main() {
  while (cin >> m >> n, m) {
    fill_n(dp[0], 20 * 100, INF);
    rep(i, n) cin >> book[i];
    cout << solve(0, 0, 0, 0) << endl;
  }
}