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

#include <bits/stdc++.h>
#define ll long long
#define db double
#define pb push_back
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define all(a) a.begin(), a.end()
using namespace std;

ll mod = 1e9 + 7;
const ll M = 500006;
ll f[M], inv[M];
ll expo(ll base, ll exponent) {
  ll ans = 1;
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base) % mod;
    base = (1LL * base * base) % mod;
    exponent >>= 1;
  }
  return ans % mod;
}
void compute() {
  f[0] = 1;
  rep(i, 1, M - 1) { f[i] = (1LL * i * f[i - 1]) % mod; }
  inv[M - 1] = expo(f[M - 1], mod - 2);
  for (ll i = M - 2; i >= 0; i--) {
    inv[i] = (1LL * (i + 1) * inv[i + 1]) % mod;
  }
}
ll C(ll n, ll r) {
  return (1LL * ((1LL * f[n] * inv[r]) % mod) * inv[n - r]) % mod;
}

vector<bool> prime(1000002, true);
void Sieve() {

  for (int p = 2; p * p <= 1000001; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      for (int i = p * p; i <= 1000001; i += p)
        prime[i] = false;
    }
  }
}

vector<ll> a(2000000);
ll func(ll val, ll n) {
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    ll tmp = a[i] / val;
    if (a[i] % val == 0)
      tmp--;
    cnt += tmp;
  }
  return cnt;
}

int main() {
  // #ifndef ONLINE_JUDGE
  //    // for getting input from input.txt
  // freopen("alchemy_input.txt", "r", stdin);
  //    // for writing output to output.txt
  // freopen("output.txt", "w", stdout);
  //    #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // compute();
  ll tests = 1, t = 1;
  // cin>>tests;
  while (tests--) {
    ll i, j, n, k;
    cin >> n >> k;
    ll mx = 0;
    rep(i, 0, n - 1) {
      cin >> a[i];
      mx = max(mx, a[i]);
    }
    if (k == 0) {
      cout << mx;
      return 0;
    }
    ll lo = 1, hi = 1e9;
    while (hi >= lo) {
      ll mid = (lo + hi) / 2;
      ll tmp = func(mid, n);
      // cout<<tmp<<"\\n";
      if (tmp > k)
        lo = mid + 1;
      else
        hi = mid - 1;
    }
    cout << lo;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define db double
#define pb push_back
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define all(a) a.begin(), a.end()
using namespace std;

ll mod = 1e9 + 7;
const ll M = 500006;
ll f[M], inv[M];
ll expo(ll base, ll exponent) {
  ll ans = 1;
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base) % mod;
    base = (1LL * base * base) % mod;
    exponent >>= 1;
  }
  return ans % mod;
}
void compute() {
  f[0] = 1;
  rep(i, 1, M - 1) { f[i] = (1LL * i * f[i - 1]) % mod; }
  inv[M - 1] = expo(f[M - 1], mod - 2);
  for (ll i = M - 2; i >= 0; i--) {
    inv[i] = (1LL * (i + 1) * inv[i + 1]) % mod;
  }
}
ll C(ll n, ll r) {
  return (1LL * ((1LL * f[n] * inv[r]) % mod) * inv[n - r]) % mod;
}

vector<bool> prime(1000002, true);
void Sieve() {

  for (int p = 2; p * p <= 1000001; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      for (int i = p * p; i <= 1000001; i += p)
        prime[i] = false;
    }
  }
}

vector<ll> a(2000000);
ll func(ll val, ll n) {
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    ll tmp = a[i] / val;
    if (a[i] % val == 0)
      tmp--;
    cnt += tmp;
  }
  return cnt;
}

int main() {
  // #ifndef ONLINE_JUDGE
  //    // for getting input from input.txt
  // freopen("alchemy_input.txt", "r", stdin);
  //    // for writing output to output.txt
  // freopen("output.txt", "w", stdout);
  //    #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // compute();
  ll tests = 1, t = 1;
  // cin>>tests;
  while (tests--) {
    ll i, j, n, k;
    cin >> n >> k;
    ll mx = 0;
    rep(i, 0, n - 1) {
      cin >> a[i];
      mx = max(mx, a[i]);
    }
    if (k == 0) {
      cout << mx;
      return 0;
    }
    ll lo = 0, hi = 1e9;
    while (hi >= lo) {
      ll mid = (lo + hi) / 2;
      ll tmp = func(mid, n);
      // cout<<tmp<<"\\n";
      if (tmp > k)
        lo = mid + 1;
      else
        hi = mid - 1;
    }
    cout << lo;
  }

  return 0;
}