Problem p02718 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define test()                                                                 \
  int t;                                                                       \
  cin >> t;                                                                    \
  for (int test = 1; test <= t; test++)
#define pb push_back
#define nl cout << "\n"
#define F first
#define S second
#define all(x) x.begin(), x.end()

template <class C> void min_self(C &a, C b) { a = min(a, b); }
template <class C> void max_self(C &a, C b) { a = max(a, b); }

const ll MOD = 1000000007;
ll mod(ll n, ll m = MOD) {
  n %= m, n += m, n %= m;
  return n;
}

const int MAXN = 1e5 + 5;
const int LOGN = 21;
const ll INF = 1e14;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

template <class T1, class T2> void add(T1 &x, T2 y, ll m = MOD) {
  x += y;
  if (x >= m)
    x -= m;
}

template <class T1, class T2> void sub(T1 &x, T2 y, ll m = MOD) {
  x -= y;
  if (x < 0)
    x += m;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio();

  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  ll total = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    total += a[i];
  }

  sort(all(a));
  int cnt = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (4 * m * a[i] >= total) {
      cnt++;
    }
  }

  if (cnt >= m)
    cout << "Yes";
  else
    cout << "No";

  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}


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

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define test()                                                                 \
  int t;                                                                       \
  cin >> t;                                                                    \
  for (int test = 1; test <= t; test++)
#define pb push_back
#define nl cout << "\n"
#define F first
#define S second
#define all(x) x.begin(), x.end()

template <class C> void min_self(C &a, C b) { a = min(a, b); }
template <class C> void max_self(C &a, C b) { a = max(a, b); }

const ll MOD = 1000000007;
ll mod(ll n, ll m = MOD) {
  n %= m, n += m, n %= m;
  return n;
}

const int MAXN = 1e5 + 5;
const int LOGN = 21;
const ll INF = 1e14;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

template <class T1, class T2> void add(T1 &x, T2 y, ll m = MOD) {
  x += y;
  if (x >= m)
    x -= m;
}

template <class T1, class T2> void sub(T1 &x, T2 y, ll m = MOD) {
  x -= y;
  if (x < 0)
    x += m;
}

int main() {
  fastio();

  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  ll total = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    total += a[i];
  }

  sort(all(a));
  int cnt = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (4 * m * a[i] >= total) {
      cnt++;
    }
  }

  if (cnt >= m)
    cout << "Yes";
  else
    cout << "No";

  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}