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 sys

from collections import Counter


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

c = dict(Counter(map(int, input().split())))

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

l = len(s_c)
if l <= k:
    print(0)
else:
    ans = 0
    for i in range(l - k):
        ans += s_c[i][1]
    print(ans)

Solution (Python):
import collections as c
N,K,*A= map(int,open(0).read().split())
ans=0
C=c.Counter(A)
C=sorted(C.values())

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

print(ans)

Solution (Python):
from collections import Counter
n,k = map(int, input().split())
a = list(map(int,input().split()))
kinds = dict(Counter(a))
kinds = sorted(kinds.values(), reverse = True)
if len(kinds) > k:
  print(sum(kinds[k:]))
else:
  print("0")

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

from collections import deque

n, k = map(int, input().split())
a = list(map(int, input().split()))
count = [0] * (n + 1)
for v in a:
    count[v] += 1
count = deque(sorted(v for v in count if v != 0))
kind = len(count)
ans = 0
while kind > k:
    v = count.popleft()
    kind -= 1
    ans += v
print(ans)

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

Solution (Python):
from collections import Counter 

N, K = map(int, input().split())
A = list(map(int, input().split()))
s = len(set(A))
t = s-K

c = Counter(A)
num = list(c.values())
num.sort()

print(sum(num[:t])) if t>0 else print(0)

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

to_del = max(0, len(c) - k)
cm = c.most_common()[::-1]
ans = sum(cm[i][1] for i in range(to_del))
print(ans)

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

to_del = max(0, len(c) - k)
cm = c.most_common()[::-1][:to_del]
ans = sum(cm[i][1] for i in range(to_del))
print(ans)

Solution (Python):
from collections import Counter

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

cnt = Counter(A)

s = len(cnt.keys())

ans = 0
kosu = sorted(cnt.values())
ind = 0
while s > k:
    s -= 1
    ans += kosu[ind]
    ind += 1

print(ans)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
int_dic = {}
for ai in a:
    if ai in int_dic:
        int_dic[ai] += 1
    else:
        int_dic [ai] = 1
num_int = len(int_dic.keys())
if num_int <= k:
    print(0)
else:
    rpl = num_int - k
    value_ls = list(int_dic.values())
    value_ls.sort()
    print(sum(value_ls[:rpl]))

Solution (Python):
from collections import defaultdict
N, K = map(int,input().split())
A = list(map(int,input().split()))
D = defaultdict(int)
for a in A:
  D[a]+=1
D = sorted(D.values())
ans = 0
if len(D)<=K:
  print(ans)
else:
    print(sum(D[:len(D)-K]))

Solution (Python):
n,k=map(int,input().split())
A=list(map(int,input().split()))
import sys
mod=10**9+7 ; inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict 
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal as D  
  
from functools import lru_cache
from bisect import bisect_left as bileft, bisect_right as biright
from fractions import Fraction as F  






C=Counter(A)
Q=list(C.values())
Q=sorted(Q,reverse=1)
if len(Q)<k:
    print(0);exit()
print(n-sum(Q[:k]))

Solution (Python):
import collections
N, K = map(int, input().split())
arr = list(map(int,input().split()))
M = len(list(set(arr))) - K 
ans = 0


if K >= len(list(set(arr))):
    print(0)
    exit()


c = collections.Counter(arr)
cnt_arr = list(sorted(c.items(), key=lambda x:x[1]))
for i in range(M):
    ans += cnt_arr[i][1]
print(ans)

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

ca = collections.Counter(aa) 
arca = [a for a in ca.items()] 
sa = sorted(arca, key = lambda x:x[1]) 
lca = len(ca) 
cnt = 0
if lca > k:
  for i in range(lca - k):
    cnt += sa[i][1]
else:
  cnt = 0
print(cnt)

Solution (Python):
from collections import Counter

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

l=Counter(l)

if len(l) <= k:
  ans=0
else:
  sl=sorted(list(l.values()))
  num=len(l)-k
  ans=sum(sl[:num])
  
print(ans)

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

c = collections.Counter(A)
score_sorted = sorted(c.items(), key=lambda x:-x[1])

