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 re
import math
from collections import defaultdict
from collections import deque
import collections
from fractions import Fraction
import itertools
from copy import deepcopy
import random
import time
import os
import queue
import sys
import datetime
from functools import lru_cache

readline=sys.stdin.readline
sys.setrecursionlimit(2000000)

alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_pow(a,b):
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def get_primes(n,type="int"):
    if n==0:
        if type=="int":return []
        else:return [False]
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=""):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def pm(x):
    return x//abs(x)
def inputintlist():
    return list(map(int,input().split()))



N,K=inputintlist()
A=inputintlist()
D=[]
for a,b in collections.Counter(A).items():
    D.append([a,b])
D.sort(key=lambda x:x[1])
llen=len(D)
ans=0
i=0
while llen>K:
    ans+=D[i][1]
    llen-=1
    i+=1
print(ans)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
memo = [0] * N
ans = 0

for a in A:
    memo[a-1] += 1
    
memo.sort()

for i in memo[:N-K]:
    ans += i

print(ans)

Solution (Python):
import collections
n, k = map(int, input().split())
list = input().split()
c_list = collections.Counter(list)
sorted_c_list = sorted(c_list.values(),reverse=True)
print(sum(sorted_c_list[k:]))

Solution (Python):

from collections import Counter
N, K = map(int, input().split())
A = list(map(int, input().split()))
c = Counter(A)
k = sorted(c.most_common(), reverse=True, key=lambda x: x[1])[K:]

ans = 0
for e in k:
    ans += e[1]
print(ans)

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

for i in a:
    if i in ball:
        ball[i] += 1
    else:
        ball[i] = 1

ball = sorted(ball.items(), key=lambda x: -x[1])

ans = 0
if len(ball) > k:
    for i in range(k):
        ans += ball[i][1]
    ans = n - ans
print(ans)

Solution (Python):
def main():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))

    res = 0
    balls = {}
    bucket = [[] for _ in range(n+1)]

    for ball in A:
        if ball in balls:
            balls[ball] += 1
        else:
            balls[ball] = 1
    
    for key, value in balls.items():
        bucket[value].append(key)

    for i in range(len(bucket)):
        for j in range(len(bucket[i])):
            if bucket[i] != [] and len(balls) > k:
                res += i
                balls.pop(bucket[i][j])

    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
n,k=map(int,input().split())
a=list(map(int,input().split()))
d=Counter(a).most_common()
ans=0
for i in range(k,len(d)):
    ans+=d[i][1]
print(ans)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
c = [0] * n
for i in a:
  c[i - 1] += 1
print(sum(sorted(c, reverse=True)[k:]))

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

D = {}
for a in A:
    if a not in D:
        D[a] = 1
    else:
        D[a] += 1
E = sorted(D.values())
c = 0
i = 0
l = len(D)
while l > K:
    c += E[i]
    l -= 1
    i += 1
print(c)

Solution (Python):
from collections import defaultdict

N, K = map(int, input().strip().split(" "))

d = defaultdict(int)

for i in [int(i) for i in input().strip().split(" ")]:
    d[i] += 1

if len(d.keys()) <= K:
    print(0)
else:
    cnt = len(d.keys()) - K
    n = 0
    for v in sorted(d.values())[:cnt]:
        n += v
    print(n)

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

count = [0] * n

for x in range(n):
    count[a[x]-1] += 1

count.sort()
length = len(count)

print(sum(count[:length - k]))

Solution (Python):
import collections

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

c = collections.Counter(alis)

jun = c.most_common()[::-1]

total = len(jun)
rem = total - k


ret = 0
if rem < 0:
    print(0)
else:
    for i in range(rem):
        ret += jun[i][1]
    print(ret)

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

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




count = [0] * (n+1)
for i in a:
  count[i] += 1
count2 = []
for i in count:
  if i:
    count2.append(i)
count2 = sorted(count2)
target = len(count2) - k 
target = max(target,0)


ans = 0
if target:
  for i in range(target):
    ans += count2[i]
