Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define pb push_back
#define F first
#define S second
#define print(a)                                                               \
  for (auto x : a)                                                             \
    cout << x << " ";                                                          \
  cout << endl
#define print1(a)                                                              \
  for (auto x : a)                                                             \
  cout << x.F << " " << x.S << endl
#define print2(a, x, y)                                                        \
  for (int i = x; i < y; i++)                                                  \
    cout << a[i] << " ";                                                       \
  cout << endl
#define mk(x, y) make_pair(x, y)
typedef long long int ll;
const ll N = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
ll n, k, m, l, r, x, y, q, u, z, a[N];

ll pow_mod_m(ll a, ll n) {
  if (!n)
    return 1;
  ll pt = pow_mod_m(a, n / 2);
  pt *= pt;
  if (n & 1)
    pt *= a;
  return pt;
}

bool check(ll mid) {
  ll steps = 0;
  rep(i, 0, n) { steps += (a[i] - 1) / mid; }
  return (steps <= k);
}

int main() {
  IOS;
  int ts = 1;
  ///  cin >> ts ;
  while (ts--) {
    cin >> n >> k;
    rep(i, 0, n) cin >> a[i];
    ll lo = 0, hi = 1e9, ans;
    while (lo <= hi) {
      ll mid = (lo + hi) >> 1;
      if (check(mid)) {
        hi = mid - 1;
        ans = mid;
      } else {
        lo = mid + 1;
      }
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define pb push_back
#define F first
#define S second
#define print(a)                                                               \
  for (auto x : a)                                                             \
    cout << x << " ";                                                          \
  cout << endl
#define print1(a)                                                              \
  for (auto x : a)                                                             \
  cout << x.F << " " << x.S << endl
#define print2(a, x, y)                                                        \
  for (int i = x; i < y; i++)                                                  \
    cout << a[i] << " ";                                                       \
  cout << endl
#define mk(x, y) make_pair(x, y)
typedef long long int ll;
const ll N = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
ll n, k, m, l, r, x, y, q, u, z, a[N];

ll pow_mod_m(ll a, ll n) {
  if (!n)
    return 1;
  ll pt = pow_mod_m(a, n / 2);
  pt *= pt;
  if (n & 1)
    pt *= a;
  return pt;
}

bool check(ll mid) {
  ll steps = 0;
  rep(i, 0, n) { steps += (a[i] - 1) / mid; }
  return (steps <= k);
}

int main() {
  IOS;
  int ts = 1;
  ///  cin >> ts ;
  while (ts--) {
    cin >> n >> k;
    rep(i, 0, n) cin >> a[i];
    ll lo = 1, hi = 1e9, ans;
    while (lo <= hi) {
      ll mid = (lo + hi) >> 1;
      if (check(mid)) {
        hi = mid - 1;
        ans = mid;
      } else {
        lo = mid + 1;
      }
    }
    cout << ans << endl;
  }
}