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

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

typedef long long ll;
typedef pair<int, int> P;

const int INF = 1e9;
const int MOD = 1e9 + 7;

ll a[210000];

bool check(ll N, ll K, ll mid) {
  ll res = 0;
  for (int i = 0; i < N; i++) {
    res += (a[i] - 1) / mid;
  }
  if (res > K)
    return true;
  else
    return false;
}

int main() {
  ll N, K;
  cin >> N >> K;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    ans = max(ans, a[i]);
  }
  if (K == 0) {
    cout << ans << endl;
    return 0;
  }
  ll right = INF;
  ll left = 0;
  for (int i = 0; i < 1000; i++) {
    ll mid = (right + left) / 2;
    if (mid == 0)
      break;
    if (check(N, K, mid)) {
      left = mid;
    } else {
      right = mid;
    }
  }
  cout << right << endl;
  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> P;

const int INF = 1e9;
const int MOD = 1e9 + 7;

ll a[210000];

bool check(ll N, ll K, ll mid) {
  ll res = 0;
  for (int i = 0; i < N; i++) {
    res += (a[i] - 1) / mid;
  }
  if (res > K)
    return true;
  else
    return false;
}

int main() {
  ll N, K;
  cin >> N >> K;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    ans = max(ans, a[i]);
  }
  if (K == 0) {
    cout << ans << endl;
    return 0;
  }
  ll right = INF;
  ll left = 0;
  for (int i = 0; i < 1000; i++) {
    ll mid = (right + left) / 2;
    if (check(N, K, mid)) {
      left = mid;
    } else {
      right = mid;
    }
  }
  cout << right << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie();                                                                   \\
  cout.tie(0)
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 200010;
int a[N], n, k;
bool check(int mid) {
  ll res = 0;
  for (int i = 1; i <= n; i++)
    res += (a[i] + mid - 1) / mid - 1;
  return res <= k;
}
int main() {
  IO;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int l = 1, r = 1e9;
  while (l < r) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}

Error: 0

Fixed submission:

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdlib.h>
#include <vector>
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
const int MAXN = 2e5 + 10;
const int mod = 1e9 + 7;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
#define eps 1e-8
#define PI 3.1415926535898
#define lowbit(x) (x & (-x))
using namespace std;
// getline(cin,s);
// set<ll>::iterator it;
// map<int,int>::iterator it;
typedef long long ll;
int k;
int n;
int a[MAXN];
int check(int x) {
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += (a[i] + x - 1) / x - 1;
  }
  // printf("x: %lld sum:%lld \\n",x,sum);
  if (sum <= k)
    return 1;
  else
    return 0;
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int l = 0, r = 1e9;
  while (r > l) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid;
    } else
      l = mid + 1;
  }
  printf("%d\\n", l);
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

#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(obj) (obj).begin(), (obj).end()
#define INF 1e9
#define LINF 1e18
typedef long long ll;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  REP(i, N) { cin >> A[i]; }
  sort(ALL(A));

  int ng = 0;
  int ok = A[N - 1];
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    ll cnt = 0;
    REP(i, N) { cnt += (A[i] - 1) / mid; }
    if (cnt <= K) { // ここに条件をかく
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;

  return 0;
}

Error: 0

Fixed submission:

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

