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 collections

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

count = collections.Counter(A)
c = []
for i in count:
    c.append([count[i],i])

c.sort()

l = len(c)
ans = 0
for i in range(0,max(0,l-K),1):
    ans += c[i][0]

print(ans)

Solution (Python):
def main():
    n, k = map(int, input().split())
    inlis = list(map(int, input().split()))
    indic = dict()
    cnt = 0
    for i in range(n):
        num = inlis[i]
        if num in indic:
            indic[num] += 1
        else:
            indic[num] = 1
            cnt += 1
    if cnt <= k:
        print(0)
    else:
        indic_sort = sorted(indic.items(), key=lambda x:x[1])
        
        sa = cnt - k
        tmp = 0
        ans = 0
        for j in range(n):
            tmp += 1
            ans += indic_sort[j][1]
            if tmp == sa:
                break
        print(ans)



if __name__ == "__main__":
    main()

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

Solution (Python):
n,k = map(int,input().split())
l = list(map(int, input().split()))
freq = [0]*(n+1)
for i in l:
    freq[i]+=1
freq.sort()
vf = len(freq) - k if len(freq)> k else 0
print(sum(freq[:vf]))

Solution (Python):
from collections import Counter

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

dic = dict(Counter(A))
ls = list(dic.items())
ls.sort(key=lambda x: x[1])

i=0
ans=0
res=len(ls)
while 1:
  if res <= K or i >= N:
    break
  ans += ls[i][1]
  i+=1
  res-=1
print(ans)

Solution (Python):
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = Counter(a)
if len(set(a)) <= k:
    print(0)
else:
    ci = list(l.values())
    ci.sort()
    i = 0
    cnt = len(ci)
    ans = 0
    while cnt > k:
        cnt -= 1
        ans += ci[i]
        i += 1
    print(ans)

Solution (Python):
N, K = input().split()
A = dict()
for i in input().split():
  if int(i) in A:
    A[int(i)]+=1
  else:
    A[int(i)]=1

hoge = []
for i in A.values():
  hoge.append(i)
hoge.sort()

sum = 0
changed = 0
while len(hoge) - changed > int(K):
  sum += hoge[changed]
  changed += 1

print(sum)

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

import collections

cA = collections.Counter(A)

sorted_ls = sorted(list(cA.values()))
sum_ls = sum(sorted_ls)


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

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
a=[0]*200000
b=[]
for i in A:a[i-1]+=1
for i in a:
    if i:b.append(i)
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());print(sum(list(sorted(collections.Counter(input().split()).values()))[:-k]))

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

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

c = Counter(a)
ans = 0
c_sort = c.most_common()

for i in range(1, syurui-k + 1):
    ans += c_sort[-i][1]
print(ans)

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
m= [0 for i in range(200001) ]
for i in range(N):
  m[A[i]] += 1
m.sort(reverse=True)
ans = 0
for i in range(K,N):
  if ( m[i] == 0 ):  break
  ans += m[i]
print(ans)

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

cnt = Counter(a)

l = len(cnt)
t = l-k
ans = 0
for i in sorted(cnt.values()):
  if t <= 0: break
  t -= 1
  ans += i
print(ans)

Solution (Python):
from collections import Counter
n,k=map(int,input().split())
L=Counter(list(map(int,input().split())))
num=max(len(L)-k,0)
ans=0
if num==0:
    print(0)
    exit()
L2=sorted(L.values())
print(sum(L2[:num]))

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

print(sum)

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

print(sum)

Solution (Python):
from collections import Counter

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

c = Counter(A)
val = sorted(c.values())

if len(val) <= K:
  print(0)
  exit()
  
print(sum(val[:len(val)-K]))

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

from collections import Counter
dic = dict(Counter(A))
ls = list(dic.items())
ls.sort(key=lambda x: x[1])

uniq = len(ls)
i = 0
ans = 0
while 1:
    if uniq <= K or i >= N:
        break
    ans += ls[i][1]
    i += 1
    uniq -= 1

print(ans)

Solution (Python):

N, K = map(int, input().split())
A = list(map(int, input().split()))
kind = len(set(A))
cnt = [0] * 202020
for a in A:
    cnt[a] += 1
