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

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

using namespace std;

namespace shamal {
template <typename F, typename IndexType, typename TargetType>
IndexType lower_bound(F const &func, IndexType left, IndexType right,
                      TargetType const &t) {
  while (true) {
    auto const mid = left + (right - left) / 2;
    auto const val = func(mid);
    if (val > t) {
      right = mid - 1;
    } else if (val < t) {
      left = mid + 1;
    } else {
      right = mid;
    }
    if (left == right)
      break;
  }
  return left;
}
}; // namespace shamal

int search(vector<int> const &w, int n, int k, int const p) {
  int offset = 0;
  int i;
  for (i = 1; i <= n; i++) {
    if (w[i] - offset > p && k > 0) {
      offset = w[i - 1];
      --k;
      --i;
    }
  }

  return (k >= 1) && ((i - 1) == n);
}

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<int> w(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    int temp;
    cin >> temp;
    w[i] = w[i - 1] + temp;
  }
  int l = 1, r = w[n];
  cout << shamal::lower_bound([&](int p) { return search(w, n, k, p); }, 1,
                              w[n], 1)
       << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

using namespace std;

template <typename T> struct hoge;

namespace shamal {
template <typename F, typename IndexType, typename TargetType>
IndexType lower_bound(F const &func, IndexType left, IndexType right,
                      TargetType const &t) {
  while (true) {
    auto const mid = left + (right - left) / 2;
    auto const val = func(mid);
    if (val > t) {
      right = mid - 1;
    } else if (val < t) {
      left = mid - 1;
    } else {
      right = mid;
    }
    if (left == right)
      break;
  }
  return left;
}
}; // namespace shamal

int search(vector<int> const &w, int n, int k, int const p) {
  int offset = 0;
  int i;
  for (i = 1; i <= n; i++) {
    if (w[i] - offset > p && k > 0) {
      offset = w[i - 1];
      --k;
      --i;
    }
  }

  return (k >= 1) && ((i - 1) == n);
}

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<int> w(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    int temp;
    cin >> temp;
    w[i] = w[i - 1] + temp;
  }
  int l = 1, r = w[n];
  auto a = shamal::lower_bound([&](int p) { return search(w, n, k, p); }, 1,
                               w[n], 1);
  cout << a << endl;
  /*
  while(true){
          if(search(w, n, k, (r-l)/2+l) == 1)
                  r = r - (r-l)/2;
          else
                  l = (r-l)/2 + l;
          if(abs(r-l) <= 1)
                  break;
  }
  if(search(w, n, k, 1) == 1)
          cout << l << endl;
  else
          cout << r << endl;
          */
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <stdio.h>
using namespace std;

bool loadable(int P, int n, int k, int w[]) {
  int num_tracks = 1, current_w = 0;

  for (int i = 0; i < n; i++) {
    if (current_w + w[i] > P) {
      current_w = 0;
      num_tracks++;
    }

    if (num_tracks > k)
      return false;

    current_w += w[i];
  }

  return true;
}

int main() {
  int P, P_min, P_max;
  int n, k;
  int w[100000];

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

  P_min = 1;
  int sum = 0;

  for (int i = 0; i < n; i++) {
    scanf("%d", w + i);
    sum += w[i];
    if (P_min < w[i])
      P_min = w[i];
  }

  P_max = sum - k + 1;

  while (P_max > P_min) {
    P = sqrt((long double)P_max * (long double)P_min);
    if (loadable(P, n, k, w)) {
      P_max = P;
    } else {
      P_min = P + 1; // P doesn't satisfy the condition, so P_min with
                     // satisfaction >= (P + 1)
    }
  }

  printf("%d\\n", P_max);
}

Error: TLE

Fixed submission:

#include <cmath>
#include <stdio.h>
using namespace std;

bool loadable(int P, int n, int k, int w[]) {
  int num_tracks = 1, current_w = 0;

  for (int i = 0; i < n; i++) {
    if (current_w + w[i] > P) {
      current_w = 0;
      num_tracks++;
    }

    if (num_tracks > k)
      return false;

    current_w += w[i];
  }

  return true;
}

