Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#pragma region

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n)-1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\\n"
#define popcnt(x) __builtin_popcount(x)

const double PI = acos(-1.0);

using ll = long long;
using P = pair<int, int>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++)
    cin >> v.at(i);
  return is;
}

template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
  cout << '(' << p.first << ", " << p.second << ')';
  return os;
}

template <class T>
void print(const vector<T> &v, const string &delimiter = " ") {
  rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i);
  cout << endl;
}

template <class T>
void print(const vector<vector<T>> &vv, const string &delimiter) {
  for (const auto &v : vv)
    print(v, delimiter);
}

#pragma endregion

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  cin >> a;

  ll ng = 0;
  ll ok = 1000000000;

  auto is_ok = [&](int x) {
    ll cnt = 0;
    rep(i, n) { cnt += (a[i] + x - 1) / x - 1; }
    return cnt <= k;
  };

  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;

    if (is_ok(mid))
      ok = mid;
    else
      ng = mid;
  }

  cout << ok << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#pragma region

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n)-1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\\n"
#define popcnt(x) __builtin_popcount(x)

const double PI = acos(-1.0);

using ll = long long;
using P = pair<int, int>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++)
    cin >> v.at(i);
  return is;
}

template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
  cout << '(' << p.first << ", " << p.second << ')';
  return os;
}

template <class T>
void print(const vector<T> &v, const string &delimiter = " ") {
  rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i);
  cout << endl;
}

template <class T>
void print(const vector<vector<T>> &vv, const string &delimiter) {
  for (const auto &v : vv)
    print(v, delimiter);
}

#pragma endregion

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  cin >> a;

  ll ng = -1;
  ll ok = 1000000000;

  auto is_ok = [&](int x) {
    ll cnt = 0;
    rep(i, n) { cnt += (a[i] + x - 1) / x - 1; }
    return cnt <= k;
  };

  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;

    if (is_ok(mid))
      ok = mid;
    else
      ng = mid;
  }

  cout << ok << endl;
}