cnt = sorted(filter(lambda x: x > 0, cnt))
ans = 0
for c in cnt:
    if kind <= K:
        break
    ans += c
    kind -= 1
print(ans)

Solution (Python):
import sys
from collections import Counter
read=sys.stdin.read

n,k,*a=map(int,read().split())

coa=Counter(a)
da=sorted(list(iter(coa)), key=coa.get)
print(sum(coa[x] for x in da[:len(da)-k]))

Solution (Python):
import collections
n, k = map(int, raw_input().split())
cvs = collections.Counter(map(int , raw_input().split())).values()
cvs.sort(key = lambda x:- x)
print sum(cvs[k:] or [0])

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
x=[0 for i in range(n)]
y=[]
s=0
for i in range(n):
  x[a[i]-1]+=1
    
for i in range(n):
  if x[i]:
    y.append(x[i])
y.sort()
z=len(y)
i=0
while(z>k):
  s+=y[i]
  z-=1
  i+=1
print(s)

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
t = []
for i in r:
    if i:
        t.append(i)
t.sort()
print(sum(t[:len(t)-k]))

Solution (Python):
from collections import Counter
def solve():
  N, K = map(int, input().split())
  A = list(map(int, input().split()))
  C = Counter(A)
  ans = sum(sorted(C.values())[:len(C)-K])
  return ans
print(solve())

Solution (Python):

import numpy as np
import sys
from collections import deque
from collections import defaultdict
from collections import Counter
import heapq

import collections
import itertools
import bisect
from scipy.special import comb
import copy
sys.setrecursionlimit(10**6)


def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


def S():
    return sys.stdin.readline()[:-1]


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


N, K = zz()
A = zz()
counter = Counter(A)
num_count = counter.most_common()
if (len(num_count) <= K):
    print(0)
    exit()

values, counts = zip(*num_count[K:])
print(sum(counts))

Solution (Python):
import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K, *A = map(int, read().split())

    counter = Counter(A)
    M = len(counter.keys())
    value = sorted(counter.values())

    ans = 0
    for i in range(M - K):
        ans += value[i]

    print(ans)

    return


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict
n,k=map(int,input().split())
a=list(map(int,input().split()))
dd=defaultdict(lambda:0)
for aa in a: dd[aa]+=1

diff=len(dd)-k
if diff<=0:
  print(0)
  exit()
l=[x for x in dd.values()]
l.sort()
print(sum(l[:diff]))

Solution (Python):
import collections

N, K = map(int, input().split())
A = list(map(int, input().split()))
A = dict(collections.Counter(A))
A = sorted(A.items(), key=lambda x: x[1], reverse=True)

cnt = 0

for i in range(N):
    if len(A) > K:
        cnt += A[-1][1]
        A.pop()
    else:
        break

print(cnt)

Solution (Python):
N,K=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.sort()
sum=0
for i in range(N-K):
    sum+=num[i]

print(sum)

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

a = []
a.append(A[0])
c = []
c.append(1)
for i in range(N-1):
    if A[i] == A[i+1]:
        c[-1] += 1
    else:
        a.append(A[i+1])
        c.append(1)

ans = 0
if len(a) <= K:
    ans = 0
else:
    c.sort()
    for i in range(len(a)-K):
        ans += c[i]

print(ans)

Solution (Python):
from collections import Counter

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

num_counts = dict()
length = len(set(num_lists))
ans = 0

if length > k:
  d = Counter()
  d.update(num_lists)
  count_sorted = list(d.values())
  count_sorted.sort()
  for i in range(length - k):
    ans += count_sorted[i]

print(ans)

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

c = collections.Counter(a_lst)
res = 0
sorted_c = sorted(c.items(), key=lambda x:x[1])
if len(c) <= k:
  print(res)
  quit()
else:
  for k, v in sorted_c[:(len(c) - k)]:
    res += v
print(res)

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

dct = {}
for i in range(n):
    if a[i] in dct:
        dct[a[i]] += 1
    else:
        dct[a[i]] = 1
