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

#include <climits>
#include <iostream>

const int MAX_N = 100000;
const int MAX_K = 100000;

bool calculateWeight(int n, int k, int w[], int maxWeight) {
  int j = 0;
  int m = 0;

  int sum = 0;

  bool success = false;

  while ((j < n) && (m < k)) {
    if (w[j] > maxWeight) {
      return false;
    }

    if ((sum + w[j]) <= maxWeight) {
      sum += w[j];
      j++;
    } else {
      m++;
      sum = 0;
    }
  }

  if (j == n) {
    success = true;
  }

  return success;
}

int main() {
  std::ios_base::sync_with_stdio(false);

  int n = 0;
  int k = 0;

  int w[MAX_N] = {0};

  std::cin >> n;
  std::cin >> k;

  int minWeight = INT_MAX;
  int maxWeight = 0;

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

    if (w[i] < minWeight) {
      minWeight = w[i];
    }

    maxWeight += w[i];
  }

  int weight = INT_MAX;

  int left = minWeight;
  int right = maxWeight;

  while (left <= right) {
    int middle = (left + right) / 2;

    //    std::cout << left << " " << middle << " " << right << std::endl;

    if (calculateWeight(n, k, w, middle) && (middle < weight)) {
      weight = middle;
      right = middle - 1;
    } else {
      left = middle + 1;
    }
  }

  std::cout << weight << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>

const int MAX_N = 100000;
const int MAX_K = 100000;

int main() {
  std::ios_base::sync_with_stdio(false);

  int n = 0;
  int k = 0;

  int w[MAX_N] = {0};

  std::cin >> n;
  std::cin >> k;

  int minWeight = INT_MAX;
  int maxWeight = 0;

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

    if (w[i] < minWeight) {
      minWeight = w[i];
    }

    maxWeight += w[i];
  }

  int weight = INT_MAX;

  for (int i = minWeight; i <= maxWeight; i++) {
    int j = 0;
    int m = 0;

    int sum = 0;

    while ((j < n) && (m < k)) {
      if (w[j] > i) {
        break;
      }

      if ((sum + w[j]) <= i) {
        sum += w[j];
        j++;
      } else {
        m++;
        sum = 0;
      }
    }

    if ((j == n) && (i < weight)) {
      weight = i;
      break;
    }
  }

  std::cout << weight << std::endl;

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool into(int);
int n, k, a;
vector<int> w;
int main() {
  int l = 0, r = 0, m;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a;
    r += a;
    w.push_back(a);
  }
  r += 1;
  while (l < r) {
    m = (l + r) / 2;
    if (into(m) == true)
      r = m;
    else
      l = m + 1;
  }
  cout << r << endl;
  return 0;
}
bool into(int p) {
  vector<int> b(k, 0);
  int j = 0;
  for (int i = 0; i < n;) {
    if (j == k) {
      return false;
    }
    if (b[j] + w[i] > p) {
      j++;
      continue;
    } else {
      b[j] += w[i];
      i++;
    }
  }
  return true;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, k, a;
  int f;
  vector<int> w;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a;
    w.push_back(a);
  }

  for (int p = 0;; p++) {
    vector<int> b(k, 0);
    int j = 0;
    f = 0;
    for (int i = 0; i < n;) {
      if (j == k) {
        f = 1;
        break;
      }
      if (b[j] + w[i] > p) {
        j++;
        continue;
      } else {
        b[j] += w[i];
        i++;
      }
    }
    if (f == 0) {
      cout << p << endl;
      break;
    }
  }
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define abs(N) ((N > 0) ? N : -1 * N)
#define lt(A, B) ((A > B) ? B : A)
#define gt(A, B) ((A < B) ? B : A)
#define print(S) (cout << S)
#define println(S) (cout << S << endl)
#define ll long long int

bool check(int *w, int n, int k, int P) {
  int cur_k = 1, cur_sum = 0;
  for (int j = 0; j < n; ++j) {
    if (cur_sum + w[j] <= P) {
      cur_sum += w[j];
    } else {
      if (cur_k == k)
        return false;
      cur_k++;
      cur_sum = 0;
      cur_sum += w[j];
    }
  }
  return true;
}

