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):
from collections import Counter

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

C = Counter(A).most_common()
L = len(C)

if L <= K:
  print(0)
  exit()

ans = 0
cnt = 0
for k,v in C:
  cnt += 1
  if K < cnt:
    ans += v
    
print(ans)

Solution (Python):
n, k = map(int, input().split())
L = list(map(int, input().split()))
d = {}
for a in L:
    if a not in d:
        d[a] = 1
    else:
        d[a] += 1
l = []
for v in d.values():
    l.append(v)
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()))

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

print(sum(li[:max(len(li) - K, 0)]))

Solution (Python):
import collections,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N,K = LI()
A = collections.Counter(LI())
value_A = sorted(A.values())[::-1]
print(sum(value_A[K:]))

Solution (Python):
import collections,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N,K = LI()
A = collections.Counter(LI())
value_A = sorted(A.values())
surplus = max(0,len(A)-K)
print(sum(value_A[:surplus]))

Solution (Python):
import collections,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N,K = LI()
A = collections.Counter(LI())
count_A = sorted([v for v in A.values()])
surplus = max(0,len(A)-K)
print(sum(count_A[:surplus]))

Solution (Python):
import collections,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N,K = LI()
A = collections.Counter(LI())
count_A = sorted([v for v in A.values()])
remainder = max(0,len(A)-K)
print(sum(count_A[:remainder]))

Solution (Python):
import collections,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N,K = LI()
A = collections.Counter(LI())
count_A = sorted([v for _,v in A.items()])
remainder = max(0,len(A)-K)
print(sum(count_A[:remainder]))

Solution (Python):
from collections import Counter

n, m = map(int, input().split())
dic = Counter(map(int, input().split()))
lis = sorted(dic.items(), key=lambda x: x[1], reverse=True)

res = 0
for i in range(min(m, len(lis))):
    res += lis[i][1]
print(n - res)

Solution (Python):
from collections import Counter

n, m = map(int, input().split())
dic = Counter(map(int, input().split()))
lis = sorted(dic.items(), key=lambda x: x[1], reverse=True)

res = 0
for i, x in enumerate(lis):
    if i < m:
        continue
    else:
       res += x[1]
print(res)

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

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

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

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

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

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

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

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

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

for i in a:
    if i in ball.keys():
        ball[i] += 1
    else:
        ball[i] = 1

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

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

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

ball = [0] * n
for i in a:
    ball[i - 1] += 1
ball.sort(reverse=True)

ans = n
for i in range(k):
    ans -= ball[i]
print(ans)

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

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

for i in a:
    ball[i - 1] += 1
ball.sort(reverse=True)

ans = n
for i in range(k):
    ans -= ball[i]
print(ans)

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

for i in a:
    ball[i - 1] += 1
ball.sort(reverse=True)

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

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
import collections
c = collections.Counter(a)
L = set(a)
b = list(L)
d = [0]*len(b)
for i in range(len(b)):
    d[i] = c[b[i]]
d = sorted(d)
if len(b) <= k:
    print(0)
else:
    print(sum(d[:len(b)-k]))

Solution (Python):
import collections

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

c = collections.Counter(a)
num_list = list(c.values())
key_list = list(c.keys())
num_list.sort(key=lambda x: x)
m = max(len(key_list)-k, 0)
ans = 0
for i in range(m):
    ans += num_list[i]
print(ans)

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

dictCount = {}

for x in A:
    dictCount.setdefault(x, 0)
    dictCount[x] += 1
sorted_count = sorted(dictCount.items(), key=lambda x: x[1])
dict_len = len(sorted_count)
change_extract = sorted_count[:dict_len - K]
extract_sum = 0
for y in change_extract:
    extract_sum += y[1]
print(extract_sum)

Solution (Python):
import sys, heapq

N, K = map(int, input().split())
A = list(map(int, sys.stdin.readline().rsplit()))
setA = set(A)
if len(setA) <= K:
    print(0)
    exit()

change = len(setA) - K

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

q = list(dic.values())
heapq.heapify(q)
res = 0
for _ in range(change):
    res += heapq.heappop(q)
print(res)

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

value = list(a.values()) 
value.sort()
n = len(value)  

if n <= K:
  print(0)
  exit()