select_num = len(dct) - k
if select_num <= 0:
    print(0)
    exit()
sorted_dct = sorted(dct.items(), key=lambda x:x[1])
ans = 0
for i in range(select_num):
    ans += sorted_dct[i][1]
print(ans)

Solution (Python):
import collections
N,K = (int(T) for T in input().split())
A = collections.Counter([int(T) for T in input().split()]).most_common()
Num,Count = zip(*A)
if len(Num)<=K:
    print(0)
else:
    print(N-sum(Count[:K]))

Solution (Python):
from collections import Counter
N, K = map(int, input().split())
A = Counter(list(map(int, input().split()))).most_common()
B = sorted([a[1] for a in A])
print(sum(B[:max(0, len(B)-K)]))

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
n=[0]*200000
x=[]
for i in a:
    n[i-1]+=1
for i in n:
    if i!=0:
        x+=[i]
y=len(x)
z=y-k
if z<=0:
    print(0)
else:
    x.sort()
    print(sum(x[:z]))

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

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

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

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

Solution (Python):
import sys
from collections import defaultdict
from heapq import *
def input(): return sys.stdin.readline().strip()


def main():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    d = defaultdict(int)
    for a in A: d[a] += 1
    q = []
    for key in d: heappush(q, -d[key])
    for _ in range(k):
        if q: heappop(q)
    print(-sum(q))


if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter

n, k = map(int, input().split())
arr = list(map(int, input().split()))
c = Counter(arr)
arr = c.most_common()[k:]
print(sum([x[1] for x in arr]))

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

for a in A:
  B[a]+=1

print(sum(sorted(B)[:-K]))

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

c_value= list(counter.values())
nl = len(c_value)
if nl <= k:
  print(0)
  sys.exit()
  
c_value.sort()

do_num = nl - k
for i in range(do_num):
  cnt += c_value[i]
print(cnt)

Solution (Python):
from collections import Counter

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

cntA = Counter(A)
cntvalA = sorted(list(cntA.values()))
ans = sum(cntvalA[i] for i in range(len(cntvalA) - K))
print(ans)

Solution (Python):
n,k=map(int,input().split())
l=list(map(int,input().split()))
d=dict()
for a in l:
    if a in d:
        d[a]+=1
    else:
        d[a]=1
ans=0
for _,c in sorted(d.items(),key=lambda x:x[1])[:len(d)-k]:
    ans+=c
print(ans)

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

from collections import Counter
C = Counter(L).most_common()

ans = 0
for i, item in enumerate(C):
    if i < K:
        continue;
    ans += item[1]

print(ans, flush=True)

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

print(c)

Solution (Python):
import collections

N,K=map(int,input().split())
A=collections.Counter(map(int,input().split()))
A=sorted(A.items(),key=lambda x:x[1])
L=len(A)
ans=0

if K>=L:
  print(ans)
else:
  for a in A:
    ans+=a[1]
    L-=1
    if L==K:
      print(ans)

Solution (Python):
from collections import Counter
from collections import deque
n, k = map(int, input().split())
a = Counter(list(map(int, input().split())))
a = [tuple(i) for i in a.items()]
a.sort(key=lambda x: x[1])
a = deque(a)
ans = 0
while k<len(a):
    ans += a.popleft()[1]
print(ans)

Solution (Python):
import collections

n,k = map(int,input().split())
a = [int(i) for i in input().split()]
b = collections.Counter(a)
c = sorted(b.items(), key=lambda x:x[1])
ans = 0
d = len(c)
i = 0
while d > k:
    d -= 1
    ans += c[i][1]
    i +=1
print(ans)

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

a_dic = {}

a = list(map(int, input().split(" ")))
for i in a:
    if i in a_dic:
        a_dic[i] += 1
    else:
        a_dic[i] = 1

cn = 0
diff = len(a_dic) - k
score_sorted = sorted(a_dic.items(), key=lambda x:x[1], reverse=True)
if diff > 0:
    for i in range(diff):
        a = score_sorted.pop()
        cn += a[1]

print(cn)

