Problem:
 Score : 300 points    Problem Statement Takahashi has N balls. Initially, an integer A_i is written on the i -th ball.  He would like to rewrite the integer on some balls so that there are at most K different integers written on the N balls.  Find the minimum number of balls that Takahashi needs to rewrite the integers on them.      Constraints  1 \leq K \leq N \leq 200000  1 \leq A_i \leq N  All input values are integers.         Input Input is given from Standard Input in the following format:  N  K  A_1  A_2 ... A_N       Output Print the minimum number of balls that Takahashi needs to rewrite the integers on them.        Sample Input 1 5 2
1 1 2 2 5      Sample Output 1 1  For example, if we rewrite the integer on the fifth ball to 2 , there are two different integers written on the balls: 1 and 2 .
On the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print 1 .       Sample Input 2 4 4
1 1 2 2      Sample Output 2 0  Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.       Sample Input 3 10 3
5 1 3 2 4 1 1 2 3 4      Sample Output 3 3

Solution (Python):
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
d={}
for i in a:
    if i in d:
        d[i]+=1
    else:
        d[i]=1
t=[]
for i in d:
    t.append(d[i])
t.sort()
print(sum(t[0:-k]))

Solution (Python):



def main():
    n, k = map(int, input().split())
    a_list = list(input().split())
    a_set = set(a_list)

    
    dic = {}
    for i in a_list:
        dic.setdefault(i, 0)
        dic[i] += 1

    freq = sorted(dic.values())

    cnt = 0
    for i in range(len(a_set) - k):
        cnt += freq[i]
        

    print(cnt)

if __name__ == '__main__':
    main()

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

b = [0]*200000
c = []
ans = 0

for x in a:
    b[x-1] += 1

for x in b:
    if x > 0:
        c.append(x)

c.sort()

y = len(c) - k

if y > 0:
    ans = sum(c[:y])

print(ans)

Solution (Python):
from collections import Counter
N, K = map(int,input().split())
A = list(map(int,input().split()))
CA = Counter(A)
values, counts = zip(*CA.most_common())
counts = counts[::-1]
LA = len(CA)
L = max(LA - K,0)
ans = 0
for i in range(L):
    ans += counts[i]
print(ans)

Solution (Python):
import collections

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

Solution (Python):


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

B = {}
for a in A:
    if a not in B:
        B[a] = 1
    else:
        B[a] += 1

C = sorted(B.items(), key=lambda x: x[1])
if len(C) <= k:
    print(0)
else:
    ans = 0
    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()))

a = sorted(a)
p = list(set(a))

if len(p) <= k:
    print('0')
    exit()

j = 0
ans = [0 for _ in range(len(p))]
ans[0] = 1
for i in range(1, n):
    if a[i] == a[i - 1]:
        ans[j] += 1
    else:
        j += 1
        ans[j] += 1

ans = sorted(ans)

print(sum(ans[:len(p) - k]))

Solution (Python):
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
A.sort()
bucket=[0*i for i in range(N+1)]
count=1
types=1
for i in range(1,N):
    if A[i]==A[i-1]:
        if i!=N-1:
            count+=1
        else:
            count+=1
            bucket[count]+=1
            break
    else:
        types+=1
        if i!=N-1:
            bucket[count]+=1
            count=1
        else:
            bucket[count]+=1
            bucket[1]+=1
            break

i=1
number_of_changes=0
while True:
    if types<=K:
        print(0)
        quit()
    elif types-bucket[i]<=K:
        print((types-K)*i+number_of_changes)
        quit()
    else:
        types-=bucket[i]
        number_of_changes+=bucket[i]*i
    i+=1

Solution (Python):
n,m=map(int,input().split())
t={}
s=list(map(int,input().split()))
for i in s:
    t[i]=t.get(i,0)+1
ss=sorted(t.values())
print(sum(ss[:len(ss)-m]))

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

ret = {}

for i in a:
    ret[i] = ret.get(i, 0) + 1

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

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