print(sum(value[:n-K]))

Solution (Python):
A,B= list(map(int,input().split()))
l_n=list(map(int,input().split()))
import collections
l_n=list(collections.Counter(l_n).values())
l_n.sort()
ans=0
for i in range(len(l_n)-B):
   ans+=l_n[i]
print(ans)

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

from collections import defaultdict
a_dict = defaultdict(int)

for a in a_l:
    a_dict[a] += 1
a_count = list(a_dict.values())
a_count.sort()
variety = len(a_count)

ans = 0
i = 0
while variety > k:
    variety -= 1
    ans += a_count[i]
    i += 1
print(ans)

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

Solution (Python):
from collections import defaultdict

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

res = defaultdict(lambda: 0)
for i in range(N):
    res[A[i]] += 1
    
cnt = len(res) - K
if cnt <= 0:
    print(0)
else:
    res_2 = sorted(res.items(), key=lambda x:x[1])
    ans = 0
    for i in range(cnt):
        ans += res_2[i][1]
    print(ans)

Solution (Python):
from collections import Counter

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

a.sort()
cnt = dict(Counter(a))
cnt = sorted(cnt.items(), key = lambda x : x[1])

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

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

print(ans)

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

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

l=sorted(table, reverse=True)
get=0
for i in range(k):
    get+=l[i]

print(sum(l)-get)

Solution (Python):


n, k = map(int, input().split())
s = map(int, input().split())
d = {}

for i in s:
    d.setdefault(i, 0)
    d[i] = d[i] + 1

if k >= len(d):
    print(0)
else:
    c = len(d) - k
    t = list(d.values())
    t.sort()
    print(sum(t[0:c]))

Solution (Python):
import collections
a,b = map(int,input().split(" "))
ar = list(map(int,input().split(" ")))
br = set(ar)
cr = collections.Counter(ar)
n = len(br) - b
dr = []
for v in cr.values():
    dr.append(v)
dr.sort()
count = 0
for i in range(n):
    count += dr[i]
print(count)

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

a_count = sorted(Counter(a_input).items(),key=lambda x:x[1])
print(sum([a[1] for a in a_count[:len(a_count)-k]]))

Solution (Python):
n,k=map(int,input().split())
from collections import Counter as co
var=sorted(co(list(map(int,input().split()))).values(),reverse=1)
print(sum(var[k:]))

Solution (Python):
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions

import copy





sys.setrecursionlimit(10000001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))




def main():
    n, k = ns()
    a = na()

    c = collections.Counter(a)

    result = sorted(c.values())

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


if __name__ == '__main__':
    main()

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

Solution (Python):
from collections import Counter

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

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

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

Solution (Python):
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


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


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


def ZIP(n): return zip(*(MAP() for _ in range(n)))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N, K = MAP()
    A = LIST()
    dic = defaultdict(int)
    for x in A:
        dic[x] += 1

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

    ans = 0
    if len(dic.keys()) > K:
        M = len(dic.keys()) - K
        for i in range(M):
            ans += dic_[i][1]
        print(ans)
    else:
        print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict

n, k = map(int, input().split())
a = list(map(int, input().split()))
h = defaultdict(int)
for x in a:
    h[x] += 1
ans = 0
i = 0
for key, v in sorted(h.items(), key=lambda x: -x[1]):
    i += 1
    if i > k:
        ans += v
print(ans)

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

Solution (Python):
from collections import defaultdict

import sys
sys.setrecursionlimit(10**6)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    n,k=mis()
    a=lmis()
    memo=defaultdict(int)
    for val in a: memo[val]+=1
    a_cnt=sorted(memo,key=memo.get)
    ans=0
    if k<len(memo):
        for i in range(len(memo)-k):
            ans+=memo[a_cnt[i]]
    print(ans)

if __name__ == "__main__":
    main()

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

Solution (Python):
from collections import Counter

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

print(sum(x[1] for x in c.most_common()[k:]))

Solution (Python):
from collections import Counter

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

print(sum(x[1] for x in c.most_common()[:k-1:-1]))

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

d = {}

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

val_sorted = sorted(d.values())

ans = 0

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

print(ans)