Solution (Python):
from collections import Counter
N, K = map(int, input().split())
A = list(map(int, input().split()))
count_A = Counter(A)
sortA = [c for ai, c in count_A.most_common()]
print(sum(sortA[K:]))

Solution (Python):

n,K=map(int,input().split())
al=list(map(int,input().split()))


pt=set(al)
dic={}
for ai in al:
    dic[ai]=dic.get(ai,0)+1
l2d=[]
for k, v in dic.items():
    l2d.append([k,v])
l2d.sort(key=lambda x: x[1])

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

Solution (Python):
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
dic = {}
for i in range(N):
    data = A[i]
    if data in dic:  dic[data] += 1
    else:  dic[data] = 1
dic = sorted(dic.values())
ans = 0
l = len(dic)
for i in range(l - K):
    ans += dic[i]
print(ans)

Solution (Python):
from  collections import Counter

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

c = Counter(A)
values, counts = zip(*c.most_common())
print(n - sum(counts[:k]))

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

import collections

c = collections.Counter(A)

lis = list()
for i in c.most_common():
    lis.append(i[:][1])

lis.sort()

ans = 0
if len(c.most_common()) > K:
    ans += sum(lis[:(len(c.most_common())- K)])
print(ans)

Solution (Python):
import collections
def main():
    n,k=map(int, input().split())
    A=[int(i) for i in input().split()]
    dd = collections.defaultdict(int)
    for i in A:
        dd[i]+=1
    res = []

    for i in dd.keys():
        res.append([i,dd[i]])

    res.sort(key = lambda x:x[1])
    type = len(res)
    result = 0
    i=0
    while(type > k):
        result += res[i][1]
        type-=1
        i+=1

    print(result)


if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
*A, = map(int, input().split())
l = len(set(A))
if l <= K:
    print(0)
    exit()

dic = {}
for a in A:
    if a in dic:
        dic[a] += 1
    else:
        dic[a] = 1
from operator import itemgetter
dic_s = sorted(dic.items(), key=itemgetter(1))
i = 0
ans = 0
while l > K:
    l -= 1
    k, v = dic_s[i]
    ans += v
    i += 1
print(ans)

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

if len(list(set(l))) <= K:
    print(0)
else:
    result = 0
    
    ii =  len(list(set(l))) - K

    c = collections.Counter(l)
    l_r = c.most_common()
    for i in range(ii):
        c = i+1
        
        result += l_r[-1*c][1]
        
    print(result)

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

if(len(set(a)) <= k):
    print(0)
else:
    dic_a = {x:0 for x in set(a)}
    for i in a:
        dic_a[i] += 1
    sum = 0
    a = sorted(dic_a.values(),reverse=True)
    for i in range(k,len(a)):
        sum += a[i]
    print(sum)

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
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
cou=Counter(a).most_common()
rem=len(cou)-k
i=-1
ans=0
while rem>0:
    ans+=cou[i][1]
    i-=1
    rem-=1;
print(ans)

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

from collections import Counter
A_ = Counter(A).most_common()[::-1]

ans=0
for _ in A_[:len(A_)-K]:
    if len(A_)-K <= 0:
        break
    ans += _[1]
    
print(ans)

Solution (Python):
N, K = map(int, input().split())
List = [0]*N
for a in map(int, input().split()):
  List[a-1] += 1
List.sort()
print(sum(l for l in List[:len(List)-K]))

Solution (Python):
import collections

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

b = collections.Counter(a)
b = list(b.values())
b.sort()
ans = 0
for i in range(len(set(a))-k):
    ans += b[i]
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):
from collections import Counter
N,K = map(int, input().split())
A = Counter(input().split())
B = A.values()
k = len(B) - K
if k <= 0:
    print(0)
else:
    print(sum(sorted(B)[0:k]))

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

Solution (Python):
from collections import Counter
n, k = map(int, input().split())
A = list(map(int, input().split()))
c = Counter(A)
cnt = 0
ans = 0
m = len(c)-k
if m < 0:
    m = 0
for k, v in sorted(c.items(), key=lambda x: x[1]):
    if cnt == m:
        break
    ans += v
    cnt += 1

print(ans)

Solution (Python):
from collections import Counter


