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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD 1000000007ll
#define pb push_back
#define vl vector<ll>
#define pll pair<ll, ll>
#define mp make_pair
using namespace std;
#define pi (long double)3.14159265358979323846
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
struct sch {
  ll s, e, t;
};
bool sortinrev(const struct sch &a, const struct sch &b) { return (a.s < b.s); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
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; // y = y/2
    x = (x * x) % p;
  }
  return res;
}
bool check(ll a[], ll n, ll k, ll len) {
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    ll x = (a[i] + len - 1) / len - 1;
    if (x > 0)
      cnt += x;
  }
  if (cnt <= k)
    return true;
  return false;
}
int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  if (k == 0) {
    cout << *max_element(a, a + n) << endl;
    return 0;
  }
  ll x = *max_element(a, a + n);
  ll l = 1, r = x, mid;
  while (1) {
    // cout<<l<<" "<<r<<endl;
    mid = (l + r) / 2;
    if (check(a, n, k, mid)) {
      if (mid == 1 || !check(a, n, k, mid - 1))
        break;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << mid << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD 1000000007ll
#define pb push_back
#define vl vector<ll>
#define pll pair<ll, ll>
#define mp make_pair
using namespace std;
#define pi (long double)3.14159265358979323846
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
struct sch {
  ll s, e, t;
};
bool sortinrev(const struct sch &a, const struct sch &b) { return (a.s < b.s); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
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; // y = y/2
    x = (x * x) % p;
  }
  return res;
}
bool check(ll a[], ll n, ll k, ll len) {
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    ll x = (a[i] + len - 1) / len - 1;
    if (x > 0)
      cnt += x;
  }
  if (cnt <= k)
    return true;
  return false;
}
int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  if (k == 0) {
    cout << *max_element(a, a + n) << endl;
    return 0;
  }
  ll x = *max_element(a, a + n);
  ll l = 0, r = x, mid;
  while (1) {
    //      cout<<l<<" "<<r<<endl;
    mid = (l + r) / 2;
    if (check(a, n, k, mid)) {
      if (mid == 0 || !check(a, n, k, mid - 1))
        break;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << mid << endl;
}