print(ans)

Solution (Python):
from collections import Counter

N, K = map(int, input().split(' '))
balls = tuple(map(int, input().split(' ')))

counter = Counter(balls)
counts = sorted(counter.values(), reverse=True)

print(sum(counts[K:]))

Solution (Python):
from collections import Counter

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

ans = 0
if k < len(Counter(a).keys()):
    kv = Counter(a).most_common()
    for i in range(1, len(Counter(a).keys()) - k + 1):
        ans += kv[-i][1]

print(ans)

Solution (Python):
from collections import Counter
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
c = Counter(A)
diff = len(c.keys()) - K
if diff<=0:
    print(0)
else:
    c_s = sorted(c.items(), key=lambda x: x[1])
    cnt=0
    for _, value in c_s:
        cnt += value
        diff -= 1
        if diff==0:
            break
    print(cnt)

Solution (Python):

import sys
from collections import Counter
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,K=map(int,input().split())
    A=list(map(int,input().split()))
    c=Counter(A)
    m=c.most_common()[::-1]
    ans=0
    for _,n in m[:-K]:
        ans+=n
    print(ans)
    

if __name__ == '__main__':
    main()

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

a=[int(i) for i in input().split()]
d=Counter(a)
a=sorted(list(d.items()),key=lambda x:x[1],reverse=True)
if len(a)<=k:
    print(0)
else:
    ans=0
    for i in range(k,len(a)):
        ans+=a[i][1]
    print(ans)

Solution (Python):
from collections import Counter

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

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

Solution (Python):
from collections import defaultdict

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

d={}
for i in range(N):
    if A[i] in d.keys():
        d[A[i]]+=1
    else:
        d[A[i]]=1

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

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

dict = {}

for a in A:
    if a not in dict:
        dict[a] = 1
    else:
        dict[a] += 1

countlist = sorted([a for a in dict.values()], reverse=True)

print(sum(countlist[K:]))

Solution (Python):
import collections
N,K = map(int,input().split())
A = list(map(int,input().split()))
A = collections.Counter(A)
res = 0
cnt = A.most_common()
if len(cnt) > K:
    for i in range(K,len(cnt)):
        res += cnt[i][1]
print(res)

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

Solution (Python):
from collections import Counter

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

c = Counter(A)
d = c.most_common()[::-1]

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

print(ans)

Solution (Python):
import sys
from collections import Counter
N,K=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
C=Counter(A)
C=list(C.values())
C.sort()
print sum(C[:-1*K])

Solution (Python):
from collections import Counter

def resolve():
	n, k = map(int, input().split())
	a = list(map(int, input().split()))
	c = Counter(a)
	lc = len(c.keys())
	if lc > k:
		v = 0
		sc = sorted(c.values())
		for i in range(lc-k):
			v += sc[i]
		print(v)
	else:
		print(0)
resolve()

Solution (Python):

def datain(num):

    data = []
    iscnt = True

    while iscnt:
        for val in map(int,input().split()):
            data.append(val)
            if len(data) == num:
                iscnt = False
                break
        
    return data

INF = 10**8
data1 = datain(2)
data2 = datain(data1[0])
count = [INF for i in range(data1[0]+1)]
num = 0
ans = 0

for val in data2:
    if count[val] == INF:
        count[val] = 0
        num += 1
    count[val] += 1

count.sort()


if num >= data1[1]:
    for val in count[0:(num-data1[1])]:
        ans += val

print(ans)

Solution (Python):
def resolve():
    
    
    
    n, k = map(int, input().split())
    
    
    
    A = list(map(int, input().split()))
    
    

    Dic = {x:0 for x in set(A)}
    if len(Dic) <= k:
        print(0)
    else:
        for y in A:
            Dic[y] += 1
        DIcsort = sorted(Dic.items(), key=lambda x: x[1])

        cnt = 0
        for i in range(len(Dic) - k):
            cnt += DIcsort[i][1]

        print(cnt)
resolve()

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

