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

/**Bismillahir Rahmanir Raheem
    Author:Refatul Fahad**/

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

#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int, int>
#define pil pair<int, ll>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"
const int N = 1e5 + 5;
const ll M = (ll)1e9 + 7;
const double pi = 2 * acos(0.0);
const double esp = 1e-9;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

ll ar[2 * N];
int main() {

  // IOS;
  /// freopen("input.txt","r",stdin);
  /// freopen("output.txt","w",stdout);

  int test = 1;
  // cin>>test;
  for (int cs = 1; cs <= test; ++cs) {
    ll n, mx = -1, ans, sum = 0ll, k;

    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
      cin >> ar[i];
      sum += ar[i];
      mx = max(mx, ar[i]);
      // st.insert(x);
    }
    if (sum <= k) {
      cout << 1 << endl;
    } else {
      sort(ar + 1, ar + n + 1);
      int l = 0, r = mx;
      ans = mx;
      while (l <= r && (l != 0 || r != 0)) {
        int mid = (l + r) >> 1;
        // if(mid<=0)break;
        int cnt = 0;
        for (int i = n; i >= 1; --i) {
          if (ar[i] / mid) {
            if (ar[i] % mid) {
              cnt += (ar[i] / mid);
            } else {
              cnt += ((ar[i] / mid) - 1);
            }
          } else {
            break;
          }
        }
        if (cnt <= k) {
          r = mid - 1;
          ans = mid;
        } else {
          l = mid + 1;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

/**Bismillahir Rahmanir Raheem
    Author:Refatul Fahad**/

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

#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int, int>
#define pil pair<int, ll>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"
const int N = 1e5 + 5;
const ll M = (ll)1e9 + 7;
const double pi = 2 * acos(0.0);
const double esp = 1e-9;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int ar[2 * N];
int main() {

  // IOS;
  /// freopen("input.txt","r",stdin);
  /// freopen("output.txt","w",stdout);

  int test = 1;
  // cin>>test;
  for (int cs = 1; cs <= test; ++cs) {
    int n, k, mx = -1, ans;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
      cin >> ar[i];
      mx = max(mx, ar[i]);
      // st.insert(x);
    }
    sort(ar + 1, ar + n + 1);
    int l = 0, r = mx;
    ans = mx;
    while (l <= r) {
      int mid = (l + r) >> 1;
      int cnt = 0;
      for (int i = n; i >= 1; --i) {
        if (ar[i] / mid) {
          if (ar[i] % mid) {
            cnt += (ar[i] / mid);
          } else {
            cnt += ((ar[i] / mid) - 1);
          }
        } else {
          break;
        }
      }
      if (cnt <= k) {
        r = mid - 1;
        ans = mid;
      } else {
        l = mid + 1;
      }
    }
    cout << ans << endl;
  }
  return 0;
}