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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#ifndef M_PI
#define M_PI 3.141592653589
#endif
#define int long long
#define double long double

#ifdef TIME
#define start                                                                  \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  cout.setf(ios::fixed);                                                       \\
  cout.precision(10);                                                          \\
  ios_base::sync_with_stdio(false);                                            \\
  int32_t START = clock()
#define finish                                                                 \\
  cout << "\\ntime: " << (clock() - START) / (CLOCKS_PER_SEC * 1.0);            \\
  return 0
#endif

#ifndef TIME
#define start                                                                  \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  cout.setf(ios::fixed);                                                       \\
  cout.precision(10);                                                          \\
  ios_base::sync_with_stdio(false)
#define finish return 0
#endif

using namespace std;

// vector input
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &i : vec) {
    cin >> i;
  }
  return is;
}

// pair output
template <typename E> ostream &operator<<(ostream &os, pair<E, E> &t) {
  os << t.first << ' ' << t.second;
  return os;
}

//"map" pair output
template <typename E> ostream &operator<<(ostream &os, pair<const E, E> &t) {
  os << t.first << ' ' << t.second;
  return os;
}

// vector output
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (T i : vec) {
    os << i << ' ';
  }
  return os;
}

// 2 dimensional vector output
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  for (vector<T> i : vec) {
    os << i << '\\n';
  }
  return os;
}

int n, k;
vector<int> a;

bool can(int len) {
  int amount = 0;
  for (int i = 0; i < n; ++i) {
    int el = a[i];
    amount += (el + len - 1) / len - 1;
    if (amount > k) {
      return false;
    }
  }
  return amount <= k;
}

int32_t main() {
  start;

  cin >> n >> k;

  a.resize(n);
  int sum = 0;
  int mx = 0;

  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
    if (mx < a[i]) {
      mx = a[i];
    }
  }

  //    sort(a.rbegin(), a.rend());

  int l = max(0ll, sum / (n + k) - 1);
  int r = mx;

  while (r != l + 1) {
    int m = (l + r) / 2;
    if (can(m)) {
      r = m;
    } else {
      l = m;
    }
  }

  cout << r << '\\n';

  finish;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#ifndef M_PI
#define M_PI 3.141592653589
#endif
#define int long long
#define double long double

#ifdef TIME
#define start                                                                  \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  cout.setf(ios::fixed);                                                       \\
  cout.precision(10);                                                          \\
  ios_base::sync_with_stdio(false);                                            \\
  int32_t START = clock()
#define finish                                                                 \\
  cout << "\\ntime: " << (clock() - START) / (CLOCKS_PER_SEC * 1.0);            \\
  return 0
#endif

#ifndef TIME
#define start                                                                  \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  cout.setf(ios::fixed);                                                       \\
  cout.precision(10);                                                          \\
  ios_base::sync_with_stdio(false)
#define finish return 0
#endif

using namespace std;

// vector input
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &i : vec) {
    cin >> i;
  }
  return is;
}

// pair output
template <typename E> ostream &operator<<(ostream &os, pair<E, E> &t) {
  os << t.first << ' ' << t.second;
  return os;
}

//"map" pair output
template <typename E> ostream &operator<<(ostream &os, pair<const E, E> &t) {
  os << t.first << ' ' << t.second;
  return os;
}

// vector output
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (T i : vec) {
    os << i << ' ';
  }
  return os;
}

// 2 dimensional vector output
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  for (vector<T> i : vec) {
    os << i << '\\n';
  }
  return os;
}

int n, k;
vector<int> a;

bool can(int len) {
  int amount = 0;
  for (int i = 0; i < n; ++i) {
    int el = a[i];
    amount += (el + len - 1) / len - 1;
    if (amount > k) {
      return false;
    }
  }
  return amount <= k;
}

int32_t main() {
  start;

  cin >> n >> k;

  a.resize(n);
  int sum = 0;
  int mx = 0;

  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
    if (mx < a[i]) {
      mx = a[i];
    }
  }

  //    sort(a.rbegin(), a.rend());

  int l = sum / (n + k) - 1;
  int r = mx;

  while (r != l + 1) {
    int m = (l + r) / 2;
    if (can(m)) {
      r = m;
    } else {
      l = m;
    }
  }

  cout << r << '\\n';

  finish;
}