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 ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define pw(b, p) pow(b, p) + 0.1
#define endl '\\n'
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void aksh() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

bool check(int m, vector<int> &a, int k) {
  int n = a.size();
  int c = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > m && m != 0)
      c += (a[i] / m);
  }

  if (c <= k)
    return 1;
  else
    return 0;
}

int32_t main() {
  aksh();
  int n, k;
  cin >> n >> k;

  vector<int> a(n);
  int mx = INT_MIN;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mx = max(mx, a[i]);
  }

  int l = 0, h = mx;
  int ans = mx;

  while (l <= h) {
    int m = (l + h) / 2;
    if (check(m, a, k)) {
      ans = min(ans, m);
      h = m - 1;
    } else
      l = m + 1;
  }

  cout << max(ans, 1ll) << endl;
}

Error: 0

Fixed submission:

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define pw(b, p) pow(b, p) + 0.1
#define endl '\\n'
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void aksh() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

bool check(int m, vector<int> &a, int k) {
  int n = a.size();
  int c = 0;
  for (int i = 0; i < n; i++) {
    c += a[i] / m;
  }

  if (c <= k)
    return 1;
  else
    return 0;
}

int32_t main() {
  aksh();
  int n, k;
  cin >> n >> k;

  vector<int> a(n);
  int mx = INT_MIN;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mx = max(mx, a[i]);
  }

  int l = 0, h = mx;
  int ans = mx;

  while (l <= h) {
    int m = (l + h) / 2;
    if (check(m, a, k)) {
      ans = min(ans, m);
      h = m - 1;
    } else
      l = m + 1;
  }

  cout << ans << endl;
}