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

#include <iostream>
#include <memory>
#include <sstream>
#include <string>

int max_num(const int n, const int k, int const *w, const int p) {
  int j = 0;
  int track = p;
  int i = 0;
  for (; i < n;) {
    if (track >= w[i]) {
      track -= w[i];
      ++i;
    } else {
      ++j;
      track = p;
      if (j == k) {
        break;
      }
    }
  }

  return i;
}

int half(int n) { return n >> 1; }

int solve(const int n, const int k, int const *w, const int l, const int r) {
  const int m = half(l + r);
  if (l == m) {
    return r;
  }
  enum class Pos { Left, Right };
  const Pos p = max_num(n, k, w, m) < n ? Pos::Right : Pos::Left;
  return solve(n, k, w, p == Pos::Left ? l : m, p == Pos::Right ? r : m);
}

int solve(const int n, const int k, int const *w) {
  return solve(n, k, w, 0, 10000 * 100000);
}

void split(int *ret, const std::string &data) {
  std::stringstream stream;
  stream << data;
  std::string buf;
  for (int i = 0; getline(stream, buf, ' '); ++i) {
    ret[i] = std::stoi(buf);
  }
  return;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int tmp[2];
  {
    std::string buf;
    getline(std::cin, buf);
    split(tmp, buf);
  }
  const int n = tmp[0];
  const int k = tmp[1];

  int w[100000];
  for (int i = 0; i < n; ++i) {
    std::cin >> w[i];
    std::cin.ignore();
  }

  std::cout << solve(n, k, w) << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <memory>
#include <sstream>
#include <string>

int solve(const int n, const int k, int const *w, const int p) {
  int j = 0;
  int track = p;
  int i = 0;
  for (; i < n;) {
    if (track >= w[i]) {
      track -= w[i];
      ++i;
    } else {
      ++j;
      track = p;
      if (j == k) {
        break;
      }
    }
  }

  if (i == n) {
    return p;
  }

  return solve(n, k, w, p + 1);
}

int solve(const int n, const int k, int const *w) { return solve(n, k, w, 0); }

void split(int *ret, const std::string &data) {
  std::stringstream stream;
  stream << data;
  std::string buf;
  for (int i = 0; getline(stream, buf, ' '); ++i) {
    ret[i] = std::stoi(buf);
  }
  return;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int tmp[2];
  {
    std::string buf;
    getline(std::cin, buf);
    split(tmp, buf);
  }
  const int n = tmp[0];
  const int k = tmp[1];

  int w[100000];
  for (int i = 0; i < n; ++i) {
    std::cin >> w[i];
    std::cin.ignore();
  }

  std::cout << solve(n, k, w) << std::endl;

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

#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 100000;
const int MAXW = 10000;
int n, k, weight[MAXN];
int getloadedNum(int P) {
  int num = 0;
  for (int i = 0; i < k; i++) {
    int temp = 0;
    while (temp + weight[num] <= P) {
      temp += weight[num];
      num++;
      if (num == n)
        return n;
    }
  }
  return num;
}
int solve() {
  int left = 0;
  int right = MAXN * MAXW;
  while (left + 1 < right) {
    int mid = (left + right) / 2;
    int maxNum = getloadedNum(mid);
    if (maxNum >= n)
      right = mid;
    else
      left = mid;
  }
  return right;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> weight[i];
  cout << solve() << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
const int MAX = 100000;
int n, k, weight[MAX];
int getloadedNum(int P) {
  int num = 0;
  bool book[MAX] = {false};
  for (int i = 0; i < k; i++) {
    int temp = P;
    for (int j = 0; j < n; j++) {
      if (book[j])
        continue;
      else {
        if (weight[j] <= temp) {
          temp -= weight[j];
          book[j] = true;
          num++;
        } else
          break;
      }
    }
  }
  return num;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> weight[i];
  int *maxw = max_element(weight, weight + n);
  int maxP = n * (*maxw) / k;
  for (int i = 1; i <= maxP; i++)
    if (getloadedNum(i) == n) {
      cout << i << endl;
      break;
    }
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

vector<int> Input(const int n) {
  vector<int> W;
  for (int i = 0; i < n; i++) {
    int w;
    cin >> w;
    W.push_back(w);
  }
  return W;
}

bool IsAllLoaded(int P, vector<int> &W, int k) {
  int TrackIndex = 0;
  int CurrentSum = 0;
  int i = 0;
  for (i = 0; i < W.size(); i++) {
    if (W[i] > P) {
      return false;
    }
    if (CurrentSum + W[i] > P) {
      TrackIndex++;
      if (TrackIndex == k) {
        return false;
      }
      CurrentSum = 0;
    }
    CurrentSum += W[i];
  }
  return true;
}

int Calc(vector<int> W, int k) {
  int Sum = accumulate(W.begin(), W.end(), 0);
  int Ave = Sum / W.size();
  int e = Sum;
  int s = Ave - 1;
  int m = (e + s) / 2;
  //	cout << s << ' ' << m << ' ' << e << endl;

  while (e > s + 1) {
    if (IsAllLoaded(m, W, k)) {
      e = m;
      m = (s + m) / 2;
    } else if (!IsAllLoaded(m + 1, W, k)) {
      s = m;
      m = (e + m) / 2;
    } else {
      return m + 1;
    }
    //		cout << s << ' ' << m << ' ' << e << endl;
  }
  return e;
}

int main() {
  int n, k; // n:???????????° k:?????????????????°
  cin >> n >> k;
  vector<int> W = Input(n);
  int P = Calc(
      W,
      k); // P:??????????????????????????????????????????????????§????????????????°????
  cout << P << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

vector<int> Input(const int n) {
  vector<int> W;
  for (int i = 0; i < n; i++) {
    int w;
    cin >> w;
    W.push_back(w);
  }
  return W;
}

bool IsAllLoaded(int P, vector<int> &W, int k) {
  int TrackIndex = 0;
  int CurrentSum = 0;
  int i = 0;
  for (i = 0; i < W.size(); i++) {
    if (W[i] > P) {
      return false;
    }
    if (CurrentSum + W[i] > P) {
      TrackIndex++;
      if (TrackIndex == k) {
        return false;
      }
      CurrentSum = 0;
    }
    CurrentSum += W[i];
  }
  return true;
}

int Calc(vector<int> W, int k) {
  int Sum = accumulate(W.begin(), W.end(), 0);
  int Ave = Sum / W.size();
  int e = Sum;
  int s = Ave - 1;
  int m = (e + s) / 2;
  //	cout << s << ' ' << m << ' ' << e << endl;

  while (e != s + 1) {
    if (IsAllLoaded(m, W, k)) {
      e = m;
      m = (s + m) / 2;
    } else if (!IsAllLoaded(m + 1, W, k)) {
      s = m;
      m = (e + m) / 2;
    }
    //		cout << s << ' ' << m << ' ' << e << endl;
  }
  return e;
}

int main() {
  int n, k; // n:???????????° k:?????????????????°
  cin >> n >> k;
  vector<int> W = Input(n);
  int P = Calc(
      W,
      k); // P:??????????????????????????????????????????????????§????????????????°????
  cout << P << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

vector<int> Input(const int n) {
  vector<int> W;
  for (int i = 0; i < n; i++) {
    int w;
    cin >> w;
    W.push_back(w);
  }
  return W;
}

bool IsAllLoaded(int P, vector<int> &W, int k) {
  int TrackIndex = 0;
  int CurrentSum = 0;
  int i = 0;
  for (i = 0; i < W.size(); i++) {
    if (W[i] > P) {
      return false;
    }
    if (CurrentSum + W[i] > P) {
      TrackIndex++;
      if (TrackIndex == k) {
        return false;
      }
      CurrentSum = 0;
    }
    CurrentSum += W[i];
  }
  return true;
}

int Calc(vector<int> W, int k) {
  int Sum = accumulate(W.begin(), W.end(), 0);
  int Ave = Sum / W.size();
  int e = Sum;
  int s = Ave;
  int m = (e + s) / 2;
  //	cout << s << ' ' << m << ' ' << e << endl;

  while (IsAllLoaded(m, W, k) || !IsAllLoaded(m + 1, W, k)) {
    if (IsAllLoaded(m, W, k) && IsAllLoaded(m + 1, W, k)) {
      e = m;
      m = (s + m) / 2;
      if (s == e)
        return m;
    } else {
      s = m;
      m = (e + m) / 2;
    }
    //		cout << s << ' ' << m << ' ' << e << endl;
  }
  return m + 1;
}

int main() {
  int n, k; // n:???????????° k:?????????????????°
  cin >> n >> k;
  vector<int> W = Input(n);
  int P = Calc(
      W,
      k); // P:??????????????????????????????????????????????????§????????????????°????
  cout << P << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

vector<int> Input(const int n) {
  vector<int> W;
  for (int i = 0; i < n; i++) {
    int w;
    cin >> w;
    W.push_back(w);
  }
  return W;
}

bool IsAllLoaded(int P, vector<int> &W, int k) {
  int TrackIndex = 0;
  int CurrentSum = 0;
  int i = 0;
  for (i = 0; i < W.size(); i++) {
    if (W[i] > P) {
      return false;
    }
    if (CurrentSum + W[i] > P) {
      TrackIndex++;
      if (TrackIndex == k) {
        return false;
      }
      CurrentSum = 0;
    }
    CurrentSum += W[i];
  }
  return true;
}

int Calc(vector<int> W, int k) {
  int Sum = accumulate(W.begin(), W.end(), 0);
  int Ave = Sum / W.size();
  int e = Sum;
  int s = Ave;
  int m = (e + s) / 2;
  //	cout << s << ' ' << m << ' ' << e << endl;

  while (IsAllLoaded(m, W, k) || !IsAllLoaded(m + 1, W, k)) {
    if (IsAllLoaded(m, W, k) && IsAllLoaded(m + 1, W, k)) {
      e = m;
      m = (s + m) / 2;
    } else if (!IsAllLoaded(m, W, k) && !IsAllLoaded(m + 1, W, k)) {
      s = m;
      m = (e + m) / 2;
    }
    //		cout << s << ' ' << m << ' ' << e << endl;
  }
  return m + 1;
}

int main() {
  int n, k; // n:???????????° k:?????????????????°
  cin >> n >> k;
  vector<int> W = Input(n);
  int P = Calc(
      W,
      k); // P:??????????????????????????????????????????????????§????????????????°????
  cout << P << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class Alloc {
public:
  int n, k;
  int w[100000];
  int solve();
  int maxcnt(int p);
};

int Alloc::solve() {

  cin >> n >> k;

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

  int a = 0;
  int b = 1000000000;

  while (a + 1 != b) {
    int c = (a + b) / 2;

    if (maxcnt(c) == n)
      b = c;
    else
      a = c;
  }

  cout << b << endl;

  return 0;
}

int Alloc::maxcnt(int p) {
  int s = 0;
  int j = 0;

  for (int i = 0; i < k; i++) {
    while (s + w[j] <= p && j < n) {
      s += w[j];
      j++;
    }

    if (j == n)
      return n;

    s = 0;
  }

  return j;
}

int main(void) {

  Alloc al;

  al.solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class Alloc {
public:
  int n, k;
  int w[100000];
  int solve();
  int maxcnt(int p);
};

int Alloc::solve() {
  cin >> n >> k;

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

  for (int p = 1;; p++) {
    if (maxcnt(p) == n) {
      cout << p << endl;
      return 0;
    }
  }
}

int Alloc::maxcnt(int p) {
  int s = 0;
  int j = 0;

  for (int i = 0; i < k; i++) {
    while (s + w[j] <= p && j < n) {
      s += w[j];
      j++;
    }

    if (j == n)
      return n;

    s = 0;
  }

  return j;
}

int main(void) {

  Alloc al;

  al.solve();

  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define INT_MAX 2147483647
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i < (int)n; i++)

using namespace std;
typedef long long int ll;

bool judge(int n, int k, int p, vector<int> a) {
  bool ret = true;
  int s = 1, t = 0;
  Loop(i, n) {
    if (t + a[i] > p) {
      s++;
      if (s > k) {
        ret = false;
        break;
      }
      t = 0;
    }
    t += a[i];
  }
  return ret;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> w(n);
  int wmax = 0, sumw = 0;
  Loop(i, n) {
    cin >> w[i];
    if (w[i] > wmax)
      wmax = w[i];
    sumw += w[i];
  }
  int left = wmax, right = sumw;
  int p = (left + right) / 2;
  while (left < right) {
    if (judge(n, k, p, w))
      right = p;
    else
      left = p + 1;
    p = (left + right) / 2;
  }
  cout << p << endl;
  return 0;
}

Error: 10

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define INT_MAX 2147483647
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i < (int)n; i++)

using namespace std;
typedef long long int ll;

bool judge(int n, int k, int p, vector<int> a) {
  bool ret = true;
  int s = 1, t = 0;
  Loop(i, n) {
    if (t + a[i] > p) {
      s++;
      if (s > k) {
        ret = false;
        break;
      }
      t = 0;
    }
    t += a[i];
  }
  return ret; //????????????true
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> w(n);
  int wmax = 0, sumw = 0;
  Loop(i, n) {
    cin >> w[i];
    if (w[i] > wmax)
      wmax = w[i];
    sumw += w[i];
  }
  int left = wmax, right = sumw;
  int p = (left + right) / 2;
  while (left < right) {
    if (judge(n, k, p, w))
      right = p;
    else
      left = p + 1;
    p = (left + right) / 2;
  }
  cout << p << endl;
  return p;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
#define MAX 100000
long long baggage[MAX];
int n, k;

bool vertification(long long P) {
  int j = 0;
  long long track = 0;
  for (int i = 0; i < n; i++) {
    if (track + baggage[i] <= P) {
      track += baggage[i];
      if (i == n - 1) {
        return true;
      }
    } else {
      if (j < k - 1) {
        j++;
        track = 0;
        i--;
      } else {
        return false;
      }
    }
  }
}

long long check() {
  long long left = 0;
  long long right = MAX * 10000;
  long long mid;
  while (right - left > 1) {
    mid = (left + right) / 2;
    if (vertification(mid)) {
      right = mid;
    } else {
      left = mid;
    }
  }
  return right;
}

int main() {
  long long result;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &baggage[i]);
  }
  result = check();
  printf("%d\\n", result);
  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#define MAX 100000
long long baggage[MAX];
int n, k;

bool vertification(long long P) {
  int j = 0;
  long long track = 0;
  for (int i = 0; i < n; i++) {
    if (track + baggage[i] <= P) {
      track += baggage[i];
    } else {
      if (j < k - 1) {
        j++;
        track = 0;
      } else {
        return false;
      }
    }
  }
  return true;
}

int check() {
  long long left = 0;
  long long right = MAX * 10000;
  long long mid;
  while (right - left > 1) {
    mid = (left + right) / 2;
    if (vertification(mid)) {
      right = mid;
    } else {
      left = mid;
    }
  }
  return right;
}

int main() {
  int result;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", baggage[i]);
  }
  result = check();
  printf("%d\\n", &result);
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <vector>
#define sf scanf
#define pf printf
#define show(x) cout << "x:" << x << endl;
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
const int inf = 1e9;
int n, k;
ll t[maxn];
int check(ll p) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    ll s = 0;
    while (s + t[i] <= p) {
      s += t[i];
      ++i;
      if (i == n)
        return n;
    }
  }
  return i;
}
ll solve() {
  ll l = 0, r = 10000 * 100000;
  while (r - l > 1) {
    ll m = (l + r) / 2;
    ll v = check(m);
    if (v >= n)
      r = m;
    else
      l = m;
  }
  return r;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> t[i];
  ll ans = solve();
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <vector>
#define sf scanf
#define pf printf
#define show(x) cout << "x:" << x << endl;
using namespace std;
typedef long long ll;
const int maxn = 15;
const int inf = 1e9;
int n, k;
ll t[maxn];
int check(ll p) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    ll s = 0;
    while (s + t[i] <= p) {
      s += t[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}
ll solve() {
  ll l = 0, r = 10000 * 100000;
  ll m;
  while (r - l > 1) {
    m = (l + r) / 2;
    ll v = check(m);
    if (v >= n)
      r = m;
    else
      l = m;
  }
  return r;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> t[i];
  ll ans = solve();
  cout << ans << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
long long a[100005], k, n;
long long maxn;
int check(long long p) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    long long s = 0;
    while (s + a[i] <= p) {
      s += a[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}
int solve() {
  long long left = 0, right = maxn, mid;
  while (right - left > 1) {
    mid = left + (right - left) / 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 >> a[i];
    maxn += a[i];
  }
  int ans = solve();
  cout << solve() << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int a[100005], k, n;
long long maxn;
int check(int p) // 最大运载量为p的时候能装的货物数
{
  int sum, num = 0, i = 0;
  for (int j = 0; j < k; j++) {
    sum = 0;
    while (sum + a[i] <= p) {
      sum += a[i];
      i++;
    }
    if (i >= n)
      return n;
  }
  return i;
}
int solve() {
  long long left = 0, right = maxn, mid;
  while (left < right - 1) {
    mid = left + (right - left) / 2;
    if (check(mid) >= n)
      right = mid;
    else
      left = mid;
  }
  return right;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxn += a[i];
  }
  cout << solve() << 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;
}


Error: TLE

Fixed submission:

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

typedef long long ll;

using namespace std;

ll t[100010];
int n, k;

int check(ll p) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    ll s = 0;
    while (s + t[i] <= p) {
      s += t[i++];
      if (i == n)
        return n;
    }
  }
  return i;
}

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

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }
  ll 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;
}
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 += T[i++];
      if (i == n)
        return n;
    }
  }
  return i;
}
int solve() {
  llong left = 0;
  llong right = 100000 * 10000;
  llong mid;
  while (right > left) {
    mid = (right + left) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid - 1;
    else
      left = mid;
  }
  return mid;
}
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: Runtime Error

#include <stdio.h>
using namespace std;

int n, k;
long long a[200015];

int c(long long p) {
  int i = 0;

  for (int j = 0; j < k; j++) {
    long long s = 0;
    while (s + a[i] <= p) {
      s += a[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int s() {
  long long left = 0;
  long long right = 1000000000;
  long long mid;
  while (right - left > 1) {
    mid = (right + left) / 2;
    int v = c(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  printf("%d\\n", s());
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
using namespace std;

int n, k;
long long a[10005];

int c(long long p) {
  int i = 0;

  for (int j = 0; j < k; j++) {
    long long s = 0;
    while (s + a[i] <= p) {
      s += a[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int s() {
  long long left = 0;
  long long right = 100000 * 10000;
  long long mid;
  while (right - left > 1) {
    mid = (right + left) / 2;
    if (c(mid) >= n)
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  printf("%lld\\n", s());
  return 0;
}