alexjercan/bugnet huggingface dataset:
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int n, k;
ll A[100010];

bool f(ll p) {
  ll temp = p;
  int x = 0;
  for (int i = 0; i < k; i++) {
    while (temp >= A[x]) {
      temp -= A[x];
      x++;
    }
    temp = p;
  }
  if (x >= n)
    return true;
  else
    return false;
}

ll binary_find(ll left, ll right) {
  ll mid;
  while (left + 1 < right) {
    mid = (left + right) / 2;
    if (f(mid))
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  ll p, sum;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    sum += A[i];
  }
  p = binary_find(0, sum);
  cout << p << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int n, k;
int A[100010];

bool f(ll p) {
  ll temp = p;
  int x = 0;
  for (int i = 0; i < k; i++) {
    while (temp >= A[x]) {
      temp -= A[x];
      x++;
    }
    temp = p;
  }
  if (x >= n)
    return true;
  else
    return false;
}

ll binary_find(ll left, ll right) {
  ll mid;
  while (left + 1 < right) {
    mid = (left + right) / 2;
    if (f(mid))
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  ll p, sum;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    sum += A[i];
  }
  p = binary_find(0, sum);
  cout << p << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#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: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int n, k;
vector<ll> A;

bool f(ll p) {
  ll temp = p;
  int x = 0;
  for (int i = 0; i < k; i++) {
    while (temp >= A[x]) {
      temp -= A[x];
      x++;
    }
    temp = p;
  }
  if (x >= n)
    return true;
  else
    return false;
}

ll binary_find(ll left, ll right) {
  ll mid;
  while (left + 1 < right) {
    mid = (left + right) / 2;
    if (f(mid))
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  ll w;
  ll p, sum;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> w;
    sum += w;
    A.push_back(w);
  }
  p = binary_find(0, sum);
  cout << p << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

#define MAX_NUM_OF_PACKS 100000
#define MAX_WEIGHT_OF_A_PACK 10000
#define MAX_TOTAL_WEIGHT MAX_NUM_OF_PACKS *MAX_WEIGHT_OF_A_PACK

int n, k, W[MAX_NUM_OF_PACKS];

bool valid(int p, int ck, int ci) {
  if (ci >= n)
    return true;
  if (ck >= k || p < W[ci])
    return false;

  int tp = p;
  while (tp >= W[ci] && ci < n) {
    tp -= W[ci];
    ci++;
  }

  return valid(p, ck + 1, ci);
}

int minimum(int left, int right) {
  int mid = (left + right) / 2;
  bool result = valid(mid, 0, 0);
  if (mid == left && result)
    return mid;
  if (result)
    return minimum(left, mid);
  else
    return minimum(mid + 1, right);
}

int main() {

  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", &W[i]);

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


Error: -11

Fixed submission:

#include <stdio.h>

int n, k, W[1000];

int valid(int p, int ck, int ci) {
  if (ci >= n)
    return 1;
  if (ck >= k || p < W[ci])
    return 0;

  int tp = p;
  while (tp >= W[ci]) {
    tp -= W[ci];
    ci++;
  }

  return valid(p, ck + 1, ci);
}

int minimum(int left, int right) {
  int mid = (left + right) / 2;
  if (mid == left || mid == right)
    return mid;
  if (valid(mid, 0, 0))
    return minimum(left, mid);
  else
    return minimum(mid + 1, right);
}

int main() {

  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", &W[i]);

  printf("%d\\n", minimum(0, 100));
  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;
}

llong 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 <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;
}

llong solve() {
  int i = 1;
  while (true) {
    if (check(i) == n)
      return i;
    i++;
  }
}

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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;

int m[100005];
int isOk(int n, int p, int cap) {
  int j = 0;
  for (int i = 0; i < p; i++) {
    int temp = cap;
    while (m[j] <= temp && j < n) {
      temp -= m[j];
      j++;
    }
  }
  return j == n ? 1 : 0;
}

int main() {
  int n, p, Max1 = 0;
  cin >> n >> p;
  for (int i = 0; i < n; i++) {
    scanf("%d", m + i);
    Max1 += m[i];
  }
  int l = 0, r = Max1, mid;
  while (l + 1 < r) {
    mid = (l + r) / 2;
    if (isOk(n, p, mid))
      r = mid;
    else
      l = mid;
  }
  printf("%d\\n", r);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;

int m[100005];

int isOk(int n, int p, int cap) {
  int i = 0, j = n - 1, cnt = 0;
  while (i < j) {
    if (i != j) {
      if (m[i] + m[j] <= cap)
        i++;
      j--;
      cnt++;
    } else if (i == j) {
      cnt++;
      break;
    }
  }
  return cnt <= p ? 1 : 0;
}

int main() {
  int n, p, Max1 = 0, Max2 = 0;
  cin >> n >> p;
  for (int i = 0; i < n; i++) {
    scanf("%d", m + i);
    Max1 += m[i];
    Max2 = max(Max2, m[i]);
  }
  sort(m, m + n);
  int l = Max2, r = Max1, mid, ans;
  while (l < r) {
    mid = (l + r) / 2;
    if (isOk(n, p, mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid;
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool judge(vector<int> &w, int k, int p) {
  vector<int> track(k, 0);
  int j = 0;

  for (int i = 0; i < w.size(); ++i) {
    if (w[i] > p)
      return false;
    if (track[j] + w[i] <= p) {
      track[j] += w[i];
    } else {
      ++j;
      if (j >= k) {
        return false;
      } else {
        track[j] = w[i];
      }
    }
  }
  return true;
}

int main() {
  int n, k, min, max, sum, mid;
  vector<int> w;

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

  min = sum / k;
  max = sum;

  while (max - min > 1) {
    mid = ceil(0.5 * (min + max));

    if (judge(w, k, mid)) {
      max = mid;
    } else {
      min = mid;
    }
  }

  if (judge(w, k, min))
    cout << min << endl;
  else
    cout << max << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

bool judge(vector<int> &w, int k, int p) {
  vector<int> track(k, 0);
  int j = 0;

  for (int i = 0; i < w.size(); ++i) {
    if (w[i] > p)
      return false;
    if (track[j] + w[i] <= p) {
      track[j] += w[i];
    } else {
      ++j;
      if (j >= k) {
        return false;
      } else {
        track[j] = w[i];
      }
    }
  }
  return true;
}

int main() {
  int n, k, min, max, sum, mid;
  vector<int> w;

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

  min = sum / k;
  max = sum;

  for (int i = 0; i < 10000; ++i) {
    mid = ceil(0.5 * (min + max));

    if (judge(w, k, mid)) {
      max = mid;
    } else {
      min = mid;
    }
  }
  cout << mid << endl;

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

#include <cstdlib>
#include <iostream>
typedef long long ll;
#define MAX 0x6fffffff

int check(ll P); // checking how many goods can be loaded by k trucks whose
                 // largest carrying limits are P
// time complexity O(n)
ll solve(); // use binary search through the interval of input limit to
            // determine min(P)
// time complexity O(logP)
// check(ll P) invoked in solve(), so the total time complexity is O(nlogP)
int n, k;
int T[100005];

using namespace std;
int main() {
  // freopen("stdin.txt", "r", stdin);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> T[i];
  }
  T[n] = MAX;
  ll ans = solve();
  cout << ans << endl;
  // fclose(stdin);
  return 0;
}

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

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


Error: -11

Fixed submission:

#include <iostream>

typedef long long ll;
#define MAX 0x3fffffff

int check(ll P); // checking how many goods can be loaded by k trucks whose
                 // largest carrying limits are P
// time complexity O(n)
ll solve(); // use binary search through the interval of input limit to
            // determine min(P)
// time complexity O(logP)
// check(ll P) invoked in solve(), so the total time complexity is O(nlogP)
int n, k;
int T[100000];

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

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

ll solve() {
  ll left = 0;
  ll right = 100000 * 10000;
  ll mid;
  while (right - left > 1) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n) {
      right = mid;
    } else {
      left = mid;
    }
  }
  return right;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

typedef long long ll;
const ll MAX = 100000;

int check(ll P); // checking how many goods can be loaded by k trucks whose
                 // largest carrying limits are P
// time complexity O(n)
ll solve(); // use binary search through the interval of input limit to
            // determine min(P)
// time complexity O(logP)
// check(ll P) invoked in solve(), so the total time complexity is O(nlogP)
int n, k;
int T[100000];

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

int check(ll P) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    int sum = 0;
    while (i < n && sum + T[i] <= P) {
      sum += T[i];
      i++;
    }
    if (i >= n) {
      return n;
    }
  }
  return i;
}

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


Error: -11

Fixed submission:

#include <iostream>

typedef long long ll;
const ll MAX = 100000;

int check(ll P); // checking how many goods can be loaded by k trucks whose
                 // largest carrying limits are P
// time complexity O(n)
ll solve(); // use binary search through the interval of input limit to
            // determine min(P)
// time complexity O(logP)
// check(ll P) invoked in solve(), so the total time complexity is O(nlogP)
int n, k;
int T[MAX];

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

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

ll solve() {
  ll left = 0;
  ll right = 100000 * 10000;
  ll mid;
  while (right - left > 1) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n) {
      right = mid;
    } else {
      left = mid;
    }
  }
  return right;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#define MAX 100000
using namespace std;
typedef signed int 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 > 1) {
    mid = (left + right) / 2;
    int v = check(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", &T[i]);
  llong ans = solve();
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#define MAX 100000
using namespace std;
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 = MAX * 10000;
  llong mid;
  while (left < right) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }
  return right;
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", &T[i]);
  llong ans = solve();
  printf("%d\\n", ans);
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define len(n) (int)n.length()
#define pb push_back
#define ll long long
#define make_pair mkp
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define dbg(n) cerr << "//" << #n << ":" << n << endl
typedef pair<ll, ll> LLARR;
#define INF 999999999

int w[100003];
int n, k;

ll check(ll p) {
  ll ret = 0;
  ll tmp = 0;
  ll i = 0;
  REP(j, k) {
    ll s = 0;
    while (s + w[i] <= p) {
      s += w[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int main() {
  ios::sync_with_stdio(false);

  cin >> n >> k;
  REP(i, n) { cin >> w[i]; }

  ll ma = 10000 * 100000;
  ll l = 0;
  ll r = ma;
  ll mid = 0;
  while (l + 1 < r) {
    mid = (l + r) / 2;
    if (check(mid) >= n) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;
  return 0;
}

Error: 0
hoge


Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define len(n) (int)n.length()
#define pb push_back
#define ll long long
#define make_pair mkp
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define dbg(n) cerr << "//" << #n << ":" << n << endl
typedef pair<ll, ll> LLARR;
#define INF 999999999

int w[100003];
int n, k;

ll check(ll p) {
  ll ret = 0;
  ll tmp = 0;
  ll i = 0;
  REP(j, k) {
    ll s = 0;
    while (s + w[i] <= p) {
      s += w[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int main() {
  ios::sync_with_stdio(false);

  cin >> n >> k;
  REP(i, n) { cin >> w[i]; }

  ll ma = 10000 * 100000;
  ll l = 0;
  ll r = ma;
  ll mid = 0;
  while (l + 1 < r) {
    mid = (l + r) / 2;
    if (check(mid) >= n) {
      r = mid;
    } else {
      l = mid;
    }
  }
  clog << "hoge" << endl;
  cout << r << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define len(n) (int)n.length()
#define pb push_back
#define ll long long
#define make_pair mkp
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define dbg(n) cerr << "//" << #n << ":" << n << endl
typedef pair<ll, ll> LLARR;
#define INF 999999999

int w[100003];
int n, k;

ll check(ll p) {
  ll ret = 0;
  ll tmp = 0;
  ll i = 0;
  REP(j, k) {
    ll s = 0;
    while (s + w[i] <= p) {
      s += w[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int main() {
  ios::sync_with_stdio(false);

  cin >> n >> k;
  REP(i, n) { cin >> w[i]; }

  ll ma = 10000 * 100000;
  ll l = 0;
  ll r = ma;
  ll mid = 0;
  while (l + 1 < r) {
    mid = (l + r) / 2;
    if (check(mid) >= n) {
      r = mid;
    } else {
      l = mid;
    }
  }

  cout << r << endl;
  return 0;
}

Error: 0
//"kita":kita


Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define len(n) (int)n.length()
#define pb push_back
#define ll long long
#define make_pair mkp
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define dbg(n) cerr << "//" << #n << ":" << n << endl
typedef pair<ll, ll> LLARR;
#define INF 999999999

int w[100003];
int n, k;

ll check(ll p) {
  ll ret = 0;
  ll tmp = 0;
  ll i = 0;
  REP(j, k) {
    ll s = 0;
    while (s + w[i] <= p) {
      s += w[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int main() {
  ios::sync_with_stdio(false);

  cin >> n >> k;
  REP(i, n) { cin >> w[i]; }
  dbg("kita");
  ll ma = 10000 * 100000;
  ll l = 0;
  ll r = ma;
  ll mid = 0;
  while (l + 1 < r) {
    mid = (l + r) / 2;
    if (check(mid) >= n) {
      r = mid;
    } else {
      l = mid;
    }
  }

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

#include <iostream>
#include <vector>
static bool a(std::vector<int> &w, int s, int k) {
  int c = 1, d = 0;
  for (auto &&e : w) {
    if (e > s) {
      return false;
    }
    if (e + d <= s) {
      d += e;
    } else {
      d = e;
      ++c;
    }
  }
  return c <= k;
}
int main() {
  int n, k, m, s = 0;
  std::cin >> n >> k;
  std::vector<int> w;
  while (std::cin >> m) {
    w.push_back(m);
    s += m;
  }
  s /= k;
  int l = s, r = 2 * s;
  for (int m = l + (r - l) / 2; l < r; m = l + (r - l) / 2) {
    if (a(w, m, k)) {
      r = m;
    } else {
      l = m + 1;
    }
  }
  std::cout << r << std::endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
int main() {
  int n, k, w[100001], s = 0;
  std::cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    std::cin >> w[i];
    s += w[i];
  }
  s /= k;
  for (int c = 1, d = 0;; ++s, c = 1, d = 0) {
    for (int i = 0; i < n; ++i) {
      if (w[i] > s) {
        break;
      }
      if (w[i] + d <= s) {
        d += w[i];
      } else {
        d = w[i];
        ++c;
      }
    }
    if (c == k) {
      break;
    }
  }
  std::cout << s << std::endl;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
 * ALDS1_4_D.cpp
 *
 *  Created on: Apr 27, 2018
 *      Author: 13743
 */
#include <iostream>

using namespace std;

int minP = 99999999;
int maxW = 0;
int n, q;
int w[100000];

void bruteForce(int prev, int k, int maxL) {
  if (k == 0 || prev == n - 1) {
    int la = 0;
    for (int i = prev + 1; i < n; i++) {
      la += w[i];
    }
    int mi = (la > maxL) ? la : maxL;
    minP = (mi < minP) ? mi : minP;
    return;
  }
  int sum = 0;
  for (int i = prev + 1; i < n; i++) {
    sum += w[i];
    if (sum > minP)
      return;
    if (i < n - 1 && (sum + w[i + 1] <= maxL)) {
      continue;
    }
    if (i < n - 1 && (sum + w[i + 1] <= maxW)) {
      continue;
    }
    bruteForce(i, k - 1, (sum > maxL) ? sum : maxL);
  }
}

bool greedyPuck(int P) {
  int cnt = 0;
  int lw = 0;
  for (int i = 0; i < n; i++) {
    lw += w[i];
    if (lw == P) {
      lw = 0;
      cnt++;
    } else if (lw > P) {
      lw = 0;
      cnt++;
      i--;
    }
  }
  if (lw > 0)
    cnt++;
  if (cnt <= q)
    return true;
  return false;
}

int main() {
  cin >> n >> q;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    maxW = (w[i] > maxW) ? w[i] : maxW;
    sum += w[i];
  }

  //	bruteForce(-1, q-1, 0);
  int l = maxW;
  int r = sum + 1;
  minP = sum;
  while (l < r) {
    int mid = (l + r) / 2;
    if (greedyPuck(mid)) {
      minP = mid;
      r = mid;
    } else {
      l = mid + 1;
    }
  }

  cout << minP << endl;
}


Error: TLE

Fixed submission:

/*
 * ALDS1_4_D.cpp
 *
 *  Created on: Apr 27, 2018
 *      Author: 13743
 */
#include <iostream>

using namespace std;

int minP = 99999999;
int maxW = 0;
int n, q;
int w[100000];

void bruteForce(int prev, int k, int maxL) {
  if (k == 0 || prev == n - 1) {
    int la = 0;
    for (int i = prev + 1; i < n; i++) {
      la += w[i];
    }
    int mi = (la > maxL) ? la : maxL;
    minP = (mi < minP) ? mi : minP;
    return;
  }
  int sum = 0;
  for (int i = prev + 1; i < n; i++) {
    sum += w[i];
    if (sum > minP)
      return;
    if (i < n - 1 && (sum + w[i + 1] <= maxL)) {
      continue;
    }
    if (i < n - 1 && (sum + w[i + 1] <= maxW)) {
      continue;
    }
    bruteForce(i, k - 1, (sum > maxL) ? sum : maxL);
  }
}

int main() {
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    maxW = (w[i] > maxW) ? w[i] : maxW;
  }

  bruteForce(-1, q - 1, 0);
  cout << minP << endl;
}