def main():
    n, k = map(int, input().split())
    As = map(int, input().split())
    ans = 0
    for num, count in Counter(As).most_common()[::-1][:-k]:
        ans += count
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter


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

val = sorted(A.values())
print(sum(val[:len(val) - K]))

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()

ans = N
for i in range(K):
  if i == len(d):
    break
  ans -= d[i][1]
print(ans)

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

count = Counter(a)
t = len(count)
ans = 0
for i in sorted(count.values()):
  if t <= k: break
  ans += i
  t -= 1
print(ans)

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

a_list=[int(i) for i in input().split()]

a_dic={}
for i in range(n):
    if a_list[i] not in a_dic:
        a_dic[a_list[i]]=1
    else:
        a_dic[a_list[i]]+=1
        


if len(a_dic)<=k:
    print(0)
else:
    
    a_value=list(a_dic.values())
    
    a_value.sort(reverse=True)
    
    print(sum(a_value[k:]))

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

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

l = []
for v in d.values():
    l.append(v)
l.sort()

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

Solution (Python):


import io
import sys
import math

def solve(n, k, a_lst):
    
    a_set = set(a_lst)
    ak = len(a_set)
    diff = ak - k
    if diff < 1:
        return 0
    
    
    a_lst.sort()
    tmp = a_lst[0]
    cnt = 0
    cnt_lst = []
    for i in range(len(a_lst)):
        if tmp != a_lst[i]:
            tmp = a_lst[i]
            cnt_lst.append(cnt)
            cnt = 0
        cnt += 1
    cnt_lst.append(cnt)
    cnt_lst.sort()
    return sum(cnt_lst[0:diff])

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

    
    ans = str( solve(n, k, a_lst) )
    
    
    print(ans)
    return ans
        

_DEB = 0   

_INPUT = """\
5 2
1 1 2 2 5
"""
_EXPECTED = """\
0
"""

def logd(str):
    """usage:
    if _DEB: logd(f"{str}")
    """
    if _DEB: print(f"[deb] {str}")


if __name__ == "__main__":
    if _DEB:
        sys.stdin = io.StringIO(_INPUT)
        print("!! Debug Mode !!")

    ans = main()

    if _DEB:
        print()
        if _EXPECTED.strip() == ans.strip(): print("!! Success !!")
        else: print(f"!! Failed... !!\nANSWER:   {ans}\nExpected: {_EXPECTED}")

Solution (Python):

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

b = [[a[0],1]]
cnt = 0

for i in a[1:] :
    if b[cnt][0] == i:
        b[cnt][1] += 1
    else:
        b.append([i, 1])
        cnt += 1

num = len(b)
b_sorted = sorted(b, key=lambda x: x[1])

ans = 0
for i, j in b_sorted[: num - k]:
    ans += j

print(ans)

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

a_list=[int(i) for i in input().split()]


dic={}
for i in a_list:
    if i not in dic:
        dic[i]=1
    else:
        dic[i]+=1






if len(dic)<=k:
    print(0)
else:
    ans=sorted(dic.items(),key=lambda x:x[1],reverse=True)
    b_list=ans[k:]
    ans_sum=0
    for i in b_list:
        ans_sum+=i[1]
    print(ans_sum)

Solution (Python):



import sys
from collections import Counter



def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solver(ball_count, limit_num, ball_write_nums):
    result = 0
    total_count = Counter(ball_write_nums)
    count_list = list(total_count.values())
    count_list.sort()
    change_num = len(count_list) - limit_num
    for j in range(0, change_num, +1):
        result += count_list[j]
    return result


if __name__ == "__main__":
    K, N = MI()
    SI = LI()
    print("{}".format(solver(K, N, SI)))

Solution (Python):
from collections import defaultdict

n,k=map(int,input().split())
l=list(map(int,input().split()))
a=defaultdict(int)
for i in l:
	a[i] += 1
a=list(a.items())
if len(a) <= k:
	print(0)
else:
	ans = 0
	a.sort(key = lambda x: x[1])
	for i in range(len(a) - k):
		ans += a[i][1]
	print(ans)

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

