Problem p02598 (C++)

Buggy submission (status: Runtime Error):
/*
--------------              |   /
      |                     |  /
      |                     | /
      |             *       |/          |    |         ------            *
      |                     |           |    |        /      \
      |             |       |\          |    |       |       |\          |
   \  |             |       | \         |    |       |       | \         |
    \ |             |       |  \        |    |        \     /   \        |
     V              |       |   \        \__/|         -----     \       |
*/
#include <bits/stdc++.h>
using namespace std;

#define EmiliaMyWife                                                           \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(NULL);
#define mem(i, j) memset(i, j, sizeof(i));
#define F first
#define S second
#define lowbit(x) ((x) & (-(x)))
#define siz(v) (int)(v).size()
typedef int64_t ll;
typedef uint64_t ull;
typedef long double ld;
const double EPS = 1e-8;
const int INF = 0x3F3F3F3F;
const ll LINF = 4611686018427387903;
const int MOD = 1e9 + 7;
/*-----------------------------------------------------------------------------------------------------*/

ll check(ll leg, vector<int> &arr) {
  ll res = 0;
  for (int &a : arr)
    res += (a / leg + !!(a % leg)) - 1;
  return res;
}

signed main() {
  EmiliaMyWife

      int n,
      k;
  cin >> n >> k;
  vector<int> arr(n);
  for (auto &a : arr)
    cin >> a;
  ll l = 0, r = 1e18;
  while (l < r) {
    ll m = (l + r) >> 1;
    if (check(m, arr) <= k)
      r = m;
    else
      l = m + 1;
  }
  cout << l << '\n';

  return 0;
}


Fixed submission:
/*
--------------              |   /
      |                     |  /
      |                     | /
      |             *       |/          |    |         ------            *
      |                     |           |    |        /      \
      |             |       |\          |    |       |       |\          |
   \  |             |       | \         |    |       |       | \         |
    \ |             |       |  \        |    |        \     /   \        |
     V              |       |   \        \__/|         -----     \       |
*/
#include <bits/stdc++.h>
using namespace std;

#define EmiliaMyWife                                                           \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(NULL);
#define mem(i, j) memset(i, j, sizeof(i));
#define F first
#define S second
#define lowbit(x) ((x) & (-(x)))
#define siz(v) (int)(v).size()
typedef int64_t ll;
typedef uint64_t ull;
typedef long double ld;
const double EPS = 1e-8;
const int INF = 0x3F3F3F3F;
const ll LINF = 4611686018427387903;
const int MOD = 1e9 + 7;
/*-----------------------------------------------------------------------------------------------------*/

ll check(ll leg, vector<int> &arr) {
  ll res = 0;
  for (int &a : arr)
    res += (a / leg + !!(a % leg)) - 1;
  return res;
}

signed main() {
  EmiliaMyWife

      int n,
      k;
  cin >> n >> k;
  vector<int> arr(n);
  for (auto &a : arr)
    cin >> a;
  ll l = 1, r = 1e18;
  while (l < r) {
    ll m = (l + r) >> 1;
    if (check(m, arr) <= k)
      r = m;
    else
      l = m + 1;
  }
  cout << l << '\n';

  return 0;
}