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):
n,k = map(int,input().split())
l = list(map(int, input().split()))

s=len(set(l))

from collections import Counter
c = Counter(l)
c = sorted(c.items(), key=lambda x:x[1])

if s-k > 0:
    x=0
    for  i in range(s-k):
        x += c[i][1]
    print(x)
else:
    print(0)

Solution (Python):
n,k = map(int,input().split())
a = sorted(list(map(str,input().split())))
def runlength(S):
    ans = []
    for s in S:
        if len(ans) == 0 or ans[-1][0] != s:
            ans.append([s,1])
        else:
            ans[-1][1] += 1
    return ans
a = runlength(a)
a.sort(key = lambda x:x[1],reverse = True)
b = a[k:]
ans = 0
for i in range(len(b)):
  ans += b[i][1]
print(ans)

Solution (Python):
from collections import Counter
n,k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

A = Counter(a)

if len(A.keys()) <= k:
    print(0)
else:
    lst = A.most_common()
    cnt = 0
    for i in range(k, len(lst)):
        cnt += lst[i][1]
    print(cnt)

Solution (Python):
n,k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
count = 1
chk = []
p = 1
if n == 1:
  print(0)
  exit()
else:
  for i in range(n-1):
    if a[i] != a[i+1]:
      count += 1
      chk.append(p)
      p = 1
    else:
      p += 1
  if a[-1] == a[-2]:
    chk.append(p)
  else:
    chk.append(1)
  chk.sort()
  
  
  if count <= k:
    print(0)
    exit()
  else:
    print(sum(chk[:count-k]))
    exit()

Solution (Python):
N,K=map(int,input().split())
A=input().split()
balls=[int(a) for a in A]
answers=[0]*200000
for a in balls:
    answers[a-1]+=1
list=[a for a in answers if a!=0]
newlist=sorted(list)
n=len(newlist)-K
answer=0
for a in range(n):
    answer+=newlist[a]
print(answer)

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

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

print(sum( [i[1] for i in counter[:-K]]))

Solution (Python):
from collections import Counter
N, K = map(int, input().split())
A = list(map(int,input().split()))
B = set(A)
C = Counter(A).most_common()[::-1]
pre_color = len(B)
ans = 0
if pre_color <= K:
    print(0)
    exit()
else:
    dec = pre_color - K
    for i in range(dec):
        ans += C[i][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):
n,k=map(int,input().split())
a=list(map(int,input().split()))
from collections import Counter
d=Counter(a)
g=len(d)
cnt=0
d=sorted(d.items(),key=lambda x:x[1])
for i , j in d:
    if g<=k:
        print(cnt)
        break
    else:
        g-=1
        cnt+=j

Solution (Python):

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

if len(a)<=k:
    print(0)
else:
    c=a.most_common()[::-1]
    print(sum([c[i][1] for i in range(len(a)-k)]))

Solution (Python):

from collections import Counter

def main():
    N,K = map(int,input().split())
    array = list(map(int,input().split()))
    cnt = 0
    diff = 0

    
    CUNT = Counter(array)
    

    array = sorted(CUNT.values(),reverse=True)
    

    ans = sum(array[K:])
    print(ans)

main()

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

import collections

count = collections.Counter(a_L)
c = count.most_common()

if len(set(a_L)) >= k:
    print(0)
    exit()

ans = 0
tmp = 0

for _,v in c:
    if tmp >= k:
        ans += v
    tmp += 1


print(ans)

Solution (Python):
N, K = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
bucket = {}
for ai in A:
    if bucket.get(ai) == None:
        bucket[ai] = 1
    else:
        bucket[ai] += 1
if len(bucket) <= K:
    print(0)
else:
    bucket_sorted = sorted(bucket.items(), key=lambda x:x[1], reverse=True)
    ans = 0
    for k, v in bucket_sorted[K:]:
        ans += v
    print(ans)

