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

/********************************
*** ******** ********** *********
*** * ****** ********* * ********
*** ** ***** ******** *** *******
*** *** **** ******* ***** ******
*** **** *** ****** ******* *****
*** ***** ** ***** +++++++++ ****
*** ****** * **** *********** ***
*** *******  *** ************* **
*** ******** ** *************** *
**********_**********************
* |   /  / \\  \\     /    /\\     *
* |  /  |   |  \\   /    /  \\    *
* | /   |   |   \\ /    /    \\   *
* |+\\   |   |    |    /______\\  *
* |  \\  |   |    |   |        | *
* |   \\  \\_/     |   |        | *
********************************/
#include <bits/stdc++.h>
#define lli long long int
#define MOD 1000000007
#define KING 1000000002
#define queen 1000002
#define fi first
#define se second
#define se second
#define lsb(i) (i & -i)
#define Pi 3.141592653589793238
#define nl cout << '\\n'
#define TEST cout << "TEST", nl
#define all(a) a.begin(), a.end()
#define remax(a, b) (a = ((a < b) ? b : a))
#define remin(a, b) (a = ((a < b) ? a : b))
#define pr pair<lli, lli>
#define pb push_back
#define eb emplace_back
#define precise(a) cout << fixed << setprecision(a)
#define mp make_pair
#define vi vector<lli>
#define vpi vector<pr>
#define P pair<lli, pr>
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define NA(i, s, n) for (lli i = s; i < n; i++)
using namespace std;

lli power(lli x, lli y, lli p = (lli)(1e13)) {
  lli res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

const lli N = 2e5 + 2;
lli n, a[N], mx = -1, k;

bool fun(lli x) {
  lli sm = 0;
  NA(i, 0, n) sm += ((a[i] / x) - ((a[i] % x == 0 ? 1 : 0)));
  return sm <= k;
}

void solve() {
  cin >> n >> k;
  NA(i, 0, n) cin >> a[i], mx = max(mx, a[i]);
  lli l = 0, r = mx, ans = r;
  while (l + 1 < r) {
    lli mid = (l + r) >> 1;
    if (fun(mid))
      ans = mid, r = mid;
    else
      l = mid;
  }
  cout << ans;
  return;
}

int main() {
  fastIO;
  lli t = 1;
  // cin >> t;
  NA(i, 0, t) {
    // cout << "Case #" << i+1 << ": \\n";
    solve();
  }
  return 0;
}


Error: WA

Fixed submission:

/********************************
*** ******** ********** *********
*** * ****** ********* * ********
*** ** ***** ******** *** *******
*** *** **** ******* ***** ******
*** **** *** ****** ******* *****
*** ***** ** ***** +++++++++ ****
*** ****** * **** *********** ***
*** *******  *** ************* **
*** ******** ** *************** *
**********_**********************
* |   /  / \\  \\     /    /\\     *
* |  /  |   |  \\   /    /  \\    *
* | /   |   |   \\ /    /    \\   *
* |+\\   |   |    |    /______\\  *
* |  \\  |   |    |   |        | *
* |   \\  \\_/     |   |        | *
********************************/
#include <bits/stdc++.h>
#define lli long long int
#define MOD 1000000007
#define KING 1000000002
#define queen 1000002
#define fi first
#define se second
#define se second
#define lsb(i) (i & -i)
#define Pi 3.141592653589793238
#define nl cout << '\\n'
#define TEST cout << "TEST", nl
#define all(a) a.begin(), a.end()
#define remax(a, b) (a = ((a < b) ? b : a))
#define remin(a, b) (a = ((a < b) ? a : b))
#define pr pair<lli, lli>
#define pb push_back
#define eb emplace_back
#define precise(a) cout << fixed << setprecision(a)
#define mp make_pair
#define vi vector<lli>
#define vpi vector<pr>
#define P pair<lli, pr>
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define NA(i, s, n) for (lli i = s; i < n; i++)
using namespace std;

lli power(lli x, lli y, lli p = (lli)(1e13)) {
  lli res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

const lli N = 2e5 + 2;
lli n, a[N], mx = -1, k;

bool fun(lli x) {
  lli sm = 0;
  NA(i, 0, n) sm += ((a[i] / x) - ((a[i] % x == 0 ? 1 : 0)));
  return sm <= k;
}

void solve() {
  cin >> n >> k;
  NA(i, 0, n) cin >> a[i], mx = max(mx, a[i]);
  lli l = 0, r = mx, ans = r;
  while (l <= r) {
    lli mid = (l + r) >> 1;
    if (fun(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans;
  return;
}

int main() {
  fastIO;
  lli t = 1;
  // cin >> t;
  NA(i, 0, t) {
    // cout << "Case #" << i+1 << ": \\n";
    solve();
  }
  return 0;
}