memo = 0
count = 0
for key,value in score_sorted:
    if count < K:
        count += 1
        memo += value
    else:
        break
print(N-memo)

Solution (Python):
from collections import Counter


N, K = map(int, input().split())
A = list(map(int, input().split()))
A_cntr = Counter(A)
print(sum(sorted(v for v in A_cntr.values())[:-K]))

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect














import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7


from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
  return int(readline())
n,K = readInts()
A = readInts()
kind = set()
for i in range(n):
    kind.add(A[i])
if len(kind) <= K:
    print(0)
else:
    C = Counter(A)
    lis = []
    for k,v in C.items():
        lis.append((k,v))
    lis = sorted(lis, key = lambda x:x[1])
    ans = 0
    for i in range(len(lis)-K):
        ans += lis[i][1]
    print(ans)

Solution (Python):
from collections import defaultdict

n, k, *a = map(int, open(0).read().split())
d = defaultdict(int)
for b in a:
    d[b] += 1

v = sorted(d.values())[::-1]

c = 0
for i in range(min(k, len(v))):
    c += v[i]

print(n - c)

Solution (Python):
from collections import Counter


def LI():
    return list(map(int, input().split()))


N, K = LI()
A = LI()
A = Counter(A)
A = sorted(A.items(), key=lambda x: x[1])
ans = 0

if len(A) > K:
    L = len(A)-K
    for i in range(L):
        ans += A[i][1]

print(ans)

Solution (Python):
import collections
N, K = map(int, input().split())
A = list(map(int, input().split()))
c = collections.Counter(A)
print(sum(sorted(c.values(),reverse=True)[K:]))

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
C = {}
for i in range(N):
    if A[i] not in C:
        C[A[i]] = 0
    C[A[i]] += 1
C = sorted(list(C.items()),key=lambda x:x[1],reverse=True)
if len(C)<=K:
    print(0)
else:
    cnt = 0
    for i in range(K,len(C)):
        cnt += C[i][1]
    print(cnt)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

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

from collections import Counter

c = Counter(a)
need = len(c) - k
ans = 0

for i in sorted(c.values()):
    if need <= 0:
        break
    ans += i
    need -= 1

print(ans)

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

Solution (Python):
from collections import defaultdict
n, k, *a = map(int, open(0).read().split())
d = defaultdict(int)

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

k = len(d) - k
ans = 0
for v in sorted(d.values()):
    if k <= 0:
        break
    ans += v
    k -= 1
print(ans)

Solution (Python):
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
from math import gcd,ceil,floor,factorial

from functools import reduce
from pprint import pprint
from statistics import mean,median,mode
import sys
sys.setrecursionlimit(10 ** 6)

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

def MI():
    return map(int,input().split())

def RI():
    return list(map(int,input().split()))

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input()) for _ in range(n) ]

ddict = defaultdict(lambda: 0)





n,k = MI()
a = RI()

for i in range(n):
    ddict[ a[i] ] += 1

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


e = d[k:]
c = 0
for i in range(len(e)):
    c += e[i][1]
print(c)

Solution (Python):
from collections import Counter
n,k = map(int, input().split())
a = Counter(list(map(int, input().split())))
ans = 0
a = sorted([i for i in a.values()], reverse=True)
if len(a) > k: ans = sum(a[k:])
print(ans)

Solution (Python):
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right 
from heapq import heapify, heappop, heappush
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)

N,K = mint()
A = lint()
d = dict()
for a in A:
    d.setdefault(a,0)
    d[a] += 1
l = []
for k in d:
    l.append(d[k])
l.sort(reverse=True)
print(N-sum(l[:K]))

Solution (Python):
from collections import Counter
N, K = map(int, input().split())
A = list(map(int, input().split()))
d = Counter(A)
B = list(d.values())
B.sort()
L = len(B)
print(sum(B[:max(L-K,0)]))

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

a = sorted(a)
shurui = 0

for i in a:
    if a_num[i] == 0:
        shurui += 1
    a_num[i] += 1

a_num = sorted(a_num)
ans = 0
i = 0

while shurui > k:
    if a_num[i] != 0:
        ans += a_num[i]
        shurui -= 1
    i += 1

