Problem p03497 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define INF 1000000000
#define pb(a) push_back(a)
#define vi vector<int>
#define vvi vector<vi>
#define P pair<int, int>
#define all(vec) (vec.begin()), (vec.end())
#define ll long long int;
#define sz(x) ((int)(x).size())
#define in(x)                                                                  \
  int x;                                                                       \
  cin >> x;
#define bit(n) (1 << (n))

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define REP3(i, m, n) for (int i = m, i##_len = (n); i < i##_len; i++)
#define FORVEC(i, v) for (int i = 0; i < sz(v); i++)

int main() {
  map<int, int> kosuu;
  int k, n;
  int buf;
  cin >> n >> k;
  REP(i, n) {
    cin >> buf;
    kosuu[buf]++;
  }
  vector<P> kkk;
  for (auto x : kosuu) {
    kkk.pb(x);
  }

  sort(all(kkk), [](auto x, auto y) -> int { return (x.second < y.second); });

  int ans = 0;
  REP(i, sz(kosuu) - k) {
    ans += kkk[0].second;
    kkk.erase(kkk.begin());
  }
  cout << ans << "\n";
  return 0;
  /*
  int ans=0;
  if(sz(kosuu)<=k){
          cout<<"0\n";
  }else{
          if(sz(kosuu)<k*2){
                  ans=n;
                  k=sz(kosuu)-k;
                  while(sz(kosuu)>k){
                          auto x=max_element(kosuu.begin(), kosuu.end(),[](auto
  a, auto b) { return a.second < b.second;
                                                                  });
                          ans-=(*x).second;
                          kosuu.erase(x);
                  }
          }else{
                  while(sz(kosuu)>k){
                          auto x=min_element(kosuu.begin(), kosuu.end(),[](auto
  a, auto b) { return a.second < b.second;
                                                                  });
                          ans+=(*x).second;
                          kosuu.erase(x);
                  }
          }
          cout<<ans<<"\n";
  }*/
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define INF 1000000000
#define pb(a) push_back(a)
#define vi vector<int>
#define vvi vector<vi>
#define P pair<int, int>
#define all(vec) (vec.begin()), (vec.end())
#define ll long long int;
#define sz(x) ((int)(x).size())
#define in(x)                                                                  \
  int x;                                                                       \
  cin >> x;
#define bit(n) (1 << (n))

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define REP3(i, m, n) for (int i = m, i##_len = (n); i < i##_len; i++)
#define FORVEC(i, v) for (int i = 0; i < sz(v); i++)

int main() {
  map<int, int> kosuu;
  int k, n;
  int buf;
  cin >> n >> k;
  REP(i, n) {
    cin >> buf;
    kosuu[buf]++;
  }
  vector<P> kkk;
  for (auto x : kosuu) {
    kkk.pb(x);
  }

  sort(all(kkk), [](auto x, auto y) -> int { return (x.second < y.second); });

  int ans = 0;
  int idx = 0;
  REP(i, sz(kosuu) - k) { ans += kkk[idx++].second; }
  cout << ans << "\n";
  return 0;
  /*
  int ans=0;
  if(sz(kosuu)<=k){
          cout<<"0\n";
  }else{
          if(sz(kosuu)<k*2){
                  ans=n;
                  k=sz(kosuu)-k;
                  while(sz(kosuu)>k){
                          auto x=max_element(kosuu.begin(), kosuu.end(),[](auto
  a, auto b) { return a.second < b.second;
                                                                  });
                          ans-=(*x).second;
                          kosuu.erase(x);
                  }
          }else{
                  while(sz(kosuu)>k){
                          auto x=min_element(kosuu.begin(), kosuu.end(),[](auto
  a, auto b) { return a.second < b.second;
                                                                  });
                          ans+=(*x).second;
                          kosuu.erase(x);
                  }
          }
          cout<<ans<<"\n";
  }*/
  return 0;
}