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

#include <cstdio>
#include <vector>

// function that checks if given a weight limit, the number of allocations does
// not exceed the number of available trucks
bool fits(int limit, int n_weights, int n_trucks, std::vector<int> &weights) {
  for (int i = 0; i < n_weights; ++i, --n_trucks) { // for each allocation
    int cur_truck_weight = 0;
    if (weights[i] > limit)
      return false; // if single weight cannot fit into limit
    while (i < n_weights && cur_truck_weight + weights[i] <= limit) {
      cur_truck_weight += weights[i];
      ++i;
    }
    --i;
  }
  return (n_trucks >= 0); // return false if allocations exceed number of trucks
}

int main() {
  int n_weights, n_trucks;
  scanf("%d %d", &n_weights, &n_trucks);
  std::vector<int> weights;
  for (int i = 0; i < n_weights; ++i) {
    int weight;
    scanf("%d", &weight);
    weights.push_back(weight);
  }

  // binary search
  int left = 0, right = 1e9;
  while (left < right) {
    int mid = (left + right) / 2;
    if (fits(mid, n_weights, n_trucks, weights)) {
      right = mid; // set upper bound
    } else {
      left = mid + 1; // set lower bound
    }
  }
  printf("%d\\n", left);
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <vector>

// function that checks if given a weight limit, the number of allocations does
// not exceed the number of available trucks
bool fits(int limit, int n_weights, int n_trucks, std::vector<int> &weights) {
  int cur_truck_weight = 0;
  for (int i = 0; i < n_weights; ++i, --n_trucks) { // for each allocation
    if (weights[i] > limit)
      return false; // if single weight cannot fit into limit
    while (i < n_weights && cur_truck_weight + weights[i] <= limit) {
      cur_truck_weight += weights[i];
      ++i;
    }
    --i;
  }
  return (n_trucks >= 0); // return false if allocations exceed number of trucks
}

int main() {
  int n_weights, n_trucks;
  scanf("%d %d", &n_weights, &n_trucks);
  std::vector<int> weights;
  for (int i = 0; i < n_weights; ++i) {
    int weight;
    scanf("%d", &weight);
    weights.push_back(weight);
  }

  // binary search
  int left = 0, right = 1e9;
  while (left < right) {
    int mid = (left + right) / 2;
    if (fits(mid, n_weights, n_trucks, weights)) {
      right = mid; // set upper bound
    } else {
      left = mid + 1; // set lower bound
    }
  }
  printf("%d\\n", left);
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
LL A[100000 + 10];
int n, k;
//?¨?????£??´§????¨?
bool check(LL P) {
  int cnt = k;
  for (int i = 0; i < n;) {
    LL t = P;
    cnt--;
    // ??????????????¨?????????????????????????????§?£??????????????£?????´§???
    if (cnt < 0)
      return false;
    while (i < n && t >= 0) {
      // ?????????????????\\?£???????????´§???
      if (t >= A[i])
        t -= A[i], i++;
      //?£????????????¨???????????????????£?
      if (t < A[i])
        break;
    }
  }
  return true;
}
// ????????????P???????°????
LL BinarySearch(LL sum) {
  LL left = 0, right = sum;
  LL mid;
  while (left + 1 < right) {
    mid = (left + right) / 2;
    //?????\\?£??\\??´§???
    if (check(mid))
      right = mid;
    else
      left = mid;
  }

  return right;
}
int main() {
  LL sum = 0;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &A[i]);
    sum += A[i];
  }
  LL P = BinarySearch(sum);
  printf("%lld\\n", P);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
LL A[100000 + 10];
int n, k;
//?¨?????£??´§????¨?
bool check(LL P) {
  int cnt = k;
  for (int i = 0; i < n;) {
    LL t = P;
    cnt--;
    // ??????????????¨?????????????????????????????§?£??????????????£?????´§???
    if (cnt < 0)
      return false;
    while (i < n && t > 0) {
      // ?????????????????\\?£???????????´§???
      if (t >= A[i])
        t -= A[i], i++;
      //?£????????????¨???????????????????£?
      if (t < A[i])
        break;
    }
  }
  return true;
}
// ????????????P???????°????
LL BinarySearch(LL sum) {
  LL left = 0, right = sum;
  LL mid;
  while (left < right) {
    mid = (left + right) / 2;
    //?????\\?£??\\??´§???
    if (check(mid))
      right = mid;
    else
      left = mid;
  }

  return mid;
}
int main() {
  LL sum = 0;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &A[i]);
    sum += A[i];
  }
  LL P = BinarySearch(sum);
  printf("%lld\\n", P);
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int n, k;
vector<int> w;
bool f(int p) {
  int l = 0, r = n - 1;
  int wei = 0;
  int cnt = 0;
  FOR(i, 0, n) {
    if (w[i] > p)
      return false;
    if (wei + w[i] <= p) {
      wei += w[i];
    } else {
      cnt++;
      wei = w[i];
    }
  }
  if (wei)
    cnt++;
  return cnt <= k;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  w.resize(n);
  FOR(i, 0, n) cin >> w[i];
  // 二分探索
  int l = 0, r = 2e9;
  while (r - l > 1) {
    int m = (r + l) / 2;
    if (f(m))
      r = m;
    else
      l = m;
  }
  cout << r << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int n, k;
vector<int> w;
bool f(int p) {
  int l = 0, r = n - 1;
  int wei = 0;
  int cnt = 0;
  int pick = 0;
  while (1) {
    cnt++;
    if (w[l] > p)
      return false;
    while (w[l] + wei <= p) {
      wei += w[l];
      l++;
      pick++;
      if (pick == n)
        break;
    }
    if (w[r] > p)
      return false;
    while (w[r] + wei <= p) {
      wei += w[r];
      r--;
      pick++;
      if (pick == n)
        break;
    }
    if (pick == n - 1)
      break;
    wei = 0;
  }
  return cnt <= k;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  w.resize(n);
  FOR(i, 0, n) cin >> w[i];
  sort(w.rbegin(), w.rend());
  // 二分探索
  ll l = 0, r = 2e9;
  while (r - l > 1) {
    ll m = (r + l) / 2;
    if (f(m))
      r = m;
    else
      l = m;
    // cout<<m<<endl;
  }
  cout << (f(r) ? r : r + 1) << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

bool deliver(int n, int k, long long W[], long long P) {
  int garbage = 0;
  for (int j = 0; j < k; j++) {
    long long s = 0;
    while (s + W[garbage] <= P) {
      s += W[garbage];
      garbage++;
      if (garbage == n)
        return true;
    }
  }
  return false;
}

int binary_search(int n, int k, long long W[]) {
  int end = n * 10000;
  int begin = 0;

  while (end - begin > 0) {
    int mid = (begin + end) / 2;
    if (deliver(n, k, W, mid)) {
      end = mid;
    } else {
      begin = mid + 1;
    }
  }
  return begin;
}

int main(void) {
  int n, k;
  long long W[100000];

  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> W[i];
  int ans = binary_search(n, k, W);
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

bool deliver(int n, int k, int W[], long long P) {
  int garbage = 0;
  for (int j = 0; j < k; j++) {
    long long s = 0;
    while (s + W[garbage] <= P) {
      s += W[garbage];
      garbage++;
      if (garbage == n)
        return true;
    }
  }
  return false;
}

int binary_search(int n, int k, int W[]) {
  int end = n * 10000;
  int begin = 0;

  while (end - begin > 0) {
    int mid = (begin + end) / 2;
    if (deliver(n, k, W, mid)) {
      end = mid;
    } else {
      begin = mid + 1;
    }
  }
  return begin;
}

int main(void) {
  int n, k;
  int W[10000];

  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> W[i];
  int ans = binary_search(n, k, W);
  cout << ans << endl;

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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int w[100003];
  REP(i, n) { cin >> w[i]; }
  int l = 0, r = 1e9 + 1;
  while (abs(r - l) > 1) {
    int p = (r + l) / 2;
    int j = 0;
    REP(i, k) {
      int tmpp = 0;
      while (tmpp + w[j] <= p) {
        tmpp += w[j];
        j++;
        if (j == n)
          break;
      }
      if (j == n)
        break;
    }
    if (j == n)
      r = p;
    else
      l = p;
  }
  cout << r << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int w[10003];
  REP(i, n) { cin >> w[i]; }
  int l = 0, r = 1e9 + 1;
  while (abs(r - l) > 1) {
    int p = (r + l) / 2;
    int j = 0;
    REP(i, k) {
      int tmpp = 0;
      while (tmpp + w[j] <= p) {
        tmpp += w[j];
        j++;
        if (j == n)
          break;
      }
      if (j == n)
        break;
    }
    if (j == n)
      r = p;
    else
      l = p;
  }
  cout << r << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int check(long P, int A[], int n, int k) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    long sum = 0;
    while (sum + A[i] <= P) {
      sum += A[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

long solve(int A[], int n, int k) {
  long left = 0;
  long right = n * 10000;
  long mid;
  while (right - left > 1) {
    mid = (left + right) / 2;
    long t = check(mid, A, n, k);
    if (t >= n)
      right = mid;
    else
      left = mid;
  }
  return right;
}

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

  int A[100000] = {0};

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

  long ans = solve(A, n, k);
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int check(long P, int A[], int n, int k) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    long sum = 0;
    while (sum + A[i] <= P) {
      sum += A[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

long solve(int A[], int n, int k) {
  long left = 0;
  long right = n * 100000;
  long mid;
  while (right - left > 1) {
    mid = (left + right) / 2;
    long t = check(mid, A, n, k);
    if (t >= n)
      right = mid;
    else
      left = mid;
  }
  return right;
}

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

  int A[10000] = {0};

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

  long ans = solve(A, n, k);
  cout << ans << endl;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

// allocation
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int n, k, maxp, w[999999], sum[999999];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &w[i]);
    sum[i] = sum[i - 1] + w[i];
    maxp = max(maxp, w[i]);
  }
  maxp = max(maxp, sum[n] / k);
  while (true) {
    int now = maxp;
    int *nowpos = sum;
    for (int i = 1; i <= k; i++) {
      // search
      nowpos = upper_bound(nowpos, sum + n + 1, now);
      now = *(nowpos - 1) + maxp;
    }
    // end
    if (nowpos == sum + n + 1)
      break;
    else
      maxp++;
  }
  printf("%d\\n", maxp);
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, k, w[999999], sum[999999];
int main() {
  scanf("%d%d", &n, &k);
  int minp = 9999999;
  for (int i = 1; i <= n; i++) {
    scanf("%d", w[i]);
    sum[i] = sum[i - 1] + w[i];
    minp = max(minp, w[i]);
  }
  minp = max(minp, sum[n] / k);
  puts("10");
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, K, min = 0, max = 0, P;
  scanf("%d %d", &n, &K);

  vector<int> w(n);
  for (int &x : w) {
    scanf("%d", &x);
    min = min > x ? min : x;
    max += x;
  }
  min -= 1;

  for (;;) {
    P = (max + min) / 2;
    auto i = w.begin(), e = w.end(), j = i;
    int k = 0;
    for (; i != e && j != e && k < K; i++, k++) {
      int p = 0;
      for (j = i; j != e; j++) {
        if (p + *j > P) {
          i = j - 1;
          break;
        } else if (p + *j == P) {
          i = j;
          break;
        } else
          p += *j;
      }
    }
    if ((i == e || j == e) && k <= K)
      max = P;
    else
      min = P;
    if (max == min + 1) {
      P = max;
      break;
    }
  }
  printf("%d\\n", P);
}


Error: TLE

Fixed submission:

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

int main() {
  int n, K, P = 0;
  scanf("%d %d", &n, &K);

  vector<int> w(n);
  for (int &x : w) {
    scanf("%d", &x);
    P = P > x ? P : x;
  }

  for (;; P++) {
    auto i = w.begin(), e = w.end();
    int k = 0;
    for (; i != e && k < K; i++, k++) {
      int p = 0;
      for (auto j = i; j != e; j++)
        if (p + *j > P) {
          i = j - 1;
          break;
        } else
          p += *j;
    }
    if (i == w.end() && k == K)
      break;
  }
  printf("%d\\n", P);
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int contena[100000];
int n, k;
bool search(int);
int main() {
  int linemax = 0;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> contena[i];
    linemax = max(linemax, contena[i]);
  }
  int ip, ap;
  ip = 0;
  ap = (1 << 31) - 1;
  while (ap - ip > 0) {
    int dummy = (ap + ip) / 2;
    if (linemax > dummy || !search(dummy)) {
      ip = dummy + 1;
    } else {
      ap = dummy;
    }
  }
  cout << ap << endl;
  return 0;
}
bool search(int maxheavy) {
  int num = 0;
  int use = 0;
  for (int i = 0; i < n; ++i) {
    if (maxheavy < num + contena[i]) {
      num = contena[i];
      if (++use == k)
        return false;
    } else {
      num += contena[i];
    }
  }
  return true;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int answer;
int *memo;
void check(int, int, int, int);
int main() {
  int track;
  int time;
  cin >> time >> track;
  memo = new int[time + 1];
  memo[0] = 0;
  int dummy;
  for (int i = 1; i <= time; ++i) {
    cin >> dummy;
    memo[i] = memo[i - 1] + dummy;
  }
  if (track == 1) {
    cout << memo[time] << endl;
    return 0;
  }
  answer = memo[time];
  for (int i = 1; i <= time - track + 1; ++i) {
    check(i, track - 1, memo[i], time);
  }
  cout << answer << endl;
  return 0;
}

void check(int back, int nsize, int nmax, int time) {
  if (answer <= nmax)
    return;
  else if (nsize == 1) {
    answer = max(nmax, memo[time] - memo[back]);
    return;
  }
  for (int i = back + 1; i <= time + 1 - nsize; ++i) {
    if (memo[i] - memo[back] > answer)
      break;
    check(i, nsize - 1, max(nmax, memo[i] - memo[back]), time);
  }
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long n = 0, k = 0, ttl = 0;
  int max = 0;

  cin >> n >> k;

  vector<long> w(n);

  for (long i = 0; i < n; i++) {
    int t;
    cin >> t;
    ttl += t;
    w[i] = t;
    if (t > max)
      max = t;
  }

  int s = 0, p = 0;
  long c = 1;

  if (max > ttl / k)
    p = max;
  else
    p = ttl / k;

  for (long i = 0; i < n; i++) {
    if (s + w[i] <= p)
      s += w[i];
    else {
      s = w[i];
      c++;
    }
    if (c > k) {
      s = 0;
      c = 1;
      i = -1;
      p++;
    }
  }

  cout << p << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long n = 0, k = 0;
  int max = 0;

  cin >> n >> k;

  vector<long> w(n);

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

  int s = 0, p = max;
  long c = 1;

  for (long i = 0; i < n; i++) {
    if (s + w[i] <= p)
      s += w[i];
    else {
      s = w[i];
      c++;
    }
    if (c > k) {
      s = 0;
      c = 1;
      i = -1;
      p++;
    }
  }

  cout << p << endl;

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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef pair<int, int> P;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)

typedef long long ll;
typedef vector<int> VI;
int w[100010];
int n, k;
bool check(int t) { // 一つのトラックの積載量をtとする.
  int sum = 0;
  int cnt = 1;
  for (int i = 0; i < n; ++i) {
    sum += w[i];
    if (sum > t) {
      cnt++;
      sum = w[i];
    }
    if (w[i] > t)
      return false;
  }
  return cnt <= k;
}
int main() {
  cin >> n >> k;
  REP(i, n) cin >> w[i];

  int dl = 0, ul = 1e9;
  for (int i = 0; i < 40; ++i) {
    int mid = dl + (ul - dl) / 2;

    if (check(mid)) {
      ul = mid - 1;
    } else {
      dl = mid + 1;
    }
  }
  cout << dl << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int n, m, cnt, k;

int main() {
  cin >> n >> k;
  vector<int> v(n, 0);
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    v[i] = a;
  }
  int sum = accumulate(v.begin(), v.end(), 0);
  int P = 0;
  if (sum % k != 0) {
    P = sum / k + 1;
  } else {
    P = sum / k;
  }
  int part_sum = 0;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    part_sum = 0;
    while (true) {
      part_sum += v[i];
      if (part_sum > P) {
        cnt++;
        i -= 1;
        part_sum = 0;
        break;
      }
      if (i == n - 1) {
        cnt++;
        break;
      }
      i++;
    }
    if (i == n - 1) {
      if (cnt <= k)
        break;
      else {
        i = -1;
        cnt = 0;
        P++;
      }
    }
  }
  cout << P << endl;

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

#include <iostream>
using namespace std;
#define MAX 100000
long long n, k, w[MAX];

long long check(long long P) {
  long long v = 0, num_track = k, available_load = P;
  while (1) {
    if (w[v] > P || v == n)
      return v;
    if (w[v] > available_load) {
      if (!(--num_track))
        return v;
      available_load = P;
    }
    available_load -= w[v];
    v++;
  }
  return v;
}

long long solve() {
  long long mid, left = 0, right = 100000 * 10000;
  while (right - left > 1) {
    mid = (left + right) / 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 >> w[i];
  }
  long long p = 0;
  cout << solve() << endl;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 1000
unsigned n, k, w[MAX];

unsigned check(unsigned P) {
  unsigned v = 0, num_track = k, available_load = P;
  while (1) {
    if (w[v] > P)
      return v;
    if (w[v] > available_load) {
      if ((!--num_track))
        return v;
      available_load = P;
    }
    available_load -= w[v];
    v++;
  }
  return v;
}

unsigned solve() {
  unsigned mid, left = 0, right = 100000 * 10000;
  while (right - left > 1) {
    mid = (left + right) / 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 >> w[i];
  }
  unsigned p = 0;
  cout << solve() << endl;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;

#define int long long
#define ll long long
typedef pair<int, int> P;
#define mod 1000000007
#define INF (1LL << 60)

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl

int n, k;
int w[123456];

bool func(int P) {
  int truck = 1, weight = 0;
  rep(i, n) {
    if (w[i] > P)
      return false;

    if (weight + w[i] <= P)
      weight += w[i];
    else {
      weight = w[i];
      truck++;
    }
  }
  if (truck <= k)
    return true;
  else
    return false;
}

signed main() {

  cin >> n >> k;
  rep(i, n) cin >> w[i];

  int ng = 0, ok = 1e9;

  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;

    //		cerr << mid << " " << func(mid) << endl;

    if (func(mid))
      ok = mid;
    else
      ng = mid;
  }

  cout << ok << endl;
  return 0;
}


Error: 0
5000 1
2500 1
1250 1
625 1
312 1
156 1
78 1
39 1
19 1
9 0
14 1
11 1
10 1


Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define int long long
#define ll long long
typedef pair<int, int> P;
#define mod 1000000007
#define INF (1LL << 60)

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl

int n, k;
int w[123456];

bool func(int P) {
  int truck = 1, weight = 0;
  rep(i, n) {
    if (w[i] > P)
      return false;

    if (weight + w[i] <= P)
      weight += w[i];
    else {
      weight = w[i];
      truck++;
    }
  }
  if (truck <= k)
    return true;
  else
    return false;
}

signed main() {

  cin >> n >> k;
  rep(i, n) cin >> w[i];

  int ng = 0, ok = 10000;

  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;

    cerr << mid << " " << func(mid) << endl;

    if (func(mid))
      ok = mid;
    else
      ng = mid;
  }

  cout << ok << endl;
  return 0;
}