Solution (Python):
ma = lambda :map(int,input().split())
n,k = ma()
A = list(ma())
import collections
co = collections.Counter(A)
l = sorted(list(co.values()))

print(sum(l[0:len(l)-k]))

Solution (Python):
def main():
    from collections import Counter

    n, k, *a = map(int, open(0).read().split())
    c = Counter(a).values()
    v = len(c) - k
    l = list(c)
    l.sort()
    ans = sum(l[:v])
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from collections import Counter

    n, k, *a = map(int, open(0).read().split())
    c = Counter(a)
    v = len(c)
    l = list(c.values())
    m = sorted(l)
    ans = sum(m[:v - k])
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from collections import Counter

    n, k, *a = map(int, open(0).read().split())
    c = Counter(a)
    v = len(c)
    l = list(c.values())
    l.sort()
    ans = sum(l[:v - k])
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter


n, k  = map(int, input().split())
m = Counter(input().split())
print(sum(sorted(m.values(), reverse=True)[k:]))

Solution (Python):
from collections import Counter

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

if len(d) > K:
    print(sum(v for k, v in d[-(len(d) - K):]))
else:
    print(0)

Solution (Python):
import collections

n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt = collections.Counter(a)
ans = 0
t=cnt.most_common()
if len(t) <= k:
    print(0)
    exit()

for i in range(k):
    ans += t[i][1]
print(n - ans)

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

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

A.sort()

cnt = []

if N ==1:
    print(1)
    exit()

tmp = 1
for i in range(1,len(A)):
    if (A[i-1] == A[i]):
        tmp+=1
    else:
        cnt.append(tmp)
        tmp=1

    if (i == (len(A)-1)):
        cnt.append(tmp)
    
cnt.sort()



if N > K:
    print(sum(cnt[:(len(cnt)-K)]))
else:
    print(0)

Solution (Python):
import collections
n, k = map(int, input().split())
a = list(map(int, input().split()))
c = collections.Counter(a)
d = c.most_common()
if len(c) <= k:
    print(0)
else:
    sum = 0
    for i in range(k, len(c)):
        sum += d[i][1]
    print(sum)

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

d = dict(collections.Counter(a))
d = sorted(d.items(), key=lambda x:x[1])
l = len(d)

cnt = 0
for _, j in d:
    if l > k:
        l -= 1
        cnt += j
    else:
        break


print(cnt)

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

d = dict(collections.Counter(a))
d = sorted(d.items(), key=lambda x:x[1])
l = len(d)

cnt = 0
for _, j in d:
    if l > k:
        l -= 1
        cnt += j

print(cnt)

Solution (Python):
def resolve():
    
    
    
    n, k = map(int, input().split())
    
    
    
    A = list(map(int, input().split()))
    
    

    Dic = {x:0 for x in set(A)}
    if len(Dic) <= k:
        print(0)
    else:
        for i in A:
            Dic[i] += 1
        Dicsort = sorted(Dic.items(), key=lambda x: x[1])

        cnt = 0
        for j in range(len(Dic) - k):
            cnt += Dicsort[j][1]

        print(cnt)
resolve()

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    n,k=map(int,readline().split())
    a=list(map(int,readline().split()))

    d=dict()
    for i in range(n):
        if a[i] not in d:
            d[a[i]]=1
        else:
            d[a[i]]+=1

    d=list(d.items())
    d.sort(key=lambda x:x[1])
    res=0
    for i in range(len(d)-k):
        res+=d[i][1]

    print(res)
    
if __name__=="__main__":
    main()

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
import collections
C = collections.Counter(A)
D = sorted(C.items(), key=lambda x: x[1])
B = len(set(A)) -K
result = 0
for a in range(B):
    result += D[a][1]

print(result)

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
x = [0]*n
for ai in a:
  x[ai-1] += 1
x.sort()
print(sum(x[:n-k]))

