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:
from collections import Counter
N,K = map(int,input().split())
A = list(map(int,input().split()))
count = Counter(A)
if K >= len(count):
    print(0)
else:
    values = sorted(list(count.values()),reverse = True)
    del values[:K]
    print(sum(values))

Submission status: Accepted

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

c = Counter(A)
kv = c.most_common()

count = 0
ans = 0
if len(kv) <= K:
    print(0)
    sys.exit(0)

for l in kv[::-1]:
    count += 1
    ans += l[1]
    if count == (len(kv) - K):
        break

print(ans)

Submission status: Accepted

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

from collections import Counter
cnt=0

a=Counter(A)
b=list(a.values())
b.sort()
leng=len(b)

for item in b:
  if leng>K:
    cnt+=item
    leng-=1
    
print(cnt)

Submission status: Accepted

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

import sys
import collections
c = collections.Counter(t)
c=sorted(list(c.values()))
ans=0
delete_count=len(c)-k
if delete_count<=0:
    print(0)
    sys.exit()
else:
    for i in range(delete_count):
        if len(c)>k:
            ans+=c[i]
            
print(ans)
sys.exit()

Submission status: Accepted

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

a=collections.Counter(A).most_common()
print(sum([a[-i-1][1] for i in range(len(a)-K)]))

Submission status: Accepted

Solution code:
import sys
from collections import Counter
n, k = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
ct = Counter(a_ls)
variety = len(ct.keys())

cnt = 0
ans = 0
for key, v in sorted(ct.items(), key=lambda x:x[1]):
    if cnt >= variety - k:
        break
    ans += v
    cnt += 1
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=[0 for i in range(n)]
z=set(a)
y=len(z)
for x in a:
    s[x-1]+=1
b=sorted(s)
f=0
ans=0
for x in b:
    if x!=0:
        f+=1
        ans+=x
    if f>=y-k:
      	break
print(ans)

Submission status: Accepted

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

from collections import Counter
s=Counter(a)
l=s.most_common(k)

ans=0
for (i,j) in l:
  ans+=j
  
print(n-ans)

Submission status: Accepted

Solution code:
import collections
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = collections.Counter(a)
cnt = 0
c = b.most_common()
s = 0
for i in range(len(c)):
	cnt += 1
	if cnt > k:
		for j in range(i,len(c)):
			s += c[j][1]
		break
print (s)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter as c
N, K = map(int, input().split())
A = tuple(map(int, input().split()))
s = dict(c(A))
if len(s) > K:
	s_s = sorted(s.items(), key=lambda x: x[1])
	l = len(s)
	i = 0
	cnt = 0
	while l > K:
		l -= 1
		cnt += s_s[i][1]
		i += 1
	print(cnt)
else:
	print(0)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict as dd
input = sys.stdin.readline
N, K = map(int, input().split())
a = list(map(int, input().split()))
d = dd(int)
for i in range(N):
  d[a[i]] += 1
ks = list(d.keys())
l = []
for k in ks:
  l.append(d[k])
l.sort(reverse = True)
res = 0
for _ in range(len(ks) - K): res += l.pop()
print(res)

Submission status: Accepted

Solution code:
from collections import *
_,k,*a = map(int,open(0).read().split())
print(sum(sorted(Counter(a).values())[:-k]))

Submission status: Accepted

Solution code:
n,k,*a=map(int,open(0).read().split())
d={}
for i in a:
    d[i]=d.get(i,0)+1