print(ans)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
data=[0]*n
for i in a:
  data[i-1]+=1
data.sort()
m=n-k
if data.count(0)>=m:
  print(0)
else:
  ans=0
  res=data.count(0)
  while res<m:
    ans+=data[res]
    res+=1
  print(ans)

Solution (Python):
N,K = map(int,input().split())
As = list(map(int,input().split()))
nc=[0]*(200000+1)
for a in As:
    nc[a] += 1
setA = set(As)
kind = len(setA)
if kind - K <= 0:
    print(0)
else:
    nc.sort()
    c = 0
    for i in range(200000+1):
        if nc[i] == 0:
            continue
        c += nc[i]
        kind -= 1
        if kind == K:
            break
    print(c)

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

cc=collections.Counter(A)
tt=list(cc.values())
tt.sort()
print(sum(tt[:len(tt)-K]))

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

A = collections.Counter(A)
A = sorted(A.items(), key=lambda x:x[1])
cnt = A[:len(A)-K]

ans = []
for c in cnt:
    ans.append(c[1])
    
print(sum(ans))

Solution (Python):
import heapq
N,K = map(int,input().split())
Alist = list(map(int,input().split()))
Ball = dict()
for i in Alist:
    if i not in Ball:
        Ball[i] = 1
    else:
        Ball[i] += 1
Balllist = []
for key, value in Ball.items():
    Balllist.append(value)
heapq.heapify(Balllist)
size = len(Balllist)
Answer = 0
for _ in range(10**7):
    if size <= K:
        break
    Answer += heapq.heappop(Balllist)
    size -= 1
print(Answer)

Solution (Python):

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

cnt = collections.Counter(A)

diff = len(cnt.keys()) - K

values = sorted(list(cnt.values()),reverse = False)
ans = values[:diff]
print(sum(ans))

Solution (Python):
from collections import Counter

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

if len(C) <= K:
    print(0)
    exit()
ans = 0
for c in C[len(C) - 1:K - 1:-1]:
    ans += c[1]
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
ins = lambda: input().rstrip()
ini = lambda: int(input().rstrip())
inm = lambda: map(int, input().split())
inl = lambda: list(map(int, input().split()))
out = lambda x: print('\n'.join(map(str, x)))

n, k = inm()
a = inl()
if len(set(a)) <= 2:
    print(0)
else:
    b = {}
    for i in a:
        if i not in b:
            b[i] = 0
        b[i] += 1
    b = list(map(lambda x: x[1], sorted(b.items(), key=lambda x: x[1])))
    print(sum(b[:len(set(a))-k]))

Solution (Python):
import sys
input = sys.stdin.readline
ins = lambda: input().rstrip()
ini = lambda: int(input().rstrip())
inm = lambda: map(int, input().split())
inl = lambda: list(map(int, input().split()))
out = lambda x: print('\n'.join(map(str, x)))

n, k = inm()
a = inl()
if len(set(a)) <= 2:
    print(0)
else:
    b = {}
    for i in a:
        if i not in b:
            b[i] = 0
        b[i] += 1
    b = sorted(b.items(), key=lambda x: x[1])
    ans = 0
    for i in range(len(set(a))-k):
        ans += b[i][1]
    print(ans)

Solution (Python):
import collections

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

d = dict(collections.Counter(a))
d = sorted(d.values())

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

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n,k = LI()
    a = LI()
    dic = collections.Counter(a)
    dlst = sorted(dic.items(),key=lambda x: x[1])
    cnt = len(dic)-k
    ans = 0

    for i in range(cnt):
        ans += dlst[i][1]
    print(ans)
        
main()

Solution (Python):


from collections import Counter

n, k = [int(i) for i in input().split()]
A = Counter([int(i) for i in input().split()])

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

ans = 0
res = len(A) - k
if res < 1:
    print(0)
else:
    for i in range(res):
        _, cnt = A[i]
        ans += cnt
    print(ans)

Solution (Python):
import collections
a = input().split()
N = int(a[0])
b = int(a[1])
c = [int(s) for s in input().split()]
d = collections.Counter(c)
e = len(d)
f = []
for myvalue in d.values():
    f.append(myvalue)
