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

#include <bits/stdc++.h>
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define Mod 1000000007
// #define Mod 998244353
#define nl "\\n"
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define f(i, a, b) for (ll i = a; i < b; ++i)
#define rf(i, a, b) for (ll i = a; i >= b; --i)
#define vll vector<ll>
#define pll pair<ll, ll>
#define mem(a, b) memset(a, b, sizeof(a))
#define printv(v)                                                              \\
  for (int i = 0; i < v.size(); i++) {                                         \\
    cout << v[i] << " ";                                                       \\
  }                                                                            \\
  cout << nl;
using namespace std;
inline bool kthbit(ll n, int k) { return (n >> k) & 1; }
ll pw(ll a, ll b) {
  ll c = 1, m = a;
  while (b) {
    if (b & 1)
      c = (c * m);
    m = (m * m);
    b /= 2;
  }
  return c;
}
//------------------------------------------------------------------------------------------------

int main() {
  fast;
  ll T = 1;
  // cin>>T; //comment for single test

  while (T--) {
    // code...
    ll N, K;
    cin >> N >> K;
    vll A;
    ll mx = -10e17;
    f(i, 0, N) {
      ll x;
      cin >> x;
      A.pb(x);
      mx = max(mx, x);
    }
    ll low = 1, high = mx, ans = mx;
    while (low <= high) {
      ll mid = low + (high - low) / 2;
      ll cnt = 0;
      f(i, 0, N) {
        if (A[i] > mid) {
          if (A[i] % mid == 0)
            cnt += A[i] / mid - 1;
          else
            cnt += A[i] / mid;
        }
      }
      if (cnt <= K) {
        ans = min(ans, mid);
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    cout << ans << nl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define Mod 1000000007
// #define Mod 998244353
#define nl "\\n"
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define f(i, a, b) for (ll i = a; i < b; ++i)
#define rf(i, a, b) for (ll i = a; i >= b; --i)
#define vll vector<ll>
#define pll pair<ll, ll>
#define mem(a, b) memset(a, b, sizeof(a))
#define printv(v)                                                              \\
  for (int i = 0; i < v.size(); i++) {                                         \\
    cout << v[i] << " ";                                                       \\
  }                                                                            \\
  cout << nl;
using namespace std;
inline bool kthbit(ll n, int k) { return (n >> k) & 1; }
ll pw(ll a, ll b) {
  ll c = 1, m = a;
  while (b) {
    if (b & 1)
      c = (c * m);
    m = (m * m);
    b /= 2;
  }
  return c;
}
//------------------------------------------------------------------------------------------------

int main() {
  fast;
  ll T = 1;
  // cin>>T; //comment for single test

  while (T--) {
    // code...
    ll N, K;
    cin >> N >> K;
    vll A;
    ll mx = -10e17;
    f(i, 0, N) {
      ll x;
      cin >> x;
      A.pb(x);
      mx = max(mx, x);
    }
    ll low = 0, high = mx, ans = mx;
    while (low <= high) {
      ll mid = low + (high - low) / 2;
      ll cnt = 0;
      f(i, 0, N) {
        if (A[i] > mid) {
          if (A[i] % mid == 0)
            cnt += A[i] / mid - 1;
          else
            cnt += A[i] / mid;
        }
      }
      if (cnt <= K) {
        ans = min(ans, mid);
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    cout << ans << nl;
  }
  return 0;
}