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

#include <algorithm>
#include <chrono>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, a, b) for (ll i = a; i > b; i--)
#define CST(x) cout << fixed << setprecision(x) // 小数点以下の桁数指定
#define ct(a) cout << a << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (1); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
static const double pi = 3.141592653589793;
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = (1LL << 31) - 1;
const ll mod = 1e9 + 7;

ll N, K;
ll A[200011];

bool isOK(ll cur) { // 最小がcur以下とすると上手くいくか？

  if (cur >= A[0])
    return true;

  ll cnt = 0;
  FOR(i, 0, N) {
    if (A[i] > cur) {
      cnt += (A[i] + cur - 1) / cur - 1;
    } else
      break;
  }
  if (cnt <= K)
    return true;
  else
    return false;
}

ll binary_search() {

  ll left = 0, right = 1e9 + 10; // ここの配列名を適宜変えよう！
  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (isOK(mid))
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> K;
  FOR(i, 0, N) cin >> A[i];
  sort(A, A + N);
  reverse(A, A + N);

  ll ans = binary_search();
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <chrono>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, a, b) for (ll i = a; i > b; i--)
#define CST(x) cout << fixed << setprecision(x) // 小数点以下の桁数指定
#define ct(a) cout << a << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (1); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
static const double pi = 3.141592653589793;
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = (1LL << 31) - 1;
const ll mod = 1e9 + 7;

ll N, K;
ll A[200001];

bool isOK(ll cur) { // 最小がcur以下とすると上手くいくか？

  if (cur > A[0])
    return true;

  ll cnt = 0;
  FOR(i, 0, N) {
    if (A[i] > cur) {
      cnt += (A[i] + cur - 1) / cur - 1;
    }
  }
  if (cnt <= K)
    return true;
  else
    return false;
}

ll binary_search() {

  ll left = -1, right = 1e9; // ここの配列名を適宜変えよう！
  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (isOK(mid))
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> K;
  FOR(i, 0, N) cin >> A[i];
  sort(A, A + N);
  reverse(A, A + N);

  ll ans = binary_search();
  cout << ans << endl;

  return 0;
}