ans = N
for k,v in ctr.most_common(K):
    ans -= v
print(ans)

Solution (Python):
from collections import Counter

N,K = list(map(int,input().split(" ")))
A = list(map(int,input().split(" ")))
A2 = dict(Counter(A))
ans=0
i = 0
r = sorted(A2.items(),key=lambda x:-x[1])
for k,v in r:
    i+=1
    ans+=v
    if i == K:
        break
print(N-ans)

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

col = collections.Counter(A)
most = col.most_common()

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

print(ans)

Solution (Python):
import collections

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

myset = set(As)
if len(myset) <= K:
    print(0)
else:
    cnt = collections.Counter()
    for d in As:
        cnt[d] += 1
    cnt_val = sorted(cnt.values())
    print(sum(cnt_val[:(len(myset)-K)]))

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

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

A.sort()

A=A+[0]
B=[]
count=1


for i in range(N):
    if A[i+1]==A[i]:
        count+=1
    else:
        ans=[A[i], count]
        B.append(ans)
        count=1

B.sort(key=lambda x: x[1])
B.reverse()
B=list(B)
B_len=len(B)

sum_count=0
ans=0

for i in range(K,B_len):
    ans+=B[i][1]

print(ans)

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

B = [0] * (N + 1)
for i in range(N):
    B[A[i]] += 1
B = [x for x in sorted(B, reverse=True) if x > 0]

ans = 0
while len(B) - K > 0:
    ans += B.pop()
print(ans)

Solution (Python):
from collections import Counter
n, k = map(int, input().split())
a = Counter(list(map(int, input().split())))
print(sum(c for (v, c) in a.most_common()[k:]))

Solution (Python):
from collections import Counter
n, k = [int(i) for i in input().split()]
a = Counter([int(i) for i in input().split()])
print(sum(c for (v, c) in a.most_common()[k:]))

Solution (Python):
from collections import Counter

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

cost = 0
print(sum(c for (v, c) in a.most_common()[k:]))

Solution (Python):
from collections import Counter

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

count_list = []
if len(set_a) <= k:
    print(0)
else:
    a = Counter(a)
    cost = 0
    for (v, c) in a.most_common()[k:]:
        cost += c
    print(cost)

Solution (Python):
from collections import Counter

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

Solution (Python):
from collections import Counter

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

c = Counter(xs)
print(sum(val for key, val in c.most_common()[k:]))

Solution (Python):


from collections import Counter 

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


num_dict = Counter(num_list)

num_tpl = num_dict.most_common()


if len(num_tpl) <= K:
    print(0)
    exit()
else:
    sum1 = 0
    
    for i in range(K):
        sum1 += num_tpl[i][1]
    
    print(N - sum1)

Solution (Python):


from collections import Counter 

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


num_dict = Counter(num_list)

num_tpl = num_dict.most_common()[::-1]

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

Solution (Python):
import Queue
n,k = map(int,raw_input().split())
arr = map(int,raw_input().split())

counts = {}
for i in arr:
	if i not in counts:
		counts[i] = 1
	else :
		counts[i] += 1

queue = Queue.PriorityQueue()

for i in counts.values():
	queue.put(i)

ans = 0
while queue.qsize() > k :
	ans += queue.get()

print ans

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

Solution (Python):
from collections import Counter
N,K = map(int, input().split())
a = [int(i) for i in input().split()]
cnt = Counter(a)
mc = cnt.most_common()
j = 0
ans = 0
while j < len(mc)-K:
  obj = mc[len(mc)-1-j]
  ans += obj[1]
  j += 1
print(ans)

Solution (Python):
from collections import Counter

N, K = map(int, input().split())
cnt = Counter(list(map(int, input().split())))
res = 0
l = len(cnt)
mc = cnt.most_common()
for i in range(l - 1, K - 1, -1):
    res += mc[i][1]
print(res)

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

Solution (Python):
N,K=map(int,input().split())
a = list(map(int,input().split()))
dictionary = {}
for number in a:
    if number not in dictionary:
        dictionary[number] = 1
    else:
        dictionary[number] += 1