int main() {
  int P, P_min, P_max;
  int n, k;
  int w[100000];

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

  P_min = 1;
  int sum = 0;

  for (int i = 0; i < n; i++) {
    scanf("%d", w + i);
    sum += w[i];
    if (P_min < w[i])
      P_min = w[i];
  }

  P_max = sum - k + 1;

  while (P_max > P_min) {
    P = sqrt(P_max * P_min);
    if (loadable(P, n, k, w)) {
      P_max = P;
    } else {
      P_min = P + 1; // P doesn't satisfy the condition, so P_min with
                     // satisfaction >= (P + 1)
    }
  }

  printf("%d\\n", P_max);
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

// #include <cstdio>

using namespace std;

static int inputArray(int A[], int num) {
  int max = 0;
  for (int i = 0; i < num; i++) {
    cin >> A[i];
    if (A[i] > max) {
      max = A[i];
    }
  }
  return max;
}

static inline int f(int A[], int num, uint64_t P, int k) {
  int i = 0, v = 0;
  for (uint64_t total = 0; (v < k) && (i < num); i++) {
    total += A[i];
    if (total > P) {
      total = 0;
      i--;
      v++;
    }
  }
  return i;
}

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

  vector<int> w(n);
  uint64_t P = inputArray(&w[0], n);

  uint64_t left = 0;
  uint64_t mid;
  uint64_t right = 100000 * 10000;
  int v = 0;
  while (1 < right - left) {
    mid = (left + right) / 2;
    v = f(&w[0], n, mid, k);
    if (v >= n) {
      right = mid;
    } else {
      left = mid;
    }
  }

  cout << right << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

#include <cstdio>

using namespace std;

static int inputArray(int A[], int num) {
  int max = 0;
  for (int i = 0; i < num; i++) {
    cin >> A[i];
    if (A[i] > max) {
      max = A[i];
    }
  }
  return max;
}

static inline int f(int A[], int num, int P, int k) {
  int i = 0, v = 0;
  for (int total = 0; (v < k) && (i < num); i++) {
    total += A[i];
    //      printf("P = %d, A[%d] = %d, total = %d, v = %d\\n", P, i, A[i],
    //      total, v);
    if (total > P) {
      total = 0;
      i--;
      v++;
    }
  }
  return i;
}

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

  vector<int> w(n);
  int P = inputArray(&w[0], n);

  //  int P = n;
  for (int v = 0; v < n; P++) {
    v = f(&w[0], n, P, k);
  }

  cout << (P - 1) << endl;

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

#include <iostream>

using namespace std;
int n, k, b[100001];
int Max = 0;
int sum = 0;

bool judge(int P) {
  if (P < Max) {
    return false;
  }
  int tr = 1;
  int rest = P;
  for (int i = 0; i < n; i++) {
    if (rest - b[i] >= 0) {
      rest = rest - b[i];
    } else {
      tr++;
      rest = P;
      rest = rest - b[i];
    }
  }
  if (tr <= k) {
    return true;
  } else {
    return false;
  }
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (b[i] > Max) {
      Max = b[i];
    }
    sum += b[i];
  }
  int Left = Max;
  int Right = sum;
  while (true) {
    int center = (Left + Right) / 2;
    if (judge(center)) {
      Right = center;
    } else {
      Left = center + 1;
    }
    if (Left >= Right) {
      cout << Left << endl;
      break;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;
int n, k, b[100001];
int Max = 0;

bool judge(int P) {
  if (P < Max) {
    return false;
  }
  int tr = 1;
  int rest = P;
  for (int i = 0; i < n; i++) {
    if (rest - b[i] >= 0) {
      rest = rest - b[i];
    } else {
      tr++;
      rest = P;
      rest = rest - b[i];
    }
  }
  if (tr <= k) {
    return true;
  } else {
    return false;
  }
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (b[i] > Max) {
      Max = b[i];
    }
  }
  int P = Max;
  int range = 10;
  while (true) {
    if (judge(P)) {
      range--;
      P -= range;
    } else {
      P += range;
    }
    if (range == 1 && judge(P)) {
      cout << P << endl;
      break;
    }
  }
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, k, w[100000], max = 1, sum = 0;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    if (max < w[i])
      max = w[i];
    sum += w[i];
  }
  int ave = sum / k;
  int t;
  // tは平均値と要素の最大値のうちの大きいほう
  if (ave < max)
    t = max;
  else
    t = ave;
  int P = t;
  while (1) {
    // Pは許容量、numは必要なトラック数
    int s = 0, num = 1;
    for (int j = 0; j < n; j++) {
      s += w[j];
      // sが許容量を超えたらnumを増やし、sの初期値を積めなかった荷物にする
      if (s > P) {
        num++;
        s = w[j];
      }
    }
    // もしトラックの数が足りたら
    if (num <= k) {
      cout << P << endl;
      break;
    }
    // 足りなければ
    else {
      P++;
    }
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, k, w[100000], max = 1, sum = 0;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    if (max < w[i])
      max = w[i];
    sum += w[i];
  }
  int ave = sum / n;
  int t;
  // tは平均値と要素の最大値のうちの大きいほう
  if (ave < max)
    t = max;
  else
    t = ave;
  int P = t;
  while (1) {
    // Pは許容量、numは必要なトラック数
    int s = 0, num = 1;
    for (int j = 0; j < n; j++) {
      s += w[j];
      // sが許容量を超えたらnumを増やし、sの初期値を積めなかった荷物にする
      if (s > P) {
        num++;
        s = w[j];
      }
    }
    // もしトラックの数が足りたら
    if (num <= k) {
      cout << P << endl;
      break;
    }
    // 足りなければ
    else {
      P++;
    }
  }
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define NMAX = 100000;
#define KMAW = 100000;
#define WMAX = 10000;
using namespace std;

bool can_load(vector<int> w_sum, int &k, int &p) {
  int loaded = 0;
  auto it = w_sum.begin();
  for (int i = 0; i < k; i++) {
    it = upper_bound(it, w_sum.end(), p + loaded);
    auto prev = it - 1;
    loaded = *prev;
  }
  return it == w_sum.end();
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> w_sum;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    int tmp;
    scanf("%d", &tmp);
    sum += tmp;
    w_sum.push_back(sum);
  }
  int right = sum;
  int left = sum / k;

  while (left < right) {
    int mid = (right + left) / 2;
    if (can_load(w_sum, k, mid)) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }

  cout << right << endl;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> w;
  vector<int> w_sum;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    int tmp;
    scanf("%d", &tmp);
    sum += tmp;
    w.push_back(tmp);
    w_sum.push_back(sum);
  }
  int p = sum / k;

  while (true) {
    int loaded = 0;
    auto it = w_sum.begin();
    for (int i = 0; i < k; i++) {
      it = upper_bound(it, w_sum.end(), p + loaded);
      auto prev = it - 1;
      loaded = *prev;
    }
    if (it == w_sum.end()) {
      break;
    } else {
      p++;
    }
  }
  cout << p << endl;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
int ReturnCap(std::vector<int> Baggage, int P, int k) {
  int v = 0, c = 0, ntrack = 0;
  for (int i = 0; i < Baggage.size(); ++i) {
    if (c > 0 && P > 0 && c + Baggage[i] > P) {
      c = 0;
      ++ntrack;
    }
    if (ntrack == k) {
      break;
    }
    if (c + Baggage[i] <= P) {
      c += Baggage[i];
      ++v;
    }
  }
  return v;
}
int main(void) {
  std::ios::sync_with_stdio(false);
  int n, k, max = 0;
  std::cin >> n >> k;
  std::vector<int> Baggage(n, 0);
  for (int i = 0; i < n; ++i) {
    std::cin >> Baggage[i];
    max += Baggage[i];
  }
  int min = 1, P = (min + max) / 2;
  while (min <= max) {
    if (n <= ReturnCap(Baggage, P, k)) {
      max = P - 1;
    } else {
      min = P + 1;
    }
    P = (min + max) / 2;
  }
  std::cout << P + 1 << std::endl;
  //	system("pause");
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
bool judge(int k, int MaxCapacity, const std::list<int> &Baggage) {
  std::ios::sync_with_stdio(false);
  int NumberTruck = 0;
  std::list<int> CopyBaggage = Baggage;
  int BaggagePerTruck, Max;
  std::list<int>::iterator itr;
  while (!(CopyBaggage.empty())) {
    BaggagePerTruck = 0;
    itr = CopyBaggage.begin();
    Max = CopyBaggage.size() + 1;
    for (int i = 0; i <= Max; i++) {
      if ((CopyBaggage.empty())) {
        break;
      }
      if (BaggagePerTruck + *itr <= MaxCapacity) {
        BaggagePerTruck += *itr;
        itr = CopyBaggage.erase(itr);
      }
      if (BaggagePerTruck == MaxCapacity) {
        break;
      }
    }
    NumberTruck++;
  }
  return k >= NumberTruck ? true : false;
}
int main(void) {
  int n, k, TotalWeight = 0, MaxCapacity;
  std::cin >> n >> k;
  std::list<int> Baggage(n);
  std::list<int>::iterator itr = Baggage.begin();
  for (int i = 0; i < n; ++i) {
    std::cin >> *itr;
    TotalWeight += *itr;
    itr++;
  }
  if (k == 1) {
    std::cout << TotalWeight << "\\n";
    return 0;
  }
  MaxCapacity = static_cast<int>(1. * TotalWeight / k + 0.5);
  while (!(judge(k, MaxCapacity, Baggage))) {
    MaxCapacity++;
  }
  std::cout << MaxCapacity << "\\n";
  //	system("pause");
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

unsigned solve(const std::vector<unsigned> w, const unsigned k) {
  const unsigned limit_weight =
      std::accumulate(w.begin(), w.end(), unsigned(0));
  unsigned max_weight = *std::max_element(w.begin(), w.end());
  if (k == 1)
    return limit_weight;
  if (w.size() <= k)
    return max_weight;

  unsigned step = limit_weight / k;

  auto is_valid_division = [&w, &k](unsigned current_max_weight) {
    size_t i = 0;
    auto rest = k;
    while (rest-- > 0) {
      unsigned sum = 0;
      for (; i < w.size() && sum + w[i] <= current_max_weight; ++i) {
        sum += w[i];
      }
    }
    return (i == w.size());
  };

FORWARD:
  for (; !is_valid_division(max_weight); max_weight += step)
    ;

  if (is_valid_division(max_weight - 1) || !is_valid_division(max_weight + 1)) {
    step = (step / 10 != 0 ? step / 10 : 1);
    for (; is_valid_division(max_weight); max_weight -= step)
      ;
    if (step != 1) {
      step = (step / 10 != 0 ? step / 10 : 1);
      goto FORWARD;
    } else {
      max_weight++;
    }
  }

  return max_weight;
}

int main() {
  std::ios::sync_with_stdio(false);
  {
    std::vector<unsigned> w;
    unsigned k;
    {
      unsigned n;
      std::cin >> n >> k;
      w.reserve(n);

      {
        unsigned buf;
        for (unsigned i = 0; i < n; ++i) {
          std::cin >> buf;
          w.push_back(buf);
        }
      }
    }
    std::cout << solve(w, k) << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

unsigned solve(const std::vector<unsigned> w, const unsigned k) {
  const unsigned limit_weight =
      std::accumulate(w.begin(), w.end(), unsigned(0));
  unsigned max_weight = *std::max_element(w.begin(), w.end());
  if (k == 1)
    return limit_weight;
  if (w.size() <= k)
    return max_weight;

  for (size_t i = 0; i != w.size(); ++max_weight) {
    i = 0;
    auto rest = k;
    while (rest-- > 0) {
      unsigned sum = 0;
      for (; i < w.size() && sum + w[i] <= max_weight; ++i) {
        sum += w[i];
      }
    }
  }

  return max_weight - 1;
}

int main() {
  std::ios::sync_with_stdio(false);
  {
    std::vector<unsigned> w;
    unsigned k;
    {
      unsigned n;
      std::cin >> n >> k;
      w.reserve(n);

      {
        unsigned buf;
        for (unsigned i = 0; i < n; ++i) {
          std::cin >> buf;
          w.push_back(buf);
        }
      }
    }
    std::cout << solve(w, k) << std::endl;
  }
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

int main() {
  int i;
  int n, k;
  long long T[100000];
  long long left = 0;
  long long right = 100000 * 10000;
  long long mid;
  int v;

  cin >> n;
  cin >> k;
  for (i = 0; i <= n - 1; i++)
    cin >> T[i];
  while (right - left > 1) {
    mid = (right + left) / 2;
    v = P(mid, T, n, k);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }
  cout << right << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

int main() {
  int i;
  int n, k;
  long long T[100000];
  long long left = 0;
  long long right = 100000 * 10000;
  long long mid;
  int v;

  cin >> n;
  cin >> k;
  for (i = 0; i <= n - 1; i++)
    cin >> T[i];
  while (right - left > 1) {
    mid = (right - left) / 2;
    v = P(mid, T, n, k);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }
  cout << right << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

int w[1000001];
int n, k;

bool check(ll p) {
  ll cost = 0;
  ll cou = 0;
  for (int i = 0; i < k; i++) {
    cost = w[cou];
    while (p - cost >= 0) {
      cou++;
      if (cou == n) {
        return true;
      }
      cost += w[cou];
    }
  }
  return false;
}

ll solve() {
  ll l = 0, r = 1000000000LL;
  while (r - l > 1) {
    ll mid = (r + l) / 2;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid;
    }
    //        cout << r << endl;
  }

  return r;
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
  }
  //    for(int i=0;i<n;i++)cout << w[i] << (i!=n-1?" ":"\\n");
  ll ans = solve();
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

typedef long long ll;

int w[10000];
int n, k;

bool check(ll p) {
  //    cout << "p : " << p << endl;
  ll cost = 0;
  ll cou = 0;
  for (int i = 0; i < k; i++) {
    cost = w[cou];
    while (p - cost >= 0) {
      //            cout << "p:cou "<<p<<":"<<cou<<endl;
      cou++;
      if (cou == n) {
        return true;
      }
      cost += w[cou];
    }
  }
  return false;
}

ll solve() {
  ll l = 0, r = 1000000000LL;
  while (r - l > 1) {
    ll mid = (r + l) / 2;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid;
    }
    //        cout << r << endl;
  }

  return r;
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
  }
  //    for(int i=0;i<n;i++)cout << w[i] << (i!=n-1?" ":"\\n");
  ll ans = solve();
  cout << ans << endl;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <vector>

using namespace std;

static int f(const vector<int> &ws, int P, int k) {
  int v = 0;
  int c = 0;
  int ntrack = 0;
  for (size_t i = 0; i < ws.size(); ++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 max = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &ws[i]);
    max += ws[i];
  }

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

  printf("%d\\n", P + 1);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <vector>

using namespace std;

static int f(const vector<int> &ws, int P, int k) {
  int v = 0;
  int c = 0;
  int ntrack = 0;
  for (size_t i = 0; i < ws.size(); ++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 max = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &ws[i]);
    max += ws[i];
  }

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

  printf("%d\\n", P);
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int search(vector<int> const &w, int n, int k, int const p) {
  int offset = 0;
  int i;
  for (i = 1; i <= n; i++) {
    if (w[i] - offset > p && k > 0) {
      offset = w[i - 1];
      --k;
      --i;
    }
  }

  return (k >= 1) && ((i - 1) == n);
}

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<int> w(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    int temp;
    cin >> temp;
    w[i] = w[i - 1] + temp;
  }
  int l = 1, r = w[n];
  while (true) {
    if (search(w, n, k, (r - l) / 2 + l) == 1)
      r = r - (r - l) / 2;
    else
      l = (r - l) / 2 + l;
    if (abs(r - l) <= 1)
      break;
  }
  if (search(w, n, k, 1) == 1)
    cout << l << endl;
  else
    cout << r << endl;
}

Error: TLE

Fixed submission:

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

using namespace std;

bool search(vector<int> const &w, int n, int k, int const p) {
  int offset = 0;
  int i;
  for (i = 1; i <= n; i++) {
    if (w[i] - offset > p && k > 0) {
      offset = w[i - 1];
      --k;
      --i;
    }
  }

  return (k == 1) && ((i - 1) == n);
}

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<int> w(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    int temp;
    cin >> temp;
    w[i] = w[i - 1] + temp;
  }
  int p = 1;
  while (!search(w, n, k, p++))
    ;
  cout << p - 1 << endl;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
using namespace std;

int w[100000], n, k;
int cntp(int p) {
  int cnt = 1, wgh = 0;
  for (int i = 0; i < n;) {
    if (wgh + w[i] <= p) {
      wgh += w[i++];
    } else {
      if (wgh > 0)
        cnt++;
      else
        return k + 1;
      wgh = 0;
    }
  }
  return cnt;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; scanf("%d", w + i++))
    ;
  int l = 0, r = 1000000000, m;
  while (l < r) {
    m = (l + r) / 2; // printf("%d %d %d %d\\n",l,m,r,cntp(m));
    if (cntp(m) > k)
      l = m + 1;
    else
      r = m;
  }
  for (m -= 20;; m++) {
    // printf("%d %d\\n",m,cntp(m));
    if (cntp(m) <= k)
      break;
  }
  printf("%d\\n", m);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
using namespace std;

int w[100000], n, k;
int cntp(int p) {
  int cnt = 1, wgh = 0;
  for (int i = 0; i < n;) {
    if (wgh + w[i] <= p) {
      wgh += w[i++];
    } else {
      if (wgh > 0)
        cnt++;
      else
        return k + 1;
      wgh = 0;
    }
  }
  return cnt;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; scanf("%d", w + i++))
    ;
  int l = 0, r = 100000, m;
  while (l < r) {
    m = (l + r) / 2; // printf("%d %d %d %d\\n",l,m,r,cntp(m));
    if (cntp(m) > k)
      l = m + 1;
    else
      r = m;
  }
  for (m -= 20;; m++) {
    // printf("%d %d\\n",m,cntp(m));
    if (cntp(m) <= k)
      break;
  }
  printf("%d\\n", m);
  return 0;
}