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

#include <bits/stdc++.h>

#ifndef M_PI
#define M_PI 3.14159265358979
#endif
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

using namespace std;

typedef long long ll;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
typedef long double ld;
typedef pair<long double, long double> pld;

const ll INF = 1e15;
const ll MOD = 1e9 + 7;

int main() {
  ll i, j, k;
  ll N, K;
  cin >> N >> K;
  vector<pair<ld, ld>> A;
  for (i = 0; i < N; i++) {
    ld a;
    cin >> a;
    A.push_back(pair<ld, ld>(a, 1));
  }

  ld total = 0;
  for (i = 0; i < N; i++) {
    total += (ld)A[i].first;
  }
  ll rk = K;
  ld perex = total / ((ld)N + (ld)K);
  for (i = 0; i < N; i++) {
    pld newp;
    ld nc = max(floor(A[i].first / perex) - 1, (ld)0);
    A[i].first = A[i].first / (nc + 1);
    A[i].second = nc + 1;
    rk -= nc;
  }

  priority_queue<pair<ld, ld>> q;
  for (i = 0; i < N; i++) {
    q.push(pld(A[i].first, 1));
  }

  for (i = 0; i < rk; i++) {
    pld maxlog = q.top();
    q.pop();
    pld newlog;
    newlog.first = maxlog.first * maxlog.second / (maxlog.second + 1);
    newlog.second = maxlog.second + 1;
    q.push(newlog);
  }
  ld maxld = q.top().first;
  ll ans = (ll)ceil(maxld);
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#ifndef M_PI
#define M_PI 3.14159265358979
#endif
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

using namespace std;

typedef long long ll;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
typedef long double ld;

const ll INF = 1e15;
const ll MOD = 1e9 + 7;

int main() {
  ll i, j, k;
  ll N, K;
  cin >> N >> K;
  vector<ld> A(N);
  for (auto &e : A)
    cin >> e;

  priority_queue<pair<ld, ld>> q;
  for (i = 0; i < N; i++) {
    q.push(pair<ld, ld>(A[i], 1));
  }
  for (i = 0; i < K; i++) {
    pair<ld, ld> maxlog = q.top();
    q.pop();
    pair<ld, ld> newlog;
    newlog.first = maxlog.first * maxlog.second / (maxlog.second + 1);
    newlog.second = maxlog.second + 1;
    q.push(newlog);
  }
  ld maxld = q.top().first;
  ll ans = (ll)ceil(maxld);
  cout << ans << 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)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int n, k;
vector<int> a;

int cut_sum(double l) {
  int ans = 0;
  rep(i, n) { ans += (int)(a[i] / l); }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  a.resize(n);
  rep(i, n) cin >> a[i];
  double l_left = 0;
  double l_right = 1e9;
  double l = (l_left + l_right) / 2;
  while (true) {
    if (cut_sum(l) <= k) {
      l_right = l;
      l = (l_left + l_right) / 2;
    } else {
      l_left = l;
      l = (l_left + l_right) / 2;
    }
    // if((int)l_right == (int)l_left) break;
    if (l_right - l_left <= 1e-6) {
      l = l_left;
      break;
    }
  }
  cout << int(ceil(l)) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  priority_queue<pair<double, int>, vector<pair<double, int>>> pq;
  rep(i, n) {
    double a;
    cin >> a;
    pq.emplace(a, 1);
  }
  rep(i, k) {
    auto p = pq.top();
    pq.pop();
    pq.emplace(p.first * p.second / (p.second + 1), p.second + 1);
  }
  double ans = pq.top().first;
  cout << int(ceil(ans)) << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n, k;
int arr[200010];
bool valid(int temp) {
  int count = 0;
  for (int i = 0; i < n; i++) {
    count += (arr[i] + temp - 1) / temp - 1;
  }
  if (count <= k)
    return true;
  else
    return false;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int l = 0, r = 1000000001;
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    if (valid(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, k;
int arr[200010];
bool valid(int temp) {
  int count = 0;
  for (int i = 0; i < n; i++) {
    count += (arr[i] + temp - 1) / temp - 1;
  }
  if (count <= k)
    return true;
  else
    return false;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int l = -1, r = 1000000001;
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    if (valid(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << "\\n";
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

bool possible(long long int mid, long long int arr[], long long int n,
              long long int k) {
  if (mid <= 0)
    return false;
  for (long long int i = 0; i < n; i++) {
    long long int p = arr[i];

    if (k >= (p - 1) / mid) {
      k -= (p - 1) / mid;
    } else {
      return false;
    }
  }
  return true;
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  long long int k;
  cin >> k;
  long long int arr[n];
  for (long long int i = 0; i < n; i++)
    cin >> arr[i];
  long long int start = 0;
  long long int end = 1000000001;
  while (start < end) {
    long long int mid = start + (end - start) / 2;
    if (possible(mid, arr, n, k)) {
      end = mid;
    } else {
      start = mid + 1;
    }
  }

  cout << end << endl;

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

bool possible(long long int mid, long long int arr[], long long int n,
              long long int k) {
  for (long long int i = 0; i < n; i++) {
    long long int p = arr[i];

    if (k >= (p - 1) / mid) {
      k -= (p - 1) / mid;
    } else {
      return false;
    }
  }
  return true;
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  long long int k;
  cin >> k;
  long long int arr[n];
  for (long long int i = 0; i < n; i++)
    cin >> arr[i];
  long long int start = 0;
  long long int end = 1000000001;
  while (start < end) {
    long long int mid = start + (end - start) / 2;
    if (possible(mid, arr, n, k)) {
      end = mid;
    } else {
      start = mid + 1;
    }
  }

  cout << end << endl;

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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;
struct aaa {
  aaa() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} aaa;
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
#define debug(v)                                                               \\
  { cerr << "\\033[1;36m[debug]\\033[m " << #v << " : " << v << endl; }

using int64 = long long;

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

  int64 l = 0;
  int64 r = a[0];
  for (int i = 1; i < n; i++)
    r = max(r, a[i]);

  while (l + 1 < r) {
    int64 m = (l + r) / 2;

    int64 t = 0;
    for (int i = 0; i < n; i++) {
      t += a[i] / m;
      if (a[i] % m == 0)
        t--;
      if (t > k)
        break;
    }

    if (t <= k)
      r = m;
    else
      l = m;
  }
  cout << r << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;
struct aaa {
  aaa() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} aaa;
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
#define debug(v)                                                               \\
  { cerr << "\\033[1;36m[debug]\\033[m " << #v << " : " << v << endl; }

using int64 = long long;

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

  int64 l = 0;
  int64 r = a[0];
  for (int i = 0; i < n; i++)
    r = max(r, a[i]);

  while (l + 1 < r) {
    int64 m = (l + r) / 2;
    int64 t = 0;

    for (int i = 0; i < n; i++) {
      t += a[i] / m;
      if (t > k)
        break;
    }

    if (t <= k)
      r = m;
    else
      l = m;
  }

  int ans = r;
  {
    int64 t = 0;
    for (int i = 0; i < n; i++) {
      t += a[i] / (r - 1);
    }
    if (t <= k)
      ans = r - 1;
  }

  if (k != 0) {
    int64 t = 0;
    for (int i = 0; i < n; i++) {
      t += a[i] / (r);
    }
    if (t > k)
      ans = r + 1;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define SZ(a) ((int)(a).size())
int main() {
#ifdef LOCAL
  freopen(".a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int64_t> A(n);
  for (int i = 0; i < n; ++i)
    cin >> A[i];
  for (int i = 0; i < n; ++i)
    A[i] *= 2;
  auto ok = [&](int64_t x) {
    int64_t c = 0;
    for (int i = 0; i < n; ++i) {
      if (A[i] % x == 0)
        c += A[i] / x - 1;
      else
        c += A[i] / x;
    }
    return c <= k;
  };
  int64_t l = 1, r = 2e9;
  while (l < r) {
    int64_t mid = (l + r) >> 1;
    if (ok(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  if (l % 2 == 0)
    cout << l / 2;
  else
    cout << (l + 1) / 2;

  return 0;
}
// 2020.08.02 20:31:43


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define SZ(a) ((int)(a).size())
int main() {
#ifdef LOCAL
  freopen(".a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int64_t> A(n);
  for (int i = 0; i < n; ++i)
    cin >> A[i];
  for (int i = 0; i < n; ++i)
    A[i] *= 2;
  auto ok = [&](int64_t x) {
    int64_t c = 0;
    for (int i = 0; i < n; ++i) {
      if (A[i] % x == 0)
        c += A[i] / x - 1;
      else
        c += A[i] / x;
    }
    return c <= k;
  };
  int64_t l = 0, r = 2e9;
  while (l < r) {
    int64_t mid = (l + r) >> 1;
    if (ok(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  if (l % 2 == 0)
    cout << l / 2;
  else
    cout << (l + 1) / 2;

  return 0;
}
// 2020.08.02 20:31:43
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define LL long long

int n;
LL k;
LL a[200000];

bool ok(LL st) {
  LL cnt = 0;
  REP(i, n) {
    LL cut = (a[i] - 1) / st;
    cnt += cut;
    // cerr<<st<<" "<<cnt<<endl;
    if (cnt > k)
      return false;
  }

  return true;
}

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

  LL lb = 1, ub = 10e9;
  while (lb < ub) {
    LL mid = (lb + ub) / 2;
    if (ok(mid)) {
      ub = mid;
    } else {
      lb = mid + 1;
    }
  }
  cout << lb << endl;
  return 0;
}


Error: 0

Fixed submission:

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define LL long long

int n;
LL k;
LL a[200000];

bool ok(LL st) {
  LL cnt = 0;
  REP(i, n) {
    LL cut = (a[i] - 1) / st;
    cnt += cut;
    // cerr<<st<<" "<<cnt<<endl;
    if (cnt > k)
      return false;
  }

  return true;
}

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

  LL lb = 0, ub = 10e9;
  while (lb < ub) {
    LL mid = (lb + ub) / 2;
    if (ok(mid)) {
      ub = mid;
    } else {
      lb = mid + 1;
    }
  }
  cout << lb << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define maxn 200005
#define LL long long
using namespace std;
LL a[maxn], k;
LL cut[maxn];
int n;
LL L = 1, R = 0;

bool check(LL t) {
  LL cnt = 0;
  for (int i = 1; i <= n; i++) {
    cut[i] = (a[i] / t) - 1 + (a[i] % t > 0);
    cnt += cut[i];
  }
  if (cnt <= k)
    return 1;
  else
    return 0;
}

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    R = max(R, a[i]);
  R++;
  while (L + 1 < R) {
    LL mid = L + R >> 1;
    //	cout<<L<<" "<<R<<" "<<mid<<endl;
    if (check(mid))
      R = mid;
    else
      L = mid;
  }
  if (check(L))
    cout << L << endl;
  else
    cout << R << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define maxn 200005
#define LL long long
using namespace std;

const double eps = 1e-7;
double a[maxn];
int k;
int n;
double L, R;

bool check(double t) {
  LL cnt = 0;
  for (int i = 1; i <= n; i++) {
    LL p;
    p = int(a[i] / t);
    cnt += p;
    if (p * t == a[i])
      cnt--;
  }
  if (cnt <= k)
    return 1;
  else
    return 0;
}

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    R = max(R, a[i]);
  R++;
  while (R - L > eps) {
    double mid = (L + R) / 2;
    //	cout<<L<<" "<<R<<" "<<mid<<endl;
    if (check(mid))
      R = mid;
    else
      L = mid;
  }
  cout << int(L + 1 - eps) << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using ll = long long;
using namespace std;
const int INFint = 1e9 + 1;
const ll INFll = (ll)1e18 + 1;
ll MOD = 1e9 + 7;

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i(0); i < N; i++) {
    cin >> A[i];
  }
  if (K == 0) {
    ll ans(0);
    for (int i(0); i < N; i++) {
      ans = max(ans, A[i]);
    }
    cout << ans << endl;
    return 0;
  }
  ll l = 0, r = 1e9 + 1;
  while (r - l > 1) {
    ll x = (l + r) / 2;
    // ccout << x << endl;
    // cout << l << " " << r << endl;
    bool ok = [&] {
      // ll nedan = A*x + B*int(to_string(x).size());
      // if (nedan <= X) return true;
      // else return false;
      ll cnt(0);
      for (int i(0); i < N; i++) {
        if (A[i] % x == 0)
          cnt += A[i] / x - 1;
        else
          cnt += A[i] / x;
      }
      if (cnt > K)
        return true;
      else
        return false;
    }();
    if (ok)
      l = x;
    else
      r = x;
  }
  int cnt(0);
  if (l == 0) {
    cout << 1 << endl;
    return 0;
  }
  for (int i(0); i < N; i++) {
    if (A[i] % l == 0)
      cnt += A[i] / l - 1;
    else
      cnt += A[i] / l;
  }
  if (cnt <= K)
    cout << l << endl;
  else
    cout << r << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using ll = long long;
using namespace std;
const int INFint = 1e9 + 1;
const ll INFll = (ll)1e18 + 1;
ll MOD = 1e9 + 7;

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i(0); i < N; i++) {
    cin >> A[i];
  }
  if (K == 0) {
    ll ans(0);
    for (int i(0); i < N; i++) {
      ans = max(ans, A[i]);
    }
    cout << ans << endl;
    return 0;
  }
  ll l = 0, r = 1e9 + 1;
  while (r - l > 1) {
    ll x = (l + r) / 2;
    // ccout << x << endl;
    // cout << l << " " << r << endl;
    bool ok = [&] {
      // ll nedan = A*x + B*int(to_string(x).size());
      // if (nedan <= X) return true;
      // else return false;
      ll cnt(0);
      for (int i(0); i < N; i++) {
        if (A[i] % x == 0)
          cnt += A[i] / x - 1;
        else
          cnt += A[i] / x;
      }
      if (cnt > K)
        return true;
      else
        return false;
    }();
    if (ok)
      l = x;
    else
      r = x;
  }
  int cnt(0);
  for (int i(0); i < N; i++) {
    if (A[i] % l == 0)
      cnt += A[i] / l - 1;
    else
      cnt += A[i] / l;
  }
  if (l == 0) {
    cout << 1 << endl;
    return 0;
  }
  if (cnt <= K)
    cout << l << endl;
  else
    cout << r << endl;
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

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

#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define PER(i, a, b) for (int i = (a); i >= (b); i--)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(S) (S).begin(), (S).end()
#define pf push_front
#define pb push_back
#define mk make_pair
#define S second
#define F first
typedef long long ll;
typedef long double lf;
typedef pair<int, int> ii;
typedef pair<ii, ii> pii;
const int MAX = 2e5 + 5;
int N, K, a[MAX];

bool check(int m) {
  int cnt = 0;
  rep(i, 0, N) cnt += (a[i] - 1) / m;
  return cnt <= K;
}

int search(int i, int j) {
  int m = (i + j) / 2;
  if (i == j)
    return m;
  if (check(m))
    return search(i, m);
  return search(m + 1, j);
}

int main(int argc, char **argv) {
  int ma = 0;
  scanf("%d%d", &N, &K);
  rep(i, 0, N) scanf("%d", a + i);
  rep(i, 0, N) ma = max(ma, a[i]);
  if (!K)
    printf("%d\\n", ma);
  else
    printf("%d\\n", search(1, 1e9));
  return 0;
}


Error: 0

Fixed submission:

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

#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define PER(i, a, b) for (int i = (a); i >= (b); i--)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(S) (S).begin(), (S).end()
#define pf push_front
#define pb push_back
#define mk make_pair
#define S second
#define F first
typedef long long ll;
typedef long double lf;
typedef pair<int, int> ii;
typedef pair<ii, ii> pii;
const int MAX = 2e5 + 5;
int N, K, a[MAX];

bool check(int m) {
  int cnt = 0;
  rep(i, 0, N) cnt += (a[i] - 1) / m;
  return cnt <= K;
}

int search(int i, int j) {
  int m = (i + j) / 2;
  if (i == j)
    return m;
  if (check(m))
    return search(i, m);
  return search(m + 1, j);
}

int main(int argc, char **argv) {
  scanf("%d%d", &N, &K);
  rep(i, 0, N) scanf("%d", a + i);
  printf("%d\\n", search(0, 1e9));
  return 0;
}
Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define endl '\\n'
#define ll long long
#define all(a) a.begin(), a.end()
#define sz(a) a.size()
using namespace std;

int n, k, a[200001], ans;

bool ok(int x) {
  int tk = k;
  for (int i = 0; i < n; i++) {
    tk -= (a[i] - 1) / x;
    if (tk < 0)
      return false;
  }
  return true;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("A.txt", "r", stdin);
#endif
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans = max(ans, a[i]);
  }
  int lb = 1, rb = ans;
  while (lb < rb) {
    int mid = (lb + rb) / 2;
    if (ok(mid)) {
      ans = mid;
      rb = mid;
    } else
      lb = mid + 1;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
#define ll long long
#define all(a) a.begin(), a.end()
#define sz(a) a.size()
using namespace std;

int n, k, a[200001], ans;

bool ok(int x) {
  int tk = k;
  for (int i = 0; i < n; i++) {
    tk -= (a[i] - 1) / x;
    if (tk < 0)
      return false;
  }
  return true;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
// freopen("A.txt", "r", stdin);
#endif
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans = max(ans, a[i]);
  }
  int lb = 0, rb = ans;
  while (lb < rb) {
    int mid = (lb + rb) / 2;
    if (ok(mid)) {
      ans = mid;
      rb = mid;
    } else
      lb = mid + 1;
  }
  cout << ans << endl;
}
Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long int lint;

#define rep(i, n) for (lint i = 0; i < n; i++)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v, hoge) upper_bound(v.begin(), v.end(), hoge)
#define lower(v, hoge) lower_bound(v.begin(), v.end(), hoge)
#define mp make_pair
#define enld endl

// index が条件を満たすかどうか
bool isOK(lint index, lint N, lint K, vector<lint> A) {
  lint ans = 0;
  rep(i, N) { ans += (A[i] / index + bool(A[i] % index) - 1); }
  if (ans <= K) {
    return true;
  } else {
    return false;
  }
}

// 汎用的な二分探索のテンプレ
lint binary_search(lint N, lint K, vector<lint> A) {
  lint ng = 0; // 「index = 0」が条件を満たすこともあるので、初期値は -1
  lint ok =
      pow(10, 9) +
      1; // 「index = a.size()-1」が条件を満たさないこともあるので、初期値は
         // a.size()

  /* ok と ng のどちらが大きいかわからないことを考慮 */
  while (abs(ok - ng) > 1) {
    lint mid = (ok + ng) / 2;

    if (isOK(mid, N, K, A))
      ok = mid;
    else
      ng = mid;
  }
  return ok;
}

int main() {
  lint N, K;
  cin >> N >> K;
  vector<lint> A(N);
  rep(i, N) { cin >> A[i]; }
  cout << binary_search(N, K, A) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long int lint;

#define rep(i, n) for (lint i = 0; i < n; i++)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v, hoge) upper_bound(v.begin(), v.end(), hoge)
#define lower(v, hoge) lower_bound(v.begin(), v.end(), hoge)
#define mp make_pair
#define enld endl

int main() {
  lint N, K;
  cin >> N >> K;
  vector<pair<double, double>> P(N);
  rep(i, N) {
    cin >> P[i].first;
    P[i].second = 1;
  }
  priority_queue<pair<double, double>> Q;
  rep(i, N) { Q.push(P[i]); }
  rep(i, K) {
    pair<double, double> tmp = Q.top();
    tmp.first *= tmp.second;
    tmp.first /= tmp.second + 1;
    tmp.second += 1;
    Q.pop();
    Q.push(tmp);
  }
  cout << lint(Q.top().first) + bool(Q.top().first - lint(Q.top().first))
       << enld;
}