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

#include <bits/stdc++.h>
using namespace std;
#pragma region
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << (#x) << "] = [", _print(x)
#else
#define debug(x...)
#endif
#pragma endregion
typedef long long ll;
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define sz(v) int((v).size())
#define int ll
#define f first
#define s second
#define mp make_pair
#define mt make_tuple
#define pb push_back

void setIO(string s) {
  // ios_base::sync_with_stdio(0); cin.tie(0);

  // freopen((s+".in").c_str(),"r",stdin);
  // freopen((s+".out").c_str(),"w",stdout);
}
int n, k;
vector<int> a;
bool can(int tar) {
  int cnt = 0;
  for (int it : a) {
    cnt += it / tar;
    if (it % tar == 0)
      --cnt;
    if (cnt > k)
      return false;
  }
  return true;
}
void solve() {
  scanf("%lld%lld", &n, &k);
  a.resize(n);
  forn(i, n) scanf("%lld", &a[i]);
  int lf = 1, rg = 1e12;
  int ans = 0;
  while (lf <= rg) {
    int mid = (lf + rg) / 2;
    if (can(mid)) {
      rg = mid - 1;
      ans = mid;
    } else {
      lf = mid + 1;
    }
  }
  printf("%lld\\n", ans);
}
signed main(void) {
  // setIO("exercise");
  int T = 1; // scanf("%lld", &T);
  while (T--)
    solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma region
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << (#x) << "] = [", _print(x)
#else
#define debug(x...)
#endif
#pragma endregion
typedef long long ll;
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define sz(v) int((v).size())
#define int ll
#define f first
#define s second
#define mp make_pair
#define mt make_tuple
#define pb push_back

void setIO(string s) {
  // ios_base::sync_with_stdio(0); cin.tie(0);

  // freopen((s+".in").c_str(),"r",stdin);
  // freopen((s+".out").c_str(),"w",stdout);
}
int n, k;
vector<int> a;
bool can(int tar) {
  int cnt = 0;
  for (int it : a) {
    cnt += it / tar;
    if (it % tar == 0)
      --cnt;
    if (cnt > k)
      return false;
  }
  return true;
}
void solve() {
  scanf("%lld%lld", &n, &k);
  a.resize(n);
  forn(i, n) scanf("%lld", &a[i]);
  int lf = 0, rg = 1e12;
  int ans = 0;
  while (lf <= rg) {
    int mid = (lf + rg) / 2;
    if (can(mid)) {
      rg = mid - 1;
      ans = mid;
    } else {
      lf = mid + 1;
    }
  }
  printf("%lld\\n", ans);
}
signed main(void) {
  // setIO("exercise");
  int T = 1; // scanf("%lld", &T);
  while (T--)
    solve();
  return 0;
}