Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define all(t) t.begin(), t.end()
#define inrange(i, a, b) (((i) >= min((a), (b))) && ((i) <= max((a), (b))))
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
#define fi first
#define se second
#define upb upper_bound
#define lwb lower_bound
#define show(x) cout << #x << " is " << x << "\n";
const ll inf = 2e18;
const ll mod = 1e9 + 7;
const ld pi = 3.141592653589793238462643383279502884;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void print(ll a[], ll n) {
  for (ll i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << "\n";
}
ll power(ll x, ll y) {
  if (y <= 0)
    return 1;
  ll ans = 1;
  x %= mod;
  while (y) {
    if (y & 1)
      ans = (x * ans) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return ans;
}
ll modInverse(ll n) { return power(n, mod - 2); }
inline ll mul(ll a, ll b) { return (a * b) % mod; }
inline ll sub(ll a, ll b) {
  ll c = (a - b);
  if (c < 0)
    c += mod;
  return c;
}
inline ll add(ll a, ll b) {
  ll c = (a + b);
  if (c >= mod)
    c -= mod;
  return c;
}
inline ll divi(ll a, ll b) { return mul(a, modInverse(b)); }

//------------------------------------------------------------------------------------------

const ll N = 2e5 + 1;
// vector<ll> adj[N];
// bool visited[N];

ll n, k;
ll a[N];

bool f(ll x) {
  int c = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] % x == 0) {
      c += a[i] / x - 1;
    } else
      c += a[i] / x;
  }
  return c <= k;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(10);

  cin >> n >> k;
  ll ma = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ma = max(ma, a[i]);
  }
  ll l = 0, r = ma;
  ll ans = 0;
  while (l <= r) {
    ll m = l + (r - l) / 2;
    if (f(m)) {
      ans = m;
      r = m - 1;
    } else
      l = m + 1;
  }
  cout << ans;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define all(t) t.begin(), t.end()
#define inrange(i, a, b) (((i) >= min((a), (b))) && ((i) <= max((a), (b))))
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
#define fi first
#define se second
#define upb upper_bound
#define lwb lower_bound
#define show(x) cout << #x << " is " << x << "\n";
const ll inf = 2e18;
const ll mod = 1e9 + 7;
const ld pi = 3.141592653589793238462643383279502884;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void print(ll a[], ll n) {
  for (ll i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << "\n";
}
ll power(ll x, ll y) {
  if (y <= 0)
    return 1;
  ll ans = 1;
  x %= mod;
  while (y) {
    if (y & 1)
      ans = (x * ans) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return ans;
}
ll modInverse(ll n) { return power(n, mod - 2); }
inline ll mul(ll a, ll b) { return (a * b) % mod; }
inline ll sub(ll a, ll b) {
  ll c = (a - b);
  if (c < 0)
    c += mod;
  return c;
}
inline ll add(ll a, ll b) {
  ll c = (a + b);
  if (c >= mod)
    c -= mod;
  return c;
}
inline ll divi(ll a, ll b) { return mul(a, modInverse(b)); }

//------------------------------------------------------------------------------------------

const ll N = 2e5 + 1;
// vector<ll> adj[N];
// bool visited[N];

ll n, k;
ll a[N];

bool f(ll x) {
  int c = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] % x == 0) {
      c += a[i] / x - 1;
    } else
      c += a[i] / x;
  }
  return c <= k;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(10);

  cin >> n >> k;
  ll ma = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ma = max(ma, a[i]);
  }
  ll l = 1, r = ma;
  ll ans = 0;
  while (l <= r) {
    ll m = l + (r - l) / 2;
    if (f(m)) {
      ans = m;
      r = m - 1;
    } else
      l = m + 1;
  }
  cout << ans;

  return 0;
}