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

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

#define REP(i, n) for (int i = 0; i < (n); i++)

const int MAX = 100000;

int function(int P, int k, int w[], int m) {
  int j = 0;
  int c = 0;
  int nk = 0;
  int np = 0;
  while (nk < k) {
    for (int i = j; i < m; i++, c++) {
      np += w[i];
      if (np > P) {
        np = 0;
        j = i;
        break;
      }
      if (i == m - 1)
        return m;
    }
    nk++;
  }
  return c;
}

int binarySearch(int key, int k, int w[], int m) {
  int left = 0;
  int right = MAX * 10000;
  int mid;
  while (left + 1 < right) {
    mid = (left + right) / 2;
    if (function(mid, k, w, m) < key)
      left = mid;
    else
      right = mid;
  }
  return right;
}

int main() {
  int n, k;
  cin >> n >> k;
  int w[n];
  REP(i, n)
  cin >> w[i];
  cout << binarySearch(n, k, w, n) << endl;
}

Error: TLE

Fixed submission:

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

#define REP(i, n) for (int i = 0; i < (n); i++)

const int MAX = 100000;

int function(int P, int k, int w[], int m) {
  int j = 0;
  int c = 0;
  int nk = 0;
  int np = 0;
  while (nk < k) {
    for (int i = j; i < m; i++, c++) {
      np += w[i];
      if (np > P) {
        np = 0;
        j = i;
        break;
      }
    }
    nk++;
  }
  return c;
}

int binarySearch(int key, int k, int w[], int m) {
  int left = 0;
  int right = MAX * 10000;
  int mid;
  while (left + 1 < right) {
    mid = (left + right) / 2;
    if (function(mid, k, w, m) < key)
      left = mid;
    else
      right = mid;
  }
  return right;
}

