Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef long double ld;
const double E = 1e-7;
const ll INF = 1e17;
#define forn(i, s, f) for (int i = s; i < f; ++i)
#define ft first
#define sec second
#define fora(i, n) for (auto i : n)
#define sz(a) (int)(a).size()
#define sort_(a) sort(a.begin(), a.end())
#define pb push_back
#define mp make_pair
#define rsort_(a)                                                              \
  sort(a.begin(), a.end());                                                    \
  reverse(a.begin(), a.end())
#define fast_ cin.tie(0), ios_base::sync_with_stdio(false)
// ____________________________________________________________
#define cina(a) forn(i, 0, sz(a)) cin >> a[i]
#define cinpr(a) cin >> a.ft >> a.sec
//--------------------------------------------------------------
#define int long long

bool f(int m, const vector<int> &a, int k) {
  int ans = 0;
  forn(i, 0, a.size()) {
    int cp = a[i];
    int deep = 0;
    if (a[i] > m)
      ans += (a[i] / m);
    /*while (cp > m) {
        ans += (1<<deep);
        deep++;
        cp = cp / 2 + (cp % 2 != 0);
    }*/
  }
  if (ans <= k)
    return true;
  return false;
}

signed main() {
  fast_;
  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  forn(i, 0, n) cin >> a[i];

  int l = 0, r = 1e9;
  while (l + 1 < r) {
    int m = (l + r) / 2;
    if (f(m, a, k)) {
      r = m;
    } else {
      l = m;
    }
  }
  // cout << l << '\n';
  if (f(l, a, k))
    cout << l;
  else
    cout << r << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef long double ld;
const double E = 1e-7;
const ll INF = 1e17;
#define forn(i, s, f) for (int i = s; i < f; ++i)
#define ft first
#define sec second
#define fora(i, n) for (auto i : n)
#define sz(a) (int)(a).size()
#define sort_(a) sort(a.begin(), a.end())
#define pb push_back
#define mp make_pair
#define rsort_(a)                                                              \
  sort(a.begin(), a.end());                                                    \
  reverse(a.begin(), a.end())
#define fast_ cin.tie(0), ios_base::sync_with_stdio(false)
// ____________________________________________________________
#define cina(a) forn(i, 0, sz(a)) cin >> a[i]
#define cinpr(a) cin >> a.ft >> a.sec
//--------------------------------------------------------------
#define int long long

bool f(int m, const vector<int> &a, int k) {
  int ans = 0;
  forn(i, 0, a.size()) {
    int cp = a[i];
    int deep = 0;
    if (a[i] > m)
      ans += (a[i] / m);
    /*while (cp > m) {
        ans += (1<<deep);
        deep++;
        cp = cp / 2 + (cp % 2 != 0);
    }*/
  }
  if (ans <= k)
    return true;
  return false;
}

signed main() {
  fast_;
  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  forn(i, 0, n) cin >> a[i];

  int l = 1, r = 1e10;
  while (l + 1 < r) {
    int m = (l + r) / 2;
    if (f(m, a, k)) {
      r = m;
    } else {
      l = m;
    }
  }
  // cout << l << '\n';
  if (f(l, a, k))
    cout << l;
  else
    cout << r << '\n';
}