Solution (Python):
from sys import stdin
from collections import Counter


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N, K = list(map(int,_in[0].split(' ')))  
    A_arr = list(map(int,_in[1].split(' ')))  
    
    counter = Counter(A_arr)
    counter = list(counter.values())
    counter.sort(reverse=True)
    cnt = sum(counter[K:])
    
    print(cnt)

Solution (Python):
N,K=map(int,input().split())
a=list(map(int,input().split()))
d=[0]*N
ans=0
for i in range(N):
    d[a[i]-1]+=1
d.sort(reverse=True)
if len(d)>K:
    ans =sum(d[K:])
print(ans)

Solution (Python):
def resolve():
    
    
    
    n, k = map(int, input().split())
    
    
    
    A = list(map(int, input().split()))
    
    

    Unique = list(set(A))
    Dic = {x:0 for x in set(A)}

    if len(set(A)) <= k:
        print(0)
    else:
        for x in A:
            Dic[x] += 1
        Dicsort = sorted(Dic.items(), key=lambda x: x[1])

        cnt = 0
        for i in range(len(set(A)) - k):
            cnt += Dicsort[i][1]
        print(cnt)
resolve()

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

Ad = {x:0 for x in set(A)}
if len(set(A)) <= K:
    print(0)
else:
    count = 0
    for a in A:
        Ad[a] += 1
    Ad = sorted(Ad.items(), key=lambda x:x[1])
    for i in range(len(set(A))-K):
        count += Ad[i][1]
    print(count)

Solution (Python):

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


d = {}
for i in a:
    if i not in d:
        d[i] = 0
    d[i] += 1
l = []
for i in d.keys():
    l.append(d[i])
l = sorted(l)
print(sum(l[:len(l)-k]))

Solution (Python):


from collections import Counter


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

def main():
    cnt_a = Counter(A).most_common()[::-1]
    task = len(cnt_a) - K
    if task <= 0:
        print(0)
        exit()
    ans = 0
    for i, v in enumerate(cnt_a, 1):
        ans += v[1]
        if i == task:
            print(ans)
            exit()

if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=len(a)
a=dict(Counter(a))
count=0
a = sorted(a.items(), key=lambda x:x[1], reverse=True)
for i,j in a:
  count+=1
  ans-=j
  if count==k:
    break
print(ans)

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

Solution (Python):
import sys
import os
import math
import bisect
import collections
import itertools

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def f(A, B, x):
    return math.floor(A * x / B) - A * math.floor(x / B)


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    A = collections.Counter(il())
    cnt = sorted([v for v in A.values()])

    ret = 0
    for i in range(len(cnt) - K):
        ret += cnt[i]

    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
if len(list(set(a)))<=k:
  print(0)
else:
  d={}
  for i in range(n):
    if a[i] in d:
      d[a[i]]+=1
    else:
      d[a[i]]=1
  d=sorted(d.items(),key=lambda x:x[1])
  m=len(d)
  cnt=0
  for i in range((len(list(set(a))))-k):
    cnt+=d[i][1]
  print(cnt)

Solution (Python):
k,n=map(int,input().split())
import collections
ans=[]
a=list(map(int,input().split()))
c=collections.Counter(a)
for i in list(set(a)):
    ans.append(c[i])
ans.sort()
ANS=0
for i in range(len(set(a))-n):
    ANS+=ans[i]
print(ANS)

Solution (Python):


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


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


C = list(set(A))


B = len(C) 

x = B - K



d = {}


for i in A:
    
    if i in d:
        d[i] += 1
    
    else:
        d[i] = 1


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

ans = 0
count = 0

if x<=0:
    print('0')
else:
    
    for key, value in d:
        
        ans = ans + value
        
        count = count + 1

        
        if count == x:
            print(ans)
            break

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections

n,k = map(int,input().split())
a = list(map(int,input().split()))
c = collections.Counter(a)
item = []
for key,value in c.items():
    item.append([value,key])
item.sort()
cnt = len(item)
i = 0
ans = 0
while True:
    if cnt <= k:
        break
    ans += item[i][0]
    i+=1
    cnt -= 1
print(ans)

