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

// R<3S
#include <bits/stdc++.h>
#define hell 1000000007
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\\n"
#define vecmax(v) max_element(all(v))
#define vecmin(v) min_element(all(v))
#define GCD(m, n) __gcd(m, n)
#define LCM(m, n) m *(n / GCD(m, n))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(x) for (auto i : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define int long long
#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vii vector<pii>
#define mii map<int, int>
#define mll map<ll, ll>

using namespace std;
int a[200005];
int n, k;
bool check(int mid) {
  int cnt = 0;
  repD(i, n - 1, 0) {
    if (a[i] <= mid)
      continue;
    int temp = a[i];

    cnt += temp / mid;
    if (cnt > k)
      return false;
  }
  return cnt <= k;
}
void solve() {

  cin >> n >> k;
  int maxm = 0;
  int ans;
  rep(i, n) cin >> a[i], maxm = max(maxm, a[i]);
  sort(a, a + n);
  int l = 1, r = maxm;

  while (l <= r) {
    int mid = l + (r - l) / 2;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
}

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

  return 0;
}


Error: WA

Fixed submission:

// R<3S
#include <bits/stdc++.h>
#define hell 1000000007
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\\n"
#define vecmax(v) max_element(all(v))
#define vecmin(v) min_element(all(v))
#define GCD(m, n) __gcd(m, n)
#define LCM(m, n) m *(n / GCD(m, n))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(x) for (auto i : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define int long long
#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vii vector<pii>
#define mii map<int, int>
#define mll map<ll, ll>

using namespace std;
int a[200005];
int n, k;
bool check(int mid) {
  int cnt = 0;
  repD(i, n - 1, 0) {
    if (a[i] <= mid)
      continue;
    int temp = a[i];
    cnt += temp / mid;
    if (cnt > k)
      return false;
  }
  return cnt <= k;
}
void solve() {

  cin >> n >> k;
  int maxm = 0;
  int ans;
  rep(i, n) cin >> a[i], maxm = max(maxm, a[i]);
  sort(a, a + n);
  int l = 0, r = maxm;

  while (l <= r) {
    int mid = l + (r - l) / 2;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
}

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

  return 0;
}