Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double lld;
ll MOD = (1e9) + 7;
const int dr[] = {+1, -1, +0, +0, +1, -1, +1, -1};
const int dc[] = {+0, +0, +1, -1, +1, -1, -1, +1};
const int kx[] = {+1, +2, -1, -2, +1, +2, -1, -2};
const int ky[] = {+2, +1, +2, +1, -2, -1, -2, -1};
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
inline void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <typename T, size_t size>
ostream &operator<<(ostream &os, const array<T, size> &arr) {
  os << '{';
  string sep;
  for (const auto &x : arr)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << ' ' << H;
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif
const int MAXN = 202020;
ll n, k;
vector<ll> lens(MAXN);
bool possible(ll val) {
  ll tot = 0;
  for (int i = 0; i < n; i++) {
    if (lens[i] % val == 0) {
      tot += (lens[i] / val) - 1;
    } else {
      tot += ((lens[i] / val));
    }
  }
  return (tot <= k);
}
int main() {
  fastIO();
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> lens[i];
  }
  ll l = 0, h = (1e9) + 5;
  while (l < h) {
    ll mid = (l + h) / 2;
    if (possible(mid)) {
      h = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << l << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double lld;
ll MOD = (1e9) + 7;
const int dr[] = {+1, -1, +0, +0, +1, -1, +1, -1};
const int dc[] = {+0, +0, +1, -1, +1, -1, -1, +1};
const int kx[] = {+1, +2, -1, -2, +1, +2, -1, -2};
const int ky[] = {+2, +1, +2, +1, -2, -1, -2, -1};
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
inline void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <typename T, size_t size>
ostream &operator<<(ostream &os, const array<T, size> &arr) {
  os << '{';
  string sep;
  for (const auto &x : arr)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << ' ' << H;
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif
const int MAXN = 202020;
ll n, k;
vector<ll> lens(MAXN);
bool possible(ll val) {
  ll tot = 0;
  for (int i = 0; i < n; i++) {
    if (lens[i] % val == 0) {
      tot += (lens[i] / val) - 1;
    } else {
      tot += ((lens[i] / val));
    }
  }
  return (tot <= k);
}
int main() {
  fastIO();
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> lens[i];
  }
  ll l = 0, h = (1e9) + 5;
  while (l < h) {
    ll mid = (l + h) / 2;
    if (mid == 0) {
      l = 1;
      break;
    }
    if (possible(mid)) {
      h = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << l << endl;
  return 0;
}