sortedDictionary = sorted(dictionary.items(), key=lambda x: x[1])

kind = len(sortedDictionary)
ans = 0
index = 0
while kind > K:
    ans += sortedDictionary[index][1]
    kind -= 1
    index +=1
print(ans)

Solution (Python):
from collections import Counter
d = Counter() 
N, K = map(int, input().split())
A = list(map(int, input().split()))
L = list(set(A))
res = 0
num = []
d.update(A)
if len(L) <= K:
    res = 0

else:
    for i in L:
        num.append(d[i])
    
    num.sort()

    for i in range(len(L) - K):
        res += num[i]

print(res)

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

Solution (Python):
from collections import defaultdict
N,K=map(int,input().split())
A=list(map(int,input().split()))
ct=defaultdict(int)
for a in A:
    ct[a]+=1
svalues=sorted(ct.items(),key=lambda x:x[1])
num=len(ct)
count=0
for k,v in svalues:
    if num<=K:
        break
    else:
        count+=v
        num-=1
print(count)

Solution (Python):


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

dict_A = {}
for a in A:
    if a not in dict_A:
        dict_A[a] = 1
    else:
        dict_A[a] += 1

if len(dict_A.keys()) <= k:
    print(0)
    exit()

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

ans = 0
for i in range(len(dict_A.keys())-k):
    ans += sorted_A[i][1]

print(ans)

Solution (Python):
N,K=map(int,input().split())
B=[0]*N
for i in map(int,input().split()):B[~-i]+=1
print(sum(sorted(B)[:-K]))

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

c = Counter()

for a in A:
    c[a] += 1
values = []
for v in dict(c).values():
    values.append(v)
values.sort()
print(sum(values[:len(values) - K]))

Solution (Python):
from collections import Counter

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

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

for i in range(len(d)-K):
    altCounter += d[i][1]

print(altCounter)

Solution (Python):
backet = {}

first_input = input().split(" ")
N = int(first_input[0])
K = int(first_input[1])

data_input = input().split(" ")


data_input.sort()


unique = set(data_input)



for n in unique:
    backet[n] = 0


for m in data_input:
    value = backet[m]
    value += 1
    
    backet[m] = value

backet_list = list(backet.values())
backet_list.sort()
backet_list.reverse()
if K < len(unique):
    ball_value = backet_list[K:]
    goukei = sum(ball_value)
    print(goukei)
else:
    print("0")

Solution (Python):
N,ika=map(int,input().split())
A=input().split()
A1=list(map(int,A))
AS={}

for i in range(N):
    if A1[i] in AS:
        AS[A1[i]] +=1
    else:
        AS[A1[i]] =1
AS=sorted(AS.items(), key=lambda x:x[1])
kesushurui=len(AS)-ika
ans=0
if kesushurui>0:
    for k in range(kesushurui):
        ans+=AS[k][1]
        
print(ans)

Solution (Python):
from collections import Counter

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

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

c = Counter(A)
kind = len(c.values())

if kind <= K:
    print(0)

else:
    dropnum = 0
    drop = kind - K
    lst = list(c.values())
    lst.sort(reverse = True)
    for _ in range(drop):
        dropnum += lst.pop()
    print(dropnum)

Solution (Python):
from collections import Counter

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

if len(c) <= K:
    print(0)
else:
    c = sorted(c.items(), key=lambda x: x[1])
    dif = len(c) - K
    cnt = 0
    for i in range(dif):
        cnt += c[i][1]
    print(cnt)

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

c = [0]*200001
for b in a:
    c[b] += 1
c.sort(reverse=True)

print(sum(c[K:]))

Solution (Python):
n, k = map(int, input().split())
b = {}
for i in input().split():
    if i in b:
        b[i] += 1
    else:
        b[i] = 1
b = sorted(b.values())
print(sum(b[:-k]))

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

