Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define debug_array(a, n)                                                      \
  for (ll bccc = (0); bccc < (n); bccc++)                                      \
  cout << a[bccc] << " "                                                       \
       << "\n"
#define debug_vector(a)                                                        \
  for (ll bccc = (0); bccc < (a.size()); bccc++)                               \
  cout << a[bccc] << " "                                                       \
       << "\n"
#define F first
#define S second
#define debug_set(s)                                                           \
  for (auto p : s)                                                             \
  cout << p << " "
#define debug_map(m)                                                           \
  for (auto p : m)                                                             \
  cout << p.F << " " << p.S << "\n"
#define trace(x) cerr << #x << ": " << x << " " << endl;
typedef long long ll;
using namespace __gnu_pbds;
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_multiset;

/* ll t = 0;
ordered_multiset me;
...
me.insert({x, t++});
me.erase(me.lower_bound({x, 0}));
cout << me.order_of_key({x, 0}) << "\n";

///////////   t is insertion time
*/
// #pragma GCC optimize("Ofast")  // works very well
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long double lld;
lld pi = 3.14159265358;
ll powr(ll a, ll b) {
  ll c = 1;
  ll kjk;
  for (kjk = 0; kjk < b; kjk++)
    c *= a;
  return c;
}
ll hell = 1000000007;
// ll hell=998244353   ;
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
//  to generate random number , use rng() ... eg cout<<rng();
bool check(ll a[], ll n, ll mid, ll k) {
  ll ans = 0;
  for (int i = 0; i < n; i++)
    ans += max(0LL, ((a[i] + mid - 1) / mid - 1));
  return ans <= k;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, k, i;
  cin >> n >> k;
  ll a[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  ll lo = 0;
  ll hi = hell;
  ll mid, ans;
  while (lo <= hi) {
    mid = ((hi + lo) / 2);
    if (check(a, n, mid, k)) {
      ans = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define debug_array(a, n)                                                      \
  for (ll bccc = (0); bccc < (n); bccc++)                                      \
  cout << a[bccc] << " "                                                       \
       << "\n"
#define debug_vector(a)                                                        \
  for (ll bccc = (0); bccc < (a.size()); bccc++)                               \
  cout << a[bccc] << " "                                                       \
       << "\n"
#define F first
#define S second
#define debug_set(s)                                                           \
  for (auto p : s)                                                             \
  cout << p << " "
#define debug_map(m)                                                           \
  for (auto p : m)                                                             \
  cout << p.F << " " << p.S << "\n"
#define trace(x) cerr << #x << ": " << x << " " << endl;
typedef long long ll;
using namespace __gnu_pbds;
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_multiset;

/* ll t = 0;
ordered_multiset me;
...
me.insert({x, t++});
me.erase(me.lower_bound({x, 0}));
cout << me.order_of_key({x, 0}) << "\n";

///////////   t is insertion time
*/
// #pragma GCC optimize("Ofast")  // works very well
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long double lld;
lld pi = 3.14159265358;
ll powr(ll a, ll b) {
  ll c = 1;
  ll kjk;
  for (kjk = 0; kjk < b; kjk++)
    c *= a;
  return c;
}
ll hell = 1000000007;
// ll hell=998244353   ;
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
//  to generate random number , use rng() ... eg cout<<rng();
bool check(ll a[], ll n, ll mid, ll k) {
  ll ans = 0;
  for (int i = 0; i < n; i++)
    ans += max(0LL, ((a[i] + mid - 1) / mid - 1));
  return ans <= k;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, k, i;
  cin >> n >> k;
  ll a[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  ll lo = 1;
  ll hi = hell;
  ll mid, ans;
  while (lo <= hi) {
    mid = ((hi + lo) / 2);
    if (check(a, n, mid, k)) {
      ans = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << ans;
  return 0;
}