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



Sample input:
5 2
1 1 2 2 5


Sample output:
1

Submission status: Accepted

Solution code:
N,K = [int(i) for i in input().split()]
AAA = [int(i) for i in input().split()]
 
count = [0 for i in range(200010)]
for A in AAA:
    count[A]+=1
ans=0
count=sorted([i for i in count if i!=0])
 
if len(count)<=K:
    print("0")
    exit()
else:
    print(sum(count[:-K]))

Submission status: Accepted

Solution code:
N,K = [int(i) for i in input().split()]
AAA = [int(i) for i in input().split()]

count = [0 for i in range(200010)]
for A in AAA:
    count[A]+=1
ans=0
count=sorted([i for i in count if i!=0])

if len(count)<=K:
    print("0")
    exit()
else:
    print(sum(count[:-K]))

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import groupby

n,k = [int(x) for x in stdin.readline().rstrip().split()]
li = list(map(int,stdin.readline().rstrip().split()))
li.sort()

lin = []
for key, value in groupby(li):
    lin.append(len(list(value)))

lin.sort()

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
As = map(int, input().split())
numbers = {}
for A in As:
    if numbers.get(A):
        numbers[A] += 1
    else:
        numbers[A] = 1

sorted_count = sorted(numbers.values(), reverse=True)
print(sum(sorted_count[K:]))

Submission status: Accepted

Solution code:
import collections

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

set_A = set(A)
c = collections.Counter(A)
cnts = []
for a, t in c.items():
    cnts.append([t, a])
cnts.sort()
ans = 0
for cnt in cnts:
    if len(set_A) <= k:
        print(ans)
        break
    else:
        ans += cnt[0]
        set_A.remove(cnt[1])

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
from collections import defaultdict as ddict
A_dict = ddict(int)
for a in A:
    A_dict[a] += 1
A_tupple = sorted(A_dict.items(), key=lambda x:x[1],reverse=True)
ans = 0
for a in A_tupple[K:]:
    ans += a[1]
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
A = list(map(int, input().split()))
B ={}
for i in A:
    if i not in B.keys():
        B[i] = 1
    else:
        B[i] += 1
C = sorted(B.items(), key=lambda x: -x[1])
count = 0
while len(C) > K:
    a,b = C.pop()
    count += b
print(count)

Submission status: Accepted

Solution code:
#081c
import collections

N,K = map(int, input().split())
A_list = [int(e) for e in input().split()]

counted = collections.Counter(A_list)
count_listed = list(counted.values())
count_listed.sort(reverse=True)

ans = 0

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

Submission status: Accepted

Solution code:
from collections import Counter

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

lenght = len(set(A))-K
ans = 0
if lenght<=0:print(ans), exit()
else:
    for k, v in Counter(A).most_common()[::-1][:lenght]:
        ans += v
print(ans)

Submission status: Accepted

Solution code:
def examC():
    N, K = LI()
    A = LI()
    d = defaultdict(int)
    for i in A:
        d[i] +=1
    cur = 0
    que = []
    heapq.heapify(que)
    for i in d.values():
        heappush(que,i)
    for i in range(len(d)-K):
        cur += heappop(que)
    print(cur)

import sys,copy,bisect,itertools,heapq
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

Submission status: Accepted

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

count = collections.Counter(list_a)
sort_c = sorted(count.items(), key=lambda x:x[1])
num_kind = len(sort_c)

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

Submission status: Accepted

Solution code:
from collections import Counter as C
N, K = map(int, input().split())
A = sorted(C([int(a) for a in input().split()]).values())[::-1]
print(sum(A) - sum(A[:K]))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
species = list(set(A))
dic = {}
for i in species:
    dic[i] = 0
for i in A:
    dic[i] += 1
if K >= len(species):
    print(0)