Solution (Python):
import itertools
N, K = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
A.sort()
Balls = [len(list(g)) for k, g in itertools.groupby(A)]
Balls.sort(reverse=True)
cnt = 0
while len(Balls) > K:
    cnt += Balls.pop()
print(cnt)

Solution (Python):
N, K = map(int, raw_input().split())
A = map(int, raw_input().split())
A.sort()

memo = [A[0], 1]
newA = []
for i in range(1, N):
	if A[i] != A[i-1]:
		newA.append(memo)
		memo = [A[i], 1]
	else:
		memo[1] = memo[1] + 1
newA.append(memo)
newA.sort(key=lambda x:x[1])

sum = 0
for i in range(0, len(newA)-K):
	sum = sum + newA[i][1]

print sum

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

sort_tuple_list = sorted(collections.Counter(A_list).items(), 
                         key=lambda x: x[1])

kind_num = len(sort_tuple_list)
remove_counter = 0

for i in sort_tuple_list:
    if kind_num <= K:
        break
    else:
        kind_num -= 1
        remove_counter += i[1]

print(remove_counter)

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

ans = 0
kind = len(ctr)
for k,v in reversed(ctr.most_common()):
    if kind <= K:
        break
    ans += v
    kind -= 1
print(ans)

Solution (Python):
import collections

n, k = map(int, input().split())
array = [int(x) for x in input().split()]
_dict = collections.Counter(array)

res = 0
tmp = len(_dict.values())
if len(_dict.values()) < k:
    print(0)
else:
    sort_dict = collections.OrderedDict(sorted(_dict.items(), key=lambda x: x[1], reverse=True))
    value_dict = sort_dict.values()
    print(sum(list(value_dict)[k:]))

Solution (Python):

from collections import Counter

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

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
n = [0 for i in range(N + 1)]
count = 1
n[A[0]] = 1
for i in range(1, N):
    if A[i] != A[i - 1]: count += 1
    n[A[i]] += 1
n.sort()
if count <= K:
    print(0)
else:
    for i in range(N + 1):
        if n[i] != 0:
            key = i
            break
    print(sum(n[key:key + count - K]))

Solution (Python):
import os
import sys

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

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

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

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

tmp = len(d)
ans = 0


for _,v in s_d:
    tmp-=1
    ans += v
    if(tmp==k):
        break

print(ans)
exit(0)

Solution (Python):
from collections import Counter

n, k = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
c = Counter(a)
cc = c.most_common()
cv = []
ans = 0

for i in range(0,len(list(c.keys()))):
    cv.append(cc[i][1])

while len(cv) > k:
    ans += cv.pop()

print(ans)

Solution (Python):
n, k = map(int, input().split())
l = sorted(list(map(int, input().split())))
counts = []
now = l[0]
count = 1
for i in range(1, n):
  if l[i] == now:
    count += 1
  else:
    counts.append(count)
    now = l[i]
    count = 1
  
  if i == n-1:
    counts.append(count)

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

Solution (Python):
N, K = map(int, input().split())
B = [0]*(N+1)
for i in input().split():
  B[int(i)] += 1
print(sum(sorted(B)[:-K]))

Solution (Python):
from collections import Counter


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

sort_count = sorted(a.values())


total = 0
l = len(sort_count)

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

Solution (Python):
from collections import Counter

N,K = map(int,input().split())
A=list(map(int,input().split()))
count = Counter(A)
count = count.most_common()
diff = len(count)-K
ans=0
if diff>0:
    for i in range(diff):
        ans+=count[len(count)-i-1][1]

print(ans)

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

Solution (Python):
from collections import Counter
N, K = map(int, input().split())
cnt = Counter(map(int, input().split()))
tot = 0
k = []
for i in cnt.values():
    k.append(i)
k.sort()
for i in range(len(cnt)-K):
    tot += k[i]
print(tot)

Solution (Python):
from collections import Counter
from collections import OrderedDict
N, K = map(int, input().split())
cnt = Counter(map(int, input().split()))
tot = 0
if len(cnt) <= K:
    print(0)