int main() {
  int n, k;
  cin >> n >> k;
  int w[n];
  REP(i, n)
  cin >> w[i];
  cout << binarySearch(n, k, w, n) << endl;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define MAX 100000
typedef long long llong;
int n, k;
llong T[MAX];

int check(llong P) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    llong s = 0;
    while (s + T[i] <= P) {
      s = s + T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

llong solve() {
  llong left = 0;
  llong right = 10000 * 100000;
  llong mid;
  while (right - left > 1) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> T[i];
  }
  llong ans = solve();
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 100000
typedef long long llong;
int n, k;
llong T[MAX];

int check(llong P) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    llong s = 0;
    while (s + T[i] < P) {
      s = s + T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

llong solve() {
  llong left = 0;
  llong right = 10000 * 10000;
  llong mid;
  while (left < right) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> T[i];
  }
  llong ans = solve();
  cout << ans << endl;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int w[100000];
int n, k;

int count(long long P) {
  int count = 0, i;
  long long Pn;
  for (i = k; i > 0; i--) {
    Pn = P;
    while (Pn - w[count] >= 0) {
      Pn -= w[count];
      count++;
      if (count == n)
        return count;
    }
  }
  return count;
}

int main() {
  int i;
  long long max = 0, right, left, mid;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> w[i];
    if (max < w[i])
      max = w[i];
  }
  max *= n / k + 1;
  left = 0;
  right = max + 1;
  while (left < right) {
    mid = (left + right) / 2;
    if (count(mid) == n)
      right = mid;
    else {
      if (count(mid + 1) == n) {
        mid++;
        break;
      } else
        left = mid + 1;
    }
  }
  cout << mid << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int w[100000];
int n, k;

int count(long long P) {
  int count = 0, i;
  long long Pn;
  for (i = k; i > 0; i--) {
    Pn = P;
    while (Pn - w[count] >= 0) {
      Pn -= w[count];
      count++;
      if (count == n)
        break;
    }
  }
  return count;
}

int main() {
  int i;
  long long max = 0, right, left, mid;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> w[i];
    if (max < w[i])
      max = w[i];
  }
  max *= n / k + 1;
  left = 0;
  right = max + 1;
  while (left < right) {
    mid = (left + right) / 2;
    if (count(mid) == n)
      right = mid;
    else {
      if (count(mid + 1) == n) {
        mid++;
        break;
      } else
        left = mid + 1;
    }
  }
  cout << mid << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
????°??????????????£? = ??????????????????
while(?????? == false){
    ????°??????????????£?++;
}

?????????:1 2 3 4 5 6
???  ???:X X X O O O
X???1???????????????O???????°??????????

????°????O??¨?????§???X?????????????????????
??????????????????????????°??????
*/
//--- --- --- debug function --- --- ---
// #define DEBUG // switch this macro
#ifdef DEBUG
#include <cstdio>
typedef int (*printf_t)(const char *format, ...);
printf_t printfd = std::printf;
#else
int printfd(const char *format, ...) { return 0; }
#endif
//--- --- --- debug function --- --- ---
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>

bool Check(const std::vector<int> Wi, int k, int P) {
  if (*std::max_element(Wi.begin(), Wi.end()) > P)
    return false;
  int p = 0;
  --k;
  for (int i = 0; i < Wi.size(); ++i) {
    if (P - p >= Wi[i]) {
      p += Wi[i];
    } else {
      p = Wi[i];
      --k;
    }
  }
  return k >= 0;
}
int main() {
  int n, k;
  std::cin >> n >> k;
  std::vector<int> Wi(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> Wi[i];
  }

  int MaxBad = std::accumulate(Wi.begin(), Wi.end(), 0) / k - 1;
  int MinGood =
      std::ceil(((double)n / k)) * *std::max_element(Wi.begin(), Wi.end());
  int P = (MaxBad + MinGood) / 2;
  printfd("Max:%d, Min:%d, P:%d\\n", MaxBad, MinGood, P);
  while (MaxBad + 1 != MinGood) {
    if (Check(Wi, k, P)) {
      MinGood = P;
    } else {
      MaxBad = P;
    }
    P = (MaxBad + MinGood) / 2;
    printfd("Max:%d, Min:%d, P:%d\\n", MaxBad, MinGood, P);
  }
  std::cout << MinGood << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

/*
????°??????????????£? = ??????????????????
while(?????? == false){
    ????°??????????????£?++;
}
*/
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

bool Check(const std::vector<int> Wi, int k, int P) {
  int p = 0;
  --k;
  for (int i = 0; i < Wi.size(); ++i) {
    if (P - p >= Wi[i]) {
      p += Wi[i];
    } else {
      p = Wi[i];
      --k;
    }
  }
  return k >= 0;
}
int main() {
  int n, k;
  std::cin >> n >> k;
  std::vector<int> Wi(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> Wi[i];
  }
  int P = *std::max_element(Wi.begin(), Wi.end());
  while (Check(Wi, k, P) == false) {
    ++P;
  }
  std::cout << P << std::endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int n, k;
long long int T[100000];
long long sum;
long long l;

int check(long long p) {
  int m = 0;
  for (int i = 0; i < k; i++) {
    long long s = 0;
    while (s + T[m] <= p) {
      s += T[m];
      m++;
      if (m >= n) {
        return n;
      }
    }
  }
  return m;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> T[i];
    sum += T[i];
  }
  l = sum / k;
  while (check(l) != n) {
    l++;
  }
  cout << l << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int n, k, l;
int T[100000];

int check(long long p) {
  int m = 0;
  for (int i = 0; i < k; i++) {
    long long s = 0;
    while (s + T[m] <= p) {
      s += T[m];
      m++;
      if (m >= n) {
        return n;
      }
    }
  }
  return m;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> T[i];
  }
  l = 0;
  while (check(l) != n) {
    l++;
  }
  cout << l << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, k, w, p, tr, c, m, pmax = 0, pmin = 0;
  list<int> wlist;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> w;
    wlist.push_front(w);
    if (pmin < w)
      pmin = w;
    pmax += w;
  }
  (pmin < pmax / k) && (pmin = pmax / k);
  list<int>::iterator itwlist = wlist.begin();
  while (pmin <= pmax) {
    m = (pmin + pmax) / 2;
    tr = 0;
    c = 0;
    p = m;
    itwlist = wlist.begin();
    while (c < n) {
      while (p >= 0) {
        p = p - *itwlist;
        c++;
        itwlist++;
      }
      tr++;
      if (tr > k) {
        pmin = m + 1;
        break;
      }
      itwlist--;
      c--;
      p = m;
    }
    if (tr <= k)
      pmax = m - 1;
  }
  cout << pmin << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, k, w, p, tr, c, m, pmax = 0, pmin = 0;
  list<int> wlist;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> w;
    wlist.push_front(w);
    if (pmin < w)
      pmin = w;
    pmax += w;
  }
  (pmin < pmax / k) && (pmin = pmax / k);
  list<int>::iterator itwlist = wlist.begin();
  while (pmin <= pmax) {
    m = (pmin + pmax) / 2;
    tr = 0;
    c = 0;
    p = m;
    while (c < n) {
      while (p >= 0) {
        advance(itwlist, c);
        p = p - *itwlist;
        c++;
        itwlist = wlist.begin();
      }
      tr++;
      c--;
      p = m;
      if (tr > k) {
        pmin = m + 1;
        break;
      }
    }
    if (tr <= k)
      pmax = m - 1;
  }
  cout << pmin << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;

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

  int P = 0;
  int j = 0;
  int t = 0;
  int l = 0;
  int r = m;
  while (l < r) {
    P = (l + r) / 2;
    while (true) {
      int s = 0;
      while (s + w[j] <= P) {
        s += w[j];
        j++;
        if (j >= n) {
          break;
        }
      }
      t++;
      if (t > k) {
        j = 0;
        t = 0;
        l = P + 1;
        break;
      }
      if (j >= n) {
        j = 0;
        t = 0;
        r = P;
        break;
      }
    }
  }

  cout << l << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;

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

  int P = 1;
  int j = 0;
  int t = 0;
  while (true) {
    int s = 0;
    while (s + w[j] <= P) {
      s += w[j];
      j++;
      if (j >= n) {
        break;
      }
    }
    t++;
    if (t > k) {
      P++;
      j = 0;
      t = 0;
      continue;
    }
    if (j >= n) {
      break;
    }
  }

  cout << P << endl;

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

#include <cstdio>

int main() {
  int i, n, k;
  scanf("%d %d", &n, &k);
  int w[n];
  int p = 0;
  int sum = 0;
  for (i = 0; i < n; i++) {
    scanf("%d", w + i);
    sum += w[i];
  }
  p = sum / k;
  if (sum % k)
    p++;
  int p_, k_;
  for (;; p++) {
    p_ = 0;
    k_ = 0;
    for (i = 0; i < n; i++) {
      if (w[i] > p)
        break;
      else if (p_ + w[i] <= p)
        p_ += w[i];
      else {
        p_ = w[i];
        k_++;
      }
      if (k_ >= k)
        break;
    }
    if (i == n)
      break;
  }
  printf("%d\\n", p);
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>

int main() {
  int i, n, k;
  scanf("%d %d", &n, &k);
  int w[n];
  int p = 0;
  for (i = 0; i < n; i++) {
    scanf("%d", w + i);
    if (w[i] > p)
      p = w[i];
  }
  int p_, k_;
  for (;; p++) {
    p_ = 0;
    k_ = 0;
    for (i = 0; i < n; i++) {
      if (p_ + w[i] <= p)
        p_ += w[i];
      else {
        p_ = w[i];
        k_++;
      }
      if (k_ >= k)
        break;
    }
    if (i == n)
      break;
  }
  printf("%d\\n", p);
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int find(int w[], int kn, int wn) {
  int k[kn], l = 0, j, right = 10000 * 100000, left = 0, mid;
  while (right - left > 1) {
    mid = (right + left) / 2;
    l = 0;
    for (j = 0; j < kn; j++) {
      k[j] = 0;
      while ((k[j] + w[l]) <= mid && l < wn) {
        k[j] += w[l];
        l++;
      }
      if (k[j] == 0) {
        break;
      }
    }
    // cout<<"wn:"<<wn<<" l:"<<l<<" right:"<<right<<" left:"<<left<<"
    // mid:"<<mid<<endl;
    if (l < wn)
      left = mid;
    else
      right = mid;
    // cout<<"wn:"<<wn<<" l:"<<l<<" right:"<<right<<" left:"<<left<<"
    // mid:"<<mid<<endl;
  }
  return right;
}
int main() {
  int wn, kn;
  cin >> wn >> kn;
  int w[wn];
  for (int i = 0; i < wn; i++) {
    scanf("%d", &w[i]);
  }
  cout << find(w, kn, wn) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int find(int w[], int kn, int max, int wn) {
  int k[kn], l = 0, j, i;
  for (i = max;; i++) {
    l = 0;
    for (j = 0; j < kn; j++) {
      k[j] = 0;
      while ((k[j] + w[l]) <= i && l < wn) {
        k[j] += w[l];
        l++;
      }
      // cout<<k[j]<<endl;
    }
    if (j >= kn && l >= wn)
      return i;
  }
  return 0;
}
int main() {
  int wn, kn, max = 0;
  cin >> wn >> kn;
  int w[wn];
  for (int i = 0; i < wn; i++) {
    scanf("%d", &w[i]);
    if (max < w[i])
      max = w[i];
  }
  cout << find(w, kn, max, wn) << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

// ------ main ------ //

int n, k, w[100000];

bool check(long long value) {
  long long v = 0;

  int c = 0;

  for (int i = 0; i < n; i++) {
    if (c >= k) {
      return false;
    }

    if (v + w[i] > value) {
      v = 0;

      c++;
      i--;
    } else {
      v += w[i];
    }
  }

  if (c > n) {
    return false;
  }

  return true;
}

int main() {
  cin >> n >> k;

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

  long long l = 0;
  long long r = (1LL << 60);
  long long m;

  while (r - l > 1) {
    m = (l + r) / 2;

    if (check(m) == true) {
      r = m;
    } else {
      l = m;
    }
  }

  cout << r << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

// ------ main ------ //

int n, k, w[100000];

bool check(long long value) {
  long long v = 0;

  int c = 0;

  for (int i = 0; i < n; i++) {
    if (c >= k) {
      return false;
    }

    if (v + w[i] > value) {
      v = 0;

      c++;
      i--;
    } else {
      v += w[i];
    }
  }

  if (c > n) {
    return false;
  }

  return true;
}

int main() {
  cin >> n >> k;

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

  long long l = 0;
  long long r = (1LL << 60);
  long long m;

  while (r - l > 0) {
    m = (l + r) / 2;

    if (check(m) == true) {
      r = m;
    } else {
      l = m;
    }
  }

  cout << r << endl;

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

#include <iostream>
using namespace std;
#define MAX 100000
typedef long long llong;

int n, k;
llong T[MAX];

int check(llong P) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    llong s = 0;
    while (s + T[i] <= P) {
      s += T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int solve() {
  llong left = 0;
  llong right = 100000 * 10000;
  llong mid;
  while (right - 1 > left) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }

  return right;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> T[i];
  llong ans = solve();
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 100000
typedef long long llong;

int n, k;
llong T[MAX];

int check(llong P) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    llong s = 0;
    while (s + T[i] <= P) {
      s += T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int solve() {
  llong left = 0;
  llong right = 100000 * 10000;
  llong mid;
  while (right > left) {
    mid = (left + right) / 2 + 0.5;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }

  return right;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> T[i];
  llong ans = solve();
  cout << ans << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define MAX 100000
typedef long long llong;

int n, k;
llong T[MAX];

int check(llong P) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    llong s = 0;
    while (s + T[i] <= P) {
      s += T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int solve() {
  llong left = 0;
  llong right = 100000 * 10000;
  llong mid;
  while (right - 1 > left) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }

  return right;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> T[i];
  llong ans = solve();
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 100000
typedef long long llong;

int n, k;
llong T[MAX];

int check(llong P) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    llong s = 0;
    while (s + T[i] <= P) {
      s += T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int solve() {
  llong left = 0;
  llong right = 100000 * 10000;
  llong mid;
  while (right > left) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }

  return right;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> T[i];
  llong ans = solve();
  cout << ans << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define MAX 100000
typedef long long llong;

int n, k;
llong T[MAX];

int check(llong P) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    llong s = 0;
    while (s + T[i] <= P) {
      s += T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int solve() {
  llong left = 0;
  llong right = 100000 * 10000;
  llong mid;
  while (right - left > 1) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }

  return right;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> T[i];
  llong ans = solve();
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 100000
typedef long long llong;

int n, k;
llong T[MAX];

int check(llong P) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    llong s = 0;
    while (s + T[i] <= P) {
      s += T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int solve() {
  llong left = 0;
  llong right = 100000 * 10000;
  llong mid;
  while (right > left) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }

  return right;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> T[i];
  llong ans = solve();
  cout << ans << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int n, k;
int *w;

bool clear(int P) {
  int cnt = 1;
  int W = 0;
  for (int i = 0; i < n; i++) {
    if (w[i] > P) {
      return false;
    }
    if (W + w[i] > P) {
      cnt++;

      if (cnt > k) {
        return false;
      }

      W = w[i];
    } else {
      W += w[i];
    }
  }
  return true;
}

int main() {
  cin >> n >> k;
  w = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> w[i];
  }

  int l = 0;
  int r = 1000000000;
  int m;
  while (l < r) {
    m = (l + r) / 2;
    if (clear(m)) {
      r = m;
    } else {
      l = m + 1;
    }
  }
  cout << l << endl;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  int w[n];
  int maxi = -1;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    maxi = max(maxi, w[i]);
  }

  int P = maxi;
  while (true) {
    int K = 1;
    int W = 0;
    for (int i = 0; i < n; i++) {
      if (W + w[i] > P) {
        K++;
        if (K > k) {
          P++;
          break;
        }
        W = w[i];
      } else {
        W += w[i];
      }
    }
    if (K <= k) {
      break;
    }
  }
  cout << P << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <vector>

using namespace std;

static int f(const int *ws, int n, int P, int k) {
  int v = 0;
  int c = 0;
  int ntrack = 0;
  for (int i = 0; i < n; ++i) {
    if (c > 0 && P > 0 && c + ws[i] > P) {
      c = 0;
      ntrack++;
    }
    if (ntrack == k) {
      break;
    }

    if (c + ws[i] <= P) {
      c += ws[i];
      v++;
    }
  }
  return v;
}

int main() {
  int n, k;

  scanf("%d %d", &n, &k);

  // vector<int> ws(n);
  int ws[100000];
  int max = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &ws[i]);
    max += ws[i];
  }

  int min = 1;
  int mid = (min + max) / 2;
  while (min < max) {
    if (f(ws, n, mid, k) >= n) {
      max = mid;
    } else { // v < n
      min = mid + 1;
    }
    mid = (min + max) / 2;
  }

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

Error: TLE

Fixed submission:

#include <cstdio>
#include <vector>

using namespace std;

static int f(const int *ws, int n, int P, int k) {
  int v = 0;
  int c = 0;
  int ntrack = 0;
  for (int i = 0; i < n; ++i) {
    if (c > 0 && P > 0 && c + ws[i] > P) {
      c = 0;
      ntrack++;
    }
    if (ntrack == k) {
      break;
    }

    if (c + ws[i] <= P) {
      c += ws[i];
      v++;
    }
  }
  return v;
}

int main() {
  int n, k;

  scanf("%d %d", &n, &k);

  // vector<int> ws(n);
  int ws[100000];
  int max = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &ws[i]);
    max += ws[i];
  }

  int min = 1;
  int mid = (min + max) / 2;
  while (min <= max) {
    if (f(ws, n, mid, k) >= n) {
      max = mid;
    } else { // v < n
      min = mid + 1;
    }
    mid = (min + max) / 2;
  }

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