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

#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <cfenv>
#include <cinttypes>
#include <cstdint>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <chrono>
#include <forward_list>
#include <initializer_list>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#if __cplusplus >= 201402L
#include <shared_mutex>
#endif

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
typedef pair<int, int> P;

const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9 + 7;
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};

int main() {

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int r = 1000000000;
  int l = 0;
  while (r - l > 1) {
    int x = (l + r) / 2;
    auto f = [&](int x) {
      ll now = 0;
      rep(i, n) { now += (a[i] - 1) / x; }
      return now <= k;
    };
    if (f(x))
      r = x;
    else
      l = x;
  }
  cout << r << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <cfenv>
#include <cinttypes>
#include <cstdint>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <chrono>
#include <forward_list>
#include <initializer_list>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#if __cplusplus >= 201402L
#include <shared_mutex>
#endif

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
typedef pair<int, int> P;

const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9 + 7;
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};

int main() {

  int n, k;
  cin >> n >> k;
  priority_queue<pair<double, double>> a;
  rep(i, n) {
    double x;
    cin >> x;
    a.push(make_pair(x, 1));
  }

  rep(i, k) {
    pair<double, double> t = a.top();
    a.pop();
    t.first *= t.second;
    t.second = t.second + 1;
    t.first = t.first / t.second;
    a.push(t);
  }
  double ans = INF;
  pair<double, double> t = a.top();
  ans = t.first;
  int ansa = ans + 0.9888;
  cout << ansa << endl;

  return 0;
}