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

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

int n, k;
int w[100000];

int getMin(int max) {
  int kk = k;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (sum + w[i] > max) {
      kk--;
      if (kk == 0)
        return false;
      sum = w[i];
    } else {
      sum += w[i];
    }
  }
  return true;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int max = 0;
  int sum = 0;

  cin >> n >> k;

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

  int left = max;
  int right = sum;
  int mid;
  bool prev_result;
  while (true) {
    mid = (left + right) / 2;
    if (getMin(mid)) {
      if (right - left <= 1) {
        if (prev_result) {
          cout << right << endl;
        } else {
          cout << left << endl;
        }
        break;
      }
      right = mid;
    } else {
      if (right - left <= 1) {
        if (prev_result) {
          cout << left << endl;
        } else {
          cout << right << endl;
        }
        break;
      }
      left = mid + 1;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int n, k;
int w[100000];

int getMin(int max) {
  int kk = k;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (sum + w[i] > max) {
      kk--;
      if (kk == 0)
        return false;
      sum = w[i];
    } else {
      sum += w[i];
    }
  }
  return true;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int max = 0;

  cin >> n >> k;

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

  while (true) {
    if (getMin(max)) {
      cout << max << endl;
      break;
    }
    max++;
  }

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

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

class cAllocateBaggage {
  int mn, mk;

  vector<int> vw, vallocatedWeight;

public:
  cAllocateBaggage(int n, int k, vector<int> &v) : mn(n), mk(k), vw(v) {}

  void alloc2();
};

void cAllocateBaggage::alloc2() {
  int left = 0;
  int right = 100000 * 10000;

  int p = 0;
  int t_index;
  bool isFound;
  int p_found = 0;

  while (left <= right) {
    t_index = 0;
    isFound = false;
    p = left + (right - left) / 2;
    vallocatedWeight.clear();
    vallocatedWeight.resize(mk);

    for (int i = 0; i < vw.size(); ++i) {
      if (vallocatedWeight[t_index] + vw[i] > p && mk == t_index + 1) {
        break;
      } else if (vallocatedWeight[t_index] + vw[i] > p) {
        ++t_index;
        --i;
      } else {
        vallocatedWeight[t_index] += vw[i];
        if ((i == (vw.size() - 1)) && t_index <= mk) {
          isFound = true;
        }
      }
    }
    if (isFound) {
      right = p - 1;
      p_found = p;
    } else {
      left = p + 1;
    }
  }
  if (!isFound)
    p = p_found;
  cout << p << endl;
}

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

  int n, k;
  cin >> n >> k;
  cin.ignore();

  vector<int> vw;

  int w;
  for (int i = 0; i < n; ++i) {
    cin >> w;
    vw.push_back(w);
  }

  cAllocateBaggage cab(n, k, vw);
  cab.alloc2();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

class cAllocateBaggage {
  int mn, mk;
  int ans;

  vector<int> vw, vallocatedWeight;
  vector<int>::iterator vimax;

public:
  cAllocateBaggage(int n, int k, vector<int> &v) : mn(n), mk(k), vw(v) {}

  void alloc();
};

void cAllocateBaggage::alloc() {
  int tindex;
  int p = 0;
  bool isFound = false;

  for (int i = 0; i < 10000 * vw.size(); ++i) {
    vallocatedWeight.clear();
    vallocatedWeight.resize(mk);
    tindex = 0;
    p = i;
    for (int j = 0; j < vw.size(); ++j) {
      if (vallocatedWeight[tindex] + vw[j] > p && mk == tindex + 1) {
        break;
      } else if (vallocatedWeight[tindex] + vw[j] > p) {
        ++tindex;
        --j;
      } else {
        vallocatedWeight[tindex] += vw[j];
        if ((j == (vw.size() - 1)) && tindex <= mk) {
          isFound = true;
        }
      }
    }
    if (isFound)
      break;
  }
  cout << p << endl;
}

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

  int n, k;
  cin >> n >> k;
  cin.ignore();

  vector<int> vw;

  int w;
  for (int i = 0; i < n; ++i) {
    cin >> w;
    vw.push_back(w);
  }

  cAllocateBaggage cab(n, k, vw);
  cab.alloc();

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

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

int num_truck(const vector<int> &w, int P) {
  int num = 1;
  int p = P;
  for (int i = 0; i < w.size(); i++) {
    if (w[i] > P)
      return -1; //??????????????´???
    if (p >= w[i]) {
      p -= w[i];
      continue;
    }
    num++;
    p = P - w[i];
  }
  return num;
}
int calc_P(const vector<int> &w, int k, int min, int max) {
  // cout << min << " " << max << endl;
  if (min > max)
    return min;
  int mid = (min + max) / 2;
  int num = num_truck(w, mid);
  if (num > k)
    return calc_P(w, k, mid + 1, max);
  if (num < k)
    return calc_P(w, k, min, mid - 1);
  if (num == num_truck(w, mid - 1))
    return calc_P(w, k, min, mid - 1);
  return mid;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> w(n);
  for (auto &i : w)
    cin >> i;
  int min_p = *max_element(w.begin(), w.end());
  int max_p = accumulate(w.begin(), w.end(), 0);
  cout << calc_P(w, k, min_p, max_p) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int num_truck(const vector<int> &w, int P) {
  int num = 1;
  int p = P;
  for (int i = 0; i < w.size(); i++) {
    if (w[i] > P)
      return -1; //??????????????´???
    if (p >= w[i]) {
      p -= w[i];
      continue;
    }
    num++;
    p = P - w[i];
  }
  return num;
}
int calc_P(const vector<int> &w, int k, int min, int max) {
  int mid = (min + max) / 2;
  int num = num_truck(w, mid);
  if (num > k)
    return calc_P(w, k, mid + 1, max);
  if (num < k)
    return calc_P(w, k, min, mid - 1);
  if (num == num_truck(w, mid - 1))
    return calc_P(w, k, min, mid - 1);
  return mid;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> w(n);
  for (auto &i : w)
    cin >> i;
  int min_p = *max_element(w.begin(), w.end());
  int max_p = accumulate(w.begin(), w.end(), 0);
  cout << calc_P(w, k, min_p, max_p) << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

long n, k;
const long MAX = 100000;
long T[MAX];

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

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

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

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <queue>
using namespace std;

int main() {
  int n, k,
      p; //???????????°????????????????????°,?±??????????????????????????°??????????
  int items[100000]; //????????????
  int ans = 0;
  int sum = 0;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> items[i];
    sum += items[i];
  }
  double a = (double)sum / k;
  //?±???????????°???°???????????????/?????????????????°??????????°??????????????????????
  // a????????§???????????¶??°p??????????????????????????¶?????????????????????p????????????
  p = (int)ceil(a);
  int testP = p; //?????????p

  while (1) { //??????p?????????
    for (int i = 0; i < n; ++i) {
      if (p >= items[i]) { //???????????????
        p -= items[i];
      } else if (p < items[i]) { //??????????????????????????£??????
        k--;                     //?????????????????????
        if (k == 0) {
          break; //??????????????????????????????????????????????????£????????????p?????????
        } else {
          p = testP; // ??°???????????????????????¨???
          p -= items[i];
        }
      } else if (p >= items[i] and
                 i == n - 1) { //??????????????????????????????????????????
        ans = p;               //??????p?????£??????p
        break;
      }
    }
    if (ans != 0)
      break;
    testP++; //?¬????p???
    p = testP;
  }
  cout << ans << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
int main(void) {
  int ObjectNumber, TracKNumber, Object;
  int TotalObject = 0;
  int Max = 0;
  vector<int> List;

  std::cin >> ObjectNumber >> TracKNumber;

  List.clear();
  for (int i = 0; i < ObjectNumber; i++) {
    std::cin >> Object;
    if (Max < Object)
      Max = Object;
    TotalObject += Object;
    List.push_back(Object);
  }

  if (TracKNumber == 1) {
    cout << TotalObject << endl;
    return 0;
  }
  int Ans = ceil((double)TotalObject / TracKNumber);
  if (Max > Ans)
    Ans = Max;

  bool IsAnswer = false;
  while (!IsAnswer) {
    int Count = 0;
    TotalObject = 0;
    for (int i = 0; i < List.size(); i++) {
      TotalObject += List[i];
      if (TotalObject > Ans) {
        Count++;
        TotalObject = 0;
        i--;
      } else if (TotalObject <= Ans && i == List.size() - 1) {
        Count++;
      }
    }
    if (Count <= TracKNumber) {
      IsAnswer = true;
    } else {
      Ans++;
    }
  }
  cout << Ans << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
int main(void) {
  int ObjectNumber, TracKNumber, Object;
  int TotalObject = 0;
  vector<int> List;

  std::cin >> ObjectNumber >> TracKNumber;

  List.clear();
  for (int i = 0; i < ObjectNumber; i++) {
    std::cin >> Object;
    TotalObject += Object;
    List.push_back(Object);
  }

  double Ans = ceil((double)TotalObject / TracKNumber);

  bool IsAnswer = false;

  while (!IsAnswer) {
    int Count = 0;
    TotalObject = 0;
    for (int i = 0; i < List.size(); i++) {
      TotalObject += List[i];
      if (TotalObject > Ans) {
        Count++;
        TotalObject = 0;
        i--;
      } else if (TotalObject <= Ans && i == List.size() - 1) {
        Count++;
      }
    }
    if (Count <= TracKNumber) {
      IsAnswer = true;
    } else {
      Ans++;
    }
  }
  cout << Ans << endl;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

#define MAX_N 1000000
#define MAX_W 10000

int check(int w[MAX_N], int n, int k, ll p) {
  int i = 0;
  REP(j, k) {
    ll sum = 0;
    for (; sum + w[i] < p; i++) {
      sum += w[i];
    }
  }
  return i;
}

int main() {
  int n, k, w[MAX_N];
  cin >> n >> k;
  REP(i, n) cin >> w[i];

  ll r = n * MAX_W;
  ll l = 0;
  while (r - l > 0) {
    ll mid = (l + r) / 2;
    int v = check(w, n, k, mid);
    if (v >= n)
      r = mid;
    else
      l = mid + 1;
  }
  cout << l - 1 << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

#define MAX_W 10000
#define MAX_N 1000000

int check(int w[MAX_W], int n, int k, ll p) {
  int i = 0;
  REP(j, k) {
    ll sum = 0;
    for (; sum + w[i] < p; i++) {
      sum += w[i];
    }
  }
  return i;
}

int main() {
  int n, k, w[MAX_W];
  cin >> n >> k;
  REP(i, n) cin >> w[i];

  ll r = n * MAX_W;
  ll l = 0;
  while (r - l > 0) {
    ll mid = (l + r) / 2;
    int v = check(w, n, k, mid);
    if (v >= n)
      r = mid;
    else
      l = mid + 1;
  }
  cout << l - 1 << endl;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<int> W;
int N, K;
int NMAX = 100000, WMAX = 10000;

bool check(int p) {
  int i, j = 0, sum;
  for (i = 0; i < K && j < N; i++) {
    sum = 0;
    for (; j < N && sum + W[j] <= p; j++) {
      sum += W[j];
    }
  }
  return (j >= N);
}
int search(int l, int r) {
  int p = (l + r) / 2, ret = l;
  if (r - l >= 1) {
    ret = check(p) ? search(l, p) : search(p + 1, r);
  }
  return ret;
}
int main() {
  cin >> N >> K;
  W = vector<int>(N);
  for (int i = 0; i < N; i++)
    cin >> W[i];
  cout << search(0, NMAX * WMAX) << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

vector<int> W;
int max(int a, int b) {
  if (a < b)
    return b;
  else
    return a;
}
int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
int R(int i, int k, int p) {
  int ret, tmp, sum;
  if (i < W.size()) {
    sum = 0;
    for (int j = i; j < W.size(); j++)
      sum += W[j];
    if (k == 1) {
      ret = max(p, sum);
    } else {
      ret = max(p, sum);
      tmp = 0;
      for (int j = i; j < W.size() - 1; j++) {
        tmp += W[j];
        ret = min(ret, R(j + 1, k - 1, max(p, tmp)));
      }
    }
  } else {
    ret = p;
  }
  return ret;
}
int main() {
  int n, k, i;
  cin >> n >> k;
  W = vector<int>(n);
  for (i = 0; i < n; i++)
    cin >> W[i];
  cout << R(0, k, 0) << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

int calc_truck_num(std::vector<int> wi, int P) {
  std::vector<int> truck;
  truck.push_back(0);
  for (int i = 0; i < wi.size(); ++i) {
    if (truck.back() + wi[i] <= P) {
      truck.back() += wi[i];
    } else {
      truck.push_back(wi[i]);
    }
  }
  return truck.size();
}

int main(int argc, char const *argv[]) {
  int n, k, w;
  int cnt = 0;
  int right = 0;
  int left = 0;
  int center;

  std::vector<int> wi;

  std::cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    std::cin >> w;
    wi.push_back(w);
    right += w;
    if (w > left)
      left = w;
  }

  while (right - left > 1) {
    center = (right + left) / 2;
    if (calc_truck_num(wi, center) > k) {
      left = center;
    } else {
      right = center;
    }
  }

  if (calc_truck_num(wi, left) <= k) {
    std::cout << left << std::endl;
  } else {
    std::cout << right << std::endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

int calc_truck_num(std::vector<int> wi, int P) {
  std::vector<int> truck;
  truck.push_back(0);
  for (int i = 0; i < wi.size(); ++i) {
    if (truck.back() + wi[i] <= P) {
      truck.back() += wi[i];
    } else {
      truck.push_back(wi[i]);
    }
  }
  return truck.size();
}

int main(int argc, char const *argv[]) {
  int n, k, w;
  int cnt = 0;
  int P = 0;

  std::vector<int> wi;

  std::cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    std::cin >> w;
    wi.push_back(w);
    if (w > P)
      P = w;
  }

  while (calc_truck_num(wi, P) > k) {
    P++;
  }

  std::cout << P << std::endl;

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



/*
https://muttan1203.hatenablog.com/entry/ALDS_1_4_D
*/
#include <iostream>

using ull = unsigned long long;
using ui = unsigned int;

int main() {
  ui n, k;
  int w[100'001];

  std::cin >> n >> k;
  for (ui i{}; i < n; ++i)
    std::cin >> w[i];

  auto check = [&](auto p) -> bool {
    ull sum{};
    ui index{};

    // k台分調べる
    for (ui i{}; i < k; ++i) {
      sum = 0;
      while (w[index] + sum <= p) { // もう一個積めるなら
        sum += w[index];            // 積んでいく
        index++;

        if (index == n)
          return true; // 積み切った
      }
    }

    return false; // 積み切れない=> pが足りない
  };

  ull left{}, right{10'000 * 100'000 + 1};
  while (right - left > 1) {
    ull mid = (right + left) / 2;
    if (check(mid))
      right = mid;
    else
      left = mid;
  }

  std::cout << right << "\\n";

  return 0;
}


Error: 0

Fixed submission:



/*
https://muttan1203.hatenablog.com/entry/ALDS_1_4_D
*/
#include <iostream>

using ull = unsigned long long;
using ui = unsigned int;

int main() {
  ui n, k;
  int w[10001];

  std::cin >> n >> k;
  for (ui i{}; i < n; ++i)
    std::cin >> w[i];

  auto check = [&](auto p) -> bool {
    ull sum{};
    ui index{};

    for (ui i{}; i < k; ++i) {
      sum = 0;
      while (w[index] + sum <= p) { // もう一個積めるなら
        sum += w[index];            // 積んでいく
        index++;

        if (index == n)
          return true; // 積み切った
      }
    }

    return false; // 積み切れない=> pが足りない
  };

  ull left{}, right{10'000 * 100'000 + 1};
  while (right - left > 1) {
    ull mid = (right + left) / 2;
    if (check(mid))
      right = mid;
    else
      left = mid;
  }

  std::cout << right << "\\n";

  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;
static const int MAXn = 100000;
static const long long MAXsum = 1000000000;

int w[MAXn], n;

int countTruck(int k) {
  int load = 0, trucknum = 1;
  for (int i = 0; i < n; i++) {
    if (load + w[i] > k) {
      trucknum++;
      load = w[i];
    } else {
      load += w[i];
    }
  }
  // cout << trucknum << " needed when max is " << k << endl;
  return trucknum;
}

int findK(long long l, long long r, int k) {
  // cout << "l =" << l << " r =" << r;
  if (l >= r) {
    return l;
  } else {
    int p = (l + r) / 2;
    // cout << " p =" << p << endl;
    int a = countTruck(p);
    if (a == k) {
      return p;
    } else if (a < k) {
      return findK(l, p, k);
    } else {
      return findK(p + 1, r, k);
    }
  }
}

int minofK(long long p, long long k, int min) {
  if (p - 1 < min)
    return p;
  else {
    if (countTruck(p - 1) == k)
      return minofK(p - 1, k, min);
    else
      return p;
  }
}

int main() {
  int k;
  long long maxw, sum;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    maxw = max((int)maxw, w[i]);
    sum += w[i];
  }

  cout << minofK(findK(maxw, sum, k), k, maxw) << endl;
}
Problem p02270 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <iostream>
#include <limits>
#include <queue>
#include <vector>

#ifdef _DEBUG
#define ASSERT(x) cassert(x)
#define DLOG(x) cout << #x " " << x << endl;
#else
#define ASSERT(x)
#define DLOG(x)
#endif

using namespace std;

bool ok(vector<int> &ws, int64_t weight_limit, int count_limit) {
  size_t index = 0;
  for (int i = 0; i < count_limit; ++i) {
    for (int64_t w = 0; w + ws[index] <= weight_limit;) {
      w += ws[index++];
      if (index == ws.size())
        return true;
    }
  }
  return false;
}

size_t search(vector<int> &ws, int k, size_t l, size_t r) {
  if (l >= r)
    return l;
  auto m = (l + r) / 2;
  if (ok(ws, m, k)) {
    return search(ws, k, l, m);
  } else {
    return search(ws, k, m + 1, r);
  }
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> ws;
  ws.reserve(n);
  for (int i = 0; i < n; ++i) {
    int wi;
    cin >> wi;
    ws.push_back(wi);
  }
  cout << search(ws, k, 0, 100'000LL * 10'000LL) << endl;
}


Error: MLE

Fixed submission:

// 23:50
#include <algorithm>
#include <cassert>
#include <iostream>
#include <limits>
#include <queue>
#include <vector>

#ifdef _DEBUG
#define ASSERT(x) cassert(x)
#define DLOG(x) cout << #x " " << x << endl;
#else
#define ASSERT(x)
#define DLOG(x)
#endif

using namespace std;

int main() {
  constexpr int NoData = numeric_limits<int>::max();
  int n, k;
  cin >> n >> k;
  k = n - k + 1;
  vector<int> ws;
  for (int i = 0; i < n; ++i) {
    int wi;
    cin >> wi;
    ws.push_back(wi);
  }
  vector<vector<pair<int, int>>> cur(k, vector<pair<int, int>>{});
  vector<vector<pair<int, int>>> next(k, vector<pair<int, int>>{});
  cur[0].push_back(make_pair(ws[0], ws[0]));
  for (int i = 1; i < n; ++i) {
    for (int j = k - 1; j >= 0; --j) {
      int min_ = NoData;
      for (auto &p : cur[j]) {
        min_ = min(min_, p.first);
      }
      if (min_ != NoData) {
        next[j] = {make_pair(max(min_, ws[i]), ws[i])};
      }
      if (j != 0) {
        for (auto &p : cur[j - 1]) {
          auto new_carry = p.second + ws[i];
          auto new_w = max(p.first, new_carry);
          if (new_w < min_) {
            next[j].push_back(make_pair(new_w, new_carry));
          }
        }
      }
    }
    cur.swap(next);
  }
  int min_ = NoData;
  for (auto &p : cur.back()) {
    min_ = min(min_, p.first);
  }
  cout << min_ << endl;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
int w[100000];
int n, k;
int work(int p) {
  int sum, i = 0, j = k;
  while (j--) {
    sum = 0;
    while (sum + w[i] <= p) {
      sum += w[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}
int main() {
  int i, left = 0, right = 1000000000, mid;
  cin >> n >> k;
  for (i = 0; i < n; i++)
    // cin>>w[i];
    scanf("%d", &w[i]);
  while (right - left > 1) {
    mid = (right + left) / 2;
    if (work(mid) < n)
      left = mid;
    else
      right = mid;
  }
  cout << right << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
long long w[10000];
long long n, k;
long long work(long long p) {
  long long sum, i = 0, j = k;
  while (j--) {
    sum = 0;
    while (sum + w[i] <= p) {
      sum += w[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}
int main() {
  long long i, left = 0, right = 1000000000, mid;
  cin >> n >> k;
  for (i = 0; i < n; i++)
    cin >> w[i];
  // scanf("%d",&w[i]);
  while (right - left > 1) {
    mid = (right + left) / 2;
    if (work(mid) < n)
      left = mid;
    else
      right = mid;
  }
  cout << right << endl;
  return 0;
}