Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;

/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)

/* REPmacro */
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (a); i > (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)

/* exchange */
#define chmin(a, b) (a) = min((ll)(a), (ll)(b))
#define chmax(a, b) (a) = max((ll)(a), (ll)(b))

/* output */
#define IN(x) cin >> x
#define DE(x) cerr << (x) << " "
#define BR cerr << "\n"
#define OUT(x) cout << (x) << endl
#define FIX cout << fixed << setprecision(10)

/* const */
const int ARRAY = 100005;
const int INF = 1001001001;          // 10^9
const ll LINF = 1001001001001001001; // 10^18
const int MOD = 1e9 + 7;

ll N = 0;
ll K;
ll ret = 0;

int main(void) {

  IN(N);
  IN(K);
  vl A(N);
  REP(i, N) { IN(A[i]); }

  ll left = 0;
  ll right = 1000000000;
  while (right - left > 1) {
    ll mid = (left + right) / 2;
    ll k = 0;
    REP(i, N) { k += (A[i] + mid - 1) / mid - 1; }
    if (k < K) {
      right = mid;
    } else if (k > K) {
      left = mid;
    } else {
      right = mid;
    }

    // DE(k);
    // DE(left);
    // DE(right);
    // BR;
  }

  ll left_k = 0;
  REP(i, N) { left_k += (A[i] + left - 1) / left - 1; }

  // ll right_k = 0;
  // REP(i, N) {
  //   right_k += (A[i] + right - 1) / right - 1;
  // }

  if (left_k > K) {
    OUT(right);
  } else {
    OUT(left);
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;

/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)

/* REPmacro */
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (a); i > (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)

/* exchange */
#define chmin(a, b) (a) = min((ll)(a), (ll)(b))
#define chmax(a, b) (a) = max((ll)(a), (ll)(b))

/* output */
#define IN(x) cin >> x
#define DE(x) cerr << (x) << " "
#define BR cerr << "\n"
#define OUT(x) cout << (x) << endl
#define FIX cout << fixed << setprecision(10)

/* const */
const int ARRAY = 100005;
const int INF = 1001001001;          // 10^9
const ll LINF = 1001001001001001001; // 10^18
const int MOD = 1e9 + 7;

ll N = 0;
ll K;
ll ret = 0;

int main(void) {

  IN(N);
  IN(K);
  vl A(N);
  REP(i, N) { IN(A[i]); }

  ll left = 1;
  ll right = 1000000000;
  while (right - left > 1) {
    ll mid = (left + right) / 2;
    ll k = 0;
    REP(i, N) { k += (A[i] + mid - 1) / mid - 1; }
    if (k < K) {
      right = mid;
    } else if (k > K) {
      left = mid;
    } else {
      right = mid;
    }

    // DE(k);
    // DE(left);
    // DE(right);
    // BR;
  }

  ll left_k = 0;
  REP(i, N) { left_k += (A[i] + left - 1) / left - 1; }

  // ll right_k = 0;
  // REP(i, N) {
  //   right_k += (A[i] + right - 1) / right - 1;
  // }

  if (left_k > K) {
    OUT(right);
  } else {
    OUT(left);
  }
}