Solution (Python):
from collections import Counter
N,K=map(int,input().split())
A=list(map(int,input().split()))
l=[]
c=Counter(A)
value=list(c.values())
key=list(c.keys())
count=0
for i in range(len(key)):
    l.append([key[i],value[i]])
l.sort(key=lambda x:x[1])

for i in range(len(key)-K):
    count=count+l[i][1]
print(count)

Solution (Python):
import collections
n, k = map(int, input().split())
A = list(map(int, input().split()))
B = collections.Counter(A)
B = sorted(B.items(), key=lambda x: x[1])
ans = 0
for i in range(len(B)-k):
    ans += B[i][1]
print(ans)

Solution (Python):
import collections
n, k = map(int, input().split())
d = collections.defaultdict(int)
b = map(int, input().split())
for e in b:
    d[e] += 1
ret = 0
a = sorted([v for v in d.values()], reverse = True)
while len(a) > k:
    ret  += a.pop()
print(ret)

Solution (Python):
from collections import Counter

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

aa = Counter(a)
ans = 0

if len(aa) > k:
	value = sorted(aa.values())
	ans = sum(value[:len(aa)-k])

print(ans)

Solution (Python):
import collections
N,K = map(int,input().split())
d = collections.defaultdict(int)
b = map(int,input().split())
for e in b:
  d[e]+=1
ret = 0
a = sorted([v for v in d.values()],reverse=True)
while len(a) > K:
  ret += a.pop()
print(ret)

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



A_dict = {}
for a in A:
  if a not in A_dict:
    A_dict[a] = 0
  A_dict[a] += 1

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


num_A = len(sorted_A)

cnt=0

for item in sorted_A:
  if num_A > K:
    cnt += item[1]
    num_A -= 1
  else:
    break
print(cnt)

Solution (Python):
import collections

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

list_a = list(collections.Counter(a).values())
dic = dict(zip(collections.Counter(a), list_a))
dic_sorted = sorted(dic.items(), key=lambda x: x[1])
del_num = dic_sorted[:len(dic_sorted)-k]

for i in del_num:
    ans += i[1]

print(ans)

Solution (Python):
import collections
def main():
  N, K = map(int, input().split())
  A =list(map(int, input().split()))
  LA = list(collections.Counter(A).values())
  LA.sort(reverse = True)
  Ans = N-sum(LA[0:K])
  if Ans > 0:
    print(Ans)
  else:
    print(0)

main()

Solution (Python):

import math
import heapq
import bisect
import numpy as np
from collections import Counter


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

B = list(Counter(A).values())
B.sort()

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

print(ans)

Solution (Python):
import collections
n,k = map(int,input().split())
a = input().split()
a = list(collections.Counter(a).values())
a.sort(reverse = True)
a = sum(a[:k])
if a == n:
  print(0)
else:
  print(n-a)

Solution (Python):
N, K = [int(x) for x in input().split(" ")]
lst = [int(x) for x in input().split(" ")]

dic= {}
cnt = 0 
swap = 0
prep = len(set(lst))-K

for i in range(1,N+1):
	dic["{}".format(i)] = 0

for i in range(0,N):
	dic["{}".format(lst[i])] += 1

sorted_dic = sorted(dic.items(), key = lambda x : x[1])
    
for i in range(0,N):
  swap += sorted_dic[i][1]
  if sorted_dic[i][1] != 0:
    cnt +=1
  if cnt >= prep:
    break
      
print(swap)

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

Asort = sorted(A)

from collections import Counter
c = Counter(A)
keys, values = zip(*c.most_common())

ans = 0
N = len(keys)

for i in range(K,N):
    ans += values[i]
    
print(ans)

Solution (Python):
import collections

n, k = map(int, input().split())
values = list(map(int, input().split()))
obj, cnt = zip(*collections.Counter(values).most_common()[::-1])
cnt = list(cnt)
diff = max(0, len(cnt) - k)
ans = sum(cnt[:diff])
print(ans)

Solution (Python):
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
count = list(Counter(a).items())
count.sort(key=lambda x:x[1])

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