Solution (Python):
import collections
n,k = map(int,input().split())
a = input().split()
a = sorted(list(collections.Counter(a).values()))
print(sum(a[:len(a)-k]))

Solution (Python):
import numpy as np
N,K = map(int,input().split())
A = np.array(input().split(), dtype=np.int32)
counter = np.bincount(A)
counter.sort()
counter = counter[::-1] 
no_change = counter[:K].sum()
answer = N - no_change
print(answer)

Solution (Python):
import collections
n,k = map(int,input().split())
a = list(map(int,input().split()))
c = collections.Counter(a)
values, counts = zip(*c.most_common())
print(sum(counts[k:]))

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))

INIT_VAL = 0
def DD2(d1,d2): return [[INIT_VAL]*d2 for _ in range(d1)]
def DD3(d1,d2,d3): return [DD2(d2,d3) for _ in range(d1)]

def to_bin(x: int) -> str: return format(x, 'b') 
def to_oct(x: int) -> str: return format(x, 'o') 
def to_hex(x: int) -> str: return format(x, 'x') 
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]

from itertools import accumulate as acc
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left



def main():
    n, k = MII()
    aa = Counter(MII())
    c = aa.most_common()[::-1][:len(aa)-k]
    print(sum([ci[1] for ci in c]))

if __name__ == '__main__':
    main()

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

import collections
ball = collections.Counter(ball)
ball = list(ball.values())
ball = sorted(ball)

if len(ball) <= k:
  print(0)
else:
  print(sum(ball[0:len(ball)-k]))

Solution (Python):
import collections
n , k = map(int, input().split())
a = list(map(int, input().split()))
c = collections.Counter(a)
x = c.most_common()
ans = 0
for i in range(k,len(x)):
    ans+=x[i][1]
print(ans)

Solution (Python):
N,K = map(int,input().split())
lis = list(map(int,input().split()))
dic = {}
for i in range(1,N+1):
    dic[i-1] = 0
for i in lis:
    dic[i-1] += 1

lis2 = list(dic.values())
lis2.sort()
print(N - sum(lis2[N-K:]))

Solution (Python):
import collections
N,K = map(int,input().split())
a = list(map(int,input().split()))
c = collections.Counter(a)
c_most = c.most_common()
if len(c) <= K:
    print(0)
else:
    print(sum([c_most[-i][1] for i in range(1,len(c)-K+1)]))

Solution (Python):
from collections import defaultdict

def main():
    N, K = map(int, input().split())
    A = [a for a in input().split()]
    L = defaultdict(int)

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

    L = sorted(list(L.items()), key= lambda x : x[1], reverse = True)
    
    cnt = 0
    typ = 0

    for i in range(len(L)):
        typ += 1
        if typ > K:
            pair = L[i]
            cnt += pair[1]
    
    print(cnt)
if __name__ == "__main__":
    main()

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

c=[0]*(n+1)
for i in range(n):
    c[a[i]]+=1
c.sort(reverse=True)
csum=0
for i in range(k):
    csum+=c[i]
print(n-csum)

Solution (Python):

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

set_l = set(l)
cnt = k - len(set_l)
d = [0] * 200001
for j in l:
    d[j] += 1
print(sum(sorted(d)[:-k]))

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
tmp = [0]*N
tmp_2 = [0]*N
Cnt=0
result = 0
for i in range(N):
  tmp[A[i]-1]+= 1
  tmp_2[A[i]-1] = 1

m = sum(tmp_2)-K
if m>0:
	tmp = sorted(tmp)
	for i in range(N):
		if tmp[i]!=0:
			result += tmp[i]
			Cnt +=1
		if Cnt == m:
			break
    
print(result)

Solution (Python):
from collections import Counter
n,k = map(int,input().split())
L = list(map(int,input().split()))
s = len(set(L))
L1 = Counter(L)
L2 = L1.most_common()
L2 = sorted(L2,key = lambda x:x[1])

sum1 =0
if s-k <= 0:
    print(0)
    exit()
