Problem:
 Score : 300 points    Problem Statement Takahashi has N balls. Initially, an integer A_i is written on the i -th ball.  He would like to rewrite the integer on some balls so that there are at most K different integers written on the N balls.  Find the minimum number of balls that Takahashi needs to rewrite the integers on them.      Constraints  1 \leq K \leq N \leq 200000  1 \leq A_i \leq N  All input values are integers.         Input Input is given from Standard Input in the following format:  N  K  A_1  A_2 ... A_N       Output Print the minimum number of balls that Takahashi needs to rewrite the integers on them.        Sample Input 1 5 2
1 1 2 2 5      Sample Output 1 1  For example, if we rewrite the integer on the fifth ball to 2 , there are two different integers written on the balls: 1 and 2 .
On the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print 1 .       Sample Input 2 4 4
1 1 2 2      Sample Output 2 0  Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.       Sample Input 3 10 3
5 1 3 2 4 1 1 2 3 4      Sample Output 3 3

Solution (Python):





import math
import fractions
import sys
import bisect
import heapq  
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr



def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod



def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors


inf = 10 ** 18
mod = 10 ** 9 + 7

n,k = lr()
a = lr()
dic = collections.Counter(a)
dic_list = list(dic.items())
dic_list.sort(key=lambda x:x[1], reverse=True)
ans = 0
if len(dic_list) <= k:
    print(ans)
else:
    for _,num in dic_list[k:]:
        ans+=num
    print(ans)

Solution (Python):
import math
import sys
import collections
n,k = map(int, input().split())
A = list(map(int, input().split()))
count=len(A)
Adel=list(set(A))
ans=len(Adel)-k

if ans<0:
    ans=0
    print(ans)
    sys.exit()
newA=collections.Counter(A)
count=list(newA.values())
count.sort()

ans=sum(count[:-k])
print(ans)

Solution (Python):
import sys
import math
import itertools as it
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":
    n,k = I2()
    a = Intl()
    aa = list(set(a))
    b = {}
    for i in aa:
        b[i] = 0
    for i in a:
        b[i] += 1
    c = [x[1] for x in b.items()]
    c.sort(reverse=True)
    s = list(it.accumulate(c))
    print(n - s[k-1] if len(aa) >= k else 0)

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))

D={}
for a in A:
    if a in D:
        D[a]+=1
    else:
        D[a]=1

if len(D)<=K:
    print(0)
else:
    E=sorted(D.values())
    print(sum(E[:len(D)-K]))

Solution (Python):
import sys
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
if N == K:
    print(0)
    sys.exit()
_A = [0] * N
for a in A:
    _A[a - 1] += 1
_A.sort()
print(sum(_A) - sum(_A[N-K:]))

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
b = set(a)
b = list(b)
ans = [0]*len(b)
tmp = a[0]
ans[0] += 1
cnt = 0

for i in range(1,n):
    if tmp == a[i]:
        ans[cnt] += 1
    else:
        cnt += 1
        ans[cnt] += 1
        tmp = a[i]

f = len(ans)-k
cnt1 = 0
if f <= 0:
    print(0)
else:
    ans.sort()
    for i in range(f):
        cnt1 += ans[i]
    print(cnt1)

Solution (Python):
from collections import Counter
N, K = map(int, input().split())
A = list(map(int ,input().split()))

d = Counter(A)

d_ = sorted(d.items(), key=lambda x:x[1], reverse=True)
n =len(d_)
ans = 0
if n <= K:
    print(0)
    exit()
else:
    for i in range(K,n):
        ans += d_[i][1]
print(ans)

Solution (Python):
from collections import defaultdict
N, K = map(int, input().split())
A = list(map(int ,input().split()))

d = defaultdict(int)
for i in A:
    d[i] += 1

d_ = sorted(d.items(), key=lambda x:x[1], reverse=True)
n =len(d_)
ans = 0
if n <= K:
    print(0)
    exit()
else:
    for i in range(K,n):
        ans += d_[i][1]
print(ans)

Solution (Python):

