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

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define square(x) pow(x, 2.0)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define max_queue(x) priority_queue<x>
#define min_queue(x) priority_queue<x, vector<x>, greater<x>>
#define big_sort(v)                                                            \\
  sort(all(v), [](int a, int b) { return a > b; }) // 大きい順にソート
using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using vs = vector<string>;
using vd = vector<long double>;
#define co(x) cout << x << endl;
#define cins(n, v)                                                             \\
  rep(i, n) { cin >> v.at(i); }
#define couts(v)                                                               \\
  rep(i, v.size()) { cout << v.at(i) << endl; }
// aよりもbが大きいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}
int main() {
  int n, k, l = 0, r = 1e9;
  cin >> n >> k;
  vi v(n);
  cins(n, v);
  while (r - l > 1) {
    int x = (l + r) / 2;
    auto f = [&](int x) {
      ll now = 0;
      rep(i, n) now += (v[i] - 1) / x;
      return now <= k;
    };
    if (f(x))
      r = x;
    else
      l = x;
  }
  cout << r << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define square(x) pow(x, 2.0)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define max_queue(x) priority_queue<x>
#define min_queue(x) priority_queue<x, vector<x>, greater<x>>
#define big_sort(v)                                                            \\
  sort(all(v), [](int a, int b) { return a > b; }) // 大きい順にソート
using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using vs = vector<string>;
using vd = vector<long double>;
#define co(x) cout << x << endl;
#define cins(n, v)                                                             \\
  rep(i, n) { cin >> v.at(i); }
#define couts(v)                                                               \\
  rep(i, v.size()) { cout << v.at(i) << endl; }
// aよりもbが大きいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}
/*
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <deque> // deque
#include <iostream> // cout, endl, cin
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <string> // string, to_string, stoi
#include <tuple> // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility> // pair, make_pair
#include <vector> // vector
*/
int main() {
  ll n, k, cut_num = 0, longi = 0;
  ll ave = 0, longv = 0;
  cin >> n >> k;
  vi v(n);
  vi cutv(n);
  vi lastv(n);
  rep(i, n) {
    cin >> v[i];
    ave += v[i];
  }
  if (k == 0) {
    rep(i, n) {
      if (longv < v[i]) {
        longv = v[i];
        longi = i;
      }
    }
    cout << longv << endl;
    return 0;
  }
  ave /= k;
  rep(i, n) {
    cutv[i] = v[i] / ave;
    cut_num += cutv[i];
    lastv[i] = v[i] / (cutv[i] + 1);
    if (longv < lastv[i]) {
      longv = lastv[i];
      longi = i;
    }
  }
  REP(j, cut_num, k) {
    cutv[longi]++;
    lastv[longi] = v[longi] / (cutv[longi] + 1);
    longv = 0;
    rep(i, n) {
      if (longv < lastv[i]) {
        longv = lastv[i];
        longi = i;
      }
    }
  }
  if (v[longi] / (cutv[longi] + 1)) {
    longv++;
  }
  cout << longv << endl;
  return 0;
}