else:
    k = []
    for i in cnt.values():
        k.append(i)
    k.sort()
    for i in range(len(cnt)-K):
        tot += k[i]
    print(tot)

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

Solution (Python):
from collections import Counter

N, K = [int(elem) for elem in input().split(' ')]
A_list = [int(elem) for elem in input().split(' ')]
assert len(A_list) == N

counter = Counter(A_list)
current_num_categories = len(counter)

count = 0
for i, num in enumerate(sorted(counter.values()), start=1):
    if i <= current_num_categories - K:
        count += num
    else:
        break

print(count)

Solution (Python):
n, k = map(int,input().split())
al = list(map(int,input().split()))
dics = {}
 
for i in range(n):
    if al[i] in dics:
        dics[al[i]] = dics[al[i]] + 1
    else:
        dics.update({al[i]:1})
        
counts = len(dics.values())
kesu = 0
sortv = list(sorted(list((dics.values()))))

i = counts
j = 0
while i > k:
    kesu = kesu + sortv[j]
    i = i - 1
    j = j + 1
print(kesu)

Solution (Python):
from collections import Counter
N, K = map(int, input().split())
a = [int(i) for i in input().split()]
s = len(set(a))
c = Counter(a)
L = sorted(c.items(), key=lambda x: x[1])

cnt = 0
i = 0
while s > K:
    cnt += L[i][1]
    s -= 1
    i += 1
print(cnt)

Solution (Python):
from collections import Counter
N, K = map(int, input().split())
A = list(map(int,input().split()))
C = Counter(A)
C = list(reversed(sorted(C.items(), key=lambda x: x[1])))

if len(C)<=K:
    print(0)
    exit(0)

ans = 0
for k in range(K,len(C)):
    ans += C[k][1]
print(ans)

Solution (Python):
import collections

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

if len(A_dict) <= K:
    print(0)
else:
    print(N-sum(dict(A_dict.most_common(K)).values()))

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

a.sort()
x, y = 1, 1
y_lst = []

for i in range(n-1):
    if a[i] == a[i+1]:
        y += 1
    else:
        x += 1
        y_lst.append(y)
        y = 1

    if i == len(a)-2:
            y_lst.append(y)

y_lst.sort()
t = x - k
if t <= 0:
    print(0)
else:
    print(sum(y_lst[:t]))

Solution (Python):
from collections import Counter
N,K = map(int,input().split())
AC = Counter(list(map(int,input().split()))).most_common()[::-1]
C = 0
if len(AC) > K:
    for i in range(len(AC)-K):
        k,v = AC[i]
        C += v
print(C)

Solution (Python):
from collections import Counter

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

Solution (Python):
import collections

N,K = map(int,input().split())
A = [0]*N
A = list(map(int,input().split()))
c = collections.Counter(A)
c_list = list(c.values())
c_list.sort()
c_list_inv = [i for i in c_list[::-1]]
c_list_sum = [i for i in c_list_inv[K::1]]
print(sum(c_list_sum))

Solution (Python):
from collections import Counter
n, k = map(int, input().split())
a = list(input().split())
len_a = len(set(a))
if len_a <= k:
    print(0)
else:
    counter = Counter(a)
    count = 0
    s = len_a - k
    sort_counter = sorted(counter.items(), key=lambda  x:x[1])
    for x in range(s):
        count += sort_counter[x][1]
    print(count)

Solution (Python):
from collections import defaultdict
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
dic = defaultdict(int)
for i in range(len(a)):
    dic[a[i]] += 1
counts = sorted(dic.values())
print(sum(counts[:-k]))

Solution (Python):
N, K = list(map(int, input().split()))
As = input().split()
dic = {}
for c in As:
    if c in dic:
        dic[c] += 1
    else:
        dic[c] = 1

values = list(sorted(dic.values()))

if len(values) <= K:
    print(0)
else:
    print(sum(values[:len(values)-K]))

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

    num = [0 for _ in range(200000)]
    s = 0
    for A in A:
        num[A-1] += 1

    count = 0
    num.sort()
    for i in range(200000-K):
        count += num[i]
    return count

