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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef double dd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int mx = 1e6 + 11;
const double PI = acos(-1);
#define MOD 1000000007

#define nl '\\n'
#define pb push_back
#define F first
#define S second
#define I insert
#define mp make_pair

#define sz(x) x.size()
#define tt                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define lo(i, a, b) for (int i = a; i < b; i++)
#define be(a) (a).begin(), (a).end()
#define rbe(a) (a).rbegin(), (a).rend()
#define mem(a, b) memset(a, b, sizeof(a))

#define su                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define file                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define frac                                                                   \\
  cout.unsetf(ios::floatfield);                                                \\
  cout.precision(10);                                                          \\
  cout.setf(ios::fixed, ios::floatfield);

#define dbg(args...)                                                           \\
  do {                                                                         \\
    cerr << #args << " : ";                                                    \\
    faltu(args);                                                               \\
  } while (0)
void faltu() { cerr << endl; }
template <typename T, typename... hello>
void faltu(T arg, const hello &...rest) {
  cerr << arg << ' ';
  faltu(rest...);
}

int main() {
  su;
  int n, k;
  cin >> n >> k;
  ll a[n], l = 1, r = 0, sum, mid;
  lo(i, 1, n + 1) {
    cin >> a[i];
    r = max(r, a[i]);
  }
  while (l < r) {
    sum = 0;
    mid = (l + r) / 2;
    lo(i, 1, n + 1) sum += (a[i] - 1) / mid;
    if (sum <= k)
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << nl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef double dd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int mx = 1e6 + 11;
const double PI = acos(-1);
#define MOD 1000000007

#define nl '\\n'
#define pb push_back
#define F first
#define S second
#define I insert
#define mp make_pair

#define sz(x) x.size()
#define tt                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define lo(i, a, b) for (int i = a; i < b; i++)
#define be(a) (a).begin(), (a).end()
#define rbe(a) (a).rbegin(), (a).rend()
#define mem(a, b) memset(a, b, sizeof(a))

#define su                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define file                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define frac                                                                   \\
  cout.unsetf(ios::floatfield);                                                \\
  cout.precision(10);                                                          \\
  cout.setf(ios::fixed, ios::floatfield);

#define dbg(args...)                                                           \\
  do {                                                                         \\
    cerr << #args << " : ";                                                    \\
    faltu(args);                                                               \\
  } while (0)
void faltu() { cerr << endl; }
template <typename T, typename... hello>
void faltu(T arg, const hello &...rest) {
  cerr << arg << ' ';
  faltu(rest...);
}

int main() {
  su;
  int n, k;
  cin >> n >> k;
  ll a[n], l = 0, r = 0, sum, mid;
  lo(i, 0, n) {
    cin >> a[i];
    r = max(r, a[i]);
  }
  while (l < r) {
    sum = 0;
    mid = (l + r) / 2;
    lo(i, 0, n) sum += (a[i] - 1) / mid;
    if (sum <= k)
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << nl;
  return 0;
}