import sys
from collections import deque, defaultdict
from math import sqrt, factorial





def solve():
    n, k = [int(x) for x in input().split()]
    a = [int(x) for x in input().split()]
    d = defaultdict(int)
    for e in a:
        d[e] += 1
    b = [(d[k], k) for k in d]
    b.sort()
    ans = 0
    i = 0
    m = len(b)
    while m - i > k:
        ans += b[i][0]
        i += 1
    print(ans)


t = 1

for case in range(1,t+1):
    ans = solve()


"""



"""

Solution (Python):
from collections import Counter
n, k = map(int, input().split())
A = list(map(int, input().split()))
d = Counter(A)
v = list(d.values())
v.sort()
ans = 0
if len(v) >= k:
    for i in range(len(v) - k):
        ans += v[i]
print(ans)

Solution (Python):

 

 
import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy
 

 










 
N,K=map(int, input().split())
A=list(map(int, input().split()))

D = collections.Counter(A)
Dval = list(D.values())
Dval.sort()

ans = 0
for i in range(len(Dval)-K):
    ans += Dval[i]

print(ans)

Solution (Python):
from collections import Counter
N,K=map(int,input().split())
N=list(map(int,input().split()))
A=Counter(N)
C=list(A.values())
C.sort(reverse=True)
if len(C)<=K:
    print(0)
else:
    ans=sum(C[-(len(C)-K):])
    print(ans)

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0
inf = float("inf")

n,k=I()
a = l()
b = Counter(a)
c = b.most_common()
x = len(c)-k
for i in range(x):
    count += c[-1][1]
    del c[-1]

print(count)

Solution (Python):
n,k = map(int, input().split())
num = [0] * 200000
an = list(map(int, input().split()))
for i in range(len(an)):
    num[an[i]] += 1
Ans = 0
num2 = [s for s in num if s != 0]
num2.sort()
length = len(num2)
if k < length:
    for i in range(length):
        Ans += num2[i]
        length = length - 1
        if length == k:
            break
print(Ans)

Solution (Python):
from collections import Counter
n,m=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
tp=Counter(a).most_common()[m:]
sumA=0
for t in tp:
  sumA+=t[1]
print(sumA)

Solution (Python):
from collections import Counter
N, K = map(int, input().split())
A = list(map(int, input().split()))
x = len(set(A))
C = Counter(A)
L = C.most_common()[::-1]
ans = 0
for l in L:
  if K >= x:
    break
  ans += l[1]
  x -= 1
print(ans)

Solution (Python):
n,lim = map(int, input().split(" "))
a = list(map(int, input().split(" ")))
num=[0 for i in range(n)]
for i in range(n):
  num[a[i] - 1] += 1
num = sorted(num, reverse=True)

print(sum(num[lim:]))

Solution (Python):
from collections import Counter

N, K = map(int, input().split())
A = list(map(int, input().split()))

cnt = Counter(A)
v = list(cnt.values())
v.sort()
print(sum(v[:-K]))

Solution (Python):
from collections import defaultdict
from collections import deque
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n,k = readInts()

d = defaultdict(int)

for i in readChars():
	d[i]+=1

d = sorted(d.items(), key=lambda x:x[1],reverse=True)
ans = 0
while len(d)>k:
	ans+=d[-1][1]
	del d[-1]
print(ans)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int, input().split()))
B = len(set(A))
from collections import Counter
if B <= K:
    print(0)
    exit()
C = Counter(A)

C = sorted(C.items(), key=lambda x:x[1],reverse=True)

count = 0
for key, value in C[K:]:
    count += value

print(count)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))

from collections import Counter

c=Counter(a).most_common()[:k]
cnt=0
for i in c:
    cnt+=i[1]

print(n-cnt)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))

from collections import Counter

c=Counter(a).most_common()[:k]

s=set([i[0] for i in c])

ans=0
for i in range(n):
    if a[i] not in s:
        ans+=1

print(ans)

Solution (Python):
from collections import Counter
N, K = map(int, input().split())
A = list(map(int, input().split()))

if len(set(A)) <= K:
  print(0)
