Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
// abc174_e
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;

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

#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define REP_REV(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define REPN_REV(i, m, n) for (int(i) = (int)(n)-1; (i) >= m; --(i))
#define ALL(x) x.begin(), x.end()

#define INF (ll)(1e9)
#define MOD (1000000007)

#define print2D(h, w, arr)                                                     \
  REP(i, h) {                                                                  \
    REP(j, w) cout << arr[i][j] << " ";                                        \
    cout << endl;                                                              \
  }
#define print_line(vec, n)                                                     \
  {                                                                            \
    for (int idx = 0; idx < (n - 1); idx++)                                    \
      cout << (vec)[idx] << " ";                                               \
    cout << (vec)[(n)-1] << endl;                                              \
  }
template <class T> void print(const T &x) { cout << x << endl; }
template <class T, class... A> void print(const T &first, const A &...rest) {
  cout << first << " ";
  print(rest...);
}
struct PreMain {
  PreMain() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} premain;

int N, K;
vector<int> A;

bool check(double x) {

  ll k = 0;
  REP(i, N) {
    k += ll(A[i] / x);

    if (floor(x) == x) {
      int x_ = int(x);
      if (A[i] % x_ == 0) {
        k--;
      }
    }
  }
  return k <= K;
}

int main() {
#ifdef LOCAL
  ifstream in("../arg.txt");
  cin.rdbuf(in.rdbuf());
#endif

  cin >> N >> K;
  A.assign(N, 0);
  REP(i, N) cin >> A[i];

  if (K == 0) {
    int ans = *max_element(ALL(A));
    print(ans);
    return 0;
  }

  double ng = 0;
  double ok = 1e9 + 1;
  while (ok - ng > 1e-7) {

    double mid = (ok + ng) / 2;

    if (check(mid)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  int ans = ceil(ok);
  if (ok - (ans - 1) < 1e-6)
    ans--;
  print(ans);

  return 0;
}


Fixed submission:
// abc174_e
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;

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

#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define REP_REV(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define REPN_REV(i, m, n) for (int(i) = (int)(n)-1; (i) >= m; --(i))
#define ALL(x) x.begin(), x.end()

#define INF (ll)(1e9)
#define MOD (1000000007)

#define print2D(h, w, arr)                                                     \
  REP(i, h) {                                                                  \
    REP(j, w) cout << arr[i][j] << " ";                                        \
    cout << endl;                                                              \
  }
#define print_line(vec, n)                                                     \
  {                                                                            \
    for (int idx = 0; idx < (n - 1); idx++)                                    \
      cout << (vec)[idx] << " ";                                               \
    cout << (vec)[(n)-1] << endl;                                              \
  }
template <class T> void print(const T &x) { cout << x << endl; }
template <class T, class... A> void print(const T &first, const A &...rest) {
  cout << first << " ";
  print(rest...);
}
struct PreMain {
  PreMain() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} premain;

int N, K;
vector<int> A;

bool check(double x) {

  ll k = 0;
  REP(i, N) {
    k += ll(A[i] / x);

    if (floor(x) == x) {
      int x_ = int(x);
      if (A[i] % x_ == 0) {
        k--;
      }
    }
  }
  return k <= K;
}

int main() {
#ifdef LOCAL
  ifstream in("../arg.txt");
  cin.rdbuf(in.rdbuf());
#endif

  cin >> N >> K;
  A.assign(N, 0);
  REP(i, N) cin >> A[i];

  if (K == 0) {
    int ans = *max_element(ALL(A));
    print(ans);
    return 0;
  }

  double ng = 0;
  double ok = 1e9 + 1;
  while (ok - ng > 1e-6) {

    double mid = (ok + ng) / 2;

    if (check(mid)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  int ans = ceil(ok);
  if (ok - (ans - 1) < 1e-6)
    ans--;
  print(ans);

  return 0;
}