g = sorted(f)
h = int(0)
if b >= int(e):
    print("0")
else:
    for i in range(int(e) - b):
        h = h + g[i]
    print(h)

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

b = len(set(a))
c = Counter(a)
c = list(sorted(c.values(), reverse = True))

if b <= k:
    print(0)
else:
    d = b - k
    print(sum(c[b-d:]))

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
x = len(set(A))
if x <= K:
    print(0)
    exit()
import collections
C = collections.Counter(A)
x -= K
result = 0
for c in C.most_common()[::-1]:
    if x == 0:
        print(result)
        exit()
    result += c[1]
    x -= 1

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

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

d = dict()
for a in A:
    d[a] = d.get(a, 0) + 1

v = sorted(list(d.values()))[::-1]

print(sum(v[k:]))

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

dif = len(l) - K
if dif <= 0:
    print(0)
    exit()
l = list(l.values())
heapq.heapify(l)

ans = 0
for i in range(dif):
    ans += heapq.heappop(l)
print(ans)

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

D=[0]*(2*10**5+1)

t=len(set(A))-K
if t<=0:
    print(0)
else:
    for a in A:
        D[a]+=1
    print(sum(sorted([d for d in D if 0<d])[:t]))

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
kosu = [0]*n
kaburi = []

for i in range(n):
    kosu[a[i]-1] += 1

for j in range(n):
    if kosu[j] != 0:
        kaburi.append(kosu[j])

kaburi.sort()
print(sum(kaburi[:len(kaburi)-k]))

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
d = {}
for i in range(N):
    if A[i] not in d:
        d[A[i]] = 1
    else:
        d[A[i]] += 1
if len(d) <= K:
    print(0)
else:
    d=sorted(d.items(), key=lambda x:x[1])
    print(sum([d[i][1] for i in range(len(d)-K)]))

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
p = [0 for _ in range(N)]
for i in range(N):
    p[A[i]-1] += 1
q = [p[i] for i in range(N) if p[i] != 0]
q.sort()
w = len(q)
print(sum(q[:w-K]))

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

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

ballcnts = list(balls.values())
ballcnts.sort()

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

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

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

Solution (Python):
from collections import Counter

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

ca = Counter(a)

print(sum(list(map(lambda x:x[1],ca.most_common()[k:]))))

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

dic = {}

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

li = [0] * (N + 1)



for a in dic:
    li[dic[a]] += 1

ans = 0
k = len(dic)

for i in range(N):
    if li[i] > 0:
        if k - li[i] >= K:
            ans += i * li[i]
            k -= li[i]
        else:
            ans += i * (k - K)
            k -= li[i]

    if k <= K:
        break

print(ans)

Solution (Python):
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    cnt = 0
    pre_n = a[0]
    cnt_kind = []
    for i in a:
        if pre_n == i:
            cnt += 1
            pre_n = i
        else:
            cnt_kind.append(cnt)
            cnt = 1
            pre_n = i

    cnt_kind.append(cnt)
    cnt_kind.sort()
    if len(cnt_kind) <= k:
        print(0)
    else:
        print(sum(cnt_kind[:len(cnt_kind)-k]))


if __name__ == '__main__':
    main()

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
B = list(D.values())
B.sort(reverse=True)
print(N-sum(B[:min(len(B), K)]))

Solution (Python):
import collections

N,K,*A = map(int, open(0).read().split())

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

print(ans)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
C = {}
for i in range(N):
    a = A[i]
    if a not in C:
        C[a] = 0
    C[a] += 1
C = sorted(list(C.items()),key=lambda x:x[1],reverse=True)
if len(C)<=K:
    print(0)
else:
    cnt = 0
    for i in range(K,len(C)):
        cnt += C[i][1]
    print(cnt)

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(i) for i in input().split()]
d = {}
for i in a:
    if i in d:
        d[i] += 1
    else:
        d[i] = 1
b = list(d.values())
b.sort(reverse=True)
print(n-sum(b[:min(len(b), k)]))

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

