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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define forl(i, a, n) for (int i = a; i < n; i++)
#define rfor(i, n, a) for (int i = n; i >= a; i--)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define ld long double
#define vi vector<int>
#define vll vector<long long>
#define pi pair<int, int>
#define pll pair<long long, long long>
#define mp make_pair
#define B begin()
#define E end()
#define S size()
#define m9 1000000007
using namespace std;

ll gcd(ll x, ll y) {
  if (x == 0)
    return y;
  return gcd(y % x, x);
}

/*
ll gcdextended(ll a, ll b, ll *x, ll *y)
{
    if (a == 0)
    {
        *x = 0, *y = 1;
        return b;
    }

    ll x1, y1;
    int gcd = gcdextended(b%a, a, &x1, &y1);

    *x = y1 - (b/a) * x1;
    *y = x1;

    return gcd;
}


///RELATIVE COPRIME
ll modinverse(ll a, ll m)
{
    ll x, y;
   ll g = gcdextended(a, m, &x, &y);
    if (g != 1)
return -1;
    else
    {
        ll res = (x%m + m) % m;
        return res;
    }
}
  */
ll powmod(ll x, ll y, ll m) {
  if (y == 0)
    return 1;
  ll p = powmod(x, y / 2, m) % m;
  p = (p * p) % m;
  return (y % 2 == 0) ? p : (x * p) % m;
}
/// M IS PRIME
ll modif(ll x, ll m) { return (powmod(x, m - 2, m)); }

#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define pin(a)                                                                 \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl
#define fck(a)                                                                 \\
  cout << a;                                                                   \\
  exit(0)
#define vvll vector<vector<ll>>
#define A(b) b.begin(), b.end()
#define vpi std::vector<pll>
#include <array>
#include <stack>
// include<bits/stdc++.h>

bool fa(vll &x, vll &y) { return x[0] < y[0]; }
bool fa1(vll &x, vll &y) { return x[1] < y[1]; }
bool f1(pll &x, pll &y) { return x.second > y.second; }
bool f2(pll &x, pll &y) { return x.first > y.first; }

bool f3(ll &x, ll &y) { return x > y; }

const ll maxn = 2e5 + 2;
vll b;
ll n;
ll k;

bool ch(ll x) {
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    ll y = b[i];
    ll h = y / x;
    if (y % x == 0)
      h--;

    cnt += h;
    // cout<<"\\n";
  }
  return (cnt <= k);
}

int main() {
  fastio;
  // freopen("test_input (1).xt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    b.pb(x);
  }

  ll l = 1, r = 1e11;

  // cout<<ch(4);
  // cout<<ch(262703497);
  while (l < r) {
    ll mid = l + (r - l) / 2ll;

    if (ch(mid)) {
      r = mid;
    } else {
      l = mid + 1ll;
    }
  }
  cout << l;

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define forl(i, a, n) for (int i = a; i < n; i++)
#define rfor(i, n, a) for (int i = n; i >= a; i--)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define ld long double
#define vi vector<int>
#define vll vector<long long>
#define pi pair<int, int>
#define pll pair<long long, long long>
#define mp make_pair
#define B begin()
#define E end()
#define S size()
#define m9 1000000007
using namespace std;

ll gcd(ll x, ll y) {
  if (x == 0)
    return y;
  return gcd(y % x, x);
}

/*
ll gcdextended(ll a, ll b, ll *x, ll *y)
{
    if (a == 0)
    {
        *x = 0, *y = 1;
        return b;
    }

    ll x1, y1;
    int gcd = gcdextended(b%a, a, &x1, &y1);

    *x = y1 - (b/a) * x1;
    *y = x1;

    return gcd;
}


///RELATIVE COPRIME
ll modinverse(ll a, ll m)
{
    ll x, y;
   ll g = gcdextended(a, m, &x, &y);
    if (g != 1)
return -1;
    else
    {
        ll res = (x%m + m) % m;
        return res;
    }
}
  */
ll powmod(ll x, ll y, ll m) {
  if (y == 0)
    return 1;
  ll p = powmod(x, y / 2, m) % m;
  p = (p * p) % m;
  return (y % 2 == 0) ? p : (x * p) % m;
}
/// M IS PRIME
ll modif(ll x, ll m) { return (powmod(x, m - 2, m)); }

#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define pin(a)                                                                 \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl
#define fck(a)                                                                 \\
  cout << a;                                                                   \\
  exit(0)
#define vvll vector<vector<ll>>
#define A(b) b.begin(), b.end()
#define vpi std::vector<pll>
#include <array>
#include <stack>
// include<bits/stdc++.h>

bool fa(vll &x, vll &y) { return x[0] < y[0]; }
bool fa1(vll &x, vll &y) { return x[1] < y[1]; }
bool f1(pll &x, pll &y) { return x.second > y.second; }
bool f2(pll &x, pll &y) { return x.first > y.first; }

bool f3(ll &x, ll &y) { return x > y; }

const ll maxn = 2e5 + 2;
ll b[maxn];
ll n;
ll k;

bool ch(ll x) {
  ll cnt = 0;
  forl(i, 0, n) {
    ll y = b[i];
    ll h = y / x;
    if (y % x == 0)
      h--;

    cnt += h;
    // cout<<"\\n";
  }
  return (cnt <= k);
}

int main() {
  fastio;
  // freopen("test_input (1).txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  cin >> n >> k;
  forl(i, 0, n) cin >> b[i];
  sort(b, b + n);

  ll l = 0, r = 1e11;

  // cout<<ch(4);
  // cout<<ch(262703497);
  while (l < r) {
    ll mid = l + (r - l) / 2;

    if (ch(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << l;

  return 0;
}