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

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PRINT(a) cout << (a) << endl

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second

#define debug(x)                                                               \\
  cerr << x << " "                                                             \\
       << "(L:" << __LINE__ << ")" << '\\n';

using ll = long long int;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;

template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;

int n, k;

bool check(int m, vi &a) {
  int cnt = 0;
  REP(i, a.size()) { cnt += (a[i] - 1) / m; }
  return cnt <= k;
}

int main() {
  cin >> n >> k;
  vi a(n);

  int l = 1;
  int r = 0;
  REP(i, n) {
    cin >> a[i];
    chmax(r, a[i]);
  }

  //  checkの戻り値が FFFTTT で一番左のTを見つけたい場合
  while (l < r) {
    ll m = (l + r) / 2;
    // cout << l << " " << r << " " << m << endl;
    if (check(m, a)) {
      r = m;
    } else {
      l = m + 1;
    }
  }
  cout << l << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PRINT(a) cout << (a) << endl

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second

#define debug(x)                                                               \\
  cerr << x << " "                                                             \\
       << "(L:" << __LINE__ << ")" << '\\n';

using ll = long long int;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;

template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;

int n, k;

bool check(int m, vi &a) {
  int cnt = 0;
  REP(i, a.size()) { cnt += a[i] / m; }
  return cnt <= k;
}

int main() {
  cin >> n >> k;
  vi a(n);

  int l = 0;
  int r = 0;
  REP(i, n) {
    cin >> a[i];
    chmax(r, a[i]);
  }

  //  checkの戻り値が FFFTTT で一番左のTを見つけたい場合
  while (l < r) {
    ll m = (l + r) / 2;
    // cout << l << " " << r << " " << m << endl;
    if (check(m, a)) {
      r = m;
    } else {
      l = m + 1;
    }
  }
  cout << l << endl;
}