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

/*
**    Author : Dhiraj Govindvira
**      Date : 09-Sep-2020 ~ Wednesday
**      Time : 07:50:37 PM
*/

#ifdef Dhiraj
#include "D:/dhiraj/Programming/debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define d(...) 11
#define cerr                                                                   \\
  if (0)                                                                       \\
  cerr
#endif

// Macro --------------------------------------------------------------------

#define ll long long int
#define FOR(i, a, b, s)                                                        \\
  for (ll i = (a); ((s) > 0 ? i < (b) : i > (b)); i += (s))
#define FOR1(b) FOR(i, 0, b, 1)
#define FOR2(i, b) FOR(i, 0, b, 1)
#define FOR3(i, a, b) FOR(i, a, b, (a) <= (b) ? 1 : -1)
#define FOR4(i, a, b, s) FOR(i, a, b, s)
#define GET5(a, b, c, d, e, ...) e
#define FORC(...) GET5(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)
#define loop(...) FORC(__VA_ARGS__)(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endl '\\n'

// Template -----------------------------------------------------------------

// multiple input
template <typename... T> inline void inp(T &...args) { ((cin >> args), ...); }
// scan vector
template <typename T> inline istream &operator>>(istream &in, vector<T> &a) {
  for (auto &x : a)
    in >> x;
  return in;
}
// scan pair
template <typename T, typename U>
inline istream &operator>>(istream &in, pair<T, U> &a) {
  in >> a.first >> a.second;
  return in;
}

// Solve --------------------------------------------------------------------

void solve(int &T) {
  ll n, k;
  cin >> n >> k;

  vector<ll> a(n);
  cin >> a;

  auto ok = [&](ll m) {
    ll ans = 0;
    loop(i, n) { ans += ((a[i] + m - 1) / m - 1); }
    return ans <= k;
  };

  ll l = 1, r = 1e9, ans = -1;
  while (l <= r) {
    ll m = (l + r) / 2;
    if (ok(m)) {
      ans = m;
      r = m - 1;
    } else
      l = m + 1;
  }
  cout << ans << endl;
}

// Main ---------------------------------------------------------------------

int main() {
#ifdef Dhiraj
  freopen("D:/dhiraj/Programming/i1.txt", "r", stdin);
  freopen("D:/dhiraj/Programming/o1.txt", "w", stdout);
  // freopen("D:/dhiraj/Programming/e1.txt", "w", stderr);
#endif

  ios::sync_with_stdio(false);
  cin.tie(0);

  int t = 1;
  for (int i = 1; i <= t; i++) {
    cerr << "Case #" << i << "\\n";
    solve(i);
  }
  return 0;
}

Error: 0

Fixed submission:

/*
**    Author : Dhiraj Govindvira
**      Date : 09-Sep-2020 ~ Wednesday
**      Time : 07:50:37 PM
*/

#ifdef Dhiraj
#include "D:/dhiraj/Programming/debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define d(...) 11
#define cerr                                                                   \\
  if (0)                                                                       \\
  cerr
#endif

// Macro --------------------------------------------------------------------

#define ll long long int
#define FOR(i, a, b, s)                                                        \\
  for (ll i = (a); ((s) > 0 ? i < (b) : i > (b)); i += (s))
#define FOR1(b) FOR(i, 0, b, 1)
#define FOR2(i, b) FOR(i, 0, b, 1)
#define FOR3(i, a, b) FOR(i, a, b, (a) <= (b) ? 1 : -1)
#define FOR4(i, a, b, s) FOR(i, a, b, s)
#define GET5(a, b, c, d, e, ...) e
#define FORC(...) GET5(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)
#define loop(...) FORC(__VA_ARGS__)(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endl '\\n'

// Template -----------------------------------------------------------------

// multiple input
template <typename... T> inline void inp(T &...args) { ((cin >> args), ...); }
// scan vector
template <typename T> inline istream &operator>>(istream &in, vector<T> &a) {
  for (auto &x : a)
    in >> x;
  return in;
}
// scan pair
template <typename T, typename U>
inline istream &operator>>(istream &in, pair<T, U> &a) {
  in >> a.first >> a.second;
  return in;
}

// Solve --------------------------------------------------------------------

void solve(int &T) {
  ll n, k;
  cin >> n >> k;

  vector<ll> a(n);
  cin >> a;
  sort(all(a));

  auto ok = [&](ll m) {
    ll ans = 0;
    loop(i, n) { ans += ((a[i] + m - 1) / m - 1); }
    return ans <= k;
  };

  ll l = 0, r = 1e9, ans = -1;
  while (l <= r) {
    ll m = (l + r) / 2;
    if (ok(m)) {
      ans = m;
      r = m - 1;
    } else
      l = m + 1;
  }
  cout << ans << endl;
}

// Main ---------------------------------------------------------------------

int main() {
#ifdef Dhiraj
  freopen("D:/dhiraj/Programming/i1.txt", "r", stdin);
  freopen("D:/dhiraj/Programming/o1.txt", "w", stdout);
  // freopen("D:/dhiraj/Programming/e1.txt", "w", stderr);
#endif

  ios::sync_with_stdio(false);
  cin.tie(0);

  int t = 1;
  for (int i = 1; i <= t; i++) {
    cerr << "Case #" << i << "\\n";
    solve(i);
  }
  return 0;
}