Problem p02598 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;
using ll = long long;
using vi = vector<int>;

#define in(v)                                                                  \\
  v;                                                                           \\
  cin >> v;
void ins() {}
template <class T, class... Rest> void ins(T &v, Rest &...rest) {
  cin >> v;
  ins(rest...);
}

#define rep(i, n) for (int i = 0, _i = (n); i < _i; ++i)
#define rrep(i, n) for (long long i = (n); i >= 0; --i)
#define all(f, c, ...)                                                         \\
  (([&](decltype((c)) cccc) {                                                  \\
    return (f)(begin(cccc), end(cccc), ##__VA_ARGS__);                         \\
  })(c))

// === debug ===
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for (size_t i = 0; i < vec.size(); ++i)
    os << (i ? ", " : "") << vec[i];
  os << "}";
  return os;
}
ostream &operator<<(ostream &os, const vector<char> &v) {
  for (size_t i = 0; i < v.size(); ++i)
    os << v[i];
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &rhs) {
  os << "(" << rhs.first << ", " << rhs.second << ")";
  return os;
}

#ifdef LOCAL
void debug() { cerr << "\\n"; }
template <class First> void debug(const First &first) { cerr << first << "\\n"; }
template <class First, class... Rest>
void debug(const First &first, const Rest &...rest) {
  cerr << first << ",";
  debug(rest...);
}
void debug2() { cerr << "\\n"; }
template <class First> void debug2(const First &first) { cerr << first << " "; }
template <class First, class... Rest>
void debug2(const First &first, const Rest &...rest) {
  cerr << first << " ";
  debug2(rest...);
}
#else
#define debug(...) 42
#define debug2(...) 42
#endif
// === end ===

int main() {
  int N, K;
  ins(N, K);

  vi A(N);
  rep(i, N) { cin >> A[i]; }
  all(sort, A);
  int ok = A.back(), ng = 0;
  auto f = [&](int mid) {
    int ans = 0;
    rep(i, N) { ans += (A[i] + mid - 1) / mid - 1; }
    return ans <= K;
  };
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    (f(mid) ? ok : ng) = mid;
  }
  cout << ok << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;
using ll = long long;
using vi = vector<int>;

#define in(v)                                                                  \\
  v;                                                                           \\
  cin >> v;
void ins() {}
template <class T, class... Rest> void ins(T &v, Rest &...rest) {
  cin >> v;
  ins(rest...);
}

#define rep(i, n) for (int i = 0, _i = (n); i < _i; ++i)
#define rrep(i, n) for (long long i = (n); i >= 0; --i)
#define all(f, c, ...)                                                         \\
  (([&](decltype((c)) cccc) {                                                  \\
    return (f)(begin(cccc), end(cccc), ##__VA_ARGS__);                         \\
  })(c))

// === debug ===
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for (size_t i = 0; i < vec.size(); ++i)
    os << (i ? ", " : "") << vec[i];
  os << "}";
  return os;
}
ostream &operator<<(ostream &os, const vector<char> &v) {
  for (size_t i = 0; i < v.size(); ++i)
    os << v[i];
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &rhs) {
  os << "(" << rhs.first << ", " << rhs.second << ")";
  return os;
}

#ifdef LOCAL
void debug() { cerr << "\\n"; }
template <class First> void debug(const First &first) { cerr << first << "\\n"; }
template <class First, class... Rest>
void debug(const First &first, const Rest &...rest) {
  cerr << first << ",";
  debug(rest...);
}
void debug2() { cerr << "\\n"; }
template <class First> void debug2(const First &first) { cerr << first << " "; }
template <class First, class... Rest>
void debug2(const First &first, const Rest &...rest) {
  cerr << first << " ";
  debug2(rest...);
}
#else
#define debug(...) 42
#define debug2(...) 42
#endif
// === end ===

int main() {
  int N, K;
  ins(N, K);

  priority_queue<pair<int, pair<int, int>>> q;
  rep(i, N) {
    int in(a);
    q.emplace(a, make_pair(a, 1));
  }

  while (K > 0) {
    auto [p, count] = q.top().second;
    q.pop();
    int ok = count + K, ng = count;
    auto f = [&, p = p](int mid) {
      return q.top().first > (p + mid - 1) / mid;
    };
    while (abs(ok - ng) > 1) {
      int mid = (ok + ng) / 2;
      (f(mid) ? ok : ng) = mid;
    }
    K -= ok - count;
    q.emplace((p + ok - 1) / ok, make_pair(p, ok));
  }
  cout << q.top().first << endl;

  return 0;
}