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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ll long long
#define int long long int
#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define modp 998244353
#define inf (ll)1e18
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
int a[200001];
int n, k;

bool check(int len) {
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    // if(len != 0)
    cnt += ((a[i] + len - 1) / len - 1);
  }
  return cnt <= k;
}

void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  if (k == 0)
    cout << a[n - 1] << endl;
  else {
    int l = 1, r = a[n - 1];
    int mn = 0;
    while (l <= r) {
      int mid = l + (r - l) / 2;
      if (check(mid)) {
        mn = mid;
        r = mid - 1;
      } else
        l = mid + 1;
    }
    cout << mn;
  }
}

int32_t main() {
  FIO
  // w(t)
  {
    solve();
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ll long long
#define int long long int
#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define modp 998244353
#define inf (ll)1e18
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
int a[200001];
int n, k;

bool check(int len) {
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += (a[i] + len - 1) / len - 1;
  }
  return cnt <= k;
}

void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  if (k == 0)
    cout << a[n - 1] << endl;
  else {
    int l = 0, r = a[n - 1];
    int mn = 0;
    while (l <= r) {
      int mid = l + (r - l) / 2;
      if (check(mid)) {
        mn = mid;
        r = mid - 1;
      } else
        l = mid + 1;
    }
    cout << mn;
  }
}

int32_t main() {
  FIO
  // w(t)
  {
    solve();
  }
  return 0;
}