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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vvll vector<vll>
#define all(v) v.begin(), v.end()
#define FOR(i, n) for (ll i = 0; i < n; i++)
#define ffo(i, a, b) for (ll i = a; i <= b; i++)
#define rfo(i, a, b) for (ll i = a; i >= b; i--)
#define space cout << "\\n\\n";
#define endl "\\n"
#define pqmx priority_queue<ll>
#define pqmn priority_queue<ll, vll, greater<ll>>
#define fps(x, y) fixed << setprecision(y) << x
#define merg(a, b, c)                                                          \\
  set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()))
#define set_ar(arr, v) memset(arr, v, sizeof(arr))
#define ctime auto start = high_resolution_clock::now()
#define etime auto stop = high_resolution_clock::now()
#define go_t                                                                   \\
  int testcases;                                                               \\
  cin >> testcases;                                                            \\
  ffo(caseno, 1, testcases)
#define ptime                                                                  \\
  auto z1z = duration_cast<microseconds>(stop - start);                        \\
  cout << "Time elapsed : " << z1z.count() << " microseconds\\n"
#define dbg1(x) cout << #x << ": " << x << '\\n';
#define dbg2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << '\\n';
#define dbg3(x, y, z)                                                          \\
  cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \\
       << z << '\\n';
#define dbg4(a, b, c, d)                                                       \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << '\\n';
#define dbg5(a, b, c, d, e)                                                    \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << '\\n';

using namespace std;
using namespace std::chrono;

const ll mod = 1e9 + 7;
const ll N = 1e6 + 6;
const ll maxN = 2e5 + 5;
const ll MAX_SIZE = 2e6 + 6;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const double PI = 3.14159265359;

ll powerM(ll x, ll y, ll M = mod) { // default argument
  ll v = 1;
  x = x % M;
  while (y > 0) {
    if (y & 1)
      v = (v * x) % M;
    y = y >> 1;
    x = (x * x) % M;
  }
  return v;
}

ll power(ll x, ll y) {
  ll v = 1;
  while (y > 0) {
    if (y & 1)
      v = v * x;
    y = y >> 1;
    x = x * x;
  }
  return v;
}

ll a[maxN];
ll n, k;

// in previous submissions, I made 2 mistakes
// 1-> if length of rod == len_req , then we don't have to divide it
// 2-> I chose the bounds of binary search wrongly by selecting l=0

bool is_poss(ll len) {
  ll cnt = 0;
  for (ll i = 0; i < n; ++i) {
    if (a[i] > len)
      cnt += (a[i] / len);
  }
  return cnt <= k;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in2.txt", "r", stdin);
  freopen("out2.txt", "w", stdout);
#endif
  FIO cin >> n >> k;
  FOR(i, n) { cin >> a[i]; }

  ll l = 1, r = 1e12;
  while (l < r) {
    ll mid = (l + r) / 2;
    if (is_poss(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << l << endl;
  return 0;
}


Error: WA

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vvll vector<vll>
#define all(v) v.begin(), v.end()
#define FOR(i, n) for (ll i = 0; i < n; i++)
#define ffo(i, a, b) for (ll i = a; i <= b; i++)
#define rfo(i, a, b) for (ll i = a; i >= b; i--)
#define space cout << "\\n\\n";
#define endl "\\n"
#define pqmx priority_queue<ll>
#define pqmn priority_queue<ll, vll, greater<ll>>
#define fps(x, y) fixed << setprecision(y) << x
#define merg(a, b, c)                                                          \\
  set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()))
#define set_ar(arr, v) memset(arr, v, sizeof(arr))
#define ctime auto start = high_resolution_clock::now()
#define etime auto stop = high_resolution_clock::now()
#define go_t                                                                   \\
  int testcases;                                                               \\
  cin >> testcases;                                                            \\
  ffo(caseno, 1, testcases)
#define ptime                                                                  \\
  auto z1z = duration_cast<microseconds>(stop - start);                        \\
  cout << "Time elapsed : " << z1z.count() << " microseconds\\n"
#define dbg1(x) cout << #x << ": " << x << '\\n';
#define dbg2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << '\\n';
#define dbg3(x, y, z)                                                          \\
  cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \\
       << z << '\\n';
#define dbg4(a, b, c, d)                                                       \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << '\\n';
#define dbg5(a, b, c, d, e)                                                    \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << '\\n';

using namespace std;
using namespace std::chrono;

const ll mod = 1e9 + 7;
const ll N = 1e6 + 6;
const ll maxN = 2e5 + 5;
const ll MAX_SIZE = 2e6 + 6;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const double PI = 3.14159265359;

ll powerM(ll x, ll y, ll M = mod) { // default argument
  ll v = 1;
  x = x % M;
  while (y > 0) {
    if (y & 1)
      v = (v * x) % M;
    y = y >> 1;
    x = (x * x) % M;
  }
  return v;
}

ll power(ll x, ll y) {
  ll v = 1;
  while (y > 0) {
    if (y & 1)
      v = v * x;
    y = y >> 1;
    x = x * x;
  }
  return v;
}

ll a[maxN];
ll n, k;

bool is_poss(ll len) {
  ll cnt = 0;
  for (ll i = 0; i < n; ++i) {
    if (a[i] > len)
      cnt += (a[i] / len);
  }
  return cnt <= k;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in2.txt", "r", stdin);
  freopen("out2.txt", "w", stdout);
#endif
  FIO cin >> n >> k;
  FOR(i, n) { cin >> a[i]; }
  ll l = 0, r = 1e12;
  while (l < r) {
    ll mid = (l + r) / 2;
    if (is_poss(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << l << endl;
  return 0;
}