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

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

#ifdef _DEBUG
#define dout cout
#define debug() if (true)
#define dvout(v) vout(v)
#else
#define dout                                                                   \\
  if (false)                                                                   \\
  cout
#define debug() if (false)
#define dvout(v)                                                               \\
  if (false)                                                                   \\
  vout(v)
#endif

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define vout(v)                                                                \\
  for (auto &i : v) {                                                          \\
    cout << i << " ";                                                          \\
  }                                                                            \\
  cout << "\\n"; // 配列出力
#define vec(v, n) vector<int> v(n, 0);
#define vecll(v, n) vector<ll> v(n, 0);
#define vecvec(v, n, m) vector<vector<int>> v(n, vector<int>(m));
#define vin(v)                                                                 \\
  for (auto &i : v)                                                            \\
    cin >> i; // 配列入力

#define mod (int)(1e9 + 7)
typedef long long ll;
typedef unsigned long long ull;
#define next asdnext
#define prev asdprev
#define _n "\\n"
#define _n2 "\\n\\n"
#define _t "\\t"

//	LLONG_MAX 9.2*1e18	9223372036854775807LL
//						999999999999999989
//	O(1e7) O(1e8)

void Main() {
  int N, K;
  cin >> N >> K;
  vec(A, N);
  int longer = -1;
  rep(i, N) {
    cin >> A[i];
    longer = max(longer, A[i]);
  }

  int l = 0, r = longer + 1;
  while (abs(l - r) > 1) {
    int mid = (l + r) / 2;
    if (mid == 0)
      mid = 1;
    ll ans = 0;
    rep(i, N) { ans += (A[i] - 1) / mid; }
    if (ans <= K)
      r = mid;
    else
      l = mid;
  }

  cout << r << _n;
}

void Test();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}


Error: 0

Fixed submission:

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

#ifdef _DEBUG
#define dout cout
#define debug() if (true)
#define dvout(v) vout(v)
#else
#define dout                                                                   \\
  if (false)                                                                   \\
  cout
#define debug() if (false)
#define dvout(v)                                                               \\
  if (false)                                                                   \\
  vout(v)
#endif

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define vout(v)                                                                \\
  for (auto &i : v) {                                                          \\
    cout << i << " ";                                                          \\
  }                                                                            \\
  cout << "\\n"; // 配列出力
#define vec(v, n) vector<int> v(n, 0);
#define vecll(v, n) vector<ll> v(n, 0);
#define vecvec(v, n, m) vector<vector<int>> v(n, vector<int>(m));
#define vin(v)                                                                 \\
  for (auto &i : v)                                                            \\
    cin >> i; // 配列入力

#define mod (int)(1e9 + 7)
typedef long long ll;
typedef unsigned long long ull;
#define next asdnext
#define prev asdprev
#define _n "\\n"
#define _n2 "\\n\\n"
#define _t "\\t"

//	LLONG_MAX 9.2*1e18	9223372036854775807LL
//						999999999999999989
//	O(1e7) O(1e8)

void Main() {
  int N, K;
  cin >> N >> K;
  vec(A, N);
  int longer = -1;
  rep(i, N) {
    cin >> A[i];
    longer = max(longer, A[i]);
  }

  int l = -1, r = longer + 1;
  while (abs(l - r) > 1) {
    int mid = (l + r) / 2;
    ll ans = 0;
    rep(i, N) { ans += (A[i] - 1) / mid; }
    if (ans <= K)
      r = mid;
    else
      l = mid;
  }

  cout << r << _n;
}

void Test();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}