Language: C++

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

/* REPmacro */
#define REP(i,n) for(int i=0,i##_len=(n);i<i##_len;++i)
#define REPR(i,n) for(int i=((int)(n)-1);i>=0;--i)
#define FOR(i,m,n) for(int i=(int)(m);i<(int)(n);++i)

/*alias*/
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define MP make_pair 

#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
//#define next asdnext
#define prev asdprev
#define itn int

/*using*/
using ll = long long;
using ull = unsigned long long;
using unsi = unsigned;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using db = double;
using plex = complex<double>;
using vs = vector<string>;

/*template*/
template<class T>inline bool amax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>inline bool amin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }

/* func */

/*struct*/
struct aaa {
	aaa() {
		cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20);
	};
}aaaaaaa;

/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7; //10^9
const db EPS = 1e-9;
const int dx[] = { 1, 1, 0, -1, -1, -1, 0, 1 }, dy[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
//Counterclockwise from the 9o'clock direction
//Take the x-axis downward and the y-axis right

//#define int ll
//#define int long long

 
signed main()
{
  int n,k;cin>>n>>k;
  
  map<int,int> mi;
  
  REP(i,n){
    int a = 0;cin>>a;
    ++mi[a];
  }

  int roop = (int)mi.size() - k;//roop>0なら続ける

  //[1,1,2,2,5] なら[1,2,5]で{2,2,1}が入る
  int sum{};
  for(int i = roop; i > 0; --i){
    
   auto minitr = mi.rbegin(); 
    sum += minitr->second;  
    
      for(auto it = mi.rbegin(); it != mi.rend(); ){
        if(it->second == minitr->second)
          mi.erase((it+1).base());
      }
  }
  
  cout << sum << endl;
  return 0;
}

  //size()がk以下になるまで繰り返す
  //省きたいとこの要素の値を確保してその数分消したと考える。
  //省きたいとこはvalueが最小値のとこ
  //確保したらeraseで消す。これを繰り返してk以下になたら終了
  //確保した要素の値が変更した数なのでそれを出力

  


  /*
  for(const auto&i: mi){
    cout << i.S << endl;
  }
  */
   
  /*
  vector<int> v1;
  for(const auto &i: mi)
    v1.EB(i.second);//[1,1,2,2,5] なら[1,2,5]で{2,2,1}が入る
  
  int change{};
  for(int i = roop; i > 0; --i){
    change += *min_element(ALL(v1));
    
    auto pmin = min_element(ALL(v1)); 
    v1.erase(pmin);
  }
  */
  
    
  //map miに対するmapは？
  /*
  mi = [1,2,5] == {2,2,1}
  あかんわ右辺が同値の時
  */

Compiler output:
In file included from /usr/include/c++/14/bits/stl_algobase.h:67,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator.h: In instantiation of 'constexpr std::reverse_iterator<_Iterator> std::reverse_iterator<_Iterator>::operator+(difference_type) const [with _Iterator = std::_Rb_tree_iterator<std::pair<const int, int> >; difference_type = long int]':
a.cc:86:24:   required from here
   86 |           mi.erase((it+1).base());
      |                        ^
/usr/include/c++/14/bits/stl_iterator.h:341:41: error: no match for 'operator-' (operand types are 'const std::_Rb_tree_iterator<std::pair<const int, int> >' and 'std::reverse_iterator<std::_Rb_tree_iterator<std::pair<const int, int> > >::difference_type' {aka 'long int'})
  341 |       { return reverse_iterator(current - __n); }
      |                                 ~~~~~~~~^~~~~
/usr/include/c++/14/bits/stl_iterator.h:618:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr decltype ((__y.base() - __x.base())) std::operator-(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)'
  618 |     operator-(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:618:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_iterator.h:341:41: note:   'const std::_Rb_tree_iterator<std::pair<const int, int> >' is not derived from 'const std::reverse_iterator<_Iterator>'
  341 |       { return reverse_iterator(current - __n); }
      |                                 ~~~~~~~~^~~~~
/usr/include/c++/14/bits/stl_iterator.h:1790:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr decltype ((__x.base() - __y.base())) std::operator-(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)'
 1790 |     operator-(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1790:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_iterator.h:341:41: note:   'const std::_Rb_tree_iterator<std::pair<const int, int> >' is not derived from 'const std::move_iterator<_IteratorL>'
  341 |       { return reverse_iterator(current - __n); }
      |                                 ~~~~~~~~^~~~~
In file included from /usr/include/c++/14/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:127:
/usr/include/c++/14/complex:370:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator-(const complex<_Tp>&, const complex<_Tp>&)'
  370 |     operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:370:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_iterator.h:341:41: note:   'const std::_Rb_tree_iterator<std::pair<const int, int> >' is not derived from 'const std::complex<_Tp>'
  341 |       { return reverse_iterator(current - __n); }
      |                                 ~~~~~~~~^~~~~
/usr/include/c++/14/complex:379:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator-(const complex<_Tp>&, const _Tp&)'
  379 |     operator-(const complex<_Tp>& __x, const _Tp& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:379:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_iterator.h:341:41: note:   'const std::_Rb_tree_iterator<std::pair<const int, int> >' is not derived from 'const std::complex<_Tp>'
  341 |       { return reverse_iterator(current - __n); }
      |                                 ~~~~~~~~^~~~~
/usr/include/c++/14/complex:388:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator-(const _Tp&, const complex<_Tp>&)'
  388 |     operator-(const _Tp& __x, const complex<_Tp>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:388:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_iterator.h:341:41: note:   mismatched types 'const std::complex<_Tp>' and 'std::reverse_iterator<std::_Rb_tree_iterator<std::pair<const int, int> > >::difference_type' {aka 'long int'}
  341 |       { return reverse_iterator(current - __n); }
      |                                 ~~~~~~~~^~~~~
/usr/include/c++/14/complex:465:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator-(const complex<_Tp>&)'
  465 |     operator-(const complex<_Tp>& __x)
      |     ^~~~~~~~
/usr/include/c++/14/complex:465:5: note:   candidate expects 1 argument, 2 provided
In file included from /usr/include/c++/14/valarray:605,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:166:
/usr/include/c++/14/bits/valarray_after.h:406:5: note: candidate: 'template<class _Dom1, class _Dom2> std::_Expr<std::__detail::_BinClos<std::__minus, std::_Expr, std::_Expr, _Dom1, _Dom2>, typename std::__fun<std::__minus, typename _Dom1::value_type>::result_type> std::operator-(const _Expr<_Dom1, typename _Dom1::value_type>&, const _Expr<_Dom2, typename _Dom2::value_type>&)'
  406 |     _DEFINE_EXPR_BINARY_OPERATOR(-, struct std::__minus)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:406:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_iterator.h:341:41: note:   'const std::_Rb_tree_iterator<std::pair<const int, int> >' is not derived from 'const std::_Expr<_Dom1, typename _Dom1::value_type>'
  341 |       { return reverse_iterator(current - __n); }
      |                                 ~~~~~~~~^~~~~
/usr/include/c++/14/bits/valarray_after.h:406:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__minus, std::_Expr, std::_Constant, _Dom, typename _Dom::value_type>, typename std::__fun<std::__minus, typename _Dom1::value_type>::result_type> std::operator-(const _Expr<_Dom1, typename _Dom1::value_type>&, const typename _Dom::value_type&)'
  406 |     _DEFINE_EXPR_BINARY_OPERATOR(-, struct std::__minus)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:406:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_iterator.h:341:41: note:   'const std::_Rb_tree_iterator<std::pair<const int, int> >' is not derived from 'const std::_Expr<_Dom1, typename _Dom1::value_type>'
  341 |       { return reverse_iterator(current - __n); }
      |                                 ~~~~~~~~^~~~~
/usr/include/c++/14/bits/valarray_after.h:406:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__minus, std::_Constant, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__minus, typename _Dom1::value_type>::result_type> std::operator-(const typename _Dom::value_type&, const _Expr<_Dom1, typename _Dom1::value_type>&)'
  406 |     _DEFINE_EXPR_BINARY_OPERATOR(-, struct std::__minus)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:406:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_iterator.h:341:41: note:   mismatched types 'const std::_Expr<_Dom1, typename _Dom1::value_type>' and 'std::reverse_iterator<std::_Rb_tree_iterator<std::pair<const int, int> > >::difference_type' {aka 'long int'}
  341 |       { return reverse_iterator(current - __n); }
      |                                 ~~~~~~~~^~~~~
/usr/include/c++/14/bits/valarray_after.h:406:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__minus, std::_Expr, std::_ValArray, _Dom, typename _Dom::value_type>, typename std::__fun<std::__minus, typename _Dom1::value_type>::result_type> std::operator-(const _Expr<_Dom1, typename _Dom1::value_type>&, const valarray<typename _Dom::value_type>&)'
  406 |     _DEFINE_EXPR_BINARY_OPERATOR(-, struct std::__minus)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:406:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_iterator.h:341:41: note:   'const std::_Rb_tree_iterator<std::pair<const int, int> >' is not derived from 'const std::_Expr<_Dom1, typename _Dom1::value_type>'
  341 |       { return reverse_iterator(current - __n); }
      |                                 ~~~~~~~~^~~~~
/usr/include/c++/14/bits/valarray_after.h:406:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__minus, std::_ValArray, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__minus, typename _Dom1::value_type>::result_type> std::operator-(const valarray<typename _Dom::value_type>&, const _Expr<_Dom1, typename _Dom1::value_type>&)'
  406 |     _DEFINE_EXPR_BINARY_OPERATOR(-, struct std::__minus)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:406:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_iterator.h:341:41: note:   mismatched types 'const std::_Expr<_Dom1, typename _Dom1::value_type>' and 'std::reverse_iterator<std::_Rb_tree_iterator<std::pair<const int, int> > >::difference_type' {aka 'long int'}
  341 |       { return reverse_iterator(current - __n); }
      |                                 ~~~~~~~~^~~~~
/usr/include/c++/14/valarray:1197:1: note: candidate: 'template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__minus, std::_ValArray, std::_ValArray, _Tp, _Tp>, typename std::__fun<std::__minus, _Tp>::result_type> std::operator-(const valarray<_Tp>&, const valarray<_Tp>&)'
 1197 | _DEFINE_BINARY_OPERATOR(-, __minus)
      | ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/valarray:1197:1: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_iterator.h:341:41: note:   'const std::_Rb_tree_iterator<std::pair<const int, int> >' is not derived from 'const std::valarray<_Tp>'
  341 |       { return reverse_iterator(current - __n); }
      |                                 ~~~~~~~~^~~~~
/usr/include/c++/14/valarray:1197:1: note: candidate: 'template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__minus, std::_Val