if __name__ == '__main__':
    print(main())

Solution (Python):
N,K=map(int,input().split())
d,ans={},0
for i in list(map(int,input().split())):
    d[i]=d.get(i,0)+1
s=sorted(d.values())
for i in range(len(s)-K):
    ans+=s[i]
print(ans)

Solution (Python):
n, k = map(int, input().split())
a = input().split()
b = dict()
for i in a:
    b[i] = b.get(i, 0) + 1
print(sum(sorted(b.values())[:-k]))

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

d = dict()
for i in a:
    d[i] = d.get(i, 0) + 1
d_sorted = sorted(d.values())
knd = len(set(a))
print(sum(d_sorted[:knd - k]))

Solution (Python):
from collections import Counter

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

c = Counter(a)

remaining = n - sum([x[1] for x in c.most_common(k)])

print(remaining)

Solution (Python):





if __name__ == '__main__':
    from collections import Counter

    ball_count, kind_count = map(int, input().split())
    ball_numbers = Counter(list(map(int, input().split())))
    sorted_ball_numbers = sorted(ball_numbers.values())
    print(sum(sorted_ball_numbers[:-kind_count]))

Solution (Python):





if __name__ == '__main__':
    from collections import Counter

    ball_count, kind_count = map(int, input().split())
    ball_numbers = Counter(list(map(int, input().split())))
    sorted_ball_numbers = sorted(ball_numbers.items(), key=lambda x: x[1])
    ball_number_key_count = len(ball_numbers.keys())

    need_rewritten_count = 0
    diff_count = ball_number_key_count - kind_count

    if diff_count > 0:
        for i, (key, value) in enumerate(sorted_ball_numbers):
            if diff_count > i:
                need_rewritten_count += value

    print(need_rewritten_count)

Solution (Python):
from collections import defaultdict
n, k = map(int, input().split())
a = list(map(int, input().split()))
dic = defaultdict(int)
for i in range(n):
    dic[a[i]] += 1

s = sorted(dic.values())
if len(dic) <= k:
    print(0)
else:
    num = 0
    for i in range(len(dic) - k):
        num += s[i]
    print(num)

Solution (Python):
from collections import Counter
from functools import reduce

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

count = Counter(A)

if len(count.keys()) <= K:
    print(0)
    exit()

L = sorted(list(count.items()), key=lambda x: x[1], reverse=True)
ans = sum([n[1] for n in L[K:]])
print(ans)

Solution (Python):
import sys

stdin = sys.stdin

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

n,k = na()
a = na()

dic = {}

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

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

Solution (Python):
from collections import Counter

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

counter = Counter(a)

lis = counter.most_common()[::-1]
individual = len(lis)

cnt = 0
ind = 0
while individual > K:
    cnt += lis[ind][1]
    ind += 1
    individual -= 1

print(cnt)

Solution (Python):
n,k = list(map(int,input().split()))
num = list(map(int,input().split()))
l = [0 for _ in range(200001)]
for i in range(n):
    l[ num[i] ] +=1
count=0
a = []
for i in l:
    if(i>0):
        count+=1
        a.append(i)
a.sort()
ans = 0
for i in range(count-k):
    ans += a[i]
print(ans)

Solution (Python):
import collections
n,k = (int(i) for i in input().split())
x = [int(i) for i in input().split()]
y = set(x)
if len(y)<=k:
	print(0)
else:
	answer = 0
	c = collections.Counter(x).most_common()
	for j in range(-1,-(len(y)-k+1),-1):
		answer += c[j][1]
	print(answer)

Solution (Python):
import sys
n,k=map(int,input().split())
a=sorted(list(map(int,input().split())))
s=set(a)
if len(s)<=k:
    print(0)
    sys.exit()
lst=[0]*n
ind=0
lst[0]=1
for i in range(n-1):
    if a[i+1]==a[i]:
        lst[ind]+=1
    else:
        ind+=1
        lst[ind]+=1
