Problem p03497 (C++)

Buggy submission (status: Runtime Error):
/*input
10 3
5 1 3 2 4 1 1 2 3 4
5 2
1 1 2 2 5
*/

/*
When you walk through a storm
Hold your head up high
And don't be afraid of the dark
At the end of the storm
There's a golden sky
And the sweet silver song
of the lark
Walk on, through the wind
Walk on, through the rain
Though your dreams be tossed
and blown
Walk on, walk on
With hope in your heart
And you'll never walk alone
YNWA
*/

// hell_hacker
// IT TAKES EVERYTHING and IT IS NOT LUCK
// PICK ME LAST -- COME OUT OF N^WHERE

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

#define endl '\n'
#define fo(i, n) for (i = 0; i < n; ++i)
#define forr(i, n) for (i = n - 1; i >= 0; --i)

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define yolo "debug_statement"

typedef long long int ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

const ll inf = 1e9;
const ld eps = 1e-9;
const ld pi = acos(-1);
const ll mod = 1000000007;

ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

#if DEBUG && !ONLINE_JUDGE

#define debug(args...) (Debugger()), args

class Debugger {
public:
  Debugger(const std::string &_separator = ", ")
      : first(true), separator(_separator) {}

  template <typename ObjectType> Debugger &operator,(const ObjectType &v) {
    if (!first)
      std::cerr << separator;
    std::cerr << v;
    first = false;
    return *this;
  }
  ~Debugger() { std::cerr << endl; }

private:
  bool first;
  std::string separator;
};

template <typename T1, typename T2>
inline std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

template <typename T1, unsigned long int N>
inline std::ostream &operator<<(std::ostream &os, const std::array<T1, N> &a) {
  bool first = true;
  os << "[";
  for (unsigned int i = 0; i < N; i++) {
    if (!first)
      os << ", ";
    os << a[i];
    first = false;
  }
  return os << "]";
}

template <typename T>
inline std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  bool first = true;
  os << "[";
  for (unsigned int i = 0; i < v.size(); i++) {
    if (!first)
      os << ", ";
    os << v[i];
    first = false;
  }
  return os << "]";
}

template <typename T>
inline std::ostream &operator<<(std::ostream &os, const std::set<T> &v) {
  bool first = true;
  os << "[";
  for (typename std::set<T>::const_iterator iii = v.begin(); iii != v.end();
       ++iii) {
    if (!first)
      os << ", ";
    os << *iii;
    first = false;
  }
  return os << "]";
}

template <typename T1, typename T2>
inline std::ostream &operator<<(std::ostream &os, const std::map<T1, T2> &v) {
  bool first = true;
  os << "[";
  for (typename std::map<T1, T2>::const_iterator iii = v.begin();
       iii != v.end(); ++iii) {
    if (!first)
      os << ", ";
    os << *iii;
    first = false;
  }
  return os << "]";
}

#else
#define debug(args...) // Just strip off all debug tokens
#endif

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  // vector<pair<int,int>>used;
  int arr[200005], used[200005] = {0};
  int i;
  int n, k;
  cin >> n >> k;
  for (i = 0; i < n; ++i) {
    cin >> arr[i];
    used[arr[i]]++;
  }
  sort(used, used + 200005);
  int kk = 0;
  for (i = 0; i < 200005; ++i)
    if (used[i])
      kk++;
  i = 0;
  int ans = 0;
  // cout << kk << endl;
  kk = kk - k;
  while (kk) {
    // cout << i << " " << used[i] << endl;
    if (used[i])
      ans += used[i], kk--;
    i++;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
/*input
10 3
5 1 3 2 4 1 1 2 3 4
5 2
1 1 2 2 5
*/

/*
When you walk through a storm
Hold your head up high
And don't be afraid of the dark
At the end of the storm
There's a golden sky
And the sweet silver song
of the lark
Walk on, through the wind
Walk on, through the rain
Though your dreams be tossed
and blown
Walk on, walk on
With hope in your heart
And you'll never walk alone
YNWA
*/

// hell_hacker
// IT TAKES EVERYTHING and IT IS NOT LUCK
// PICK ME LAST -- COME OUT OF N^WHERE

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

#define endl '\n'
#define fo(i, n) for (i = 0; i < n; ++i)
#define forr(i, n) for (i = n - 1; i >= 0; --i)

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define yolo "debug_statement"

typedef long long int ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

const ll inf = 1e9;
const ld eps = 1e-9;
const ld pi = acos(-1);
const ll mod = 1000000007;

ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

#if DEBUG && !ONLINE_JUDGE

#define debug(args...) (Debugger()), args

class Debugger {
public:
  Debugger(const std::string &_separator = ", ")
      : first(true), separator(_separator) {}

  template <typename ObjectType> Debugger &operator,(const ObjectType &v) {
    if (!first)
      std::cerr << separator;
    std::cerr << v;
    first = false;
    return *this;
  }
  ~Debugger() { std::cerr << endl; }

private:
  bool first;
  std::string separator;
};

template <typename T1, typename T2>
inline std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

template <typename T1, unsigned long int N>
inline std::ostream &operator<<(std::ostream &os, const std::array<T1, N> &a) {
  bool first = true;
  os << "[";
  for (unsigned int i = 0; i < N; i++) {
    if (!first)
      os << ", ";
    os << a[i];
    first = false;
  }
  return os << "]";
}

template <typename T>
inline std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  bool first = true;
  os << "[";
  for (unsigned int i = 0; i < v.size(); i++) {
    if (!first)
      os << ", ";
    os << v[i];
    first = false;
  }
  return os << "]";
}

template <typename T>
inline std::ostream &operator<<(std::ostream &os, const std::set<T> &v) {
  bool first = true;
  os << "[";
  for (typename std::set<T>::const_iterator iii = v.begin(); iii != v.end();
       ++iii) {
    if (!first)
      os << ", ";
    os << *iii;
    first = false;
  }
  return os << "]";
}

template <typename T1, typename T2>
inline std::ostream &operator<<(std::ostream &os, const std::map<T1, T2> &v) {
  bool first = true;
  os << "[";
  for (typename std::map<T1, T2>::const_iterator iii = v.begin();
       iii != v.end(); ++iii) {
    if (!first)
      os << ", ";
    os << *iii;
    first = false;
  }
  return os << "]";
}

#else
#define debug(args...) // Just strip off all debug tokens
#endif

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  // vector<pair<int,int>>used;
  int arr[200005], used[200005] = {0};
  int i;
  int n, k;
  cin >> n >> k;
  for (i = 0; i < n; ++i) {
    cin >> arr[i];
    used[arr[i]]++;
  }
  sort(used, used + 200005);
  int kk = 0;
  for (i = 0; i < 200005; ++i)
    if (used[i])
      kk++;
  i = 0;
  int ans = 0;
  // cout << kk << endl;
  kk = max(0, kk - k);
  while (kk) {
    // cout << i << " " << used[i] << endl;
    if (used[i])
      ans += used[i], kk--;
    i++;
  }
  cout << ans << endl;
  return 0;
}