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

#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
// 多倍長整数型
/* ---------------------- ここから ---------------------- */
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
// 任意長整数型
using Bint = mp::cpp_int;
// 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
using Real = mp::number<mp::cpp_dec_float<1024>>;
/* ---------------------- ここまで ---------------------- */

#define INF 1e9
#define LINF 1e19
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \\
  std::vector<type> c(n);                                                      \\
  for (auto &i : c)                                                            \\
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll lb = 0, ub = INF + INF;
  ll mx = 0;
  if (k == 0) {
    rep(i, n) chmax(mx, a[i]);
    cout << mx << endl;
    return 0;
  }

  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    ll tmp = 0;
    rep(i, n) {
      if (a[i] < mid)
        continue;
      tmp += ((a[i] + mid - 1) / mid) - 1;
    }
    // cout << mid << " " << tmp << endl;
    if (tmp <= k)
      ub = mid;
    else
      lb = mid;
  }
  cout << ub << endl;
}


Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
// 多倍長整数型
/* ---------------------- ここから ---------------------- */
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
// 任意長整数型
using Bint = mp::cpp_int;
// 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
using Real = mp::number<mp::cpp_dec_float<1024>>;
/* ---------------------- ここまで ---------------------- */

#define INF 1e9
#define LINF 1e19
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \\
  std::vector<type> c(n);                                                      \\
  for (auto &i : c)                                                            \\
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll lb = 0, ub = INF + INF;
  ll mx = 0;
  if (k == 0) {
    rep(i, n) chmax(mx, a[i]);
    cout << mx << endl;
    return 0;
  }

  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    ll tmp = 0;
    rep(i, n) { tmp += (a[i] / mid); }
    // cout << mid << " " << tmp << endl;
    if (tmp <= k)
      ub = mid;
    else
      lb = mid;
  }
  ll res;
  for (int i = 1; i >= -1; i--) {
    ll now = lb + i;
    ll cnt = 0;
    cnt += (a[i] / now + (a[i] % now == 0 ? 0 : 1));
    if (cnt <= k)
      res = now;
  }
  if (res > lb)
    res = lb;
  cout << res + 1 << endl;
}