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:
# -*- coding: utf-8 -*-
import collections
N, K=map(int,input().split())
A=list(map(int,input().split()))
C=collections.Counter(A)
KEY=list(C.keys())
count=0
if len(KEY)>K:
    D=len(KEY)-K
    L=C.most_common()[::-1]
    for ii in range(D):
        count+=L[ii][1]

print(count)

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
_, K = map(int, input().split())
A = list(map(int, input().split()))
_, cnt = zip(*Counter(A).most_common()[::-1])
print(sum(cnt[:len(cnt) - K]))

Submission status: Accepted

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

Submission status: Accepted

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

from collections import Counter
c = Counter(list(map(int, input().split())))

#Counterからの取り出し
l = []
m = []
for x, y in c.items():
    l += [y]
    m += [x]

L = sorted(l)
M = len(m)

cnt = 0 
if M <= k:
    print(0)
else:
    for i in range(M-k):
        cnt += L[i]
    print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter
a,b=map(int, input().split())
c = Counter(list(map(int, input().split())))
cnt = 0
l = []
m = []

for x,y in c.items():
    l += [y]
    m += [x]

M = len(m)
L = sorted(l)
if M <= b:
    print(0)
if M > b:
    for i in range(M-b):
        cnt += L[i]
    print(cnt)

Submission status: Accepted

Solution code:
# cook your dish here
import operator
N,K=map(int,input().split())
lst=list(map(int,input().split()))
x=[]
x=set(lst)
if len(x)<=K:
    print(0)
else:
    freq={}
    for i in lst:
        if i in freq:
            freq[i]+=1
        else:
            freq[i]=1
    # l=[]
    # l=list(freq.items())
    s={}
    s = dict(sorted(freq.items(), key=operator.itemgetter(1)))
    # print(s)
    # l.sort()
    cc=abs(len(x)-K)
    # print(cc)
    v=0
    for key,value in s.items():
        
        v+=s[key]
        # print(key)
        cc-=1
        if cc==0:
            print(v)
            break

Submission status: Accepted

Solution code:
from collections import Counter as C

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

c = C(a).values()
d = len(c) - k
if 0 < d:
    print(sum(sorted(c)[:d]))
else:
    print(0)

Submission status: Accepted

Solution code:
from collections import Counter, deque

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

ans = 0
d = deque(sorted(Counter(a).values()))
while k < len(d):
    ans += d.popleft()
else:
    print(ans)

Submission status: Accepted

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

d={}

for x in range(n):
  if a[x] not in d:
    d[a[x]]=0
  d[a[x]]+=1
  
ans=0
ans1=[]
for g in d:
  ans1.append(d[g])
  
if len(ans1)<=k:
  print(0)
  
else:
  ans1.sort()
  r=len(ans1)-k
  for t in range(r):
    ans+=ans1[t]
    
  print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split()) 
A = list(map(int,input().split()))
A.sort()
temp = 1
V = [] 
for i in range(N-1):
    if A[i] == A[i+1]:
        temp+=1 
    else:
        V.append(temp)
        temp = 1

V.append(temp)
V.sort() 
ans = N- sum(V[-K:])
print(ans)

Submission status: Accepted

Solution code:
import collections
N,K=map(int,input().split())
L=list(map(int,input().split()))
L=sorted(L)
c = collections.Counter(L)
c=c.most_common()
ans=0
if len(c)>K:
  for i in range(len(c)-K):
    ans+=c[len(c)-i-1][1]
  print(ans)
else:
  print(0)

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt_A = sorted(Counter(A).values())

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

Submission status: Accepted

Solution code:
import collections as c
n,k=map(int,input().split());a=c.Counter(map(int,input().split()))
print([0,sum(sorted([*a.values()])[:len(a)-k])][len(a)>k])

Submission status: Accepted

Solution code:
import collections as c
n,k=map(int,input().split());a=c.Counter(map(int,input().split()))
print([0,sum(sorted([i for i in a.values()])[:len(a)-k])][len(a)>k])

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(a)
i = 0
cnt = 0
for key, val in sorted(c.items(), key=lambda x: -x[1]):
    # print(key, val)
    cnt += val
    i += 1
    if i >= k:
        break
        
print(n - cnt)

Submission status: Accepted

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

Chou_List = collections.Counter(N_List)
if len(Chou_List) <= K:
    print(0)
else:
    Chou_List = sorted(list(Chou_List.values()),reverse=True)
    print(sum(Chou_List[K:]))

Submission status: Accepted

Solution code:
from collections import Counter

n, k = map(int, input().split())
a = [int(an) for an in input().split()]
ans = 0
set_a = set(a)
num_of_types = len(set_a)
if num_of_types > k:
  cnt = Counter(a)
  ans = sum(sorted(cnt.values())[: num_of_types - k])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#