Solution (Python):
import collections

n, k = map(int, input().split())
a = list(map(int, input().split()))
a_cnt = collections.Counter(a).most_common()[::-1]
ans = 0
n = len(list(set(a)))
if n <= k:
    print(0)
    exit()
for v in range(n):
    ans += a_cnt[v][1]
    n -= 1
    if n <= k:
        print(ans)
        exit()

Solution (Python):
def NotsoDiverse(n , k , al):

    lis = [0]*(n + 1)

    for i in range(n):
        lis[al[i]] += 1

    lis.sort(reverse=True)

    ans = sum(lis[k:])
    return ans

def main():
    n , k = map(int , input().split())
    al = list(map(int , input().split()))
    print(NotsoDiverse(n , k , al))

if __name__ == '__main__':
    main()

Solution (Python):
n, k = [int(e) for e in input().split()]
a = list(map(str, input().split()))
elem_dict = {}
ans = 0
for i in a:
    if i not in elem_dict:
        elem_dict[i] = 1
    else:
        elem_dict[i] += 1
elem_sorted = sorted(elem_dict.items(), key=lambda x:x[1])
for i in range(len(elem_sorted)-k):
    ans = ans + elem_sorted[i][1]
print(ans)

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

d = {}
for a in As:
    if a not in d:
        d[a] = 1
    else:
        d[a] += 1


diff = len(d) - K
ans = 0
if diff > 0:
    new_d = sorted(d.items(), key=lambda x: x[1])
    
    for x, y in new_d[:diff]:
        ans += y
    print(ans)
else:
    print('0')

Solution (Python):
import collections

n, k = map(int, input().split())
a = [int(_) for _ in input().split()]

num = len(set(a))
if num <= k:
    print(0)
else:
    num = num - k
    c = collections.Counter(a).most_common()
    ans = 0
    for i in reversed(c):
        if num > 0:
            ans += i[1]
        else:
            break
        num -= 1
    print(ans)

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

Solution (Python):
import collections

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

obj, cnt = zip(*collections.Counter(values).most_common()[::-1])
cnt = [i for i in cnt]

N_diff = max(0, len(cnt) - K)
ans = sum(cnt[: N_diff])
print(ans)

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

Solution (Python):

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

Solution (Python):
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
from collections import defaultdict
dd = defaultdict(int)

N,K=MI()
A=LI()

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

for k,v in dd.items():
    li.append(v)
    
li.sort(reverse = True)

print(N - sum(li[:min(K,N)]))

Solution (Python):
N, K = input().split()
A = [int(n) for n in input().split()]
kind = dict()

for n in A:
    if n not in kind:
        kind[n] = 0
    kind[n] += 1
diff = len(kind) - int(K)
if diff > 0:
    values = sorted(kind.values())
    print(sum(values[:diff]))
else:
    print(0)

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

dic=Counter(num)
d=sorted(dic.items(),key=lambda x:x[1])
l=len(d)
if l<=k:
  ans=0
else:
  ans=sum([d[i][1] for i in range(l-k)])
print(ans)

Solution (Python):
from collections import Counter

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

print(sum(sorted(c.values(), reverse=True)[k:]))

Solution (Python):
from collections import Counter

n, k = map(int, input().split())
b_list = list(map(int, input().split()))
mycounter = Counter(b_list)

ans = n

for i in mycounter.most_common(k):
  ans -= i[1]
print(ans)

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

checker = dict()

for a in ass:
    checker.setdefault(a, 0)
    checker[a] += 1
    
ss = sorted(checker.values())
c = len(ss)
ans = 0

for s in ss:
    if c <= k:
        break
    else:
        ans += s
        c -= 1
print(ans)

Solution (Python):
n,k = map(int,input().split())
a = [int(i) for i in input().split() ]

d = {}
for i in a:
  if i in d:
    d[i] += 1
  else:
    d[i] = 1
    
    
d = sorted(d.values())
if len(d) > k:
  ans = 0
  for i in range(len(d)-k):
    ans += d[i]
  print(ans)