b=list(d.values())
b.sort()
print(sum(b[:-k]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
import heapq

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

c = collections.Counter(A)
value_list = list(c.values())
heapq.heapify(value_list)
# print(value_list)

ans = 0
for i in range(len(value_list) - K):
    ans += heapq.heappop(value_list)

print(ans)

Submission status: Accepted

Solution code:
import collections
N,K = map(int,input().split())
A = list(map(int,input().split()))
c = collections.Counter(A).most_common()[::-1]
k = len(c)
if k<=K:print(0)
else: print(sum([e[1] for e in c[:k-K]]))

Submission status: Accepted

Solution code:
import collections as cl
N, K, *A = map(int, open(0).read().split())
c = sorted(cl.Counter(A).values())
print(sum(c[:-K]))

Submission status: Accepted

Solution code:
import collections

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

print(sum([x[1] for x in collections.Counter(a).most_common()[k:]]))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import collections


def solve(N: int, K: int, A):
    c = collections.Counter(A).most_common()
    z = 0
    m = len(c)
    for i in range(m - K):
        z += c[m - i -1][1]
    print(z)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(int)

for key in a_li:
    d[key] += 1
d = sorted(d.items(), key=lambda x:x[1])
    
s = len(d) - k
ans = 0

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=set(a)
a.sort()
if k>=len(set(a)):
    print(0)
else:
    cnt=[a[0]]
    ans=[]
    for i in range(1,n):
        if a[i]!=a[i-1]:
            ans.append(cnt)
            cnt=[]
            cnt.append(a[i])
        else:
            cnt.append(a[i])
        if i==n-1 and cnt!=[]:
            ans.append(cnt)
    for j in range(len(ans)):
        ans[j]=len(ans[j])
    ans.sort()
    answ=0
    for i in range(len(set(s))-k):
        answ+=ans[i]
    print(answ)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n, k = map(int, input().split())
    aa = list(map(int, input().split()))
    ac = Counter(aa)
    now = len(ac)
    del_n = now - k
    ans = 0
    for v in sorted(ac.values()):
        if del_n <= 0:
            break
        ans += v
        del_n -= 1
    print(ans)

main()

Submission status: Accepted

Solution code:
from collections import Counter


def main(n: int, k: int, a: list):
    calc = Counter(a)
    calc = sorted(calc.values())

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


if __name__ == '__main__':
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    main(n, k, a)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

s = len(set(a))
m = s - k
if m <= 0:
    print(0)
    exit()
    
a.sort()
l = [0] * (a[-1] + 1)
for i in a:
    l[i] += 1

l.sort()
ans = 0
for x in l:
    if x == 0:
        pass
    else:
        ans += x
        m -=1
        if m == 0:
            print(ans)
            exit()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

n, k = map(int, input().split())
d = defaultdict(int)
for key in input().split():
  d[key] += 1
  
l = len(d)
print(sum(sorted(d.values())[:l-k]))

Submission status: Accepted

Solution code:
import math
import heapq
from collections import Counter

#N,K=map(int, input[].split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
#x=list(map(int, input[].split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき

N,K=map(int, input().split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
A=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき

S=Counter(A)
s= S.most_common(K)

print(N-[sum(x) for x in zip(*s)][1])

Submission status: Accepted

Solution code:
import heapq

N,K=map(int,input().split())
A=list(map(int,input().split()))
l=[0]*max(A)
ans=0
for item in A:
    l[item-1]+=1
    
heapq.heapify(l)

while len(l)>K:
    ans+=heapq.heappop(l)
    
print(ans)

Submission status: Accepted

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

b = list()
cn = 1

for i in range(1,n+1):
    if(a[i]==a[i-1]):
        cn += 1
    else:
        b.append((a[i-1],cn))
        cn = 1
        
sn = 0        
    
b.sort(key = lambda x : (x[1],x[0]),reverse = True)    
    
if(len(b)<=k):
    print(0)
else:
    for i in range(len(b)-1,k-1,-1):
        sn += b[i][1]
    print(sn)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())	
a = list(map(int,input().split()))
cnt={}
for i in a:
    if i not in cnt:
        cnt[i]=1
    else:
        cnt[i]+=1
num=[]
for i in cnt:
    num.append(cnt[i])
num.sort()
num_l=len(num)
ans=i=0
while num_l > k:
    ans+=num[i]
    i+=1
    num_l-=1
print(ans)

Submission status: Accepted

Solution code:
def slove():
    import sys
    import collections
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    a = list(map(int, input().rstrip('\n').split()))
    a = collections.Counter(a).most_common()[:k]
    cnt = 0
    for k, v in a:
        cnt += v
    print(n - cnt)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
import heapq

N,K=map(int,input().split())
A=list(map(int,input().split()))
l=[0]*max(A)
ans=0
for item in A:
    l[item-1]+=1
    
heapq.heapify(l)

while len(l)>K:
    ans+=heapq.heappop(l)
    
print(ans)

Submission status: Accepted

Solution code:
import collections as c

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

A=c.Counter(A)
A=list(A.items())
A.sort(key=lambda x: -x[1])
#print(A)
#print([A[i][1] for i in range(K)])

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

Submission status: Accepted

Solution code:
import collections
n,k=map(int,input().split())
a=list(map(int,input().split()))
print(sum([x[1] for x in collections.Counter(a).most_common()[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 a in A:
    d[a] += 1

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

diff = L - K
ans = 0
for k, v in sorted([x for x in d.items()], key=lambda x: x[1])[:diff]:
    ans += v

print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n,k = LI()
    a = LI()
    d = defaultdict(lambda:0)
    for i in a:
        d[i] += 1
    d = list(d.values())
    d.sort()
    ans = 0
    for i in range(len(d)-k):
        ans += d[i]
    print(ans)
    return

#B
def B():

    return

#C
def C():

    return

#D
def D():

    return

#E
def E():

    return

#Solve
if __name__ == "__main__":
    A()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    a = list(map(int, input().rstrip('\n').split()))
    import collections
    a = collections.Counter(a).most_common()
    if len(a) < k:
        print(0)
    else:
        cnt = 0
        for i in range(k, len(a)):
            cnt += a[i][1]
        print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import os,re,sys,operator,math
from collections import Counter,deque
from operator import itemgetter
from itertools import accumulate,combinations,groupby
from sys import stdin,setrecursionlimit
from copy import deepcopy
import heapq

setrecursionlimit(10**6)
n,k=map(int,stdin.readline().rstrip().split())
a=[int(i) for i in stdin.readline().rstrip().split()]
t=deque(Counter(a).most_common())
ans=0
while len(t)>k:
    now=t.pop()
    ans+=now[1]
print(ans)

Submission status: Accepted

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

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

h = {}
for i in a:
    if i in h:
        h[i] += 1
    else:
        h[i] = 1
h = sorted(h.items(), key=lambda x:x[1])

x = 0
for i in range(len(h)-k):
    x += h[i][1]
print(x)

Submission status: Accepted

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

counts = {}
for a in A:
    if a not in counts:
        counts[a] = 1
    else:
        counts[a] += 1
        
count_pairs = []
for key,val in counts.items():
    count_pairs.append((val, key))
    
    
count_pairs.sort()
count_pairs.reverse()
if len(count_pairs) <= K:
    print(0)
else:
    non_change_num = 0
    for i in range(K):
        val, _ = count_pairs[i]
        non_change_num += val
    print(N - non_change_num)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(a)
vs = []
for v in c.values():
  vs.append(v)
vs.sort(reverse=True)

i = 0
if len(vs) <= k:
  print(i)
  exit()
else:
  while len(vs) > k:
    i += vs[-1]
    vs.pop()
  else:
    print(i)
    exit()

Submission status: Accepted

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

dic = defaultdict(int)
for a in A:
    dic[a] += 1

key_n = len(dic)

ans = 0
if key_n > K:
    dic2 = sorted(dic.items(), key=lambda x:x[1])
    for i in range(key_n - K):
        ans += dic2[i][1]
print(ans)

Submission status: Accepted

Solution code:
import collections

N, k = map(int, input().split())
S = list(map(int, input().split()))
S_unique_count = list(collections.Counter(S).values())

S_unique_count.sort()

ans = sum(S_unique_count[0:len(S_unique_count)-k])
print(ans)

Submission status: Accepted

Solution code:
import collections
N,K=map(int,input().split())
A=list(map(int,input().split()))
c=collections.Counter(A)
C=sorted(list(c.items()),key=lambda x: x[1])
t=len(C)
ans=0
for i in range(len(C)):
  if t>K:
    ans+=C[i][1]
    t-=1
  else:
    break
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n,k = map(int,input().split())
nums = input().split()
count_per_nums = defaultdict(int)
for n in nums:
  count_per_nums[n] += 1
count_per_nums = sorted(count_per_nums.items(), key=lambda x:x[1])
ans = 0
for i in range(len(count_per_nums)-k):
  ans += count_per_nums[i][1]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC 86, C - not so diverse
from collections import Counter

n, k = map(int, input().split(' '))
A = [int(el) for el in input().split(' ')]

c = Counter(A)
if len(c) < k:
    print(0)
else:
    total = 0
    for a, b in c.most_common()[k:]:
        total += b
    print(total)

Submission status: Accepted

Solution code:
# ABC081C - Not so Diverse (ARC086C)
from collections import Counter


def main():
    n, k = map(int, input().rstrip().split())
    A = sorted(Counter(input().split()).values(), reverse=True)
    print(sum(A[k:]))


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()]
a.sort()
a.append("9")
cnt=1
ans=[]
for i in range(0,n):
    if a[i]==a[i+1]:
        cnt+=1
    else:
        ans.append(cnt)
        cnt=1
ans.sort()
sum=0
for i in range(len(ans)-k):
    sum+=ans[i]
print(sum)

Submission status: Accepted

Solution code:
from collections import Counter
n,k=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
count=Counter(a)
value=sorted(list(count.values()))[::-1]
print(sum(value[k:]))

Submission status: Accepted

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

d = {}
ans = 0

for a in A:
    if a not in d.keys():
        d[a] = 1
    else:
        d[a] += 1

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

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

print(ans)

Submission status: Accepted

Solution code:
# 少ないやつを変えたほうが少なくてすむので嬉しい
n, k = map(int, input().split())
arr = list(map(int, input().split()))
hash = dict()
for ai in arr:
    if ai in hash:
        hash[ai] += 1
    else:
        hash[ai] = 1
hash = list(sorted(hash.items(), key=lambda x: x[1]))
ans = 0
for i in range(len(hash) - k):
    ans += hash[i][1]
print(ans)

Submission status: Accepted

Solution code:
# ARC086C - Not so Diverse (ABC081C)
from collections import Counter


def main():
    _, k, *a = map(int, open(0).read().split())
    print(sum(sorted(Counter(a).values(), reverse=True)[k:]))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC086C - Not so Diverse (ABC081C)
from collections import Counter


def main():
    n, k = list(map(int, input().rstrip().split()))
    lst = list(map(int, input().rstrip().split()))
    cnt = sorted(Counter(lst).values(), reverse=True)
    print(sum(cnt[k:]))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import collections
a,b =map(int,input().split())
c = input().split()
t = collections.Counter(c)
values, counts = zip(*t.most_common()[::-1])
print(sum(counts[0:len(counts)-b]))

Submission status: Accepted

Solution code:
import collections
n, k = map(int, input().split())
dat_a = list(map(int,input().split()))
d = collections.Counter(dat_a)
r_num =  len(d)- k
res = 0
r = d.most_common()[::-1]
for i in range(r_num):
    res += r[i][1]
print(res)

Submission status: Accepted

Solution code:
from collections import Counter

n, k = map(int, input().split())
A = sorted(Counter(list(map(int, input().split()))).most_common(), key=lambda x:x[1])
cnt = 0
if len(A) > k:
    for i in range(len(A)-k):
        cnt += A[i][1]
print(cnt)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
al=list(map(int,input().split()))
al.sort()
tl=[]
pv=al[0]
cnt=0
for i in al:
    if i==pv:
        cnt+=1
    else:
        tl.append(cnt)
        pv=i
        cnt=1
tl.append(cnt)
tl.sort()
if len(tl)<=k:
    print(0)
else:
    print(sum(tl[:len(tl)-k]))

Submission status: Accepted

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

n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
set_a = set(a)
count_a = collections.Counter(a).most_common()[::-1]
if len(set_a)-k > 0:
    for i in range(len(set_a)-k):
        ans += count_a[i][1]
print(ans)

Submission status: Accepted

Solution code:
from collections import *
_,k,*a = map(int,open(0).read().split())
print(sum(sorted(Counter(a).values(),reverse=True)[k:]))

Submission status: Accepted

Solution code:
from collections import Counter

n, k = list(map(int, input().rstrip().split()))
lst = list(map(int, input().rstrip().split()))
cnt = sorted(Counter(lst).values(), reverse=True)
print(sum(cnt[k:]))

Submission status: Accepted

Solution code:
# ARC086C - Not so Diverse (ABC081C)
n, k = list(map(int, input().rstrip().split()))
lst = sorted(list(map(int, input().rstrip().split())))
dic = {}
for i in lst:
    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

cnt = sorted([i for i in dic.values()], reverse=True)
print(sum(cnt[k:]))

Submission status: Accepted

Solution code:
# ARC086C - Not so Diverse (ABC081C)
n, k = list(map(int, input().rstrip().split()))
lst = sorted(list(map(int, input().rstrip().split())))
dic = {}
for i in lst:
    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

cnt = sorted([i for i in dic.values()], reverse=True)
print(n - sum(cnt[:k]))

Submission status: Accepted

Solution code:
# ARC086C - Not so Diverse
n, k = list(map(int, input().rstrip().split()))
lst = sorted(list(map(int, input().rstrip().split())))
dic = {}
for i in lst:
    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

cnt = sorted([i for i in dic.values()], reverse=True)
ans = 0
for i in range(len(cnt)):
    if k > 0:
        k -= 1
        continue
    ans += cnt[i]
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
d={}
for x in a:
     if x in d:
          d[x]+=1
     else:
          d[x]=1
l=list(d.values())
l.sort()
if k >= len(l):
     print(0)
else:
     print(sum(l[:-k]))

Submission status: Accepted

Solution code:
from collections import Counter

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

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
balls = sorted(input().split())
kinds = len(set(balls))
amo = [1] * kinds

balls.append(0)
j = 0
for i in range(n):
    if balls[i] == balls[i+1]:
        amo[j] += 1
    else:
        j += 1
amo.sort()
print(sum(amo[:max(0, kinds-k)]))

Submission status: Accepted

Solution code:
# N, K <=200000
# As < N
from collections import Counter
N, K = [int(x) for x in input().split()]
As = [int(x) for x in input().split()]

counter = Counter()
for a in As:
    counter[a]+=1

B = sorted([(v, k) for k, v in counter.items()])

print(sum(v for v, k in B[:max(0, len(counter)-K)]))

Submission status: Accepted

Solution code:
import math
import fractions
import copy
#以下てんぷら
def j(q):
    if q==1: print("YES")
    elif q == 0:print("NO")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def printrow(a):
    for i in range(len(a)):
        print(a[i])
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


#n = ip()                                     #入力整数1つ
n,k= (int(i) for i in input().split())       #入力整数横2つ以上
a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#a=[input() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#全足しに4 3sum変数使用はsum関数使用できないので注意
a.sort()
cnum = a[0]
ct =1
st = []
for i in range(1,n):
    if a[i] != cnum:
        cnum = a[i]
        st.append(ct)
        ct = 1
    else:ct+=1
st.append(ct)
st.sort(reverse=True)
total = 0
for i in range(min(k,len(st))):
    total+=st[i]
print(n-total)

Submission status: Accepted

Solution code:
import collections

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

A_cou = collections.Counter(A)
B = A_cou.most_common()
l = len(B)
count = 0

if K >= l:
    print(0)
else:
    for i in range(l - K):
        count += B[-i-1][1]
    print(count)

Submission status: Accepted

Solution code:
from collections import Counter
N,K = (int(i) for i in input().split())
d = Counter([int(i) for i in input().split()])
if len(d) <= K:
    print(0)
    exit()

tmp = 0
for _,p in d.most_common(K):
    tmp += p
print(N-tmp)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a_list=[int(i) for i in input().split()]
count={}
for a in a_list:
    count[a]=count.get(a,0)+1
val=list(count.values())
val.sort(reverse=True)
print(sum(val[k:]))

Submission status: Accepted

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

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

item = cnt.items()
item_srt = sorted(item, key=lambda x: x[1])
l = len(item_srt)
for j in item_srt:
    if l <= k:
        break
    ans += j[1]
    l -= 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections


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


main()

Submission status: Accepted

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

li = {}
for i in A:
    if(not i in li):
        li[i]=1
    else:
        li[i]+=1

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

Submission status: Accepted

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

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

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

if k >= len(d):
    ans = 0
else:
    ans = sum([ v for k, v in d[:len(d)-k]])

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
arr=list(map(int,input().split()))
cnt=[0]*(n+1)
cnt2=[]
for i in range(n):
  cnt[arr[i]]+=1
for i in range(1,n+1):
  if cnt[i]!=0:
    cnt2.append(cnt[i])
cnt2=sorted(cnt2)
if len(cnt2)<=k:
  print(0)
else:
  print(sum(cnt2[:len(cnt2)-k]))

Submission status: Accepted

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

count = {}
for i in A:
    if i not in count:
        count[i] = 0
    count[i] += 1

cs = sorted(count.items(), key=lambda x: x[1])
ans = 0
for i in range(len(cs) - K):
    ans += cs[i][1]
print(ans)

Submission status: Accepted

Solution code:
import collections
N, K = map(int, input().split())
A = list(map(int, input().split()))
c = collections.Counter(A)
a = list(c.values())
if len(a) <= K:
  print(0)
else:
  a.sort(reverse=True)
  print(N-sum(a[:K]))

Submission status: Accepted

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

from collections import Counter
b = Counter(a)
counter = []
for key in b.keys():
    counter.append(b[key])
counter.sort()
print(sum(counter[:len(counter) - nk[1]]))

Submission status: Accepted

Solution code:
from collections import Counter

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

print(sum(sorted(Counter(A).values(), reverse=True)[K:]))

Submission status: Accepted

Solution code:
from collections import *
N,K=map(int,input().split())
print(sum(v for _,v in Counter(map(int,input().split())).most_common()[K:]))

Submission status: Accepted

Solution code:
from collections import Counter

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

print(sum(v for _, v in Counter(A).most_common()[K:]))

Submission status: Accepted

Solution code:
import collections as col

N, K = map(int,input().split())
A = list(map(int,input().split()))
L = col.Counter(A).most_common()
ans = 0

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

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

print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A=input().split()
from collections import defaultdict
d = defaultdict(int)
for i in A:
    d[i]+=1
var = len(d.keys())
l = list(d.values())
l.sort()
print(sum(l[:max(var-K,0)]))

Submission status: Accepted

Solution code:
import collections

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

B = sorted(list(collections.Counter(A).values()),reverse=True)

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