else:
  c = Counter(A).most_common()[::-1]
  ans = 0
  for i in range(len(set(A)) - K):
    ans += c[i][1]
    
  print(ans)

Solution (Python):
N,K=map(int, input().split())
a=list(map(int, input().split()))


import collections

c = collections.Counter(a)
c2=sorted(c.values())

sum=0
for i in range(len(c)-K):
        sum+=c2[i]
print(sum)

Solution (Python):
import sys
import collections

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007



def main():
	
	n, k = tin()
	
	al = lin()
	cc = collections.Counter(al)
	cv = [cc[k] for k in cc]
	cv.sort()
	
	print(sum(cv[:-k]))
	
	
	
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):






import collections





N,K = map(int, input().split())
A = list(map(int, input().split()))





mylist = collections.Counter(A).most_common()
mylist = list(reversed(mylist))
ans = 0
if len(mylist) > K:
    for a,n in mylist[:len(mylist)-K]:
        ans += n
print(ans)

Solution (Python):
n, k = map(int, input().split())
a_l = sorted(list(map(int, input().split())))
base = list(set(a_l))
num = len(base)-k
ans = []

if num <= 0:
    print(0)
else:
    c_idx = 1
    c_num = a_l[0]
    for idx, v in enumerate(a_l[1:]):
        if c_num == v:
            c_idx += 1
            continue
        else:
            ans.append(c_idx)
            c_idx = 1
            c_num = v
    ans.append(c_idx)

    print(sum(sorted(ans)[:num]))

Solution (Python):
from collections import defaultdict
import sys
input = lambda: sys.stdin.readline().rstrip()
n, k = map(int, input().split())
a = list(map(int, input().split()))
dd = defaultdict(int)

for i in a:
    dd[i] += 1

if len(dd) <= k:
    print(0)
else:
    dd = sorted(dd.items(), key=lambda x: x[1])
    ans = 0
    for i in range(len(dd) - k):
        ans += dd[i][1]
    print(ans)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
x=[]
ch=[0]*(n+1)
co=0
for i in a:
  ch[i]+=1
for i in ch:
  if i !=0:
    co+=1
    x.append(i)
x.sort()

print(max(sum(x[:len(x)-k]),0))

Solution (Python):
from collections import Counter
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


n, k = LI()
a = LI()
c = Counter(a)
l = sorted(c.items(), key=lambda x: x[1])
types = len(l)
if types <= k:
    print(0)
    sys.exit()
ans = 0
i = 0
while types > k:
    ans += l[i][1]
    i += 1
    types -= 1
print(ans)

Solution (Python):
import collections
import sys

if __name__ == "__main__":
    N, K = [int(x) for x in input().split(" ")]
    A = [int(x) for x in input().split(" ")]
    counts = dict(collections.Counter(A))
    counts = {k: v for k, v in sorted(counts.items(), key=lambda item: item[1])}

    if len(counts) <= K:
        print(0)
        sys.exit(0)
    nums = len(counts) - K
    res = 0

    for key in counts:
        if nums <= 0:
            break
        res += counts[key]
        nums -= 1
    print(res)

Solution (Python):
import sys 
import collections

N, K = map(int, input().split())
A = list(map(int, input().split()))

if len(set(A)) <= K :
    print(0)
    sys.exit()

A_Counter = collections.Counter(A)
A_Counterlist = sorted(list(A_Counter.values()))


count_num = 0
count_set = len(set(A))

for i in range(len(A_Counterlist)):
    count_set = count_set - 1
    count_num = count_num + A_Counterlist[i]

    if count_set == K:
        print(count_num)
        sys.exit()

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=set(A)
import collections
C=collections.Counter(A)
Most=C.most_common()[K:]
ans=0
for i in Most:
  ans+=i[1]
print(ans)

Solution (Python):
from collections import Counter
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=Counter(a)
v=list(c.values())
n=len(v)
if n>k:
    ans=sum(sorted(v)[:n-k])
    print(ans)
else:
    print(0)

Solution (Python):
from collections import defaultdict