else:
  print(0)

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

count=0
values=sorted(B.values())
for i in range(len(B)-K):
    count+=values[i]
    

print(count)

Solution (Python):
import numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=np.zeros(N)
for i in A:
  B[i-1]+=1
C=sorted([i for i in B if i != 0])
ans=0
for i in range(len(C)-K):
  ans+=C[i]
print(int(ans))

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

Solution (Python):
from collections import Counter
from operator import itemgetter
n,k = map(int,input().split())
dic = Counter(list(map(int,input().split())))
x = len(dic) - k
lis = sorted(dic.items(), key = itemgetter(1))
ans = 0
for i in range(x):
    ans += lis[i][1]
print(ans)

Solution (Python):
from collections import Counter
n,k = map(int,input().split())
dic = Counter(list(map(int,input().split())))
x = len(dic) - k
lis = sorted(dic.items(), key = lambda z:z[1])
ans = 0
for i in range(x):
    ans += lis[i][1]
print(ans)

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

list_num = [0 for i in range(N+1)]
for i in range(N) :
    list_num[list_A[i]] += 1

list_num.sort(reverse = True)

ans = N
for i in range(K) :
    ans -= list_num[i]

if ans > 0 :
    print(ans)
else :
    print(0)

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

import collections
c = collections.Counter(a)
m = c.most_common()
ans = 0
if len(m)<=k:
    print(0)
else:
    for i in range(k, len(m)):
        ans += m[i][1]
    print(ans)

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

c=collections.Counter(a)
num_kind=len(set(a)) 
diff = num_kind-k 
values, counts = zip(*c.most_common()[::-1]) 
print(sum(counts[:diff]))

Solution (Python):
n,k = map(int,input().split())
a = [int(x) for x in input().split()]
d = {}
ans_list = []
ans = 0

for aa in a:
    if aa not in d:
        d[aa] = 0
    d[aa] += 1

for v in d.values():
    ans_list.append(v)

ans_list.sort()

for i in range(len(ans_list) - k):
    ans += ans_list[i]

print(ans)

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

stay = 0
for k,v in c.most_common(k):
    stay += v
print(n-stay)

Solution (Python):
from collections import Counter
N, K = map(int, input().split())
A = [int(i) for i in input().split()]
c = Counter(A)
if len(c) > K:
    v = list(c.values())
    v.sort(reverse=True)
    print(sum(v[K:]))
else:
    print(0)

Solution (Python):
from collections import Counter

n,k=map(int,input().split())
a=list(map(int,input().split()))
num_count = Counter(a)
num_count = sorted(num_count.values())
print(sum(num_count[:-k]))

Solution (Python):
from collections import Counter

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

tmp = Counter(A).most_common()[::-1]
ans = 0
for i in range(len(tmp)-K):
    ans += tmp[i][1]

print(ans)

Solution (Python):
import collections
N,K=map(int,input().split())
A=list(map(int,input().split()))
c=collections.Counter(A)
cm=c.most_common()
t=len(cm)
j=-1
ans=0
while(t>K):
    ans+=cm[j][1]
    t-=1
    j-=1
print(ans)

Solution (Python):
from collections import Counter
n, k = map(int,input().split())
a = list(map(int,input().split()))
ctr = Counter(a)
stay = 0
for k,v in ctr.most_common(k):
    stay += v
print(n - stay)

Solution (Python):
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce

n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt = Counter(a).most_common()[::-1]


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

Solution (Python):
from collections import Counter 

def main():
    n, k = map(int, input().split())
    num_list = list(map(int, input().split()))

    count_list = Counter(num_list)

    tmp_list = []

    for v in count_list.values():
        tmp_list.append(v)

    tmp_list.sort()
    
    count = 0
    for i in range(len(tmp_list)-k):
        count += tmp_list[i]

    print(count)

if __name__ == '__main__':
    main()

Solution (Python):




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

balls={}