#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(obj) (obj).begin(), (obj).end()
#define INF 1e9
#define LINF 1e18
typedef long long ll;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  REP(i, N) { cin >> A[i]; }
  sort(ALL(A));

  int ng = -1;
  int ok = A[N - 1];
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    ll cnt = 0;
    REP(i, N) { cnt += (A[i] - 1) / mid; }
    if (cnt <= K) { // ここに条件をかく
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << endl;

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

#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f;
using namespace std;
ll a[200005];
ll n, k;
ll ans = inf;
bool check(ll m) {
  ll tot = 0;
  for (int i = 0; i < n; i++) {
    tot = tot + (a[i] % m == 0 ? (a[i] / m) : (a[i] / m + 1)) - 1;
  }
  if (tot <= k)
    return true;
  else
    return false;
}
ll bound(ll l, ll r) {
  if (l == r || l + 1 == r) {
    if (check(l))
      ans = min(ans, l);
    if (check(r))
      ans = min(ans, r);
    return ans;
  }
  ll mid = (l + r) >> 1;
  if (check(mid)) {
    ans = min(ans, mid);
    return bound(l, mid);
  } else
    return bound(mid + 1, r);
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << bound(1, 1e9) << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f;
using namespace std;
ll a[200005];
ll n, k;
ll ans = inf;
bool check(ll m) {
  ll tot = 0;
  for (int i = 0; i < n; i++) {
    tot = tot + (a[i] % m == 0 ? (a[i] / m) : (a[i] / m + 1)) - 1;
  }
  if (tot <= k)
    return true;
  else
    return false;
}
ll bound(ll l, ll r) {
  if (l == r || l + 1 == r) {
    if (check(l))
      ans = min(ans, l);
    if (check(r))
      ans = min(ans, r);
    return ans;
  }
  ll mid = (l + r) >> 1;
  if (check(mid)) {
    ans = min(ans, mid);
    bound(l, mid);
  } else
    bound(mid + 1, r);
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << bound(1, 1e9) << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;

bool isOK(ll x, ll k, vector<ll> &a) {
  ll count = 0;
  ll n = a.size();
  rep(i, n) count += (ll)ceil((double)a[i] / x) - 1;
  if (count <= k)
    return true;
  else
    return false;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  ll left = 0;
  ll right = 1000000000;

  while (right - left > 1) {
    ll mid = left + (right - left) / 2;
    if (isOK(mid, k, a))
      right = mid;
    else
      left = mid;
  }
  cout << right << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
using P = pair<double, ll>;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n), cut(n);
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  ll csum = 0;
  rep(i, n) {
    cut[i] = k * a[i] / sum;
    csum += cut[i];
  }
  rep(i, k - csum) {
    ll num = 0;
    ll plus;
    rep(j, n) {
      if (a[j] / (cut[j] + 1) > num) {
        num = a[j] / (cut[j] + 1);
        plus = j;
      }
    }
    cut[plus]++;
  }
  double ans = 0.0;
  rep(i, n) { ans = max(ans, (double)a[i] / (cut[i] + 1)); }
  int aa = ans;
  if ((double)aa != ans)
    aa++;
  cout << aa << endl;

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

#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

ll is(vector<ll> &a, ll m, ll k) {
  ll count = 0;
  for (ll i = 0; i < a.size(); i++) {
    count = count + a[i] / m;
    if (a[i] % m == 0)
      count--;
  }

  if (count > k)
    return 0;
  else if (count == k)
    return 1;

  return 2;
}

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

  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  sort(a.begin(), a.end());

  cout.setf(ios::fixed);
  cout << setprecision(0);
  if (!k) {
    cout << a[n - 1] << endl;
    return 0;
  } else if (k == 1) {
    if (n == 1)
      cout << ((ll)round(a[0] / 2)) << endl;
    else
      cout << max((ll)round(a[n - 1] / 2), a[n - 2]) << endl;
  }

  double ans = 0;
  ll l = 1, r = 1000000000;
  while (l <= r) {
    ll m = l + (r - l) / 2;
    ll count = is(a, m, k);
    if (count >= 1) {
      ans = m;
      r = m - 1;
    } else {
      l = m + 1;
    }
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

ll is(vector<ll> &a, ll m, ll k) {
  ll count = 0;
  for (ll i = 0; i < a.size(); i++) {
    count = count + a[i] / m;
    if (a[i] % m == 0)
      count--;
  }

  if (count > k)
    return 0;
  else if (count == k)
    return 1;

  return 2;
}

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

  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  sort(a.begin(), a.end());

  cout.setf(ios::fixed);
  cout << setprecision(0);
  if (!k) {
    cout << a[n - 1] << endl;
    return 0;
  } else if (k == 1) {
    if (n == 1)
      cout << ((ll)round(a[0] / 2)) << endl;
    else
      cout << max((ll)round(a[n - 1] / 2), a[n - 2]) << endl;
  }

  double ans = 0;
  ll l = 0, r = 1000000000;
  while (l <= r) {
    ll m = l + (r - l) / 2;
    ll count = is(a, m, k);
    if (count >= 1) {
      ans = m;
      r = m - 1;
    } else {
      l = m + 1;
    }
  }

  cout << ans << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main() {

  ll n, k;
  cin >> n >> k;

  vector<ll> v(n);

  ll maxm = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    maxm = max(maxm, v[i]);
  }

  ll lo = 1, hi = maxm, mid;
  ll ans = 1e10;
  while (lo <= hi) {
    mid = (lo + hi) / 2;
    ll moves = 0;
    for (int i = 0; i < n; i++) {
      if (v[i] < mid) {
        continue;
      }
      if (v[i] % mid == 0) {
        moves += v[i] / mid - 1;
      } else {
        moves += v[i] / mid;
      }
    }
    if (moves <= k) {
      ans = min(ans, mid);
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }

  cout << ans << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main() {

  ll n, k;
  cin >> n >> k;

  vector<ll> v(n);

  ll maxm = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    maxm = max(maxm, v[i]);
  }

  ll lo = 0, hi = maxm, mid;
  ll ans = 1e9;
  while (lo <= hi) {
    mid = (lo + hi) / 2;
    ll moves = 0;
    for (int i = 0; i < n; i++) {
      if (v[i] < mid) {
        continue;
      }
      if (v[i] % mid == 0) {
        moves += v[i] / mid - 1;
      } else {
        moves += v[i] / mid;
      }
    }
    if (moves <= k) {
      ans = min(ans, mid);
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }

  if (ans == 1e9) {
    cout << 0 << "\\n";
  } else {
    cout << ans << "\\n";
  }
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t n, k, i, l, r, m, s;
  cin >> n >> k;
  vector<int64_t> a(n);
  for (i = 0; i < n; i++)
    cin >> a.at(i);
  sort(a.begin(), a.end());
  l = 0;
  r = a.at(n - 1);
  while (r - l > 1) {
    m = (l + r) / 2;
    s = 0;
    for (i = 0; i < n; i++) {
      s += a.at(i) / m - 1;
      if (a.at(i) % m > 0)
        s++;
      if (s > k)
        break;
    }
    if (s <= k)
      r = m;
    else
      l = m;
  }
  cout << r << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t n, k, i;
  cin >> n >> k;
  vector<vector<int64_t>> a(n, vector<int64_t>(3));
  for (i = 0; i < n; i++) {
    cin >> a.at(i).at(0);
    a.at(i).at(1) = a.at(i).at(0);
    a.at(i).at(2) = 1;
  }
  sort(a.begin(), a.end());
  for (i = 0; i < k; i++) {
    a.at(n - 1).at(2)++;
    a.at(n - 1).at(0) = a.at(n - 1).at(1) / a.at(n - 1).at(2);
    if (a.at(n - 1).at(1) % a.at(n - 1).at(2) > 0)
      a.at(n - 1).at(0)++;
    sort(a.begin(), a.end());
  }
  cout << a.at(n - 1).at(0) << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define F first
#define S second
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pi = pair<int, int>;
using ld = long double;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int lb = 0, ub = 1e+9 + 1;
  while (ub - lb > 1) {
    int mid = (ub + lb) / 2;
    int cnt = 0;
    rep(i, n) { cnt += (a[i] - 1) / mid; }
    (cnt <= k ? ub : lb) = mid;
  }
  cout << ub << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define F first
#define S second
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pi = pair<int, int>;
using ld = long double;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int lb = -1, ub = 1e+9 + 1;
  while (ub - lb > 1) {
    int mid = (ub + lb) / 2;
    int cnt = 0;
    rep(i, n) { cnt += (a[i] - 1) / mid; }
    (cnt <= k ? ub : lb) = mid;
  }
  cout << ub << "\\n";
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

bool check(int n, vector<ll> &a, ll k, ll x) {
  ll cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (a[i] + x - 1) / x - 1;
  return k >= cnt;
}

int main() {
  int n;
  cin >> n;
  ll k;
  cin >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ll l = 0, r = 1e9, m;
  while (r - l > 1) {
    m = (r + l) / 2;

    if (check(n, a, k, m))
      r = m;
    else
      l = m;
  }

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

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

bool check(int n, vector<ll> &a, ll k, ll x) {
  ll cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (a[i] + x - 1) / x - 1;
  return k >= cnt;
}

int main() {
  int n;
  cin >> n;
  ll k;
  cin >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ll l = -1, r = 1e9, m;
  while (r - l > 1) {
    m = (r + l) / 2;

    if (check(n, a, k, m))
      r = m;
    else
      l = m;
  }

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const ll mod = 1e9 + 7;
const ll mmod = 998244353;

vector<ll> dx = {1, -1, 0, 0};
vector<ll> dy = {0, 0, 1, -1};

vector<string> g;
vector<vector<bool>> used;
vector<ll> d;

bool check(const vector<double> &v, ll &k, double len) {
  ll cur = 0;
  for (int i = 0; i < v.size(); ++i) {
    cur += (ll)((v[i] - 0.1) / len);
    if (cur > k) {
      return 1;
    }
  }
  return 0;
}

int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(0);

  ll n, k;
  cin >> n >> k;

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

  double l = 0;
  double r = 1e10;

  while ((r - l) > 1e-5) {
    double m = (l + r) / 2.0;
    if (check(v, k, m)) {
      l = m;
    } else {
      r = m;
    }
  }
  cout << (ll)ceil(r) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const ll mod = 1e9 + 7;
const ll mmod = 998244353;

vector<ll> dx = {1, -1, 0, 0};
vector<ll> dy = {0, 0, 1, -1};

vector<string> g;
vector<vector<bool>> used;
vector<ll> d;

bool check(const vector<double> &v, ll &k, double len) {
  ll cur = 0;
  for (int i = 0; i < v.size(); ++i) {
    cur += (ll)((v[i] - 0.1) / len);
    if (cur > k) {
      return 1;
    }
  }
  return 0;
}

int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(0);

  ll n, k;
  cin >> n >> k;

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

  double l = 0;
  double r = 1e10;

  while ((r - l) > 1e-7) {
    double m = (l + r) / 2.0;
    if (check(v, k, m)) {
      l = m;
    } else {
      r = m;
    }
  }
  cout << (ll)ceil(l) << endl;

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000007
using namespace std;

int main() {
  long n, k;
  cin >> n >> k;
  long a[n];
  long maxa = 0;
  rep(i, n) {
    cin >> a[i];
    if (maxa < a[i])
      maxa = a[i];
  }
  long li = 0, ri = maxa;
  long mid = (li + ri) / 2;
  while (li + 1 < ri) {
    long tmp = 0;
    mid = (li + ri) / 2;
    rep(i, n) {
      // tmp += ceil((double)a[i]/(double)mid)-1;
      tmp += (a[i] - 1) / mid;
    }
    if (tmp <= k)
      ri = mid;
    else if (tmp > k)
      li = mid;
  }
  cout << ri << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000007
using namespace std;

int main() {
  long n, k;
  cin >> n >> k;
  long a[n];
  long maxa = 0;
  rep(i, n) {
    cin >> a[i];
    if (maxa < a[i])
      maxa = a[i];
  }
  long li = 1, ri = maxa;
  long mid = (li + ri) / 2;
  while (li <= ri) {
    long tmp = 0;
    mid = (li + ri) / 2;
    rep(i, n) {
      // tmp += ceil((double)a[i]/(double)mid)-1;
      tmp += (a[i] - 1) / mid;
    }
    if (tmp <= k)
      ri = mid;
    else if (tmp > k)
      li = mid;
  }
  cout << mid << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

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

template <typename T> ll binary_search(ll limit, bool (*func)(ll)) {
  ll left = 1, right = limit;
  while (right >= left) {
    ll mid = left + (right - left) / 2;
    bool ans = func(mid);
    if (ans)
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  ll m = 0;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    m = max(A[i], m);
  }
  auto f = [&](ll key) -> bool {
    ll cnt = 0;
    for (int i = 0; i < N; ++i) {
      cnt += (A[i] / key) + min(A[i] % key, 1ll) - 1;
    }
    if (cnt <= K)
      return true;
    return false;
  };
  ll left = 0, right = m + 1;
  while (right - left > 1) {
    ll mid = (left + right) / 2;
    bool ans = f(mid);
    if (ans)
      right = mid;
    else
      left = mid;
  }
  cout << right << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  priority_queue<tuple<double, ll, ll>> p;
  int N, K;
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    ll a;
    cin >> a;
    // 切った後の長さ、-何回切ったか、もとの長さ
    // 切った回数が小さいものから切っていく
    p.push(make_tuple(a, -1, a));
  }
  for (int i = 0; i < K; ++i) {
    auto x = p.top();
    p.pop();
    ll original = get<2>(x);
    ll cut = -get<1>(x) + 1;
    p.push(make_tuple((double)original / cut, -cut, original));
  }
  auto x = p.top();
  cout << (ll)ceil(get<0>(x)) << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using Graph = vector<vector<int>>;
using Grid = vector<vector<char>>;
using vin = vector<int>;
using ll = long long;
using vll = vector<ll>;
using vbl = vector<bool>;
using vch = vector<char>;
using pinin = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rell(i, n) for (ll i = 0; i < (ll)(n); i++)
#define sort(x) sort(x.begin(), x.end());
#define rese(x) reverse(x.begin(), x.end());

string yes = "Yes", no = "No";

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vll a(n);
  rep(i, n) cin >> a[i];
  ll ng = -1;
  ll ok = 1e9 + 100;
  while (ok - ng > 1) {
    ll mid = (ok + ng) / 2;
    if (mid == 0) {
      cout << 1 << endl;
      return 0;
    }
    ll sum = 0;
    rep(i, n) { sum += (a[i] - 1) / mid; }
    if (sum > k)
      ng = mid;
    else
      ok = mid;
  }
  cout << ok << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using Graph = vector<vector<int>>;
using Grid = vector<vector<char>>;
using vin = vector<int>;
using ll = long long;
using vll = vector<ll>;
using vbl = vector<bool>;
using vch = vector<char>;
using pinin = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rell(i, n) for (ll i = 0; i < (ll)(n); i++)
#define sort(x) sort(x.begin(), x.end());
#define rese(x) reverse(x.begin(), x.end());

string yes = "Yes", no = "No";

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vll a(n);
  rep(i, n) cin >> a[i];
  ll ng = -1;
  ll ok = 1e9 + 100;
  while (ok - ng > 1) {
    ll mid = (ok + ng) / 2;
    ll sum = 0;
    rep(i, n) { sum += (a[i] - 1) / mid; }
    if (sum > k)
      ng = mid;
    else
      ok = mid;
    if (ok == 0) {
      cout << 1 << endl;
      return 0;
    }
  }
  cout << ok << endl;
}