dic = {}
for i in li:
  if i in dic:
    dic[i] += 1
  else:
    dic[i] = 1

val = sorted(dic.values())

ans = 0
if k > len(val):
  print(ans)
else:
  a = len(val) - k
  ans = sum(val[:a])
  print(ans)

Solution (Python):

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

dic = {}
for a in range(len(A)):
    tmp = A[a]
    if not tmp in dic:
        dic[tmp] = 1
    else:
        dic[tmp] += 1

values = sorted(dic.values())
ans = 0
if len(values)<=K:
    pass
else:
    tmp = len(values)-K
    values = values[:tmp]
    ans = sum(values)

print(ans)

Solution (Python):
from collections import Counter

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

c = Counter(a)
c = dict(sorted(c.items(), key=lambda x:x[1], reverse=True))

ans = sum(list(c.values())[k:])
print(ans)

Solution (Python):

import collections
N, K = map(int, input().split())
A = list(map(int, input().split()))
C = collections.Counter(A)
C = sorted(C.values())

lenght = len(C)

print(sum(C[:(lenght-K)]))

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

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
from collections import defaultdict
dic = defaultdict(int)
for a in A:
    dic[a] += 1
sort_dic = sorted(dic.items(), key = lambda x:x[1])
n = len(sort_dic)
if n<=K: print(0);exit()
ans = 0
for i in range(n-K):
    ans += sort_dic[i][1]
print(ans)

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

a.sort()
ans = 0
a.append(-1)
x = []
tmp = 0
for i in range(n):
  if a[i] != a[i+1]:
    tmp += 1
    x.append(tmp)
    tmp = 0
  else:
    tmp += 1
    
x.sort(reverse=True)
print(sum(x[k:]))

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

count = [0]*(n+1)

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

cnt = sorted(count)
nlis = []
for i in range(n+1):
    if cnt[i] != 0:
        nlis.append(cnt[i])
if len(nlis) <= k:
    print(0)
    exit()

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

Solution (Python):
def ra(a): 
    ll,l=[],1
    for i in range(len(a)-1):
        if a[i]==a[i+1]:
            l+=1
        else:
            ll.append(l)
            l=1
    ll.append(l)
    return ll
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=sorted(ra(sorted(a)))
print(sum(b)-sum(b[-k:]))

Solution (Python):
from collections import defaultdict

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

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

av = list(sorted(d.values()))
ks = 0
idx = 0
print(sum(av[0:-k]))

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

from collections import Counter
ca = Counter(a)


c = sorted(ca.values())
print(sum(c[:-k]))

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
li = [0]*10**6
for i in range(N):
    li[A[i]] += 1

li.sort(reverse=True)

print(sum(li[K:]))

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

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] = 1
  else: d[i] += 1

ans = 0
t = len(d.keys())
if t <= k:
  print(0)
else:
  p = t-k
  for i in sorted(d.values()):
    if p == 0: break
    ans += i
    p -= 1
  print(ans)

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

Solution (Python):



import sys
from collections import Counter


def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solver(givenBall, selectKind, ball_num):
    result = 0
    summary_Ball = Counter(ball_num)
    
    sortedCount = sorted(list(summary_Ball.values()))
    
    ballAllKind = len(sortedCount)
    changeNumberKind = ballAllKind - selectKind
    if changeNumberKind <= 0:
        result = 0
    else:
        for j in range(0, changeNumberKind, +1):
            result += sortedCount[j]
    return result


if __name__ == "__main__":
    N, K = MI()
    Ai = LI()
    print("{}".format(solver(N, K, Ai)))

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
sa=set(a)
nums=len(sa)
if nums<=k:
    print(0)
else:
    num=[0]*n
    for i in range(n):
        num[a[i]-1]+=1
    num.sort()
    ans=0
    for i in range(n-k):
        ans+=num[i]
    print(ans)

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

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

ans = 0
for i in range(min(K, len(ca))):
    ans += ca[i][1]

print(N-ans)

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

import collections
c = collections.Counter(a)

v,cts = zip(*c.most_common())
x = [int(i) for i in cts]
print(sum(x[k:]))