for num in A_list:
    if str(num) not in balls:
        balls[str(num)] = 1
    else:
        balls[str(num)] += 1


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

result = 0
if len(balls) > K:
    for i in range(len(balls) - K):
        result += balls_list[i][1]

print(result)

Solution (Python):
def main():
    n, target = map(int, input().split())
    count = {}
    numbers = list(map(int, input().split()))
    for num in numbers:
        if num in count:
            count[num] += 1
        else:
            count[num] = 1
    count = sorted(count.values())
    print(sum(count[:-target]) if len(count) > target else 0)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import collections
input = sys.stdin.readline

def main():
    N, K = map(int, input().split())
    a_list = list(input().split())
    int_freq = dict(collections.Counter(a_list))
    sorted_freqs = sorted(int_freq.values(), reverse=True)
    print(sum(sorted_freqs[K:]))

if __name__ == '__main__':
    main()

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
d ={}
for key in a:
    if key in d:
        d[key] += 1
    else:
        d[key] = 1

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

Solution (Python):
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt = Counter(a)
cnt_sorted = sorted(cnt.items(), key=lambda x: x[1])
num = len(cnt.keys()) - k
ans = 0
for i in range(num):
    ans += cnt_sorted[i][1]
print(ans)

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

print(sum(sorted(Counter(A).values(),reverse=True)[K:]))

Solution (Python):
n,k = map(int, input().split())
A = [int(x) for x in input().split()]
d = {}
for a in A:
  if a in d:
    d[a] +=1
  else:
    d[a] =1
    
d =sorted(d.values())
if len(d) > k:
  ans = 0
  for i in range(len(d)-k):
    ans += d[i]
  print(ans)
else:
  print(0)

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

num = [0] * 200005
kind = 0
for i in a:
    if num[i] == 0:
        kind += 1
    num[i] += 1
num.sort()
ans = 0
for i in range(200005):
    if kind <= k:
        break
    if num[i] == 0:
        continue
    ans += num[i]
    kind -= 1
print(ans)

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

lis = [0] * (max(a)+1)

for i in range(n):
    lis[a[i]-1] += 1
lis.sort()



ans = 0
for i in range(len(lis)-k):
    ans += lis[i]

print(ans)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
dic = {}
B = []
for a in A:
  if a in dic:
    dic[a] += 1
  else:
    dic[a] = 1
for v in dic.values():
  B.append(v)
B.sort()
if len(B) <= K:
  print(0)
else:
  print(sum(B[:-K]))

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
a2=sorted(a)
kindnum=[]
kind=1
now=a2[0]
ren=1
for i in range(1,n):
    if now!=a2[i]:
        kindnum.append(ren)
        kind+=1
        now=a2[i]
        ren=1
        continue
    ren+=1
kindnum.append(ren)
kindnum2=sorted(kindnum)
if kind<=k:
    print(0)
else:
    ans=0
    for i in range(kind-k):
        ans+=kindnum2[i]
    print(ans)

Solution (Python):
N,K = map(int,input().split())
A = [int(x) for x in input().split()]
d = {}
for a in A:
	if a in d:
		d[a] += 1
	else:
		d[a] = 1

d = sorted(d.values())

if len(d)>K:
	ans = 0
	for i in range(len(d)-K):
		ans += d[i]
	print(ans)
else:
	print(0)

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

from collections import defaultdict
d = defaultdict(lambda: 0)

for i in a:
  d[str(i)] += 1
  
ds = sorted(d.values(), reverse = True)
print(sum(ds[k:]))

Solution (Python):
n, k = map(int, input().split())
aList = list(map(int, input().split()))
num = [0 for _ in range(n + 1)]
for i in aList:
    num[i] += 1
num.sort()
num = num[:-k]
print(sum(num))

Solution (Python):
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
hash = [0 for i in range(n+1)]

if len(set(a))<=k:
  print(0)
  exit()