Solution (Python):
def resolve():
    '''
    code here
    '''
    import collections
    N, K = [int(item) for item in input().split()]
    A_list = [int(item) for item in input().split()]

    cnt_dict = collections.Counter(A_list)

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

    delta = len(cnt_dict) - K

    if delta > 0:
        res = 0
        cnt = 0
        for i, v in temp_dict:
            if cnt < delta:
                res += v
                cnt += 1
            else:
                break
    else:
        res = 0

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

Solution (Python):
import sys
from collections import Counter

input = sys.stdin.readline


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

    c = Counter(A)
    n = len(c.keys())
    ans = 0
    if not n <= K:
        for _, v in c.most_common()[::-1]:
            n -= 1
            ans += v
            if n <= K:
                break
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
di = {}
for i in a:
    if i in di:
        di[i] += 1
    else:
        di[i] = 1
di = list(di.values())
di.sort()
ans = 0
for i in range(max(len(set(a))-k, 0)):
    ans += di[i]
print(ans)

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

Solution (Python):
import sys
from collections import Counter
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    count = Counter(A)
    L = len(count)
    if L<=K:
        print(0)
    else:
        B = list(count.values())
        B.sort()
        print(sum(B[:L-K]))



if __name__ == '__main__':
    main()

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

print(sum(lis2[:N-K:]))

Solution (Python):
def main():
    n, k = map(int, input().split())
    count = {}
    a = [int(x) for x in input().split()]
    for aa in a:
        if aa in count:
            count[aa] += 1
        else:
            count[aa] = 1
    count = sorted(count.items(), key=lambda x: x[1], reverse=True)
    print(sum(count[i][1] for i in range(k, len(count))))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

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

c = Counter(A).most_common()
l = [i for i in c[K:]]
ans = 0
for x,y in l:
    ans += y
print(ans)

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

import collections
c = collections.Counter(A)

if len(c.keys()) > k:
    num = len(c.keys()) - k
    print(sum(sorted(c.values())[:num]))
else:
    print(0)

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

if len(list(set(a))) <= k:
    print(0)
else:
    ans = 0
    cnt = [0] *(max(a) + 1)

    for i in range(n):
        cnt[a[i]] += 1
    cnt.sort(reverse=True)
    tmp = 0
    for c in cnt:
        ans += c
        tmp += 1
        if k == tmp:
            ans = n - ans
            print(ans)
            exit()

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

K = len(list(set(a)))

if K <= k:
    print(0)
else:
    rewrite = K - k
    ans = 0
    cnt = [0] *(max(a) + 1)

    for i in range(n):
        cnt[a[i]] += 1
    cnt.sort()
    
    for c in cnt:
        if 0 < c:
            ans += c
            rewrite -= 1
            if rewrite == 0:
                print(ans)
                exit()

Solution (Python):



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


def main():
    d = {}
    for a in A:
        if a in d:
            d[a] += 1
        else:
            d[a] = 1
    
    l = sorted(d.values())
    k = max(0, len(l) - K)
    ans = 0
    for x in l[:k]:
        ans += x
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter

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

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

def it():
    for k, v in sorted(c.items(), key=lambda kv: kv[1]):
        if len(c) <= K:
            break
        else:
            yield v
            del c[k]

print(sum(it()))

Solution (Python):
from collections import Counter

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

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

print(sum(A[:len(A) - K]))

Solution (Python):
from collections import defaultdict

N, K = map(int, input().split())
A = list(map(int, input().split()))
dict = defaultdict(int)
ans = 0
for i in A:
    dict[i] += 1
dict = list(dict.items())
dict = sorted(dict, key=lambda x: x[1])
for i in range(len(set(A)) - K):
    ans += dict[i][1]
print(ans)

Solution (Python):
n,k,*a = map(int,open(0).read().split())
b = {}
for i in a:
  if i in b:
    b[i] += 1
  else:
    b[i] = 1
ans = 0
a = sorted(list(b.values()))
for i in range(max(0,len(a)-k)):
  ans += a[i]
print(ans)

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

dic={}

for i in a:
  if i in dic.keys():
    dic[i]+=1
  else:
    dic[i]=1
ans=0
nums=list(dic.values())
nums.sort()
ans = 0
for i in range(max(0,len(nums)-k)):
  ans += nums[i]
print(ans)