else:
    for i in range(s-k):
        sum1 += L2[i][1]
print(sum1)

Solution (Python):
from collections import Counter
n,k = map(int,input().split())
a_ls = list(map(int, input().split()))
c = Counter(a_ls)
value_ls = list(c.values())
value_ls.sort(reverse=True)
ans = 0
for i in range(k, len(value_ls)):
    ans += value_ls[i]
print(ans)

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

c=[0]*(n+1)
for i in range(n):
    c[a[i]]+=1
c.sort(reverse=True)
csum=0
for i in range(k):
    csum+=c[i]
print(n-csum)

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

d = {}
for a in A:
    d.setdefault(a, 0)
    d[a] += 1
t = sorted(d.values())
print(sum(t[:-K]))

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

num = [0] * (n+1)
for i in range(n):
   num[a[i]] += 1

nonzero = sorted([i for i in num if i!=0])

sumR = max(0, len(nonzero) - k)
ans = sum(nonzero[:sumR])
print(ans)

Solution (Python):
def main():
    N,K=map(int,input().split())
    A=[int(x) for x in input().split()]
    
    dictionary={}
    for num in A:
        if num in dictionary:
            dictionary[num]+=1
        else:
            dictionary[num]=1
    
    ans=0  
    i=0
    
    if len(dictionary)<=K:
        ans=0
    else:
        for value in sorted(dictionary.values()):
            if (i==len(dictionary)-K):
                break
            else:
                ans+=value
                i+=1
    print(ans)


if __name__ == "__main__":
    main()

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

a.sort()
num = [0] * n
c = 0
t = 0
for i in range(n-1):
    if a[i] != a[i+1]:
        c += 1
        num[a[i]-1] += c
        t += 1
        c = 0
    else:
        c += 1

s = sum(num)
t += 1
num[a[i+1]-1] = n - s
num.sort()

ans = 0
for i in range(n):
    if num[i] == 0:
        continue
    if t <= k:
        print(ans)
        break
    ans += num[i]
    t -= 1

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

d = {}
for t in a:
    if t in d:
        d[t]+=1
    else:
        d[t]=1
ans = 0
if len(d) <= k:
    print(ans)
else:
    dl = sorted(list(zip(d.values(), d.keys())))
    for i in range(len(dl)-k):
        ans += dl[i][0]
    print(ans)

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

adic=defaultdict(int)
for a in alist:
  adic[a]+=1


if len(adic)<=K:
  print(0)
else:
  freq_list=[]
  for k,v in adic.items():
    freq_list.append(v)
  freq_list.sort(reverse=True)
  
  print(sum(freq_list[K:]))

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

written = set(ball)
count = [0 for _ in range(max(written))]

if len(written) > k:
    for i in ball:
        count[i - 1] += 1
rewrite = [c for c in count if c > 0]
rewrite.sort()
print(sum(rewrite[: len(written) - k]))

Solution (Python):
N, K = map(int, input().split())
ball_count = [0 for _ in range(N)]
for ball in list(map(int, input().split())):
    ball_count[ball-1] += 1

ball_count.sort()
ball_count.reverse()
answer = 0



print(sum(ball_count[K:]))

Solution (Python):
import collections

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

c = collections.Counter(a).most_common()
cnt = len(c)
i = 1
ans = 0
while cnt > k:
    cnt -= 1
    ans += c[-i][1]
    i += 1

print(ans)

Solution (Python):
import collections
import sys
sys.setrecursionlimit(2000000)





n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
a_count = collections.Counter(a)
a_list = list(a_count.items())
a_sorted = sorted(a_list, key=lambda x: x[1])

count = 0
for i in range(len(a_sorted)-k):
    count += a_sorted[i][1]

print(count)

Solution (Python):
import collections as col

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

L= len(set(A))
A = col.Counter(A)

Aval = A.values()
Aval = sorted(Aval)

ans = 0

if L <= K:
    print(0)
    
else:
    for i in range(L-K):
        ans += Aval[i]
    print(ans)