else:
  ans=0
  for i in a:
    hash[i]+=1
  hash.sort()
  L=hash
  
  x = len(set(a)) - k
  j=0
  m=0
  while m!=x:
    if L[j]==0:
      j+=1
      continue
    else:
      ans+=L[j]
      j+=1
      m+=1
  print(ans)

Solution (Python):
def keycount(li):
    from collections import defaultdict
    count = defaultdict(int)
    for i in li:
        count[i] += 1
    dst = sorted([i for k, i in count.items()])
    return dst
num, shurui = map(int, input().split())
ball = input().split()
ballset = set(ball)
if shurui < len(ballset):
    count = len(ballset) - shurui
    dst = sum(keycount(ball)[:count])
else:
    dst = 0
print(dst)

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

A_dict = {x:0 for x in A_set}
for i in A:
    A_dict[i] += 1
A_dict = sorted(A_dict.items(), key=lambda x:x[1])

ans = 0
if len(A_set) > K:
    for i in range(len(A_set) - K):
        ans += A_dict[i][1]
        
print(ans)

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

Solution (Python):
import collections
import numpy

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

A = collections.Counter(A)
C=A.most_common()

if len(C)> K:
	dels=len(C) - K
	delsList=numpy.array(C[-dels:])
	ans=sum(delsList)[1]
else :
	ans=0

print(ans)

Solution (Python):
N, K = [int(x) for x in input().split()]
A_lst = [int(x) for x in input().split()]
dict = {}
for i in A_lst:
    if not i in dict.keys():
        dict[i] = 1
    else:
        dict[i] += 1

val_lst = list(dict.values())
val_lst.sort()
k = len(val_lst)
cnt = 0

for i in val_lst:
    if k <= K:
        break
    else:
        k -= 1
        cnt += i

print(cnt)

Solution (Python):
N, K = [int(x) for x in input().split()]
A_lst = [int(x) for x in input().split()]
dict = {}
for i in A_lst:
    if not i in dict.keys():
        dict[i] = 1
    else:
        dict[i] += 1

lst = []
for i in dict.keys():
    lst.append(dict[i])
lst.sort()
k = len(lst)
cnt = 0

for i in lst:
    if k <= K:
        break
    else:
        k -= 1
        cnt += i

print(cnt)

Solution (Python):
N, K = [int(x) for x in input().split()]
A_lst = [int(x) for x in input().split()]
dict = {}
for i in A_lst:
    if not i in dict:
        dict[i] = 1
    else:
        dict[i] += 1

lst = []
for i in dict.keys():
    lst.append(dict[i])
lst.sort()
k = len(lst)
cnt = 0

for i in lst:
    if k <= K:
        break
    else:
        k -= 1
        cnt += i

print(cnt)

Solution (Python):
N, K = [int(x) for x in input().split()]
A_lst = [int(x) for x in input().split()]
dict = {}
for i in A_lst:
    if not i in dict:
        dict[i] = 1
    else:
        dict[i] += 1

lst = []
for i in dict.keys():
    lst.append(dict[i])
lst.sort()
k = len(lst)
cnt = 0
i = 0

while k > K:
    k -= 1
    cnt += lst[i]
    i += 1

print(cnt)

Solution (Python):
import collections

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

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

a_count=collections.Counter(a)

aco=sorted(a_count.values())
ans=0


for i in range(len(aco)-k):
    ans+=aco[i]
    

print(ans)

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

arr = [0] * 200000
for i in range(len(a)):
    arr[a[i]] += 1

arr.sort()
arr.reverse()

s = 0
for i in range(k):
    s += arr[i]
    
print(n-s)

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
from collections import Counter
A=Counter(A)
ret=N
for i in A.most_common(K):
  ret-=i[1]
print(ret)

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):
from collections import Counter
from operator import itemgetter
import numpy as np

N, K = map(int, input().split())
A = list(map(int, input().split()))
c = np.array(sorted(Counter(A).items(), key=itemgetter(1), reverse=True))
a_to_replace = c[K:]
print(sum(a_to_replace[:,1]))