n, k = map(int, input().split())
a = list(map(int, input().split()))
d = defaultdict(int)

for x in a:
  d[x] += 1

new_d = sorted(d.items(), key=lambda x: x[1])
ans = 0
cnt = 0
for x in new_d:
  if(cnt < len(new_d) - k):
    ans += x[1]
    cnt += 1
  else:
    break
print(ans)

Solution (Python):
from collections import defaultdict


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    d = defaultdict(int)
    for a in A:
        d[a] += 1

    d_sorted = sorted(d.items(), key=lambda x: x[1], reverse=True)

    tmp = 0
    for i, k in enumerate(d_sorted):
        if i < K:
            tmp += k[1]
        else:
            break

    print(N-tmp)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

N, K = map(int, input().split())
num = [0]*N
for x in input().split():
    num[int(x)-1] += 1
val = N-num.count(0)
if K >= val:
    print('0')
else:
    num.sort()
    count = 0
    for i in num:
        if i == 0:
            continue
        else:
            count += i
            val -= 1
            if K >= val:
                print(count)
                sys.exit()

Solution (Python):
import collections
N,K = map(int,input().split())
a = list(map(int,input().split()))
b = a.copy()
b = list(set(b))

a = collections.Counter(a)
a = sorted(a.values())
ans = 0
for i in range(len(b)-K):
    ans += a[i]
print(ans)

Solution (Python):
import collections

N, K = map(int,input().split())
A = list(map(int,input().split()))

cA = collections.Counter(A)

ans = 0
if len(cA) > K:
  _, counts = zip(*cA.most_common()[::-1])
  ans =sum(counts[:len(cA)-K])
else:
  ans = 0
print(ans)

Solution (Python):
from collections import Counter
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=Counter(a)
v=list(c.values())
n=len(v)
if n>k:
    ans=sum(sorted(v)[:n-k])
    print(ans)
else:
    print(0)

Solution (Python):
N, K = map(int,input().split())
A = list(map(int,input().split()))

a = list(set((A)))
check = len(a)
dff = check - K
if dff <= 0:
  print(0)
  exit(0)
ans_list = [0]*check
A.sort()
index = 0
for i in range(len(A)):
  
  if i >= 1 and A[i] != A[i-1]:
    index += 1
  ans_list[index] += 1
ans_list.sort()
result = sum(ans_list[:dff])
print(result)

Solution (Python):
from collections import Counter

def main():
    n,k=map(int,input().split())
    a=map(int,input().split())
    c = Counter(a)
    d = len(c) - k
    minic = sorted(c.items(), key=lambda x: x[1])
    ans = 0
    for m in minic[:d]:
        ans += m[1]
    print(ans)
    
if __name__ == "__main__":
    main()

Solution (Python):
import collections
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
if len(list(set(a))) <= k:
  ans = 0
else:
  m = len(list(set(a))) - k
  c = collections.Counter(a)
  c = dict(c)
  c = sorted(c.items(), key=lambda x:x[1])
  for i in range(m):
    ans += c[i][1]
print(ans)

Solution (Python):
N, K = map(int, input().split())
A=list(map(int, input().split()))

l=[0 for i in range(N)]


for i in range(N):
  l[A[i]-1] += 1

l=sorted(l)

ans=0


for i in range(N-K):
  ans += l[i]
print(ans)

Solution (Python):
N, K = map(int, input().split())
A=list(map(int, input().split()))

l=[0 for i in range(N)]


for i in range(N):
  l[A[i]-1] += 1

l=sorted(l)

ans=0


for i in range(N-K):
  ans += l[i]
print(ans)

Solution (Python):
from collections import Counter
import heapq

n,k=map(int,input().split())
a=list(map(int,input().split()))
kind = len(set(a))
if kind<=k:
    print(0)
else:
    temp = list(Counter(a).values())
    heapq.heapify(temp)
    ans=0
    for _ in range(kind-k):
        ans+=heapq.heappop(temp)
    print(ans)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=[0 for i in range(n)]
for i in range(n):
    l[a[i]-1]+=1