lst.sort()
ans,cou=0,0
for i in range(n):
    if lst[i]!=0 and cou<len(s)-k:
        ans+=lst[i]
        cou+=1
print(ans)

Solution (Python):
from collections import defaultdict

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

dd=defaultdict(int)
for a in arr:
    dd[a]+=1

brr=sorted(dd.values())
ans=0
for k in range(len(brr)-k):
    ans+=brr[k]
print(ans)

Solution (Python):
from collections import Counter

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

Solution (Python):
n,k = map(int,input().split())
a = {}
for i in input().split():
    a[int(i)] = a.get(int(i),0)+1
b = list(a.values())
b.sort()
print(sum(b[:len(b)-k]))

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

numlist = {}
for i in A:
    if i in numlist:
        numlist[i] += 1
    else:
        numlist[i] = 1
numbers = numlist.values()
numbers = sorted(numbers)

change = 0
vari = len(numbers)
for i in range(vari):
    if K >= vari - i:
        break
    change += numbers[i]

print(change)

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

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

countup.sort()
result = 0
f = list(filter(lambda x: x != 0, countup))

for i, v in enumerate(f):
 if i >= len(f) - k:
    print(result)
    break
 result += v

Solution (Python):
N,K=map(int,input().split())
a=list(map(int,input().split()))
a=sorted(a)
a.append(-1)

ans=[]
count=1

for i in range(N):
    if a[i+1]==a[i]:
        count += 1
    else:
        ans.append(count)
        count=1

ans=sorted(ans,reverse=True)

if K >= len(ans):
    print(0)
else:
    print(sum(ans[K:]))

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
import collections
data=collections.Counter(a)
data=sorted(data.items(),key=lambda x:x[1])
count=0
if len(data)<=k:
    print(0)
    exit()
else:
    for i in range(len(data)-k):
        count+=data[i][1]
print(count)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
kind=[]
count=1
for i in range(len(a)-1):
  if(a[i]==a[i+1]):count+=1
  else:
    kind.append(count)
    count=1
kind.append(count)
kind.sort()
change=0
for i in range(k):
  if(len(kind)!=0):change+=kind.pop()
print(n-change)

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect


sys.setrecursionlimit(10000)


def chkprint(*args):
    names = {id(v):k for k,v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))


def to_bin(x):
    return bin(x)[2:]


def dim2input(N):
    li = []
    for _ in range(N):
        li.append(list(map(int, input())))
    return li

""" input template
S = input()
N = int(input())
L = list(map(int, input().split()))
a, b = list(map(int, input().split()))
SL = list(input())
"""



dp = None

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

    D = collections.defaultdict(lambda: 0)

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

    L = []
    for v in D.values():
        L.append(v)

    L.sort(reverse=True)

    i = 0
    S = 0
    U = min(K, len(L))
    while i < U:
        S += L[i]
        i += 1

    print(sum(L) - S)


main()

Solution (Python):


N, K = map(int, input().rstrip().split())
ball = {}
for i in input().rstrip().split():
    if i not in ball:
        ball[i] = 1
    else:
        ball[i] += 1
ball_sort = sorted(ball.items(), key = lambda x: x[1])
if len(ball_sort) > K:
    cnt = 0
    for i in range(len(ball_sort) - K):
        cnt += ball_sort[i][1]
    print(cnt)
else:
    print(0)

Solution (Python):
import collections as col
n,k = map(int,input().split())
a   = list(map(int,input().split()))
count_dict = col.Counter(a)
cdmc       = count_dict.most_common(k)
sum_cdmc = 0

for k,v in cdmc:
    sum_cdmc += v
print(n - sum_cdmc)

Solution (Python):



import collections

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

count_rmv = 0
count_int = collections.Counter(A).most_common()
num_int_type = len(count_int)

while num_int_type > K:
    num_int_type -= 1
    count_rmv += count_int[num_int_type][1]

print(count_rmv)