# abc081 c
#

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """5 2
1 1 2 2 5"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4 4
1 1 2 2"""
        output = """0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """10 3
5 1 3 2 4 1 1 2 3 4"""
        output = """3"""
        self.assertIO(input, output)


def resolve():
    N, K = map(int, input().split())
    a = list(map(int, input().split()))
    an = [0] * N
    for i in a:
        an[i-1] += 1
    an.sort(reverse=True)
    ans = 0
    for i in an:
        if K == 0:
            ans += i
        else:
            K -= 1
    print(ans)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

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

	lst = {}

	for num in A:
		if num in lst:
			lst[num] += 1
		else:
			lst[num] = 1

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

	len_lst_sorted = len(lst_sorted)

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

  
if __name__ == "__main__":
  	main()

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

print(sum(B[0:N-K]))

Submission status: Accepted

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]

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

d = [0]*(n+1)

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

d.sort()

print(sum(d[:n-k+1]))

Submission status: Accepted

Solution code:
from collections import Counter
n,k=map(int,input().split())
a=list(map(int,input().split()))
t=list(set(a))
c=sorted(Counter(a).most_common(),key=lambda x:x[1])
num=len(t)-k
if num<0:
    print(0)
    exit()
else:
    ans=0
    for i in range(num):
        ans+=c[i][1]
    print(ans)

Submission status: Accepted

Solution code:
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
from collections import Counter
dic=Counter(a)
dic=sorted(dic.items(),key=lambda x:x[1])

ans=0
cnt=0
l=max(len(dic)-k,0)
for i in dic:
  if cnt==l:
    break
  ans=ans+i[1]
  cnt=cnt+1
print(ans)

Submission status: Accepted

Solution code:
_, k = map(int, input().split())
d = {}
for i in map(int, input().split()):
    if i in d:
        d[i] += 1
    else:
        d[i] = 1
d = sorted(d.values(), reverse=True)
print(sum(v for v in d[k:]))

Submission status: Accepted

Solution code:
import collections

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

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

c.sort()

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

print(ans)

Submission status: Accepted

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



if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(sum)

Submission status: Accepted

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

import collections

cA = collections.Counter(A)

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


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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
a=[0]*200000
b=[]
for i in A:a[i-1]+=1
for i in a:
    if i:b.append(i)
b.sort()
ans=0
for i in range(len(b)-K):ans+=b[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

cnt = Counter(a)

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

Submission status: Accepted

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

Submission status: Accepted

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

print(sum)

Submission status: Accepted

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

print(sum)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter
read=sys.stdin.read

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
r = [0 for _ in range(n)]
for i in a:
    r[i-1] += 1
t = []
for i in r:
    if i:
        t.append(i)
t.sort()
print(sum(t[:len(t)-k]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import sys
from collections import deque
from collections import defaultdict
from collections import Counter
import heapq

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


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


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


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


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


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

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter

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


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

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

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

    print(ans)

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import collections

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

cnt = 0

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

print(cnt)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
num=[0 for i in range(N)]
for i in range(N):
    num[A[i]-1]+=1

num.sort()
sum=0
for i in range(N-K):
    sum+=num[i]

print(sum)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

c_value= list(counter.values())
nl = len(c_value)
if nl <= k:
  print(0)
  sys.exit()
  
c_value.sort()
#print(c_value)
do_num = nl - k
for i in range(do_num):
  cnt += c_value[i]
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(ans, flush=True)

Submission status: Accepted

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

print(c)

Submission status: Accepted

Solution code:
import collections

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

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

Submission status: Accepted

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

a_dic = {}

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

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

print(cn)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#n=int(input())
n,K=map(int,input().split())
al=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from  collections import Counter

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

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

Submission status: Accepted

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

import collections

c = collections.Counter(A)

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

lis.sort()

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

Submission status: Accepted

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

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

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

    print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Ad = {x:0 for x in set(A)}
if len(set(A)) <= K:
    print(0)
else:
    count = 0
    for a in A:
        Ad[a] += 1
    Ad = sorted(Ad.items(), key=lambda x:x[1])
    for i in range(len(set(A))-K):
        count += Ad[i][1]
    print(count)

Submission status: Accepted

Solution code:
from collections import Counter
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
cou=Counter(a).most_common()
rem=len(cou)-k
i=-1
ans=0
while rem>0:
    ans+=cou[i][1]
    i-=1
    rem-=1;
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

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

b = collections.Counter(a)
b = list(b.values())
b.sort()
ans = 0
for i in range(len(set(a))-k):
    ans += b[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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