else:
    tmp = sorted(dic.values(),reverse=1)
    print(sum(tmp[K:]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
Alist = Counter(list(map(int, input().split()))).most_common(K)
for key, value in Alist:
    N -= value
print(N)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
a = [int(i) for i in input().split()]
adict = {}
for ai in a:
    adict[str(ai)] = adict.get(str(ai), 0)+1
a = sorted(adict.values())
print(sum(a[:len(a)-k]))

Submission status: Accepted

Solution code:
import sys
import collections

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')

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

c = collections.Counter(nums)
d = c.most_common()[::-1]
print(sum([d[i][1] for i in range(len(d) - K)]))

Submission status: Accepted

Solution code:
import heapq
import collections as c
N, K = map(int, input().split())
A = c.Counter(list(map(int, input().split())))
S = len(list(A.keys()))

V = list(A.values())
heapq.heapify(V)

ans = 0
for _ in range(max(0,S-K)):
	ans += heapq.heappop(V)

print(ans)

Submission status: Accepted

Solution code:
import sys
import collections
input = sys.stdin.readline

N,K = map(int,input().split())
a = list(map(int,input().split()))
c = collections.Counter(a)
d = len(c)
ans = 0
c_sorted = sorted(c.items(),key = lambda x:x[1])
for i in c_sorted:
    if K >= d:
        break
    ans += i[1]
    d -= 1
print(ans)

Submission status: Accepted

Solution code:
import collections

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

a_counter=collections.Counter(a)
li=a_counter.most_common()[k:]
ans=0
for i in li:
    ans+=i[1]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(aa)
ans = 0
if len(c) > k:  
  cc = c.most_common()[::-1]
  for i in range(len(cc)-k):
    ans += cc[i][1]

print(ans)

Submission status: Accepted

Solution code:
N, K = [int(i) for i in input().split()]
B = sorted([int(i) for i in input().split()])
D ={}
bag = 0

for i in B:
    if i in D:
        D[i] += 1
    else:
        D[i] = 1

LLL = sorted(list(D.values()))
AA = len(D) - K
if len(D) <= K:
    print(0)
else:
    for i in range(AA):
        bag += LLL[i]
    print(bag)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
input = sys.stdin.readline

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

k = len(list(set(A)))
d = defaultdict(int)

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

print(sum(sorted(d.values())[:max(0, k - K)]))

Submission status: Accepted

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

for i in input().split():
  B[int(i)] += 1

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

Submission status: Accepted

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

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

N, K = two_int()
A = many_int()

dicts={}
for i in A:
    if i in dicts:
        dicts[i]+=1
    else:
        dicts[i]=1

items = []
for k,v in dicts.items():
    items.append([k,v])

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

sums = 0
for i in items[:len(items) - K]:
    sums += i[1]

print(sums)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
d = {}
cnt = 0
for v in a:
  if not v in d:
    d[v] = 1
    cnt += 1
  else:
    d[v] += 1
if cnt - K < 0:
  print(0)
else:
  s = sorted(d.items(), key=lambda x:x[1])
  ans = 0
  for i in range(cnt - K):
    ans += s[i][1]
  print(ans)

Submission status: Accepted

Solution code:
import collections
N,K=map(int,input().split())
a=list(map(int,input().split()))
table=collections.defaultdict(int)
for i in range(N):
  table[a[i]]+=1
table= list(table.items())
if len(table)<=K:
  print(0)
else:
  ans=0
  table.sort(key =lambda x:x[1])
  for i in range(len(table)-K):
    ans+=table[i][1]
  print(ans)

Submission status: Accepted

Solution code:
import sys
import collections
input = sys.stdin.readline
n,k = map(int,input().split())

A = list(map(int,input().split()))
sum = 0
c = collections.Counter(A)
c = c.most_common()
if len(c) > k:
  a = len(c)-k
  c = c[-a:]
  for i in range(a):
    sum += c[i][1]
  print(sum)
else:
  print(0)

Submission status: Accepted

Solution code:
from collections import Counter
n,k = map(int,input().split())
a = list(Counter(map(int,input().split())).values())
a.sort()
print(sum(a[:-k]))

Submission status: Accepted

Solution code:
import sys
import collections
# import math
# import string
# import bisect
# import re
# import itertools
# import statistics

def main():
    n, k, *a = (int(x) for x in sys.stdin.read().split())

    c = collections.Counter(a)
    diffs = len(c)
    if diffs <= k: print(0); exit()
    remainder = diffs - k
    count = 0
    l = sorted(c.values())
    b = collections.Counter(l)
    for i, j in sorted(b.items()):
        if remainder - j > 0:
            count += j * i
            remainder -= j
        else:
            count += remainder * i
            break
    
    print(count)

if __name__ == "__main__":
    # execute only if run as a script
    main()

Submission status: Accepted

Solution code:
import sys
import collections
# import math
# import string
import bisect
# import re
# import itertools
# import statistics

def main():
    n, k, *a = (int(x) for x in sys.stdin.read().split())

    c = collections.Counter(a)
    diffs = len(c)
    if diffs <= k: print(0); exit()
    remainder = diffs - k
    count = 0
    l = sorted(c.values())
    for i in range(1, n+1):
        b = bisect.bisect_right(l, i) - bisect.bisect_left(l, i)
        if remainder - b >= 0:
            count += b * i
            remainder -= b
        else:
            count += remainder * i
            break
    
    print(count)

if __name__ == "__main__":
    # execute only if run as a script
    main()

Submission status: Accepted

Solution code:
import heapq

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

L = [0]*(N+1)
for i in range(N):
    L[A[i]] += 1
    
h = []
for i in range(N+1):
    if L[i] > 0:
        heapq.heappush(h, L[i])
        
ans = 0
while len(h) > K:
    ans += heapq.heappop(h)
    
print(ans)

Submission status: Accepted

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

b = [0] * n
for i in range(n):
    b[a[i] - 1] += 1

b.sort(reverse=True)

ans = 0

for i in range(k):
    ans +=b[i] 

if n - ans <= 0:
    print(0)
else:
    print(n - ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
c=[]
d,p=0,a[0]
for x in a:
  if p==x:
    d+=1
  else:
    c.append(d)
    d=1
    p=x
c.append(d)
c.sort(reverse=1)
k=min(k,len(c))
print(sum(c[k:]))

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
n=list(map(int,input().split()))
import collections as c
lis=c.Counter(n).most_common()[b:]
ans=0
for i in lis:
    ans+=i[1]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

N,K=map(int,input().split())
A=list(map(int,input().split()))
L=collections.Counter(A)
values,counts=zip(*L.most_common())
counts=list(counts)

Colors=len(counts)
Change=0
while Colors>K:
    Change+=counts[-1]
    counts.pop()
    Colors-=1

print(Change)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
cnt=1
lst=[]
for i in range(1,n):
    if a[i]==a[i-1]:
        cnt+=1
    else:
        lst.append(cnt)
        cnt=1
lst.append(cnt)
lst.sort()
if k>=len(lst):
    print(0)
else:
    count=0
    for i in range(len(lst)-k):
        count+=lst[i]
    print(count)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = map(int,input().split())
L = [0]*(N+1)
uniq = set()

for i in A:
    L[i] += 1
    uniq.add(i)

if len(uniq) <= K:
    print(0)
else:
    L = sorted(L)
    count = 0
    ans = 0
    for i in L:
        if i != 0:
            count += 1
            ans += i
        if count == len(uniq)-K:
            break
    print(ans)

Submission status: Accepted

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

    cnt = [0] * N

    for ai in A:
        cnt[ai - 1] += 1

    cnt.sort()
    ans = sum(cnt[:N-K])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    CNT = [0] * (N + 1)

    for ai in A:
        CNT[ai] += 1

    # print(CNT)

    CNT2 = [i for i in CNT if i != 0]
    CNT2.sort()
    # print(CNT2)

    x = len(CNT2) - K
    print(sum(CNT2[:x]))
    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
nums = {}
for a in A:
    if a in nums:
        nums[a] += 1
    else:
        nums[a] = 1
sorted_nums = list(sorted(nums.items(), key=lambda x: x[1]))
nums_len = len(sorted_nums)
ans = 0
if nums_len > K:
    for i in range(nums_len - K):
        ans += sorted_nums[i][1]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

count = sorted(list(Counter(a).values()), reverse = True)

print(n - sum(count[:k]))

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(a) # Counter({1: 2, 2: 2, 5: 1})
count = c.values() # [2, 2, 1] (type: 'dict_values')

l_count = list(count) # to list 
l_count.sort(reverse = True) # to descending order

print(n - sum(l_count[:k])) # n - (sum of first k values from count[])

Submission status: Accepted

Solution code:
from collections import Counter

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

count = []
c = Counter(a) # dict {'a': 3, 'b': 4, ...}
for i in c:
    count.append(c[i])

count.sort(reverse = True)
print(n - sum(count[:k]))

Submission status: Accepted

Solution code:
#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))

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


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

li = sorted(li,reverse = True)

ttl = 0
for i in range(n+1):
    ttl += li[i]

    if i == k-1:
        break

ans = n-ttl

print(ans)

Submission status: Accepted

Solution code:
import sys
import collections
sys.setrecursionlimit(10 ** 7)


def main():
    N, K = map(int, input().split())
    c = collections.Counter(input().split())
    c = sorted(c.values())
    k = len(c) - K
    if k <= 0:
        print(0)
    else:
        print(sum(c[:k]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import collections
sys.setrecursionlimit(10 ** 7)


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
grps =[] 
from collections import Counter 
c=Counter(l)
l=[c[i] for i in c]
l.sort(reverse=1)
print(n-sum(l[:k]))

Submission status: Accepted

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

d={}
for Ai in A:
    if Ai not in d:
        d[Ai]=1
    else:
        d[Ai]+=1

l=sorted(list(d.values()))

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

Submission status: Accepted

Solution code:
import sys
n, k = map(int, input().split())
a = list(map(int, input().split()))
numMap = {}
ans = 0

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

if len(numMap) <= k:
    print(0)
    sys.exit()

cnts = []
for cnt in numMap.values():
    cnts.append(cnt)
cnts.sort()
temp = len(numMap)

for cnt in cnts:
    if temp == k:
        break
    ans += cnt
    temp -= 1

# print(numMap)
# print(cnts)
print(ans)

Submission status: Accepted

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

score = dict()

for i in range(N):
	if A[i] in score:
		score[A[i]] += 1
	else:
		score[A[i]] = 1
		
score_sorted = sorted(score.items(), key=lambda x:x[1])
# print(score_sorted)
ret = 0

if len(score_sorted) - K > 0:
	for i in range(len(score_sorted) - K):
		ret += score_sorted[i][1]
	print(ret)
else:
	print(0)

Submission status: Accepted

Solution code:
import collections
N,K=map(int,input().split())
li=list(map(int,input().split()))
li_count=collections.Counter(li)
li_count_most=li_count.most_common()[::-1]
change=0
S=max(len(li_count)-K,0)
for s in range(S):
    change+=li_count_most[s][1]
print(change)

Submission status: Accepted

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

M = max(A)

L = [0] * (M+1)

for i in A:
    L[i] += 1

D = []    
for i in range(M+1):
    if L[i] > 0:
        D.append(L[i])

D.sort()

nn = len(D)

if nn <= K:
    print(0)

else:
    print(sum(D[:nn-K]))

Submission status: Accepted

Solution code:
n,k=map(lambda x: int(x), input().split())
a=list(map(lambda x: int(x), input().split()))
dic={}
for i in range(n):
    dic[a[i]]=dic.get(a[i],0)+1
count=list(dic.values())
count.sort(reverse=True)
if len(count) <= k:
    print(0)
else:
    print(sum(count[k:]))

Submission status: Accepted

Solution code:
import collections

def test():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = collections.Counter(a).most_common()
    ans = 0; i = -1
    c = len(b)
    while k < c:
        ans = ans + b[i][1]
        c = c - 1
        i = i - 1
    print(ans)


if __name__ == "__main__":
    test()

Submission status: Accepted

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


count = {}
for i in range(n):
    if a[i] not in count:
        count[a[i]] = 0
    count[a[i]] += 1
ans = 0
value = list(count.values())
value.sort()
for i in range(max(len(value) - k, 0)):
    ans += value[i]
print(ans)

Submission status: Accepted

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

import collections

c = collections.Counter(A)

cnt=0
res=0
for i in c.most_common():
        if cnt >= K:
                res+=i[1]
        else:
                cnt+=1


print(res)

Submission status: Accepted

Solution code:
N,K=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
dic={}
for e in ls:
  if (e in dic):
    dic[e]+=1
  else:
    dic[e]=1
ls2=list(dic.items())
ls2=sorted(ls2, key=lambda x : -x[1])
ans=0
for i in range(K,len(ls2)):
  ans+=ls2[i][1]
print(ans)

Submission status: Accepted

Solution code:
#ABC081C
import collections
N, K =map(int,input().split())
A = list(map(int,input().split()))
c = collections.Counter(A)
NumA = list(c.values())
NumA.sort()
ans = 0
#print(NumA)
for i in range(len(set(A))-K):
    ans += NumA[i]
print(ans)

Submission status: Accepted

Solution code:
import sys


n,k = [int(_) for _ in input().split()]
l = [int(_) for _ in input().split()]
ans = 0
d = {}
for i in range(n):
    if l[i] in d:
        d[l[i]] += 1
    else:
        d[l[i]] = 1
d_sorted = sorted(d.items(), key = lambda x:x[1], reverse=True)
if len(d_sorted) <= k:
    ans = 0
    print(ans)
    sys.exit()
else:
    for j in range(k,len(d_sorted)):
        ans += d_sorted[j][1]
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
number = [0]*n
for i in range(n):
    number[a[i]-1]+=1
number.sort(reverse=True)
ans = 0

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

Submission status: Accepted

Solution code:
def input():
    import sys
    return sys.stdin.readline().rstrip()


def main():
    import math
    import collections
    import itertools

    n, k = map(int, input().split())
    as_ = list(map(int, input().split()))
    counter_a = collections.Counter(as_)
    counter_a = sorted(counter_a.items(), key=lambda x: x[1])
    types = len(counter_a)
    ans = 0
    for num, count in counter_a:
        if types > k:
            ans += count
            types -= 1
        if types <= k:
            print(ans)
            break

if __name__ == '__main__':
    main()

Submission status: Accepted

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

List = collections.Counter(A)

cnt = list(List.values())
cnt.sort()

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=len(set(a))
if s<=k:
    print(0)
else:
    l=[0]*(max(a)+1)
    for i in range(n):
        l[a[i]]+=1
    l.sort()
    l=l[::-1]
    print(sum(l[k:]))

Submission status: Accepted

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    A = [int(i) for i in input().split()]
    from collections import Counter
    c = sorted(Counter(A).values(),reverse=True)
    ans = 0
    while K < len(c):
        ans += c.pop()
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections as col

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

cnt = col.Counter(li)
cnt2 = list(cnt.values())
cnt2 = sorted(cnt2)
cnt2 = list(reversed(cnt2))
print(max(0,n-sum(cnt2[0:k])))

Submission status: Accepted

Solution code:
# coding: utf-8
N, K = map(int, input().split())
A = list(map(int, input().split()))
H = [0] * N
for i in range(N):
    H[A[i]-1] += 1
H.sort(reverse=True)
print(sum(H[K:]))

Submission status: Accepted

Solution code:
import collections

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

A_count = collections.Counter(A)

_, counts = zip(*A_count.most_common()[::-1])

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

Submission status: Accepted

Solution code:
import collections

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

A_count = collections.Counter(A)

_, counts = zip(*A_count.most_common()[::-1])

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

Submission status: Accepted

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

l = [0] * (N + 1)
for i in range(N):
    l[A[i]] += 1
l.sort(reverse=True)
print(N - sum(l[:K]))

Submission status: Accepted

Solution code:
from collections import defaultdict
n, k = map(int, input().split())
a = list(map(int, input().split()))
d = defaultdict(int)

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

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

Submission status: Accepted

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

cnt=sorted(Counter(A).values())
print(sum(cnt[:-k]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

count = Counter(ali)
sortcount = sorted(count.items(), key=lambda x:x[1])

diverse = len(count)
erase = diverse - k
if erase <= 0:
    print(0)
    exit()

num = 0
for key, val in sortcount[:erase]:
    num += val
print(num)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
Data = list(map(int,input().split()))
Dic = {}
for d in Data:
    if d in Dic.keys():
        Dic[d]+=1
    else:
        Dic[d]= 1
kind = len(Dic)

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

ans = 0
cnt = kind-K
for k,v in sortedDic:
    if cnt <= 0:
        break
    else:
        cnt -= 1
        ans += v
print(ans)

Submission status: Accepted

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

a.sort()
count = 1
b = []

for i in range(n-1):
    if a[i] != a[i+1]:
        b.append(count)
        count = 1
    else:
        count += 1

b.append(count)

b.sort(reverse=True)

count = 0
if k < len(b):
    for i in range(k, len(b)):
        count += b[i]

print(count)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
A = list(map(int,input().split()))
nums = {}
for a in A:
    if a in nums:
        nums[a] += 1
    else:
        nums[a] = 1
B = sorted(nums.values())
m = len(B) - k
ans = 0
for i in range(m):
    ans += B[i]
print(ans)

Submission status: Accepted

Solution code:
import collections

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

counter = collections.Counter(a)
counter_sorted = sorted(counter.items(), key=lambda x: x[1], reverse=True)
ans = n
for i in range(min(k, len(counter_sorted))):
    ans -= counter_sorted[i][1]
print(ans)

Submission status: Accepted

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

ass_a = {}
for aa in A:
    if aa not in ass_a:
        ass_a[aa] = 1
    else:
        ass_a[aa] += 1
ass_a = sorted(ass_a.items(), key=lambda x:x[1])

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

Submission status: Accepted

Solution code:
import collections

n, k = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
nums = collections.Counter(A)

l = len(nums)

if l <= k:
    print(0)
    quit()


ans = 0
for c in sorted(nums.values()):
    ans += c
    l -= 1
    if l <= k:
        break
print(ans)

Submission status: Accepted

Solution code:

import collections

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

ca = collections.Counter(A)

cc = ca.most_common()

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

print(len(A) - cnt)

Submission status: Accepted

Solution code:
import sys
from functools import reduce
import copy
import math
from pprint import pprint
import collections


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def int_inputs(num_of_input):
    ins = [int(input()) for i in range(num_of_input)]
    return ins


def solve(inputs):
    [N, K] = string_to_int(inputs[0])
    A = string_to_int(inputs[1])
    counter = collections.Counter(A)
    i = len(counter)
    values = counter.most_common()
    change_num = 0
    while i > K:
        change_num += (values[i-1][1])
        i -= 1
    return change_num


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    ret = solve(inputs(2))
    print(ret)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(A)

cnt = 0
most_common = c.most_common()
if K < len(c.keys()):
    for i in range(1,abs(len(c.keys()) - K)+1):
            cnt += most_common[-i][1]
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter
n,k = map(int,input().split())
ary = Counter(list(map(int,input().split())))
if len(ary.keys()) > k:
    print(sum(sorted(ary.values())[:len(ary.keys())-k]))
else:print(0)

Submission status: Accepted

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

cnt = [0] * (N+1)

for a in A:
    cnt[a] += 1
#print(cnt)
cnt.sort(reverse = True)
#print(cnt)
print(sum(cnt[K:]))

Submission status: Accepted

Solution code:
from collections import Counter

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

res = sum(a_[:len(a_)-k])

print(res)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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