Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
/*******  All Required define Pre-Processors and typedef Constants *******/
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.rbegin(), cont.rend()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define Dcout(a) cout << setprecision(20) << a << endl
#define MP make_pair
#define PB push_back
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
using ll = long long;
using PAIR = pair<int, int>;
using Graph = vector<vector<int>>;
const ll INF = (1LL << 60);
const double pi = acos(-1.0);
#define debug cout << "line : " << __LINE__ << " debug" << endl
/****** Template of some basic operations *****/
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
/**************************************/
/******** User-defined Function *******/

/**************************************/
/********** Main()  function **********/
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  sort(all(a));

  int ng = -1;
  int ok = 1e9;

  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    // 答えをxとして全てのlogsはx以下にk回以内で切れるのか
    auto f = [&](int x) {
      ll now = 0;
      rep(i, n) { now += (a[i] - 1) / x; }
      // 切れる場合はtrueを返す
      return now <= k;
    };
    if (f(mid))
      ok = mid;
    else
      ng = mid;
  }

  cout << ok << endl;

  return 0;
}
/********  Main() Ends Here *************/


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
/*******  All Required define Pre-Processors and typedef Constants *******/
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.rbegin(), cont.rend()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define Dcout(a) cout << setprecision(20) << a << endl
#define MP make_pair
#define PB push_back
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
using ll = long long;
using PAIR = pair<int, int>;
using Graph = vector<vector<int>>;
const ll INF = (1LL << 60);
const double pi = acos(-1.0);
#define debug cout << "line : " << __LINE__ << " debug" << endl
/****** Template of some basic operations *****/
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
/**************************************/
/******** User-defined Function *******/

/**************************************/
/********** Main()  function **********/
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  sort(all(a));

  int ng = 0;
  int ok = 1e9;

  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    // 答えをxとして全てのlogsはx以下にk回以内で切れるのか
    auto f = [&](int x) {
      ll now = 0;
      rep(i, n) { now += (a[i] - 1) / x; }
      // 切れる場合はtrueを返す
      return now <= k;
    };
    if (f(mid))
      ok = mid;
    else
      ng = mid;
  }

  cout << ok << endl;

  return 0;
}
/********  Main() Ends Here *************/