Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;

template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

template <typename T> map<T, T> getPrimeFactor(T n) {
  map<T, T> res;
  for (T i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}

template <typename T> bool IsPrimeNumber(T num) {
  if (num <= 2)
    return true;
  else if (num % 2 == 0)
    return false;

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }

  return true;
}

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// 繰り返し二乗法
ll pow2(ll n, ll p, ll mod) {
  if (p == 0 || n == 1) {
    return 1;
  } else {
    ll ret = pow2(n * n % mod, p / 2, mod);
    if (p % 2 == 1) {
      ret *= n;
    }
    return ret % mod;
  }
}

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define repeq(i, s, e) for (ll i = s; i <= e; i++)

int main() {
  ll N, K;
  cin >> N >> K;

  priority_queue<ll> pq;
  rep(i, 0, N) {
    ll a;
    cin >> a;
    pq.push(a);
  }

  ll l_min = 0;
  ll l_max = pq.top();
  ll ans;

  while (l_min != l_max) {
    ans = (l_max - l_min) / 2 + l_min;
    priority_queue<ll> pq2 = pq;

    ll cnt = 0;
    while (cnt <= K && !pq2.empty() && ans < pq2.top()) {
      cnt += (pq2.top() - 1) / ans;
      pq2.pop();
    }

    if (cnt > K) {
      l_min = ans + 1;
    } else {
      l_max = ans;
    }
  }
  ans = l_min;
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;

template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

template <typename T> map<T, T> getPrimeFactor(T n) {
  map<T, T> res;
  for (T i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}

template <typename T> bool IsPrimeNumber(T num) {
  if (num <= 2)
    return true;
  else if (num % 2 == 0)
    return false;

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }

  return true;
}

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// 繰り返し二乗法
ll pow2(ll n, ll p, ll mod) {
  if (p == 0 || n == 1) {
    return 1;
  } else {
    ll ret = pow2(n * n % mod, p / 2, mod);
    if (p % 2 == 1) {
      ret *= n;
    }
    return ret % mod;
  }
}

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define repeq(i, s, e) for (ll i = s; i <= e; i++)

int main() {
  ll N, K;
  cin >> N >> K;

  priority_queue<ll> pq;
  rep(i, 0, N) {
    ll a;
    cin >> a;
    pq.push(a);
  }

  ll l_min = 1;
  ll l_max = pq.top();
  ll ans;

  while (l_min != l_max) {
    ans = (l_max - l_min) / 2 + l_min;
    priority_queue<ll> pq2 = pq;

    ll cnt = 0;
    while (cnt <= K && !pq2.empty() && ans < pq2.top()) {
      cnt += (pq2.top() - 1) / ans;
      pq2.pop();
    }

    if (cnt > K) {
      l_min = ans + 1;
    } else {
      l_max = ans;
    }
  }
  ans = l_min;
  cout << ans << endl;
}