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

#include <bits/stdc++.h>
#define ll long long
#define PB push_back
#define MP make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, l, h) for (int i = (int)(l); i <= (int)(h); ++i)
#define FORD(i, h, l) for (int i = (int)(h); i >= (int)(l); --i)

// #define max(a,b) a>b?a:b
// #define min(a,b) a>b?b:a

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

#define dump(x) cerr << #x << " = " << (x) << endl
#define ALL(t) (t).begin(), (t).end()

using namespace std;

#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

int sum() { return 0; }

template <typename T, typename... Args> T sum(T a, Args... args) {
  return a + sum(args...);
}

int n;
std::vector<int> v;
bool ok(int mid, int k) {
  if (mid == 0)
    return false;
  int ko;
  for (int i = 0; i < n; i++) {
    ko = (v[i] - 1) / mid;
    k -= ko;
  }
  if (k >= 0)
    return true;
  else
    return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int k;
  cin >> n >> k;
  // std::vector<int> v(n);
  v.resize(n);

  REP(i, n) cin >> v[i];
  sort(v.begin(), v.end());
  int l = 0, r = v[n - 1] + 1;
  int mid, ans = 0;
  while (l <= r) {
    mid = l + (r - l) / 2;

    if (ok(mid, k)) {
      ans = mid; // min(mid , ans);
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:


#include <bits/stdc++.h>
#define ll long long
#define PB push_back
#define MP make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, l, h) for (int i = (int)(l); i <= (int)(h); ++i)
#define FORD(i, h, l) for (int i = (int)(h); i >= (int)(l); --i)

// #define max(a,b) a>b?a:b
// #define min(a,b) a>b?b:a

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

#define dump(x) cerr << #x << " = " << (x) << endl
#define ALL(t) (t).begin(), (t).end()

using namespace std;

#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

int sum() { return 0; }

template <typename T, typename... Args> T sum(T a, Args... args) {
  return a + sum(args...);
}

int n;
std::vector<int> v;
bool ok(int mid, int k) {
  int ko;
  for (int i = 0; i < n; i++) {
    ko = (v[i] - 1) / mid;
    k -= ko;
  }
  if (k >= 0)
    return true;
  else
    return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int k;
  cin >> n >> k;
  // std::vector<int> v(n);
  v.resize(n);

  REP(i, n) cin >> v[i];
  sort(v.begin(), v.end());
  int l = 0, r = v[n - 1] + 1;
  int mid, ans;
  while (l <= r) {
    mid = l + (r - l) / 2;

    if (ok(mid, k)) {
      ans = mid; // min(mid , ans);
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << ans;
  return 0;
}