Problem p03497 (C++)

Buggy submission (status: Runtime Error):
/*input
 */
#include <bits/stdc++.h>
using namespace std;
#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORd(i, a, b) for (ll i = (a)-1; i >= (b); i--)
#define FORall(it, a)                                                          \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed << setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
#define db(x) cerr << #x << " = " << (x) << " " << endl;
#define endln cerr << "\n";
const ll MN = 1e6 + 3;
void output() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  map<ll, ll> vals;
  FOR(i, 0, n) {
    cin >> a[i];
    vals[a[i]]++;
  }
  vector<ll> mins;
  for (pair<ll, ll> p : vals) {
    mins.pb(p.second);
  }
  sort(mins.begin(), mins.end());
  ll ans = 0;
  if (mins.size() >= k) {
    FOR(i, 0, mins.size() - k) { ans += mins[i]; }
    cout << ans << endl;
  } else {
    cout << 0 << endl;
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  FOR(i, 0, t) { output(); }
  return 0;
}

Fixed submission:
/*input
 */
#include <bits/stdc++.h>
using namespace std;
#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORd(i, a, b) for (ll i = (a)-1; i >= (b); i--)
#define FORall(it, a)                                                          \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed << setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
#define db(x) cerr << #x << " = " << (x) << " " << endl;
#define endln cerr << "\n";
const ll MN = 1e6 + 3;
void output() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  map<ll, ll> vals;
  FOR(i, 0, n) {
    cin >> a[i];
    vals[a[i]]++;
  }
  vector<ll> mins;
  for (pair<ll, ll> p : vals) {
    mins.pb(p.second);
  }
  sort(mins.begin(), mins.end());
  ll ans = 0;
  if (mins.size() >= k) {
    FOR(i, 0, mins.size() - k) { ans += mins[i]; }
    cout << ans << endl;
  } else {
    cout << 0 << endl;
  }
}
int main() {
#ifdef LOCAL_PROJECT
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  FOR(i, 0, t) { output(); }
  return 0;
}