Solution (Python):
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
dic_A = {}
for i in a_list:
    if i in dic_A:
        dic_A[i] += 1
    else:
        dic_A[i] = 1
ans = 0
cnt = len(dic_A)
for k, v in sorted(dic_A.items(), key=lambda x: x[1]):
    if cnt <= K:
        break
    ans += v
    cnt -= 1
print(ans)

Solution (Python):

def myAnswer(N:int,K:int,A:list) -> int:
   kinds = 0
   dic = {}
   for a in A:
      if(a in dic.keys()):
         dic[a]+=1
      else:
         dic[a]=1
         kinds +=1
   if(kinds <= K):return 0
   sub = kinds - K
   ans = 0
   dic2 = sorted(dic.items(), key=lambda x:x[1])
   for key,value in dic2:
      if(sub == 0):
         break
      else:
         ans += value
         sub -= 1
   return ans

def modelAnswer():
   return
def main():
   N,K = map(int,input().split())
   A = list(map(int,input().split()))
   print(myAnswer(N,K,A[:]))
if __name__ == '__main__':
   main()

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

Solution (Python):
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
key = dict()
for i in range(n):
  if a[i] in key:
    key[a[i]] += 1
  else:
    key[a[i]] = 1
count = []
c = 0
for value in key.values():
  count.append(value)
  c += 1
count.sort()
ans = 0
for i in range(max([0, c-k])):
  ans += count[i]
print(ans)

Solution (Python):


from collections import deque,Counter













N,K = map(int,input().split())
arrA = list(map(int,input().split()))
cnt = Counter(arrA)
shurui_num = len(cnt.keys())
cnts_lis = list(cnt.values())
cnts_lis.sort()
if shurui_num<=K:
    print(0)
else:
    lis = cnts_lis[::-1]
    ans = sum(lis[K:])
    print(ans)

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

import collections

n,k = iim()
c = collections.Counter(iil())

l = sorted(c.values(),reverse=True)

print(sum(l[k:]))

Solution (Python):

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

nums = [int(i) for i in input().split()]
c = Counter(nums)
kinds = len(c)
if kinds <= k:
    ans = 0
else:
    ans = 0
    common = c.most_common()[::-1]
    for i in range(kinds - k):
        ans += common[i][1]
print(ans)

Solution (Python):
def mycounter(d,i):
    if i in d:
        d[i]+=1
    else:
        d[i]=1
    return d


n,k=map(int,input().split())
d=dict()
for i in map(int,input().split()):
    d=mycounter(d,i)
p=0
f=len(d)


for i in sorted(d.values()):
    if f>k:
        p+=i
        f-=1
    else:
        print(p)
        break

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

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

    ctr = Counter(a)

    ans = 0
    rest = max(0, len(ctr.keys()) - K)
    for cnt in sorted(ctr.values()):
        if rest == 0: break
        ans += cnt
        rest -= 1
    print(ans)


if __name__ == '__main__':
    main()

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

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

from collections import defaultdict
dic = defaultdict(int)

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


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

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,k = inpl()
a = inpl()
c = Counter(a)
res = []
for key in list(c):
    res.append(c[key])
res.sort()
ans = 0
ln = len(res)
if ln > k:
    ans = sum(res[:ln-k])
print(ans)

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

Solution (Python):
from collections import Counter
n,k=map(int,input().split())
L=list(map(int,input().split()))
c=Counter(L)
c1=(sorted(list(c.values()))[::-1])
print(sum(c1[k::]))

Solution (Python):


from collections import Counter


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


a_sets = set(a_nums)
a_len = len(a_sets)


if a_len-K<=0:
    print(0)
else:
    
    a_counter = Counter(a_nums)
    a_counter = a_counter.most_common()[::-1]

    
    ans = 0
    for i in range(a_len - K):
        ans += a_counter[i][1]
    print(ans)

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

import collections





c = collections.Counter(li)



v = list(c.values())

v.sort()


lenv = len(v)

if lenv <= k:
    print(0)
else:
    count = 0
    all_sum = 0
    for vv in v:
        count += 1
        all_sum += vv
        if lenv - count <= k:
            print(all_sum)
            break

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

