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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#define vi vector<int>
#define lvi vector<long long>
#define pi pair<int, int>
#define lpi pair<long long, long long>
#define mp make_pair
#define pb push_back
#define ll long long
#define N 100005
#define faltu                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define mod 1000000007
#define mod1 1000000000000000007
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    multiordered_set;
ll int pw(ll int a, ll int b, ll md) {
  if (b == 0)
    return 1;
  ll int t = pw(a, b / 2, md);
  if (b % 2 == 0)
    return (t * t) % mod;
  else
    return ((t * t) % mod * a) % mod;
}
vector<int> pm;
void prm(int n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
  for (int p = 2; p <= n; p++)
    if (prime[p])
      pm.push_back(p);
}
bool cmp(const pair<ll int, ll int> &a, const pair<ll int, ll int> &b) {
  if (a.first == b.first)
    return a.second < b.second;
  return a.first < b.first;
}
ll int gcd(ll int a, ll int b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}
bool isPrime(int n) {
  if (n <= 1)
    return false;
  for (int i = 2; i < n; i++)
    if (n % i == 0)
      return false;
  return true;
}
map<int, int> pa;
int find(int i) {
  if (pa[i] == i)
    return i;
  return pa[i] = find(pa[i]);
}
void join(int i, int j) {
  int a = find(i);
  int b = find(j);
  if (b >= a)
    pa[b] = a;
  else
    pa[a] = b;
}
int ddx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int ddy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int n, m;
ll p[N], h[N], x[N], y[N];
vector<int> ed[N];
int main() {
  faltu;
  int t = 1;
  // cin>>t;
  while (t--) {
    ll int n, k, final = 0;
    cin >> n >> k;
    ll int a[n + 1];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    ll int l = 1, r = 1e9;
    while (l <= r) {
      ll int mid = (l + r) >> 1;
      ll int ans = 0;
      for (ll int i = 0; i < n; i++) {
        ll int cnt = 0;
        cnt = a[i] / mid;
        if (a[i] % mid)
          cnt++;
        if (cnt)
          ans += cnt - 1;
      }
      if (ans <= k) {
        final = mid;
        r = mid - 1;
      } else
        l = mid + 1;
    }
    cout << final << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#define vi vector<int>
#define lvi vector<long long>
#define pi pair<int, int>
#define lpi pair<long long, long long>
#define mp make_pair
#define pb push_back
#define ll long long
#define N 100005
#define faltu                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define mod 1000000007
#define mod1 1000000000000000007
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    multiordered_set;
ll int pw(ll int a, ll int b, ll md) {
  if (b == 0)
    return 1;
  ll int t = pw(a, b / 2, md);
  if (b % 2 == 0)
    return (t * t) % mod;
  else
    return ((t * t) % mod * a) % mod;
}
vector<int> pm;
void prm(int n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
  for (int p = 2; p <= n; p++)
    if (prime[p])
      pm.push_back(p);
}
bool cmp(const pair<ll int, ll int> &a, const pair<ll int, ll int> &b) {
  if (a.first == b.first)
    return a.second < b.second;
  return a.first < b.first;
}
ll int gcd(ll int a, ll int b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}
bool isPrime(int n) {
  if (n <= 1)
    return false;
  for (int i = 2; i < n; i++)
    if (n % i == 0)
      return false;
  return true;
}
map<int, int> pa;
int find(int i) {
  if (pa[i] == i)
    return i;
  return pa[i] = find(pa[i]);
}
void join(int i, int j) {
  int a = find(i);
  int b = find(j);
  if (b >= a)
    pa[b] = a;
  else
    pa[a] = b;
}
int ddx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int ddy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int n, m;
ll p[N], h[N], x[N], y[N];
vector<int> ed[N];
int main() {
  faltu;
  int t = 1;
  // cin>>t;
  while (t--) {
    ll int n, k, final = 0;
    cin >> n >> k;
    ll int a[n + 1];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    ll int l = 0, r = 1e9;
    while (l <= r) {
      ll int mid = (l + r) >> 1;
      ll int ans = 0;
      for (ll int i = 0; i < n; i++) {
        ll int cnt = 0;
        cnt = a[i] / mid;
        if (a[i] % mid)
          cnt++;
        if (cnt)
          ans += cnt - 1;
      }
      if (ans <= k) {
        final = mid;
        r = mid - 1;
      } else
        l = mid + 1;
    }
    cout << final << endl;
  }
}