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

/*****************************************************
 *     created by: nayakashutosh9
 * "Winners Never Quit and Quitters Never Win".
 *****************************************************/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
#define int long long int
#define endl '\\n'
#define mod 1000000007
#define inf 1e18
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
const int N = 300005;
/**********************************************************************************************/

vector<int> a;
bool check(int n, int cur, int k) {
  int c = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] <= cur)
      continue;
    c += (a[i] / cur);
    if (c > k)
      return false;
  }
  return c <= k;
}
void solve() {
  int n, k;
  cin >> n >> k;
  a.clear();
  a.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int mid, ans, lo = 1, hi = *max_element(a.begin(), a.end());
  ans = hi;
  while (lo <= hi) {
    mid = (lo + hi) / 2;
    if (check(n, mid, k)) {
      ans = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << ans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1, z = 1;
  // cin >> t; cin.ignore();
  while (t--) {
    // cout << "Case #" << (z++) << ": ";
    // clock_t start = clock();
    solve();
    // clock_t end = clock();
    // cout << (end-start) << endl;
    // print execution time in ms
  }

  return 0;
}

Error: 0

Fixed submission:

/*****************************************************
 *     created by: nayakashutosh9
 * "Winners Never Quit and Quitters Never Win".
 *****************************************************/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
#define int long long int
#define endl '\\n'
#define mod 1000000007
#define inf 1e18
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
const int N = 300005;
/**********************************************************************************************/

vector<int> a;
bool check(int n, int cur, int k) {
  int c = 0;
  for (int i = 0; i < n; i++) {
    c += (a[i] / cur);
  }
  return c <= k;
}
void solve() {
  int n, k;
  cin >> n >> k;
  a.clear();
  a.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int mid, ans, lo = 0, hi = *max_element(a.begin(), a.end());
  ans = hi;
  while (lo <= hi) {
    mid = (lo + hi) / 2;
    if (check(n, mid, k)) {
      ans = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << ans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1, z = 1;
  // cin >> t; cin.ignore();
  while (t--) {
    // cout << "Case #" << (z++) << ": ";
    // clock_t start = clock();
    solve();
    // clock_t end = clock();
    // cout << (end-start) << endl;
    // print execution time in ms
  }

  return 0;
}