A = sorted(dict(A).items(), key=lambda x:x[1])
ans = 0
if not len(A) <= K:
  n = len(A) - K
  ans = 0
  for i in range(n):
    ans += A[i][1]
print(ans)

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

from collections import defaultdict

cnt = defaultdict(int)
for a in A:
    cnt[a] += 1
cnt = [c for c in cnt.values()]
cnt.sort()

print(sum(cnt[:-K]))

Solution (Python):
n,K = map(int,input().split())
a = list(map(int,input().split()))
cnt = {}
for x in a:
    if x not in cnt:cnt[x] = 1
    else:cnt[x] +=1
res = []
for k in cnt:
    v = cnt[k]
    res.append(v)
res.sort()
ans = 0
for i in range(len(res)):
    if len(res) - i <=K:
        
        print(ans)
        break
    ans +=res[i]

Solution (Python):
N,K=map(int, input().split())
A=list(map(int, input().split()))
L=[0]*200000
for a in A:
  L[a-1]+=1
L=sorted(L)
ans=0
count=len(set(A))
for l in L:
  if count<=K:
    break
  if l!=0:
    ans+=l
    count-=1
    
print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
A = LIST()

dic = defaultdict(int)

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

d_k = len(dic)
to_decri = d_k - K 

d = sorted(dic.items(), key=lambda pair: pair[1], reverse=False)

ans = 0

for i in range(to_decri):
    ans += d[i][1]
print(ans)

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

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

Solution (Python):
from heapq import heappush, heappop
from itertools import permutations, accumulate, combinations
import math
import bisect
import numpy as np
from collections import defaultdict, deque, Counter
from operator import itemgetter
import sys
input = sys.stdin.readline





def main():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    c = Counter(A)
    l = [i for i in c.values()]
    l.sort()
    total = len(l)
    i = total - k
    ans = sum(l[:i])
    print(max(ans, 0))


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    from collections import defaultdict
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    counter = defaultdict(int)
    for a in A:
        counter[a] += 1
    counter = sorted(counter.items(), key=lambda x: x[1])
    ans = 0
    for i in range(len(counter)-K):
        ans += counter[i][1]
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def resolve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = {}
    for i in range(n):
        if a[i] in b:
            b[a[i]] += 1
        else:
            b[a[i]] = 1
    c = sorted(b.values())
    if len(c) <= k:
        print(0)
    else:
        print(sum(c[:(len(c)-k)]))
resolve()

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)
n = len(C)
if n<=K:
    cnt = 0
else:
    cnt = 0
    for i in range(K,n):
        cnt += C[i][1]
print(cnt)

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

kinds = len(d)
d = sorted(d.items(), key=lambda x: x[1])
ans = 0

for i in range(kinds - k):
    ans += d[i][1]

print(ans)

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

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




D = {}
for a in A:
    try:
        D[a] += 1
    except:
        D[a] = 1
D = sorted(D.values())
print(sum(D[:len(D)-k]))

Solution (Python):
from collections import Counter
n,k  = map(int, input().split())
a = list(map(int,input().split()))
b = sorted(Counter(a).values())
ans = 0
for i in range(max((len(set(a)) -k,0))):
    ans += b[i]
print(ans)

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

import collections

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




if len(C) <= K:
    print(0)
else:
    print(N - sum([C[i][1] for i in range(K)]))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from collections import Counter

n, k, *a = map(int, read().split())
counter = sorted(Counter(a).values())
ans = 0
for i in range(len(counter) - k):
    ans += counter[i]
print(ans)

Solution (Python):
from collections import Counter

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

c = Counter(a)

ans = 0
balls = []
for i in c.items():
    balls.append(i)

balls.sort(key= lambda x:x[1], reverse=True)
cnt = 0
for i in range(len(balls)):
    cnt += 1
    if cnt <= k:
        continue
    else:
        ans += balls[i][1]

print(ans)

Solution (Python):
def main():
    N, K = (int(i) for i in input().split())
    A = [int(i) for i in input().split()]
    from collections import Counter
    c = Counter(A)
    v = sorted(c.values())
    ans = 0
    if len(c.keys()) <= K:
        pass
    else:
        ans += sum(v[:len(v) - K])
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

N, K = map(int, input().split())
A = list(map(int, input().split()))
c = Counter(A).most_common()[::-1]

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

print(ans)