int BinarySearch(int *w, int n, int k, int init_l, int init_r) {
  //	println("=============================");
  int l = init_l, r = init_r, c;
  bool res_l, res_r, res_c;
  res_l = check(w, n, k, l);
  res_r = check(w, n, k, r);
  if (res_l)
    return l;
  while (true) {
    c = (l + r) / 2;
    res_c = check(w, n, k, c);
    //		printf("res[%d]=%d,res[%d]=%d,res[%d]=%d\\n",l,res_l,c,res_c,r,res_r);
    if (res_c) {
      r = c;
    } else {
      l = c;
    }
    if (r - 1 == l) {
      return r;
    }
  }

  return -1;
}

int main() {
  int n, k;
  scanf("%d %d", &n, &k);

  int max_w = -1, sum_w = 0;
  int w[n];
  for (int i = 0; i < n; ++i) {
    scanf("%d", &w[i]);
    max_w = gt(max_w, w[i]);
    sum_w += w[i];
  }
  int ret = BinarySearch(w, n, k, max_w, sum_w);
  println(ret);
  //	for (int P = max_w; P <= sum_w; ++P) {
  //		if(check(w,n,k,P)){
  //			println(P);
  //			return 0;
  //		}
  //	}
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define abs(N) ((N > 0) ? N : -1 * N)
#define lt(A, B) ((A > B) ? B : A)
#define gt(A, B) ((A < B) ? B : A)
#define print(S) (cout << S)
#define println(S) (cout << S << endl)
#define ll long long int

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

  int max_w = -1, sum_w = 0;
  int w[n];
  for (int i = 0; i < n; ++i) {
    cin >> w[i];
    max_w = gt(max_w, w[i]);
    sum_w += w[i];
  }
  //	int min_P = lt(max_w,(sum_w+1)/k);

  for (int P = max_w; P <= sum_w; ++P) {
    //		printf("======= P = %d\\n",P);
    int cur_k = 1, cur_sum = 0;
    for (int j = 0; j < n; ++j) {
      if (cur_sum + w[j] <= P) {
        //				printf("tr[%d] : %d + %d = %d <=
        //%d\\n",cur_k-1,cur_sum,w[j],cur_sum+w[j],P);
        cur_sum += w[j];
      } else {
        //				printf("tr[%d] : %d + %d = %d >
        //%d\\n",cur_k-1,cur_sum,w[j],cur_sum+w[j],P);
        cur_k++;
        cur_sum = 0;
        //				printf("tr[%d] : %d + %d = %d >
        //%d\\n",cur_k-1,cur_sum,w[j],cur_sum+w[j],P);
        cur_sum += w[j];
      }
    }
    if (cur_k <= k) {
      println(P);
      return 0;
    }
  }
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void show(long long arr[], long long size) {
  cout << arr[0] << flush;
  for (int i = 1; i < size; i++) {
    cout << ' ' << arr[i] << endl;
  }
  cout << endl;
}
/////////////////////////////////////////////////////////////////

int check(long long maxcarry, int w[], int w_size, int t) {
  int w_i = 0;
  for (int t_j = 0; t_j < t; t_j++) {
    long long sum = 0;
    for (; sum + w[w_i] <= maxcarry; w_i++) {
      sum += w[w_i];
      if (w_i == w_size) {
        return w_size;
      }
    }
  }
  return w_i;
}

long long solve(int w[], int w_size, int t) {

  long long maxcarry_front = 0, maxcarry_back = 100000 * 10000, maxcarry_center;
  while (maxcarry_back - maxcarry_front > 1) {
    maxcarry_center = (maxcarry_front + maxcarry_back) / 2;
    int canCarry = check(maxcarry_center, w, w_size, t);
    if (canCarry >= w_size) {
      //			printf("?????§?????????(%d
      //)???????????????????????????????????°( %d ) >= ???????????°( %d )\\n",
      //maxcarry_center, canCarry, w_size);

      maxcarry_back = maxcarry_center;
    } else {
      //			printf("?????§?????????(%d
      //)???????????????????????????????????°( %d )??? ???????????°( %d
      //)??????????°???????!\\n", maxcarry_center, canCarry, w_size);

      maxcarry_front = maxcarry_center;
    }
  }
  return maxcarry_back;
}

int main() {
  int w_size, t;
  cin >> w_size >> t;
  int *w = new int[w_size];
  for (int i = 0; i < w_size; i++)
    cin >> w[i];

  cout << solve(w, w_size, t) << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int check(long long maxcarry, int w[], int w_size, int t) {
  int i = 0;
  for (int j = 0; j < t; j++) {
    long long s = 0;
    while (s + w[i] <= maxcarry) {
      s += w[i];
      i++;
      if (i == w_size) {
        return w_size;
      }
    }
  }
  return i;
}

long long solve(int w[], int w_size, int t) {

  long long front = 0, back = 100000 * 10000, center;
  while (back - front >= 1) {
    center = (front + back) / 2;
    int v = check(center, w, w_size, t);
    if (v >= w_size) {
      back = center;
    } else {
      front = center;
    }
  }
  return back;
}

int main() {
  int w_size, t;
  cin >> w_size >> t;
  int *w = new int[w_size];
  for (int i = 0; i < w_size; i++)
    cin >> w[i];

  cout << solve(w, w_size, t) << endl;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;

int main() {
  int n, k;
  int total = 0;
  vector<int> w;
  cin >> n >> k;
  REP(i, n) {
    int x;
    cin >> x;
    w.push_back(x);
    total += x;
  }
  int p = total / k;
  while (1) {
    int num = 0;
    int track[k];
    REP(i, k) {
      track[i] = 0;
      while (num < n && i < k && track[i] + w[num] <= p) {
        track[i] += w[num];
        num++;
      }
    }
    if (num == n)
      break;
    p++;
  }
  cout << p << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;

int main() {
  int n, k;
  int total = 0;
  vector<int> w;
  cin >> n >> k;
  REP(i, n) {
    int x;
    cin >> x;
    w.push_back(x);
    total += x;
  }
  int p = 1;
  while (1) {
    int num = 0;
    int track[k];
    REP(i, k) {
      track[i] = 0;
      while (num < n && i < k && track[i] + w[num] <= p) {
        track[i] += w[num];
        num++;
      }
    }
    if (num == n)
      break;
    p++;
  }
  cout << p << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define MAX 19980328
typedef long long LL;

int n, k;
LL T[MAX];

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

int solve() {
  LL left = 0;
  LL right = 100000 * 100000;
  LL 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];
  LL ans = solve();
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 100000
typedef long long LL;

int n, k;
LL T[MAX];

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

int solve() {
  LL left = 0;
  LL right = 100000 * 100000;
  LL 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];
  LL ans = solve();
  cout << ans << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
long long n, m;
long long a[100000];
bool search(long long v);
int main() {

  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int l, r;
  l = 0;
  r = 100000 * 10000;
  while (r - l > 1) {
    long long mi = (l + r) / 2;
    if (search(mi))
      r = mi;
    else
      l = mi;
  }
  cout << r << endl;
}
bool search(long long v) {
  long long n2 = 0, m2 = 0, b = 0;
  while (n2 < n && m2 < m) {
    if (b + a[n2] <= v) {
      b += a[n2];
      n2++;
    } else {
      b = 0;
      m2++;
    }
  }
  if (n2 == n && m2 < m)
    return true;
  return false;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int n, m;
int a[100000];
bool search(int v);
int main() {

  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int l, r;
  l = 0;
  r = 1 << 24;
  while (r > 0) {
    int mi = (l + r) / 2;
    if (search(mi))
      r = mi;
    else
      l = mi;
  }
  cout << r << endl;
}
bool search(int v) {
  int n2 = 0, m2 = 0, b = 0;
  while (n2 < n && m2 < m) {
    if (b + a[n2] <= v) {
      b += a[n2];
      n2++;
    } else {
      b = 0;
      m2++;
    }
  }
  if (n2 == n && m2 < m)
    return true;
  return false;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

bool loading(int n, int k, int w[], long long maxAmount) {
  int cnt = 1;
  long long amount = 0;
  for (int i = 0; i < n; i++) {
    if (w[i] > maxAmount)
      return false;
    if (amount + w[i] <= maxAmount) {
      amount += w[i];
    } else {
      cnt++;
      amount = w[i];
    }
    if (k < cnt)
      return false;
  }
  return true;
}

int main() {
  int n;
  int k;
  int w[100000];
  long long min, max, mid;

  long long maxAmount = 1000000000;

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

  min = 0;
  max = maxAmount;
  while (min < max) {
    mid = (min + max) / 2;
    if (loading(n, k, w, mid)) {
      maxAmount = mid;
      max = mid;
    } else {
      min = mid + 1;
    }
  }

  cout << maxAmount << endl;
  return 0;
}

Error: 0

Fixed submission:

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

bool loading(int n, int k, int w[], long long maxAmount) {
  int cnt = 1;
  long long amount = 0;
  for (int i = 0; i < n; i++) {
    if (w[i] > maxAmount)
      return false;
    if (amount + w[i] <= maxAmount) {
      amount += w[i];
    } else {
      cnt++;
      amount = w[i];
    }
    if (k < cnt)
      return false;
  }
  return true;
}

int main() {
  int n;
  int k;
  int w[10000];

  long long maxAmount = 1000000000;

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

  long long min, max, mid;
  min = 0;
  max = maxAmount;
  while (min < max) {
    mid = (min + max) / 2;
    if (loading(n, k, w, mid)) {
      maxAmount = maxAmount < mid ? maxAmount : mid;
      max = mid;
    } else {
      min = mid + 1;
    }
  }

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

// Allocation
#include <iostream>

using namespace std;

const int MAX_N = 100000;
const int MAX_Wi = 10000;

typedef long long llong;

int n, k;
llong T[MAX_N];

// 最大積載量 P の k 台のトラックで何個の荷物を積めるかを調べる関数
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;
}

// 2分探索を用いて、検索を実施する関数
int solve() {
  llong left = 0;
  // ( 荷物の個数　) * ( 1個あたりの最大重量 )
  llong right = MAX_N * MAX_Wi;
  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(void) {
  cin >> n >> k;

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

  llong ans = solve();

  cout << ans << '\\n';
}


Error: TLE

Fixed submission:

// Allocation
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>

using namespace std;

#define MAX_N 100000
#define MAX_K 100000

int main(void) {
  int n, k, kk, sum = 0, truck_p = 0, max_p = 0;
  int wi[MAX_N];

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

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

    // 荷物の総重量を求める
    sum += wi[i];
    // 荷物の最大重量を求める
    max_p = max(max_p, wi[i]);
  }

  // 1台のトラックあたりに載せられる平均重量を求める
  if (k > 1) {
    truck_p = (sum / k) + 1;

    if (truck_p < max_p) {
      // 1つの荷物が極端に重いことを考慮
      truck_p = max_p;
    }
  } else {
    // トラックが1台の時
    truck_p = sum;
  }

  while (1) {
    sum = 0;
    kk = 0;

    for (int i = 0; i < n; i++) {
      if (sum + wi[i] <= truck_p) {
        // トラックに載せられる時は、重量として加算する
        sum += wi[i];
      } else {
        // 次のトラックに載せ替える時
        kk++;
        sum = wi[i];
      }
    }

    if ((sum <= truck_p) && kk == k - 1) {
      // 最後のトラックまで荷物を載せて、トラックも規定重量以内の時
      break;
    } else {
      // 題意の条件を満たしていない時は、重量を増やして再検索する
      truck_p++;
    }
  }

  printf("%d\\n", truck_p);

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

#include "iostream"

using namespace std;

int nes(int n, int w[10000], int m) {
  int ne = 1;
  int x = 0;

  for (int i = 0; i < n; i++) {
    if (x + w[i] <= m)
      x += w[i];
    else {
      ne++;
      x = w[i];
    }
  }
  return ne;
}

int main() {

  int n, k;
  int w[100000], max;

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

  int head, mid, tail;

  head = max;
  tail = max * n / k + 1;
  mid = (head + tail) / 2;
  int ne;
  while (tail - head > 0) {

    mid = (head + tail) / 2;
    ne = nes(n, w, mid);
    if (ne <= k)
      tail = mid;
    else
      head = mid + 1;

  } /*
   do {
           mid--;
           ne = nes(n, w, mid);
   } while (ne <= k && mid >= max);
   */
  cout << tail << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include "iostream"

using namespace std;

int nes(int n, int w[10000], int m) {
  int ne = 1;
  int x = 0;

  for (int i = 0; i < n; i++) {
    if (x + w[i] <= m)
      x += w[i];
    else {
      ne++;
      x = w[i];
    }
  }
  return ne;
}

int main() {

  int n, k;
  int w[100000], max;

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

  int head, mid, tail;

  head = max;
  tail = max * n / k + 1;
  mid = (head + tail) / 2;
  int ne;
  while (head != tail) {

    ne = nes(n, w, mid);
    if (ne == k)
      tail = mid + 1;
    else if (ne < k)
      tail = mid;
    else
      head = mid + 1;

    mid = (head + tail) / 2;
  } /*
   do {
           mid--;
           ne = nes(n, w, mid);
   } while (ne <= k && mid >= max);
   */
  cout << mid << endl;

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

#include <iostream>
using namespace std;
#define MAX 100000
typedef long long ll;

int n, k;
ll T[MAX];
int check(ll p) {
  int i = 0;
  for (int j = 0; j < k; ++j) {
    ll s = 0;
    while (s + T[i] <= p) {
      s += T[i];
      i++;
      if (i == n)
        return i;
    }
  }
  return i;
}
int slove() {
  ll left = 0;
  ll right = 100000 * 10000;
  while (left + 1 < right) {
    ll mid = left + (right - left) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main(int argc, char const *argv[]) {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> T[i];
  }
  ll ans = slove();
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 100000
typedef long long ll;

int n, k;
ll T[MAX];
int check(ll p) {
  int i = 0;
  for (int j = 0; j < k; ++j) {
    ll s = 0;
    while (s + T[i] <= p) {
      s += T[i];
      i++;
      if (i == n)
        return i;
    }
  }
  return i;
}
int slove() {
  ll left = 0;
  ll right = 100000 * 10000;
  while (left < right) {
    ll mid = left + (right - left) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main(int argc, char const *argv[]) {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> T[i];
  }
  ll ans = slove();
  cout << ans << endl;
  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define MAX 100000
using namespace std;

long T[MAX];
int n, k;

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

int main(int argc, char const *argv[]) {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> T[i];
  }
  long long left = 0;
  long long right = 10000 * 100000;
  long long mid;
  while (right - left > 1) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n) {
      right = mid;
    } else {
      left = mid;
    }
  }

  cout << right << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define MAX 100000

long long T[MAX];
int n, k;

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

int solve() {
  long long left = 0;
  long long right = 10000 * 100000;
  long long 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(int argc, char const *argv[]) {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> T[i];
  }

  cout << solve() << endl;

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

// ポイント:P > max,
// はじめからかこえるだけかこっていくというやり方が理想の切り方
#include <cmath>
#include <cstdio>
#include <iostream>
#define MAX_LUGGAGE 100000 // 最大の荷物の数
#define MAX_truck 100000   // 最大のトラックの数
#define MAX_WEIGHT 10000   // 荷物の最大重量
#define ZERO 0

using namespace std;

int l_num;                          // number of luggage
int t_num;                          // num of truck
int luggage_array[MAX_LUGGAGE + 1]; // 荷物の重量を保管する配列
int luggage_set[MAX_LUGGAGE + 1];

int main() {
  int max = ZERO; // 最大重量の荷物の重量
  int tmp_sum = 0;
  int P;
  int sum = 0;
  scanf("%d", &l_num);
  scanf("%d", &t_num);

  for (int i = 0; i < l_num; i++)
    scanf("%d", &luggage_array[i]);
  for (int i = 0; i < l_num; i++) {
    if (luggage_array[i] > max)
      max = luggage_array[i];
    sum += luggage_array[i];
  }

  if (sum / t_num > max)
    P = sum / t_num;
  else
    P = max;
  for (;; P++) {
    int t_tmp = 1;
    for (int i = 0; i < l_num; i++) {
      tmp_sum += luggage_array[i];
      if (tmp_sum > P) {
        t_tmp++;
        tmp_sum = luggage_array[i];
      }
    }
    tmp_sum = 0;
    if (t_tmp <= t_num)
      break;
  }

  cout << P << endl;
  return 0;
}

Error: TLE

Fixed submission:

// ポイント:P > max,
// はじめからかこえるだけかこっていくというやり方が理想の切り方
#include <cstdio>
#include <iostream>
#define MAX_LUGGAGE 100000 // 最大の荷物の数
#define MAX_truck 100000   // 最大のトラックの数
#define MAX_WEIGHT 10000   // 荷物の最大重量
#define ZERO 0

using namespace std;

int l_num;                          // number of luggage
int t_num;                          // num of truck
int luggage_array[MAX_LUGGAGE + 1]; // 荷物の重量を保管する配列
int luggage_set[MAX_LUGGAGE + 1];

int main() {
  int max = ZERO; // 最大重量の荷物の重量
  int tmp_sum = 0;
  int P;
  scanf("%d", &l_num);
  scanf("%d", &t_num);

  for (int i = 0; i < l_num; i++)
    scanf("%d", &luggage_array[i]);
  for (int i = 0; i < l_num; i++) {
    if (luggage_array[i] > max)
      max = luggage_array[i];
  }

  for (P = max;; P++) {
    int t_tmp = 1;
    for (int i = 0; i < l_num; i++) {
      tmp_sum += luggage_array[i];
      if (tmp_sum > P) {
        t_tmp++;
        tmp_sum = luggage_array[i];
      }
    }
    tmp_sum = 0;
    if (t_tmp <= t_num)
      break;
  }

  cout << P << endl;
  return 0;
}