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

#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\\n")
#define NO() printf("NO\\n")
#define Yes() printf("Yes\\n")
#define No() printf("No\\n")
#define ll long long
#define ld long double
#define pb push_back
#define MOD 1000000007
#define LLMN LONG_LONG_MIN
#define LLMX LONG_LONG_MAX
#define LDMX LDBL_MAX
#define LDMN LDBL_MIN
#define MAX 1000000
#define pll pair<ll, ll>
#define vl vector<ll>
#define vll vector<vector<ll>>
#define vb vector<bool>
#define vbb vector<vector<bool>>

ll mulmod(ll, ll);
ll fastpower(ll, ll);
void getprime(vector<bool> &);
void getfact(vector<ll> &, vector<ll> &);
ll ncr(ll, ll, vector<ll> &, vector<ll> &);

void solve() {
  ll n, k;
  cin >> n >> k;
  vl a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll low = 1, high = 0;
  for (auto i : a)
    high = max(high, i);
  ll ans = -1;
  while (low <= high) {
    ll mid = low + (high - low) / 2;
    ll need = 0;
    for (ll i = 0; i < n; i++) {
      need += ceil(1.0 * a[i] / mid) - 1;
    }
    if (need <= k) {
      ans = mid;
      high = mid - 1;
    } else
      low = mid + 1;
  }
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

ll mulmod(ll a, ll b) {
  ll res = 0;
  a = a % MOD;
  while (b > 0) {
    if (b % 2 == 1)
      res = (res + a) % MOD;

    a = (a * 2) % MOD;
    b /= 2;
  }
  return res % MOD;
}

ll fastpower(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n % 2 != 0) {
      res = (res * a) % MOD;
    }
    n /= 2;
    a = (a * a) % MOD;
  }
  return res;
}

void getprime(vector<bool> &prime) {
  prime[0] = false;
  prime[1] = false;

  for (ll i = 2; i * i <= MAX; i++) {
    if (prime[i]) {
      for (ll j = i * i; j <= MAX; j += i)
        prime[j] = false;
    }
  }
}

void getfact(vector<ll> &fact, vector<ll> &ifact) {
  fact[0] = 1;
  ifact[0] = 1;

  for (ll i = 1; i <= MAX; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
    ifact[i] = (ifact[i - 1] * fastpower(i, MOD - 2)) % MOD;
  }
}

ll ncr(ll n, ll r, vector<ll> &fact, vector<ll> &ifact) {
  ll res;
  res = (((fact[n] * ifact[r]) % MOD) * ifact[n - r]) % MOD;
  return res;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\\n")
#define NO() printf("NO\\n")
#define Yes() printf("Yes\\n")
#define No() printf("No\\n")
#define ll long long
#define ld long double
#define pb push_back
#define MOD 1000000007
#define LLMN LONG_LONG_MIN
#define LLMX LONG_LONG_MAX
#define LDMX LDBL_MAX
#define LDMN LDBL_MIN
#define MAX 1000000
#define pll pair<ll, ll>
#define vl vector<ll>
#define vll vector<vector<ll>>
#define vb vector<bool>
#define vbb vector<vector<bool>>

ll mulmod(ll, ll);
ll fastpower(ll, ll);
void getprime(vector<bool> &);
void getfact(vector<ll> &, vector<ll> &);
ll ncr(ll, ll, vector<ll> &, vector<ll> &);

void solve() {
  ll n, k;
  cin >> n >> k;
  vl a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll low = 0, high = 0;
  for (auto i : a)
    high = max(high, i);
  ll ans = low;
  while (low != high) {
    ll mid = low + (high - low) / 2;
    ll need = 0;
    for (ll i = 0; i < n; i++) {
      need += (a[i] - 1) / mid;
    }
    if (need <= k) {
      ans = mid;
      high = mid;
    } else
      low = mid + 1;
  }
  cout << low;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

ll mulmod(ll a, ll b) {
  ll res = 0;
  a = a % MOD;
  while (b > 0) {
    if (b % 2 == 1)
      res = (res + a) % MOD;

    a = (a * 2) % MOD;
    b /= 2;
  }
  return res % MOD;
}

ll fastpower(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n % 2 != 0) {
      res = (res * a) % MOD;
    }
    n /= 2;
    a = (a * a) % MOD;
  }
  return res;
}

void getprime(vector<bool> &prime) {
  prime[0] = false;
  prime[1] = false;

  for (ll i = 2; i * i <= MAX; i++) {
    if (prime[i]) {
      for (ll j = i * i; j <= MAX; j += i)
        prime[j] = false;
    }
  }
}

void getfact(vector<ll> &fact, vector<ll> &ifact) {
  fact[0] = 1;
  ifact[0] = 1;

  for (ll i = 1; i <= MAX; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
    ifact[i] = (ifact[i - 1] * fastpower(i, MOD - 2)) % MOD;
  }
}

ll ncr(ll n, ll r, vector<ll> &fact, vector<ll> &ifact) {
  ll res;
  res = (((fact[n] * ifact[r]) % MOD) * ifact[n - r]) % MOD;
  return res;
}