Problem p02598 (C++)

Buggy submission (status: Runtime Error):

/**
 *    author        : Saurav Paul
 *    created       : August 03, 2020 11:41 AM
 *    Problem Name  : E - Logs
 *    Problem Limit : 2000 ms , 1024 MB
 *    Problem Url   : https://atcoder.jp/contests/abc174/tasks/abc174_e
 *    @genarated by : ai-virtual-assistant
 **/

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

#define bug() debug() <<
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef PAUL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define dbg(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#ifdef ONLINE_JUDGE
#define cerr                                                                   \
  if (false)                                                                   \
  cerr
#endif

int main() {
  ios_base::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;

  vector<long long> v(n);

  for (auto &x : v) {
    cin >> x;
  }

  sort(v.begin(), v.end(), [](int a, int b) { return (a > b); });

  long long l = 0, r = v[0];
  while (l <= r) {
    long long mid = (l + r) >> 1;

    bool ok = true;
    int temp = k;

    bug() dbg(mid);
    for (int i = 0; i < n; i++) {
      if (v[i] <= mid)
        break;

      long long t = (v[i] - 1) / mid;

      temp -= t;
      if (temp < 0) {
        ok = false;
        break;
      }
    }

    if (ok == false) {
      l = mid + 1;
    } else {
      r = mid - 1;
    }

    bug() dbg(mid) dbg(l) dbg(r) dbg(ok);
    /* cout << l << " " << r  << " " << ok  << " "<<temp <<endl; */
  }

  cout << l << endl;

  return 0;
}


Fixed submission:

/**
 *    author        : Saurav Paul
 *    created       : August 03, 2020 11:41 AM
 *    Problem Name  : E - Logs
 *    Problem Limit : 2000 ms , 1024 MB
 *    Problem Url   : https://atcoder.jp/contests/abc174/tasks/abc174_e
 *    @genarated by : ai-virtual-assistant
 **/

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

#define bug() debug() <<
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef PAUL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define dbg(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#ifdef ONLINE_JUDGE
#define cerr                                                                   \
  if (false)                                                                   \
  cerr
#endif

int main() {
  ios_base::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;

  vector<long long> v(n);

  for (auto &x : v) {
    cin >> x;
  }

  sort(v.begin(), v.end(), [](int a, int b) { return (a > b); });

  long long l = 1, r = v[0];
  while (l <= r) {
    long long mid = (l + r) >> 1;

    bool ok = true;
    int temp = k;

    bug() dbg(mid);
    for (int i = 0; i < n; i++) {
      if (v[i] <= mid)
        break;

      long long t = (v[i] - 1) / mid;

      temp -= t;
      if (temp < 0) {
        ok = false;
        break;
      }
    }

    if (ok == false) {
      l = mid + 1;
    } else {
      r = mid - 1;
    }

    bug() dbg(mid) dbg(l) dbg(r) dbg(ok);
    /* cout << l << " " << r  << " " << ok  << " "<<temp <<endl; */
  }

  cout << l << endl;

  return 0;
}