alexjercan/bugnet huggingface dataset:
Problem p00181 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int M, N;
int W[100];

bool C(int x) {
  int cnt = 0;
  int cur = 0;
  while (cur < N) {
    int next = cur;
    int sum = 0;
    while (next < N && sum + W[next] <= x)
      sum += W[next++];
    cur = next;
    cnt++;
  }
  return cnt <= M;
}

signed main() {
  while (cin >> M >> N, M || N) {
    rep(i, N) cin >> W[i];
    int lb = *max_element(W, W + N) - 1, ub = 1500000;
    while (ub - lb > 1) {
      int mid = (ub + lb) / 2;
      if (C(mid))
        ub = mid;
      else
        lb = mid;
    }
    cout << ub << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int M, N;
int W[100];

bool C(int x) {
  int cnt = 0;
  int cur = 0;
  while (cur < N) {
    int next = cur;
    int sum = 0;
    while (next < N && sum + W[next] <= x)
      sum += W[next++];
    cur = next;
    cnt++;
  }
  return cnt <= M;
}

signed main() {
  while (cin >> M >> N, M || N) {
    rep(i, N) cin >> W[i];
    int lb = *min_element(W, W + N) - 1, ub = 1500000;
    while (ub - lb > 1) {
      int mid = (ub + lb) / 2;
      if (C(mid))
        ub = mid;
      else
        lb = mid;
    }
    cout << ub << endl;
  }
  return 0;
}
Problem p00181 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int M, N;
int t[128];
int dp[32][128];

int dfs(int m, int n) {
  //  printf("%d %d\\n", m, n);
  if (m == M && n == N)
    return 0;
  if (m == M)
    return INF;
  if (n == N)
    return 0;
  if (dp[m][n] >= 0)
    return dp[m][n];

  int res = INF;
  for (int i = n + 1; i < N + 1; i++) {
    res = min(res, max(dfs(m + 1, i), t[i] - t[n]));
  }

  return dp[m][n] = res;
}

int main() {
  while (scanf("%d%d", &M, &N) && M + N) {
    memset(dp, -1, sizeof(dp));
    t[0] = 0;
    rep(i, N) scanf("%d", t + i + 1);
    rep(i, N) t[i + 1] += t[i];

    printf("%d\\n", dfs(0, 0));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int m, n, book[128], shelf[32];

int dfs(int copy, int step) {
  //  fprintf(stderr, "%d %d\\n", copy, step);
  if (step == m)
    return INF;
  if (copy == n) {
    int res = 0;
    rep(i, m) res = max(res, shelf[i]);
    return res;
  }

  int res = INF;
  shelf[step] += book[copy];
  res = min(res, dfs(copy + 1, step));
  shelf[step] -= book[copy];

  shelf[step + 1] = book[copy];
  res = min(res, dfs(copy + 1, step + 1));
  shelf[step + 1] -= book[copy];
  return res;
}

int main() {
  while (scanf("%d%d", &m, &n) && m + n) {
    rep(i, n) { scanf("%d", book + i); }
    memset(shelf, 0, sizeof(shelf));
    printf("%d\\n", dfs(0, 0));
  }
  return 0;
}
Problem p00181 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

using namespace std;

int n, m;
int hon[101];
int nibun() {
  int R, L;
  R = L = 0;
  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 (hon[i] > H) {
        dan = 10000;
        break;
      }
      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() {
  int cont = 0;
  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", nibun());
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

int n, m;
int hon[101];
int nibun() {
  int R, L;
  R = L = 0;
  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 (hon[i] > H) {
        dan = 10000;
        break;
      }
      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() {
  int cont = 0;
  while (1) {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; i++) {
      scanf("%d", &hon[i]);
    }
    printf("%d\\n", nibun());
  }
}
Problem p00181 (C++) — buggy submission, verdict: Runtime Error

// suhan lee, saitama university;
#include <iostream>
using namespace std;
int putbook(int *list, int bookwi, int stair, int width) {

  int dara[100];
  int j = 0;
  for (int i = 0; i < 100; i++)
    dara[i] = 0;
  for (int i = 0; i < bookwi; i++)
    if (dara[j] + list[i] <= width)
      dara[j] += list[i];
    else if (list[i] > width) {
      // cout <<"too big book"<<endl;
      return 0;
    } else
      dara[++j] += list[i];
  //  cout <<"hondara";
  for (int i = 0; i < stair; i++)
    // cout <<dara[i]<<endl;
    // cout <<"kugiri";
    if (j < stair)
      return 1; // no problem
    else
      return 0;
}

int main() {

  int list[100];

  int bookwi, stair, temp;
  int max, min;
  while (1) {
    cin >> stair >> bookwi;
    if (bookwi == 0 && stair == 0)
      return 0;
    for (int i = 0; i < bookwi; i++)
      cin >> list[i];
    max = 10000000;
    min = 1;
    // cout << putbook( list,bookwi,stair,1800 )<<endl;
    while (1) {
      if (min == max)
        break;
      if (putbook(list, bookwi, stair, (max + min) / 2))
        max = (max + min) / 2;
      else
        min = (max + min) / 2 + 1;
      //   cout <<endl<<max<<" max&min   "<<min<<endl;
    }
    cout << max << endl;
  }
}

Error: 0

Fixed submission:

// suhan lee, saitama university;
#include <iostream>
using namespace std;
int putbook(int *list, int bookwi, int stair, int width) {

  int dara[20];
  int j = 0;
  for (int i = 0; i < 20; i++)
    dara[i] = 0;
  for (int i = 0; i < bookwi; i++)
    if (dara[j] + list[i] <= width)
      dara[j] += list[i];
    else if (list[i] > width) {
      // cout <<"too big book"<<endl;
      return 0;
    } else
      dara[++j] += list[i];
  //  cout <<"hondara";
  for (int i = 0; i < stair; i++)
    // cout <<dara[i]<<endl;
    // cout <<"kugiri";
    if (j < stair)
      return 1; // no problem
    else
      return 0;
}

int main() {

  int list[100];

  int bookwi, stair, temp;
  int max, min;
  while (1) {
    cin >> stair >> bookwi;
    if (bookwi == 0 && stair == 0)
      return 0;
    for (int i = 0; i < bookwi; i++)
      cin >> list[i];
    max = 1000000;
    min = 1;
    // cout << putbook( list,bookwi,stair,1800 )<<endl;
    while (1) {
      if (min == max)
        break;
      if (putbook(list, bookwi, stair, (max + min) / 2))
        max = (max + min) / 2;
      else
        min = (max + min) / 2 + 1;
      //   cout <<endl<<max<<" max&min   "<<min<<endl;
    }
    cout << max << endl;
  }
}
Problem p00181 (C++) — buggy submission, verdict: Runtime Error

// 2?????¢?´¢????????£????§£????????????????????????????????§??????
#include <cstdio>
#include <iostream>

using namespace std;

int gcd(int m, int n) {
  if ((0 == m) || (0 == n))
    return 1;
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}

int main(void) {

  while (1) {

    int floor, n, book[1000] = {0};
    cin >> floor >> n;

    if (floor == 0)
      return 0;

    for (int i = 0; i < n; i++) {
      cin >> book[i];
    }

    //?????§??¬?´???°????±???????
    int m = book[0];
    int w_avr = book[0];
    for (int i = 1; i < n; i++) {
      m = gcd(m, book[i]);
      w_avr += book[i];
    }
    //?????????????????§?????¬??????????????????????????°??§?????£?????°??????????????¬????????§?????????
    int ans = w_avr / floor;

    for (int i = 0; i < n; i++) {
      if (ans < book[i]) {
        ans = book[i];
      }
    }
    while (ans % m != 0) {
      ans = ans - 1;
    }
    while (1) {
      int count = floor;
      int length = ans;
      int i = 0;
      while (count != 0 && i < n) {
        if (length >= book[i]) {
          length -= book[i];
          i++;
        } else {
          length = ans;
          count = count - 1;
        }
      }
      if (count != 0) {
        break;
      }
      ans += m;
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

// 2?????¢?´¢????????£????§£????????????????????????????????§??????
#include <cstdio>
#include <iostream>

using namespace std;

int gcd(int m, int n) {
  if ((0 == m) || (0 == n))
    return 0;
  while (m != n) {
    if (m > n)
      m = m - n;
    else
      n = n - m;
  }
  return m;
}

int main(void) {

  while (1) {

    int floor, n, book[101] = {0};
    cin >> floor >> n;

    if (floor == 0)
      return 0;

    for (int i = 0; i < n; i++) {
      cin >> book[i];
    }

    //?????§??¬?´???°????±???????
    int m = book[0];
    int w_avr = book[0];
    for (int i = 1; i < n; i++) {
      m = gcd(m, book[i]);
      w_avr += book[i];
    }
    //?????????????????§?????¬??????????????????????????°??§?????£?????°??????????????¬????????§?????????
    int ans = w_avr / floor;

    for (int i = 0; i < n; i++) {
      if (ans < book[i]) {
        ans = book[i];
      }
    }
    while (ans % m != 0) {
      ans++;
    }
    while (1) {
      int count = floor;
      int length = ans;
      int i = 0;
      while (count != 0 && i < n) {
        if (length >= book[i]) {
          length -= book[i];
          i++;
        } else {
          length = ans;
          count = count - 1;
        }
      }
      if (count != 0) {
        break;
      }
      ans += m;
    }
    cout << ans << endl;
  }
}
Problem p00181 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 30;
const double EPS = 1e-9;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)

int sum[101];

int main() {
  sum[0] = 0;
  int n, m;
  int l;
  while (cin >> m >> n && (n || m)) {
    rep(i, n) {
      cin >> l;
      sum[i + 1] = sum[i] + l;
    }
    int ub = INF;
    int lb = 0;
    while (ub - lb > 1) {
      int mb = (ub + lb) / 2;
      int pos = 0;
      rep(i, m) {
        rep2(j, pos + 1, n + 1) {
          if (sum[j] - sum[pos] > mb) {
            pos = j - 1;
            break;
          }
          if (j == n)
            pos = n;
        }
      }
      if (pos == n)
        ub = mb;
      else
        lb = mb;
    }
    cout << ub << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

bool comp(int i, int j) { return i > j; }

int main() {
  int n, m;
  while (cin >> m >> n) {
    const int mmax = 30;
    int books[mmax];
    if (m == 0 && n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> books[i];
    }
    while (n > m) {
      sort(books, books + n, comp);
      books[n - 2] += books[n - 1];
      n--;
    }
    int size = -1;
    for (int i = 0; i < n; i++) {
      size = max(size, books[i]);
    }
    cout << size << endl;
  }
  return 0;
}
Problem p00181 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int n, m;
vector<int> cost;
vector<vector<int>> dp;
void init() {
  cost.clear();
  dp.clear();
}

bool input() {
  cin >> m >> n;
  if (n == 0 && m == 0)
    return 0;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    cost.push_back(tmp);
  }
  return 1;
}

int rec(int i, int x) {
  if (i == n) {
    return 0;
  }
  if (x == m) {
    return INT_MAX - 1500010;
  }
  if (dp[i][x] != 0) {
    return dp[i][x];
  }
  dp[i][x] = INT_MAX - 1500010;
  int c = cost[i];
  for (int j = i + 1; j < cost.size() + 1; j++) {
    dp[i][x] = min(dp[i][x], max(rec(j, x + 1), c));
    c += cost[j];
  }
  return dp[i][x];
}

int solve() {
  dp.resize(n);
  for (int i = 0; i < n; i++) {
    dp[i].resize(m);
  }
  return rec(0, 0);
}

int main() {
  while (init(), input()) {
    cout << solve() << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int n, m;
vector<int> cost;
void init() { cost.clear(); }

bool input() {
  cin >> m >> n;
  if (n == 0 && m == 0)
    return 0;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    cost.push_back(tmp);
  }
  return 1;
}

int rec(int i, int x) {
  int res = INT_MAX - 1500010;
  if (i == n) {
    return 0;
  }
  if (x == m) {
    return res;
  }
  int c = cost[i];
  for (int j = i + 1; j < cost.size() + 1; j++) {
    res = min(res, max(rec(j, x + 1), c));
    c += cost[j];
  }
  return res;
}

int solve() {
  int res = INT_MAX - 1500010;
  res = min(res, rec(0, 0));
  return res;
}

int main() {
  while (init(), input()) {
    cout << solve() << endl;
  }
}
Problem p00181 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

long long int len[1000];

int n, m;

bool check(long long int w) {
  int count = 0;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (len[i] > w) {
      return false;
    }
  }
  for (int i = 0; i < n; i++) {
    sum += len[i];
    if (sum > w) {
      sum = len[i];
      count++;
    }
  }
  count += 1;
  return count <= m;
}

long long int bin_search(long long int wmin, long long int wmax) {
  if (wmax - wmin <= 1) {
    if (check(wmin)) {
      return wmin;
    } else {
      return wmax;
    }
  }
  long long int wmid = (wmin + wmax) / 2;
  if (check(wmid)) {
    wmax = wmid;
  } else {
    wmin = wmid;
  }
  bin_search(wmin, wmax);
}

int main() {

  while (true) {
    cin >> m >> n;
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> len[i];
    }

    cout << bin_search(0, 2000000) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

long long int len[1000];

int n, m;

bool check(long long int w) {
  int count = 0;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (len[i] > w) {
      return false;
    }
  }
  for (int i = 0; i < n; i++) {
    sum += len[i];
    if (sum > w) {
      sum = len[i];
      count++;
    }
  }
  return count <= m;
}

long long int bin_search(long long int wmin, long long int wmax) {
  if (wmax - wmin <= 1) {
    if (check(wmin)) {
      return wmin;
    } else {
      return wmax;
    }
  }
  long long int wmid = (wmin + wmax) / 2;
  if (check(wmid)) {
    wmax = wmid;
  } else {
    wmin = wmid;
  }
  bin_search(wmin, wmax);
}

int main() {

  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> len[i];
    }

    cout << bin_search(0, 2000000) << endl;
  }

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

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

int m, n;
int w[100];

std::vector<std::vector<int>> memo(21, std::vector<int>(101));

const int MAX_WIDTH = 1500000;

int dfs(int i, int j) {
  if (memo[i][j] != -1) {
    return memo[i][j];
  }

  if (i >= m) {
    return memo[i][j] = ((j < n) ? -1 : 0);
  }
  if (j >= n) {
    return memo[i][j] = 0;
  }

  int ans = INT_MAX;

  int width = 0;
  int res;
  for (int k = j; k < n; ++k) {
    width += w[k];

    res = dfs(i + 1, k + 1);
    if (res >= 0) {
      ans = std::min(ans, std::max(width, res));
    }
  }

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

int main(void) {
  while (std::cin >> m >> n) {
    if (m == 0 && n == 0)
      break;
    for (int i = 0; i < n; ++i) {
      std::cin >> w[i];
    }

    for (int i = 0; i <= m; ++i) {
      for (int j = 0; j <= n; ++j) {
        memo[i][j] = -1;
      }
    }
    std::cout << dfs(0, 0) << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>

int m, n;
int w[100];

const int MAX_WIDTH = 1500000;

int dfs(int i, int j) {
  if (i >= m) {
    return (j < n) ? -1 : 0;
  }
  if (j >= n) {
    return 0;
  }

  int ans = INT_MAX;

  int width = 0;
  int res;
  for (int k = j; k < n; ++k) {
    width += w[k];

    res = dfs(i + 1, k + 1);
    if (res >= 0) {
      ans = std::min(ans, std::max(width, res));
    }
  }
  return ans;
}

int main(void) {
  while (std::cin >> m >> n) {
    if (m == 0 && n == 0)
      break;
    for (int i = 0; i < n; ++i) {
      std::cin >> w[i];
    }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

#define INF (1 << 28)
#define MAX_N 100
#define MAX_M (20 + 1)

int m, n;
int book[MAX_N];
int length[MAX_N];

int len(int from, int to) { // [from, to)
  return length[to] - length[from];
}

int dp[MAX_N][MAX_M];
int solve(int index, int rem) {
  if (rem == 1)
    return len(index, n);
  if (index == n)
    return 0;
  if (dp[index][rem] >= 0)
    return dp[index][rem];
  int min_len = INF;
  for (int i = index + 1; i <= n; i++) {
    min_len = min(min_len, max(solve(i, rem - 1), len(index, i)));
  }
  return dp[index][rem] = min_len;
}

void init() {
  for (int i = 0; i < n; i++)
    scanf("%d", &book[i]);
  length[0] = 0;
  for (int i = 1; i <= n; i++)
    length[i] = length[i - 1] + book[i - 1];
  memset(dp, -1, sizeof(dp));
}

int main() {
  while (scanf("%d %d", &m, &n), m || n) {
    init();
    printf("%d\\n", solve(0, m));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;

#define INF (1 << 28)
#define MAX_N 100

int m, n;
int book[MAX_N];

int len(int from, int to) { // [from, to)
  int l = 0;
  for (int i = from; i < to; i++) {
    l += book[i];
  }
  return l;
}

int solve(int index, int rem, int max_len) {
  if (rem == 1)
    return max(max_len, len(index, n));
  if (index == n)
    return max_len;
  int min_len = INF;
  for (int i = index + 1; i <= n; i++) {
    min_len = min(min_len, solve(i, rem - 1, max(max_len, len(index, i))));
  }
  return min_len;
}

int main() {
  while (scanf("%d %d", &m, &n), m || n) {
    for (int i = 0; i < n; i++)
      scanf("%d", &book[i]);
    printf("%d\\n", solve(0, m, 0));
  }
  return 0;
}
Problem p00181 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring> // require memset
#include <fstream> // require freopen
#include <iostream>

using namespace std;

const int MaxW = 1500000;
int m,
    n; // {IÌi 1  m  20, {Ìª 1  n  100
int w[100];

bool calc_m(int curr_w) {
  int l[20];
  memset(l, 0, sizeof(l));

  int j = 0;
  for (int i = 0; i < n; i++) {
    while (j < m && l[j] + w[i] > curr_w)
      j++;
    if (j == m)
      return false;
    l[j] += w[i];
  } // end for

  return true;
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.persistence", "r", stdin );

  for (;;) { // {IÌ ans  1500000
    scanf("%d%d", &m, &n);
    if (!m)
      break;
    memset(w, 0, sizeof(w));

    for (int i = 0; i < n; i++) {
      scanf("%d", w + i);
    } // end for
    int l = w[0];
    int r = MaxW;
    int mid = 0;
    int res = MaxW;
    while (l <= r) {
      mid = (l + r) / 2;
      if (calc_m(mid)) {
        res = min(res, mid);
        r = mid - 1;
      } else {
        l = mid + 1;
      } // end if
    }   // end while
    cout << res << endl;
  } // end loop

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring> // require memset
#include <fstream> // require freopen
#include <iostream>

using namespace std;

int m,
    n; // {IÌi 1  m  20, {Ìª 1  n  100
int w[100];

bool calc_m(int curr_w) {
  int cm = 0;
  int j = 0;
  while (j < n) {
    int cw = 0;
    while (cw + w[j] <= curr_w && j < n) {
      cw += w[j];
      j++;
    } // end while
    cm++;
    if (cm == m)
      false;
  } // end if

  return true;
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.persistence", "r", stdin );

  const int MaxW = 1500000;

  for (;;) { // {IÌ ans  1500000
    scanf("%d%d", &m, &n);
    if (!m)
      break;
    memset(w, 0, sizeof(w));

    for (int i = 0; i < n; i++) {
      scanf("%d", w + i);
    } // end for
    int l = w[0];
    int r = MaxW;
    int mid = 0;
    while (l <= r) {
      mid = (l + r) / 2;
      if (calc_m(mid)) {
        r = mid - 1;
      } else {
        l = mid + 1;
      } // end if
    }   // end while
    cout << mid << endl;
  } // end loop

  return 0;
}