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

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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<long long> vl;
typedef pair<long long, long long> pl;
typedef vector<vector<long long>> matrix;

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define all(c) c.begin(), c.end()
#define f(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) f(i, 0, n)
#define tr(c, i) for (typeof(c).begin() i = c.begin(); i != c.end(); i++)
#define present(c, x) (c.find(x) != c.end())        // for set and map
#define cpresent(c, x) (find(all(c), x) != c.end()) // for vectors
#define w(t)                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define epsilon 1e-9
#define MOD (ll)1e9

void doublade() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

bool isValid(const vl &a, ll mid, ll k) {
  ll cnt = 0;
  for (ll i = a.size() - 1; i >= 0; i--) {
    if (a[i] > mid) {
      cnt += a[i] / mid;
    } else
      break;

    if (cnt > k)
      return false;
  }
  return true;
}

ll solve(const vl &a, ll k, ll l, ll r) {
  ll ans = r;
  while (l <= r) {
    ll mid = l + (r - l) / 2;
    // cout<<l<<" "<<r<<" "<<mid<<"\\n";
    if (isValid(a, mid, k)) {
      r = mid - 1;
      ans = mid;
    } else
      l = mid + 1;
  }
  return ans;
}

int main() {
  doublade();
  // w(t) {
  ll n, k;
  cin >> n >> k;
  ll sum = 0;
  vl a(n);
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  if (k >= sum) {
    cout << "1\\n";
    return 0;
  } else {
    sort(all(a));
    ll l = 0, r = a[n - 1];
    cout << solve(a, k, l, r) << "\\n";
  }
  // }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<long long> vl;
typedef pair<long long, long long> pl;
typedef vector<vector<long long>> matrix;

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define all(c) c.begin(), c.end()
#define f(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) f(i, 0, n)
#define tr(c, i) for (typeof(c).begin() i = c.begin(); i != c.end(); i++)
#define present(c, x) (c.find(x) != c.end())        // for set and map
#define cpresent(c, x) (find(all(c), x) != c.end()) // for vectors
#define w(t)                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define epsilon 1e-9
#define MOD (ll)1e9

void doublade() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

bool isValid(const vl &a, ll mid, ll k) {
  ll cnt = 0;
  for (ll i = a.size() - 1; i >= 0; i--) {
    if (a[i] > mid) {
      cnt += a[i] / mid;
    } else
      break;

    if (cnt > k)
      return false;
  }
  return true;
}

ll solve(const vl &a, ll k, ll l, ll r) {
  ll ans = r;
  while (l <= r) {
    ll mid = l + (r - l) / 2;
    // cout<<l<<" "<<r<<" "<<mid<<"\\n";
    if (isValid(a, mid, k)) {
      r = mid - 1;
      ans = mid;
    } else
      l = mid + 1;
  }

  return ans;
}

int main() {
  doublade();
  // w(t) {
  ll n, k;
  cin >> n >> k;
  vl a(n);
  rep(i, n) { cin >> a[i]; }
  sort(all(a));
  ll l = 0, r = a[n - 1];
  cout << solve(a, k, l, r) << "\\n";
  // cout<<isValid(a, 7, k)<<"\\n";
  // }
  return 0;
}