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

#pragma GCC optimize("O3")

#include <bits/stdc++.h>
#define DEBUG true
#ifdef ONLINE_JUDGE
#undef DEBUG
#define DEBUG false
#endif

using namespace std;

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

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

#define MAXN ((ll)2e5 + 5)
#define MOD ((ll)1e9 + 7)
#define INF ((ll)1e9 + 9)
#define ll long long
#define _ << " " <<
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cerr << x << endl;
#define pb push_back
#define all(x) x.begin(), x.end()
#define endl "\\n"
#define pii pair<ll, ll>
#define mid ((l + r) / 2)

ll n, k;
map<ll, ll> mp;

bool check(ll t) {
  double total = 0;
  for (auto x : mp) {
    ll cost = 0;
    if (x.first > t)
      cost = (x.first / t) - (x.first % t == 0);
    total += cost * x.second;
  }

  return total <= k;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (ll i = 0; i < n; ++i) {
    ll a;
    cin >> a;
    mp[a]++;
  }

  ll l = 1, r = 1e18;

  while (l < r) {
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }

  cout << fixed << (ll)(ceil(l)) << endl;
}

/* stuff you should look for
 * ll overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 */

Error: 0
[t = 500000000000000000][total = 0.000000]
[t = 250000000000000000][total = 0.000000]
[t = 125000000000000000][total = 0.000000]
[t = 62500000000000000][total = 0.000000]
[t = 31250000000000000][total = 0.000000]
[t = 15625000000000000][total = 0.000000]
[t = 7812500000000000][total = 0.000000]
[t = 3906250000000000][total = 0.000000]
[t = 1953125000000000][total = 0.000000]
[t = 976562500000000][total = 0.000000]
[t = 488281250000000][total = 0.000000]
[t = 244140625000000][total = 0.000000]
[t = 122070312500000][total = 0.000000]
[t = 61035156250000][total = 0.000000]
[t = 30517578125000][total = 0.000000]
[t = 15258789062500][total = 0.000000]
[t = 7629394531250][total = 0.000000]
[t = 3814697265625][total = 0.000000]
[t = 1907348632812][total = 0.000000]
[t = 953674316406][total = 0.000000]
[t = 476837158203][total = 0.000000]
[t = 238418579101][total = 0.000000]
[t = 119209289550][total = 0.000000]
[t = 59604644775][total = 0.000000]
[t = 29802322387][total = 0.000000]
[t = 14901161193][total = 0.000000]
[t = 7450580596][total = 0.000000]
[t = 3725290298][total = 0.000000]
[t = 1862645149][total = 0.000000]
[t = 931322574][total = 0.000000]
[t = 465661287][total = 0.000000]
[t = 232830643][total = 0.000000]
[t = 116415321][total = 0.000000]
[t = 58207660][total = 0.000000]
[t = 29103830][total = 0.000000]
[t = 14551915][total = 0.000000]
[t = 7275957][total = 0.000000]
[t = 3637978][total = 0.000000]
[t = 1818989][total = 0.000000]
[t = 909494][total = 0.000000]
[t = 454747][total = 0.000000]
[t = 227373][total = 0.000000]
[t = 113686][total = 0.000000]
[t = 56843][total = 0.000000]
[t = 28421][total = 0.000000]
[t = 14210][total = 0.000000]
[t = 7105][total = 0.000000]
[t = 3552][total = 0.000000]
[t = 1776][total = 0.000000]
[t = 888][total = 0.000000]
[t = 444][total = 0.000000]
[t = 222][total = 0.000000]
[t = 111][total = 0.000000]
[t = 55][total = 0.000000]
[t = 27][total = 0.000000]
[t = 13][total = 0.000000]
[t = 6][total = 2.000000]
[t = 3][total = 4.000000]
[t = 5][total = 2.000000]
[t = 4][total = 3.000000]
[l = 4]


Fixed submission:

#pragma GCC optimize("O3")

#include <bits/stdc++.h>
#define DEBUG true
#ifdef ONLINE_JUDGE
#undef DEBUG
#define DEBUG false
#endif

using namespace std;

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

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

#define MAXN ((ll)2e5 + 5)
#define MOD ((ll)1e9 + 7)
#define INF ((ll)1e9 + 9)
#define ll long long
#define _ << " " <<
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cerr << x << endl;
#define pb push_back
#define all(x) x.begin(), x.end()
#define endl "\\n"
#define pii pair<ll, ll>
#define mid ((l + r) / 2)

ll n, k;
map<ll, ll> mp;

bool check(ll t) {
  double total = 0;
  for (auto x : mp) {
    ll cost = 0;
    if (x.first > t)
      cost = (x.first / t) - (x.first % t == 0);
    total += cost * x.second;
  }

  TRACE(t, total);
  return total <= k;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (ll i = 0; i < n; ++i) {
    ll a;
    cin >> a;
    mp[a]++;
  }

  ll l = 0, r = 1e18;

  while (l < r) {
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }

  TRACE(l);
  cout << fixed << (ll)(ceil(l)) << endl;
}

/* stuff you should look for
 * ll overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 */