l.sort()
ans=0
for i in range(n-k):
    ans+=l[i]
print(ans)

Solution (Python):
x = input().split()
n, k = int(x[0]), int(x[1])
x = input().split()
l = [int(i) for i in x]
l = sorted(l)
x = set(l)
diff = len(x) - k
cnt_dic = {}
cnt =0
if diff <= 0:
    print(0)
else:
    same=1
    for i in range(1, n):
        if l[i] == l[i-1]:
            same += 1
        else:
            cnt_dic[l[i-1]] = same
            same = 1
    cnt_dic[l[n-1]] = same
    
    res = sorted(cnt_dic.items(), key = lambda kv:(kv[1], kv[0]))
    
    sum = 0
    for i in range(diff):
        sum += res[i][1]
    print(sum)

Solution (Python):
 
import heapq
N, K = map(int, input().split())
array = [0] * N
count = 0
A = list(map(int, input().split()))
for i in A:
    array[i-1] += 1
if(array.count(0) >= N - K):
    print(0)
else:
    array_plus = [i for i in array if i > 0]
    array_plus = sorted(array_plus)
    l = array_plus[:len(array_plus)-K]
    print(sum(l))

Solution (Python):
N,K=map(int,input().split())
*A,=map(int,input().split())

countA=[0]+[0]*N
for a in A:
    countA[a]+=1

countA.sort()
ans=sum(countA)-sum(countA[-i]for i in range(1,K+1))
print(ans)

Solution (Python):

from collections import Counter
n,k = map(int,input().split())
a = list(map(int,input().split()))

c = Counter(a)

c = c.most_common()


syurui = len(c)
cnt = 0
for i in range(len(c)):
    if syurui > k:
        cnt += c[-1][1]
        a = c.pop(-1)
        syurui -= 1
    else:
        break
    
print(cnt)

Solution (Python):
from collections import Counter

N, K = map(int, input().split())
A = [int(i) for i in input().split()]

counter = Counter(A)
counter = list(counter.items())
counter = sorted(counter, key=lambda x: x[1])

ans = 0
for i in range(len(counter)-K):
    ans += counter[i][1]

print(ans)

Solution (Python):
n, k = map(int, input().split())
A = list(map(int, input().split()))
num = [0]*n
for a in A:
    num[a-1] += 1
num.sort(reverse=True)
cnt = 0
for i in range(k):
    cnt += num[i]
ans = n - cnt
print(ans)

Solution (Python):
from collections import Counter

n, k = map(int, input().split())
dic = Counter(list(map(int, input().split())))
kv = dic.items()
kv = sorted(kv, key=lambda x: x[1])
ans = 0
cnt = 0
while len(kv)-cnt > k:
    ans += kv[cnt][1]
    cnt+=1
print(ans)

Solution (Python):
N, K=map(int, input().split())
A=list(map(int, input().split()))
sa=set(A)
if len(sa)<=K:
  print(0)
else:
  
  sub=len(sa)-K
  
  A.sort()
  A.append(A[-1])
  a=[0]*len(sa)
  j=0
  for i in range(N):
    a[j]+=1
    if A[i]!=A[i+1]:
      j+=1
  
  a.sort()
  print(sum(a[:sub]))

Solution (Python):
import collections

N,K = map(int, input().split())
A = list(map(int, input().split()))
C = collections.Counter(A)
_,mostV =zip(*C.most_common())
changeV = len(mostV) - K
if changeV >0:
    cnt = 0
    for i in range(1,changeV+1):
        cnt += mostV[-i]
    print(cnt)
else:
    print(0)

Solution (Python):
from collections import Counter

N, K = map(int,input().split())
C = Counter(list(map(int,input().split())))

print(sum(sorted(list(C.values()), reverse=True)[K:]))

Solution (Python):
import numpy as np
n,k=map(int,input().split())
a=list(map(int,input().split()))

A=np.array(a)
counter=np.bincount(A)
ct=counter[counter.nonzero()]
arr=ct.tolist()
arr=sorted(arr)

dis=len(set(A))-k

