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

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define maxn 200005
#define vi vector<int>
#define vl vector<ll>
#define mii map<int, int>
#define mli map<ll, int>
#define forit(it, v) for (auto it = v.begin(); it != v.end(); ++it)
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
void solve() {
  ll n, k;
  cin >> n >> k;
  vl a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  ll beg = 1, end = (ll)1e9, ans = end;
  while (beg <= end) {
    ll mid = beg + (end - beg) / 2;
    ll c = 0, c1 = 0;
    for (int i = 0; i < n; ++i) {
      c1 = a[i] / mid;
      if (a[i] % mid == 0) {
        c1--;
      }
      if (round((a[i]) / (c1 + 1)) > mid) {
        c1++;
      }
      c += c1;
    }
    if (c <= k) {
      ans = mid;
      end = mid - 1;
    } else {
      beg = mid + 1;
    }
  }
  cout << ans;
  // ll n,k;
  // cin>>n>>k;
  // priority_queue<float> q;
  // for (int i = 0; i < n; ++i)
  // {
  //     float x;
  //     cin>>x;
  //     q.push(x*1.0);
  // }
  // int i=0;
  // while(i<k)
  // {
  //     i++;
  //     float x=q.top();
  //     cout<<x<<" ";
  //     q.pop();
  //     q.push(x/2.0f);
  //     q.push(x-x/2.0f);
  // }
  // ll v=q.top();
  // cout<<v;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastIO;
  // sieve();
  int t = 1;
  // int t;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

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


Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define maxn 200005
#define vi vector<int>
#define vl vector<ll>
#define mii map<int, int>
#define mli map<ll, int>
#define forit(it, v) for (auto it = v.begin(); it != v.end(); ++it)
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
void solve() {
  ll n, k;
  cin >> n >> k;
  vl a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  ll beg = 0, end = (ll)1e9, ans = end;
  while (beg <= end) {
    ll mid = beg + (end - beg) / 2;
    ll c = 0, c1 = 0;
    for (int i = 0; i < n; ++i) {
      c1 = a[i] / mid;
      if (a[i] % mid == 0) {
        c1--;
      }
      if (round((a[i]) / (c1 + 1)) > mid) {
        c1++;
      }
      c += c1;
    }
    if (c <= k) {
      ans = mid;
      end = mid - 1;
    } else {
      beg = mid + 1;
    }
  }
  cout << end;
  // ll n,k;
  // cin>>n>>k;
  // priority_queue<float> q;
  // for (int i = 0; i < n; ++i)
  // {
  //     float x;
  //     cin>>x;
  //     q.push(x*1.0);
  // }
  // int i=0;
  // while(i<k)
  // {
  //     i++;
  //     float x=q.top();
  //     cout<<x<<" ";
  //     q.pop();
  //     q.push(x/2.0f);
  //     q.push(x-x/2.0f);
  // }
  // ll v=q.top();
  // cout<<v;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastIO;
  // sieve();
  int t = 1;
  // int t;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}