N, K = map(int, input().split())
A = list(map(int, input().split()))
cnt = Counter(A)
vs = list(cnt.values())
vs.sort()
print(sum(vs[:max(0, len(vs)-K)]))

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

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

if u == k:
    print(0)
else:
    vals = sorted(d.values(), reverse=True)
    print(sum(vals[k:]))

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

items = []
for key, value in c.items():
    items.append((key, value))
items.sort(key=lambda x: x[1])

ans = 0
for key, value in items[:max(0, len(items)-k)]:
    ans+=value
print(ans)

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

c = collections.Counter(a).most_common()[::-1]
p = len(c) - k
if p <= 0:
  print(0)
else:
  print(sum(list(map(lambda x:x[1], c[:p]))))

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

result = 0
for _, x in reversed(a.most_common()):
  if k >= all_balls:
    break
  result += x
  all_balls -= 1

print(result)

Solution (Python):
from collections import Counter

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

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

print(sum(B[K:]))

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

C = collections.Counter(A)

if len(C) <= K:
    print(0)
else:
    ret = 0
    for c in C.most_common()[K:]:
        ret += c[1]
    print(ret)

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

Solution (Python):









import os
import sys


from collections import Counter


def main():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    a_cnt = sorted(list(map(list, Counter(A).items())), reverse = True, key = lambda x: x[1])
    init_kind = len(set(A))
    ans = 0
    while len(a_cnt) > k:
        _, num = a_cnt.pop()
        ans += num
    print(ans)




if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
N,K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
ca = Counter(A)
count = [[k, ca[k]] for k in ca]
count.sort(key = lambda x: x[1])
tn = 0
for i in range(len(count) - K):
    tn += count[i][1]
print(tn)

Solution (Python):
from collections import Counter

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

c = Counter(A)

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

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

a1 = sorted(a)
a2 = []
count = 1
for i in range(n - 1):
    if a1[i] == a1[i + 1]:
        count += 1
    else:
        a2.append(count)
        count = 1
a2.append(count)
l = sorted(a2)
print(sum(l[:len(l) - k]))

Solution (Python):


import sys
read = sys.stdin.read
readline = sys.stdin.readline

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

from collections import Counter

d = Counter(a)
d = sorted(d.values())

L = len(d)
if L >= k:
    print(sum(d[:L-k]))
else:
    print(0)

Solution (Python):
import sys
from collections import Counter

N, K = map(int, sys.stdin.readline().split())
A = list(map(int, sys.stdin.readline().split()))

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

if len(counter) <= K:
    print(0)
else:
    ans = 0
    needs = len(counter) - K
    for key, count in counter:
        needs -= 1
        ans += count

        if needs == 0:
            break

    print(ans)

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

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

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

b=[]
for i in d.values():
    b.append(i)

b.sort()
l=len(b)
if l<=k:
    print(0)
else:
    print(sum(b[0:l-k]))

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

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


def main():
    n, k = map(int, readline().rstrip().split())
    a = list(map(int, readline().rstrip().split()))
    c = Counter(a)
    print(sum(sorted(c.values(), reverse=True)[k: len(c)]))


if __name__ == '__main__':
    main()

Solution (Python):

from collections import Counter

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

Solution (Python):
n, k = map(int,input().split())
lsa = list(map(int,input().split()))
ls = [0 for i in range(2 * (10 ** 5))]
for i in range(n):
    ls[lsa[i] - 1] += 1
ls2 = [i for i in ls if i != 0]
ls2.sort()

ans = 0

if len(ls2) > k:
    t = len(ls2) - k
    ls3 = [ls2[i] for i in range(t)]
    ans += sum(ls3)

print(ans)

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

Solution (Python):
import collections

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

balls = collections.Counter(A)

num = []
for x in balls.values():
    num.append(x)

num.sort()

d = len(num) - k

if d <= 0:
    print(0)
    exit()

ans = 0
for i in range(d):
    ans += num[i]

print(ans)

Solution (Python):





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

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

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


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

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

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

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

    return arr



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



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


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

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

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

        if x == y:
            return

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

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

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

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

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

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

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

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

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


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

    divisors.sort()
    return divisors


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

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