cnt=0
for i in range(dis):
    cnt+=arr[i]

print(cnt)

Solution (Python):
import sys
input = sys.stdin.readline
from bisect import bisect_left
from itertools import accumulate


def read():
    N, K = map(int, input().strip().split())
    A = map(int, input().strip().split())
    return N, K, A


def solve(N, K, A, NMAX=200001):
    bucket = [0 for i in range(NMAX)]
    for a in A:
        bucket[a] += 1
    bucket.sort()
    S = list(accumulate(bucket))
    l = bisect_left(bucket, 1)
    r = NMAX - K
    return S[r-1] - S[l-1]


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = [0 for i in range(n)]
for i in range(n):
    l[a[i]-1] += 1
l.sort()
cnt = 0
for i in range(n-k):
    cnt += l[i]
print(cnt)

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
r = [0 for _ in range(n)]
for i in a:
    r[i-1] += 1
u = []
for i in r:
    if i:
        u.append(i)
u.sort(reverse=True)
print(n-sum(u[:k]))

Solution (Python):
from collections import Counter
n, k, *lst = map(int, open(0).read().split())
c = Counter(lst)
lenc = len(c)
if lenc <= k:
  print(0)
  quit()
print(sum(sorted(c.values())[:lenc - k]))

Solution (Python):
from collections import Counter


N, K = map(int, input().split())
A = sorted((v, k) for k, v in Counter(map(int, input().split())).items())
print(sum(v for v, k in A[: max(0, len(A) - K)]))

Solution (Python):
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction



mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]






n,k=l()
A=l()
d={}
for i in range(n):
    if(A[i] not in d):
        d[A[i]]=0
    d[A[i]]+=1
x=[]
for ks in d:
    x.append([d[ks],ks])
x.sort()
if(len(x)<=k):
    print(0)
else:
    s=0
    for i in range(len(x)-k):
        s+=x[i][0]
    print(s)

Solution (Python):
N,K = map(int,input().split())
A = [int(i) for i in input().split()]

from collections import Counter
A_C = Counter(A)
LEN = len(A_C)
ans = 0
for i,(k,v) in enumerate(sorted(A_C.items(),key=lambda x:x[1])):
    
    if i < LEN-K:
        ans +=v
print(ans)

Solution (Python):
n, k = map(int, input().split())
A = list(map(int, input().split()))
from collections import Counter
C = Counter(A)
dif = len(C) - k
if dif <= 0:
	print(0)
else:
	print(sum(sorted(C.values())[:dif]))

Solution (Python):
n, k = map(int, input().split())
A = list(map(int, input().split()))
from collections import Counter
C = Counter(A).most_common()
dif = len(C) - k
if dif <= 0:
	print(0)
else:
	s = 0
	for i in range(dif):
		s += C[-(i + 1)][1]
	print(s)

Solution (Python):
import math
import collections
import fractions
import itertools

def solve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    c = collections.Counter(a).most_common()
    c.reverse()
    ans = 0
    for i in range(max(len(set(a))-k, 0)):
        ans += c[i][1]
    print(ans)
    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
from collections import Counter

N, K = map(int, input().split())
c = Counter(map(int, input().split()))
current_kinds = len(c.keys())
remove_ball = 0

for kind, num in sorted(c.items(), key=lambda x: x[1]):
    if current_kinds <= K:
        break
    current_kinds -= 1
    remove_ball += num
print(remove_ball)

Solution (Python):

import sys
from collections import Counter

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
A = lr()
counter = Counter(A)
counter = counter.most_common()
answer = 0
for k, v in counter[K:]:
    answer += v

print(answer)

Solution (Python):
from collections import Counter

N, K = map(int, input().split())
A = list(map(int, input().split()))

C = list(Counter(A).most_common())
count = 0
while len(C) > K:
    tmp = C.pop()
    count += tmp[1]
print(count)

Solution (Python):
from collections import Counter
n,k = map(int,input().split())
a_li = list(map(int,input().split()))
counter_a = Counter(a_li)
most_a = counter_a.most_common()
dum = len(most_a)
if dum <= k:
    print(0)
