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

#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
using ll = long long;
#define ull unsigned ll
#define endl "\\n"
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define mst(a, b) memset(a, b, sizeof(a))
#define pi pair<int, int>
#define pl pair<ll, ll>
#define ppi pair<int, pii>
#define mt make_tuple
#define eb emplace_back
using namespace std;

const ll N = 2e5 + 5, mod = 1e9 + 7;
const int inf = 1e9 + 5;

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  if (a.F == b.F)
    return a.S < b.S;
  return a.F > b.F;
}
ll power(ll x, ll p) {
  ll r = 1ll;
  x = x % mod;
  while (p > 0) {
    if (p & 1)
      r = (r * x) % mod;
    p = p >> 1;
    x = (x * x) % mod;
  }
  return r;
}

void solve() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans = max(ans, a[i]);
  }
  int l = 0, r = inf;
  while (l <= r) {
    int mid = (l + r) / 2;
    int cnt = 0;
    if (mid == 0) {
      cout << 1 << endl;
      return;
    }
    for (int i = 0; i < n; i++) {
      cnt += (a[i] - 1) / mid;
      if (cnt > k)
        break;
    }
    if (cnt > k) {
      l = mid + 1;
    } else {
      r = mid - 1;
      ans = mid;
    }
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); // cout.tie(0);
  /*#ifndef ONLINE_JUDGE
      freopen("Input.txt", "r", stdin);
  #endif*/
  // freopen("running_on_fumes_chapter_1_input (1).txt", "r", stdin);
  // freopen("Output.txt", "w", stdout);
  int t = 1, p;
  // cin >> t;
  for (p = 1; p <= t; p++) {
    // cout << "Case #" << p << ": ";
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
using ll = long long;
#define ull unsigned ll
#define endl "\\n"
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define mst(a, b) memset(a, b, sizeof(a))
#define pi pair<int, int>
#define pl pair<ll, ll>
#define ppi pair<int, pii>
#define mt make_tuple
#define eb emplace_back
using namespace std;

const ll N = 2e5 + 5, mod = 1e9 + 7;
const int inf = 1e9 + 5;

bool cmp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  if (a.F == b.F)
    return a.S < b.S;
  return a.F > b.F;
}
ll power(ll x, ll p) {
  ll r = 1ll;
  x = x % mod;
  while (p > 0) {
    if (p & 1)
      r = (r * x) % mod;
    p = p >> 1;
    x = (x * x) % mod;
  }
  return r;
}

void solve() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans = max(ans, a[i]);
  }
  int l = 0, r = inf;
  while (l <= r) {
    int mid = (l + r) / 2;
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += (a[i] - 1) / mid;
      if (cnt > k)
        break;
    }
    if (cnt > k) {
      l = mid + 1;
    } else {
      r = mid - 1;
      ans = mid;
    }
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); // cout.tie(0);
  /*#ifndef ONLINE_JUDGE
      freopen("Input.txt", "r", stdin);
  #endif*/
  // freopen("running_on_fumes_chapter_1_input (1).txt", "r", stdin);
  // freopen("Output.txt", "w", stdout);
  int t = 1, p;
  // cin >> t;
  for (p = 1; p <= t; p++) {
    // cout << "Case #" << p << ": ";
    solve();
  }
  return 0;
}