Solution (Python):
N, K = map(int, input().split())
d = {}
for x in map(int, input().split()):
    n = d.get(x, 0) + 1
    d[x] = n

cnt = len(d)
ret = 0
for k, v in sorted(d.items(), key=lambda item: item[1]):
    if K >= cnt:
        print(ret)
        exit()

    ret += v
    cnt -= 1

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

a.sort()

count = 1
num_count = 1
num_count_list = list()
for i in range(1, n):
    if(a[i] == a[i - 1]):
        num_count += 1
    else:
        num_count_list.append(num_count)
        num_count = 1
        count += 1

num_count_list.append(num_count)

num_count_list.sort()
result = 0
if(count > k):
    loop = count - k

    for i in range(loop):
        result += num_count_list[i]

print(result)

Solution (Python):

import sys
import collections

def solve(N: int, K: int, A: "List[int]"):
    r = 0

    c = collections.Counter(A).most_common()
    while K < len(c):
        _, cnt = c.pop()
        r+=cnt
    return r


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    print(solve(N, K, A))

if __name__ == '__main__':
    main()

Solution (Python):
import collections as c

n,k = map(int,input().split())
l = list(map(int,input().split()))
count = c.Counter(l)
ans = 0
check = 0
right = len(count)-k
if right <= 0:
    print(0)
    exit()
for i,j in count.most_common()[::-1]:
    ans += j
    check += 1
    if check == right:
        print(ans)
        exit()

Solution (Python):
n, k = input().split()
n, k = int(n), int(k)
balls = input().split()
dic = {}
for x in balls:
	if x not in dic:
		dic[x] = 1
	else:
		dic[x] += 1
change = 0
qnt = list(dic.values())
qnt.sort()
dif = len(qnt) - k
for x in range(0, dif):
	change += qnt[x]
print(change)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
count = 0
x = dict()
ans = 0
for i in range(N):
    if A[i] in x:
        x[A[i]] += 1
    else:
        x[A[i]] = 1
        count += 1
l = []
if count <= K:
    print(0)
else:
    for idx in x:
        l.append(x[idx])
    l.sort()
    for i in range(count-K):
        ans += l[i]

    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N,K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
import collections
c = collections.Counter(A)
li = list(c.values())
li.sort()
li.reverse()
ans = 0
for i in range(K,len(li)) :
    ans += li[i]
print(ans)

Solution (Python):
import math
import sys
import numpy as np
import collections


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


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

a1 = collections.Counter(a)
num = [0]*len(a1)
counter = 0

for i in a1.keys():
  num[counter] = a1[i]
  counter = counter + 1

num.sort()
tot = 0
for i in range(len(num)-k):
  tot = tot + num[i]

print(tot)

Solution (Python):
n,k = map(int,input().split())
nList = list(map(int,input().split()))
nSet = list(set(nList))
nDic = {}

for i in nList:
  if not(i in nDic.keys()):
    nDic[i] = 1
  else:
    nDic[i] += 1

countList = sorted(nDic.values())
print(sum(countList[:(len(nSet)-k)]))

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

Solution (Python):
import collections

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

ans=0
A_=set(A)
if len(A_)<=K:
    pass
elif len(A_)>K:
    r=len(A_)-K
    A_Cnt=collections.Counter(A)
    A_Cnt_Sort=A_Cnt.most_common()[::-1]
    for a in A_Cnt_Sort[:r]:
        ans+=a[-1]

print(ans)

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

AS = list(map(int,input().split()))
balls = {}

for a in AS:
  if a in balls:
    balls[a] += 1
  else:
    balls[a] = 1

dic2 = sorted(balls.items(), key=lambda x:x[1])
amount = len(dic2) - k

count = 0
for i in range(amount):
  count += dic2[i][1]
print(count)

Solution (Python):
from collections import Counter
n,k = map(int,input().split())
a = Counter(map(int, input().split()))
cnt = 0
for v in sorted(a.values())[:len(a)-k]:
    cnt += v
print(cnt)