else:
    ans = 0
    for i in range(dum-1,k-1,-1):
        ans += most_a[i][1]
    print(ans)

Solution (Python):
import collections

n, k = map(int, input().split())
l = list(map(int, input().split()))

c = collections.Counter(l)
c = c.most_common()

m = len(c)
if m <= k:
    print(0)

else:
    ans = [c[i][1] for i in range(k, m)]
    print(sum(ans))

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))

from collections import Counter
c=Counter(a)

variation=len(set(a))
if variation<=k:
    print(0)
    exit()

ans=0
for key, val in sorted(c.items(), key=lambda x: x[1]):
    ans+=val
    variation-=1
    if variation<=k:
        break
print(ans)

Solution (Python):
import collections

N, K = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
cnt = collections.defaultdict(int)
for num in arr:
    cnt[num] += 1

vals = sorted(cnt.values())

if len(cnt) > K:
    for i in range(len(cnt) - K):
        ans += vals[i]

print(ans)

Solution (Python):
import collections
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
C = sorted(collections.Counter(A).most_common(),key=lambda x: x[1])
count = 0
for i in range(len(C)-K):
    count += C[i][1]
print(count)

Solution (Python):
n, k = map(int, input().split())
*a, = map(int, input().split())
d = {}
for i in a:
    d[i] = d.get(i, 0)+1
cnt = len(d)
ans = 0
for _, i in sorted(d.items(), key=lambda x: x[1]):
    if cnt <= k:
        break
    ans += i
    cnt -= 1
print(ans)

Solution (Python):
import collections

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    Aitems = list(collections.Counter(A).items())
    Aitems.sort(key=lambda x: x[1], reverse=True)
    ans = 0    
    remain = len(Aitems) - K
    for r in range(remain):
        tmp = Aitems.pop()
        ans += tmp[1]
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
import sys

N, K = map(int, input().split())
A = list(map(int, input().split()))

d = Counter(A)

d_keys = d.keys()
d_values = d.values()



if len(set(d_keys)) <= K:
    print(0)
    sys.exit()

d_values = sorted(d_values)
ans = sum(d_values[: len(d_values) - K])

print(ans)

Solution (Python):
from collections import Counter, deque

n, k = map(int, input().split())
A = list(map(int, input().split()))

now_cnt = len(list(set(A)))

if (now_cnt <= k):
    print(0)
    exit()

c = Counter(A)
c_sorted = sorted(c.items(), key=lambda x: x[1])
dec = now_cnt - k
cnt = 0
L = deque()

for (i, v) in c_sorted:
    L.append([i, v])

while True:
    if (dec != 0):
        L[0][1] -= 1
        L[-1][1] += 1
        cnt += 1
    else:
        print(cnt)
        exit()

    if (L[0][1] == 0):
        L.popleft()
        dec -= 1

print(cnt)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n,k=map(int, input().split())
    l=list(map(int,input().split()))
    from collections import Counter
    c=Counter(l)
    cntl=c.most_common()
    kosu=len(cntl)
    ans=0
    if kosu<=k:
        print(0)
    else:
        for i in range(k,kosu):
            ans+=cntl[i][1]
        print(ans)
resolve()

Solution (Python):
from collections import defaultdict


N, K = map(int, input().split())
A = list(map(int, input().split()))

dictNum = defaultdict(int)
for a in A:
    dictNum[a] += 1

listNum = sorted(list(dictNum.values()))
if len(listNum) <= K:
    print(0)
else:
    print(sum(listNum[:len(listNum)-K]))

Solution (Python):
import heapq as hp
n,k = map(int, input().split())
a = list(map(int, input().split()))
s = [0 for i in range(n)]
for i in a:
    s[i-1] += 1
t = hp.nlargest(k,s)
print(max(n-sum(t),0))

Solution (Python):
from collections import Counter
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

cnt = Counter(a)
div = max(0, len(cnt) - k)

if div != 0:
  l = sorted(cnt.values())
  print(sum(l[:div]))
  
else:
  print(0)