Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define ld long double
#define mp make_pair
#define pii pair<int, int>
#define mod 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)
#define repp(i, a, n) for (int i = a; i < n; i++)
#define all(v) v.begin(), v.end()
#define input(arr, n)                                                          \
  for (ll i1 = 0; i1 < n; i1++)                                                \
  cin >> arr[i1]
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \
       tree_order_statistics_node_update> // s.order_of_key(val)
                                          // *s.find_by_order(ind)
int main() {
  fio;
  int t = 1;
  // cin>>t;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    ll a[n];
    rep(i, n) cin >> a[i];
    ll l = 0, r = (ll)1e9, mid = 0;
    ll res = INT_MAX;
    while (l <= r) {
      mid = l + (r - l) / 2;
      int trials = 0;
      rep(i, n) {
        if (a[i] > mid) {
          trials += ((a[i] + mid - 1) / mid) - 1;
        }
      }
      if (trials > k)
        l = mid + 1;
      else {
        r = mid - 1;
        res = min(res, mid);
      }
    }
    cout << res << endl;
  }
}

Fixed submission:
#pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define ld long double
#define mp make_pair
#define pii pair<int, int>
#define mod 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)
#define repp(i, a, n) for (int i = a; i < n; i++)
#define all(v) v.begin(), v.end()
#define input(arr, n)                                                          \
  for (ll i1 = 0; i1 < n; i1++)                                                \
  cin >> arr[i1]
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \
       tree_order_statistics_node_update> // s.order_of_key(val)
                                          // *s.find_by_order(ind)
int main() {
  fio;
  int t = 1;
  // cin>>t;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    ll a[n];
    rep(i, n) cin >> a[i];
    ll l = 1, r = (ll)1e9, mid = 0;
    ll res = INT_MAX;
    while (l <= r) {
      mid = l + (r - l) / 2;
      int trials = 0;
      rep(i, n) {
        if (a[i] > mid) {
          trials += ((a[i] + mid - 1) / mid) - 1;
        }
      }
      if (trials > k)
        l = mid + 1;
      else {
        r = mid - 1;
        res = min(res, mid);
      }
    }
    cout << res << endl;
  }
}