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

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

/*#ifndef ONLINE_JUDGE
    #include <lol.h>
#endif*/
#define mp make_pair
#define pb push_back
#define pf push_front
#define sz(a) (int)((a).size())
#define fr(i, n) for (ll i = 0; i < n; i++)
#define fr1(i, n) for (ll i = 1; i <= n; i++)
#define frk(i, k, n) for (ll i = k; i < n; i++)
#define rfr(i, n) for (ll i = n - 1; i >= 0; i--)
#define foa(i, a, n) for (ll i = 0; i < n; i += a)
#define f first
#define s second
#define mod 1000000007
#define all(x) (x).begin(), (x).end()
#define endl "\\n"
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define vsort(x) sort(all(x))
#define debug cout << "Hey Sweetie, Stop Right Here......" << endl;

typedef long long int ll;
typedef stack<ll> stll;
typedef queue<ll> qll;
typedef priority_queue<ll> pqll;
typedef set<ll> sll;
typedef multiset<ll> msll;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
typedef vector<pair<ll, ll>> vpll;
typedef map<ll, ll> mll;
typedef unordered_map<ll, ll> umll;
typedef long double ld;
ll modmulti(ll a, ll b, ll m) { return ((a % m) * (b % m)) % m; }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll power(ll x, ll y, ll m) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2, m) % m;
  p = (p * p) % m;

  return (y % 2 == 0) ? p : (x * p) % m;
}
ll power2(ll x, ll y) {
  if (y == 0)
    return 1;
  ll p = power2(x, y / 2);
  p = (p * p);

  return (y % 2 == 0) ? p : (x * p);
}
bool cmp(pair<ll, ll> &a, pair<ll, ll> &b) {
  if (a.first == b.first)
    return (a.second < b.second);
  else
    return (a.first > b.first);
}

ll bin(vll a, ll left, ll right, ll x) {
  ll mid = left + (right - left) / 2;
  if (a[mid] == x)
    return mid;
  if (left <= right) {
    if (a[mid] < x)
      return bin(a, mid + 1, right, x);
    else if (a[mid] > x)
      return bin(a, left, mid - 1, x);
  }
  return -1;
}
ll findclosest(ll n) {
  ll i;
  for (i = 0; i <= 64; i++) {
    if (power2(2, i) <= n)
      continue;
    else
      break;
  }
  return power2(2, i - 1);
}
ll get_signi(ll n) {
  while (n >= 10) {
    n = n / 10;
  }
  return n;
}
ll get_digits(ll n) {
  ll i = 0;
  while (n >= 10) {
    n = n / 10;
    i++;
  }
  return i;
}
bool is_palindrome(string t) {
  for (int i = 0; i < t.size() / 2; i++) {
    if (t[i] != t[t.size() - i - 1])
      return false;
  }
  return true;
}
int main() {
  fastio;
  ll n, m;
  cin >> n >> m;
  vll a(n);
  ll sum = 0;
  fr(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  ll cnt = 0;
  ll need = (sum / (m * 4));
  if (sum % (4 * m) == 0)
    need--;
  fr(i, n) {
    if (a[i] > need)
      cnt++;
  }
  if (cnt >= m)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  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;

/*#ifndef ONLINE_JUDGE
    #include <lol.h>
#endif*/
#define mp make_pair
#define pb push_back
#define pf push_front
#define sz(a) (int)((a).size())
#define fr(i, n) for (ll i = 0; i < n; i++)
#define fr1(i, n) for (ll i = 1; i <= n; i++)
#define frk(i, k, n) for (ll i = k; i < n; i++)
#define rfr(i, n) for (ll i = n - 1; i >= 0; i--)
#define foa(i, a, n) for (ll i = 0; i < n; i += a)
#define f first
#define s second
#define mod 1000000007
#define all(x) (x).begin(), (x).end()
#define endl "\\n"
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define vsort(x) sort(all(x))
#define debug cout << "Hey Sweetie, Stop Right Here......" << endl;

typedef long long int ll;
typedef stack<ll> stll;
typedef queue<ll> qll;
typedef priority_queue<ll> pqll;
typedef set<ll> sll;
typedef multiset<ll> msll;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
typedef vector<pair<ll, ll>> vpll;
typedef map<ll, ll> mll;
typedef unordered_map<ll, ll> umll;
typedef long double ld;
ll modmulti(ll a, ll b, ll m) { return ((a % m) * (b % m)) % m; }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll power(ll x, ll y, ll m) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2, m) % m;
  p = (p * p) % m;

  return (y % 2 == 0) ? p : (x * p) % m;
}
ll power2(ll x, ll y) {
  if (y == 0)
    return 1;
  ll p = power2(x, y / 2);
  p = (p * p);

  return (y % 2 == 0) ? p : (x * p);
}
bool cmp(pair<ll, ll> &a, pair<ll, ll> &b) {
  if (a.first == b.first)
    return (a.second < b.second);
  else
    return (a.first > b.first);
}

ll bin(vll a, ll left, ll right, ll x) {
  ll mid = left + (right - left) / 2;
  if (a[mid] == x)
    return mid;
  if (left <= right) {
    if (a[mid] < x)
      return bin(a, mid + 1, right, x);
    else if (a[mid] > x)
      return bin(a, left, mid - 1, x);
  }
  return -1;
}
ll findclosest(ll n) {
  ll i;
  for (i = 0; i <= 64; i++) {
    if (power2(2, i) <= n)
      continue;
    else
      break;
  }
  return power2(2, i - 1);
}
ll get_signi(ll n) {
  while (n >= 10) {
    n = n / 10;
  }
  return n;
}
ll get_digits(ll n) {
  ll i = 0;
  while (n >= 10) {
    n = n / 10;
    i++;
  }
  return i;
}
bool is_palindrome(string t) {
  for (int i = 0; i < t.size() / 2; i++) {
    if (t[i] != t[t.size() - i - 1])
      return false;
  }
  return true;
}
int main() {
  fastio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, m;
  cin >> n >> m;
  vll a(n);
  ll sum = 0;
  fr(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  ll cnt = 0;
  fr(i, n) {
    if (a[i] >= (sum / (m * 4)))
      cnt++;
  }
  if (cnt >= m)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}