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

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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

class Prints {
private:
  class __Prints {
  public:
    __Prints(const char *sep, const char *term) : sep(sep), term(term) {}
    template <class... Args> void operator()(Args &&...args) const {
      print(std::forward<Args>(args)...);
    }

  private:
    const char *sep, *term;
    void print() const { std::cout << term; }
    void print_rest() const { std::cout << term; }
    template <class T, class... Tail>
    void print(T &&head, Tail &&...tail) const {
      std::cout << head, print_rest(std::forward<Tail>(tail)...);
    }
    template <class T, class... Tail>
    void print_rest(T &&head, Tail &&...tail) const {
      std::cout << sep << head, print_rest(std::forward<Tail>(tail)...);
    }
  };

public:
  Prints() {}
  __Prints operator()(const char *sep = " ", const char *term = "\\n") const {
    return __Prints(sep, term);
  }
  template <class T>
  void print_vec(const std::vector<T> &vec, const char *sep = " ",
                 const char *term = "\\n") const {
    const size_t sz = vec.size();
    for (size_t i = 0; i < sz; i++)
      std::cout << vec[i] << (i == sz - 1 ? term : sep);
  }
};

Prints prints;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void solve() {
  ll N, K;
  cin >> N >> K;
  Vl A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll left = 0, right = 1001001001ll;
  while (right - left > 1) {
    ll mid = (left + right) >> 1;
    ll cnt = 0;
    for (ll i = 0; i < N; i++) {
      ll x = (A[i] + mid - 1) / mid;
      cnt += x - 1;
    }
    if (cnt <= K) {
      right = mid;
    } else {
      left = mid;
    }
  }
  prints()(right);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
  ifstream ifs("./in_out/input.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
  ofstream ofs("./in_out/output.txt");
  cout.rdbuf(ofs.rdbuf());
#endif
  cout << setprecision(18) << fixed;
  solve();
  cout << flush;
  return 0;
}

Error: TLE

Fixed submission:

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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

class Prints {
private:
  class __Prints {
  public:
    __Prints(const char *sep, const char *term) : sep(sep), term(term) {}
    template <class... Args> void operator()(Args &&...args) const {
      print(std::forward<Args>(args)...);
    }

  private:
    const char *sep, *term;
    void print() const { std::cout << term; }
    void print_rest() const { std::cout << term; }
    template <class T, class... Tail>
    void print(T &&head, Tail &&...tail) const {
      std::cout << head, print_rest(std::forward<Tail>(tail)...);
    }
    template <class T, class... Tail>
    void print_rest(T &&head, Tail &&...tail) const {
      std::cout << sep << head, print_rest(std::forward<Tail>(tail)...);
    }
  };

public:
  Prints() {}
  __Prints operator()(const char *sep = " ", const char *term = "\\n") const {
    return __Prints(sep, term);
  }
  template <class T>
  void print_vec(const std::vector<T> &vec, const char *sep = " ",
                 const char *term = "\\n") const {
    const size_t sz = vec.size();
    for (size_t i = 0; i < sz; i++)
      std::cout << vec[i] << (i == sz - 1 ? term : sep);
  }
};

Prints prints;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void solve() {
  ll N, K;
  cin >> N >> K;
  auto f = [](Pll l, Pll r) {
    ll a = l.first * r.second;
    ll b = l.second * r.first;
    return a < b;
  };
  priority_queue<Pll, vector<Pll>, decltype(f)> pq(f);
  for (ll i = 0; i < N; i++) {
    ll a;
    cin >> a;
    pq.emplace(a, 1);
  }
  while (K > 0) {
    auto [a, b] = pq.top();
    pq.pop();
    pq.emplace(a, b + 1);
    --K;
  }
  auto [a, b] = pq.top();
  prints()((a + b - 1) / b);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
  ifstream ifs("./in_out/input.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
  ofstream ofs("./in_out/output.txt");
  cout.rdbuf(ofs.rdbuf());
#endif
  cout << setprecision(18) << fixed;
  solve();
  cout << flush;
  return 0;
}