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:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n,k = f()
    a = f()

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

    print(n - sum([i[1] for i in items[:k]]))

solve()

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

length = len(a)
ans = 0

for i in range(length-k):
    ans += a[-1][1]
    a.pop(-1)
    
print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect
import math
input = sys.stdin.readline

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

m = {}
for v in l:
    if m.get(v) != None:
        m[v] += 1
    else:
        m[v] = 1
        
m = list(m.values())
m.sort()
t = len(m) - k
if t < 0:
    t = 0
print(sum(m[:t]))

Submission status: Accepted

Solution code:
from collections import Counter

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

key, val = zip(*Counter(A).most_common())
k = len(val)

for i in range(len(val)):
    if k <= K:
        break
    count += val[len(val)-i-1]
    k -= 1

print(count)

Submission status: Accepted

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

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

cnt = list(num.values())
cnt.sort()
print(sum(cnt[:len(cnt)-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).most_common()

ans = 0

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

print(ans)

Submission status: Accepted

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

counter = [0]*(N+1)
integers = 0
for a in A:
    if counter[a] == 0:
        integers += 1
    counter[a] += 1
counter = sorted(counter)
ans = 0
for c in counter:
    if c > 0:
        ans += c
        integers -= 1
    if integers <= K:
        break
print(ans)

Submission status: Accepted

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math



N,K = map(int, sys.stdin.readline().rstrip().split())
An = list(map(int, sys.stdin.readline().rstrip().split()))

amap = {}
for a in An:
  if a not in amap:
    amap[a] = 1
  else:
    amap[a] += 1

nl = []
for i in list(amap.values()):
  nl.append(i)
nl = sorted(nl)

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

Submission status: Accepted

Solution code:
from collections import defaultdict

N,K = map(int, input().split())
a = list(map(int, input().split()))
cnt_d = defaultdict(lambda: 0)
for ai in a:
    cnt_d[ai] += 1

cnt_v = sorted([v for k,v in cnt_d.items()])
print(sum(cnt_v[:-K]))

Submission status: Accepted

Solution code:
from collections import defaultdict
from operator import itemgetter

N,K = map(int, input().split())
a = list(map(int, input().split()))
cnt_d = defaultdict(lambda: 0)
for ai in a:
    cnt_d[ai] += 1

cnt_l = sorted(cnt_d.items(), key=itemgetter(1))
ans = sum([v for k,v in cnt_l[:-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)
tot=0
for i in c.most_common(min(k,len(set(A)))):
  tot+=i[1]
print(n-tot)

Submission status: Accepted

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

o = []
v = A[0]
z = 0
for i in range(1,N):
    if v != A[i]:
        o.append(i-z)
        v = A[i]
        z = i
o.append(N-z)
o.sort()
print(sum(o[:-K]))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=list(map(int,input().split()))
d={}

c=0
for i in l:
  if i in d:
    d[i]+=1
  else:
    d[i]=1
l1=d.values()
if m>=len(l1):
  print(0)
else:
  l1=sorted(l1)
  for i in range(len(l1)-m):
    c+=l1[i]
  print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
d=dict()
for i in map(int,input().split()):
  if i not in d:
    d[i]=1
  else:
    d[i]+=1
m=list(d.values())
m.sort()
a=len(m)-k
ans=0
for i in m:
  if a<=0:
    break
  a-=1
  ans+=i
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
from sys import stdin
N, K = [int(_) for _ in stdin.readline().rstrip().split()]
A = Counter([int(_) for _ in stdin.readline().rstrip().split()])
print(sum((i[1] for i in A.most_common()[K:])))

Submission status: Accepted

Solution code:
n, k = [int(i) for i in input().split(" ")]
a = [int(i) for i in input().split(" ")]
c = [0] * (n + 1)

for i in a:
    c[i] += 1
    
c.sort(reverse=True)
print(sum(c[k:]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

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

dict = collections.Counter(a_n)

v_list = list(dict.values())
v_list_s = sorted(v_list)

cnt = 0
if len(v_list_s) <= K:
    print(0)
else:
    x = len(v_list_s) - K
    for i in range(x):
        cnt += v_list_s[i]
    print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

counts = defaultdict(int)
for a in As:
    counts[a] += 1

counts = sorted(counts.values())

if len(counts) <= K:
    print(0)
else:
    total = 0
    for i in range(len(counts) - K):
        total += counts[i]
    print(total)

Submission status: Accepted

Solution code:
import bisect

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

data = []
i = 0

while i < len(A):
    x = A[i]
    a = bisect.bisect_left(A, x)
    b = bisect.bisect_right(A, x)
    data.append([x, b - a])
    i = b
    
data = sorted(data, key = lambda x: x[1])
m = len(data) - K
ans = 0

for i in range(0, m):
    ans += data[i][1]
    
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
B = A.copy()
B = list(set(B))
n = len(B)

def counter(A):
 counter = {}
 for a in A:
  if(a in counter):
   counter[a] += 1
  else:
   counter[a] = 1
 return counter

ans = 0
if(n > K):
  dict = counter(A)
  count = 1
  #value降順ソート
  for k, v in sorted(dict.items(), key=lambda x: -x[1]):
    if(count > K):
     ans += v
    count += 1
    
print(ans)

Submission status: Accepted

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

dic = {}                
for a in A:
    dic[a] = dic.get(a,0) + 1
    
C = []
for k in dic:
    C.append([k,dic[k]])
    
C.sort(key = lambda x: x[1])

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = [int(_) for _ in input().split()]
num = len(set(A))
cnt = [0]*(N+1)
ans = 0

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

for i in range(N+1):
    if cnt[i] == 0:
        cnt[i] = N+1
        
cnt = sorted(cnt)
for i in range(max(0, num-K)):
    ans += cnt[i]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

if __name__ == '__main__':
    n, k = map(int, input().split())
    a = sorted(Counter(int(s) for s in input().split()).values())
    print(sum(a[:len(a) - k]))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
import collections
a=collections.Counter(A)
a=sorted(a.items(),key=lambda x:x[1])
k=len(a)
ans=0
i=0
while k>K:
        ans+=a[i][1]
        i+=1
        k-=1
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, K] = [int(input_raw[0]), int(input_raw[1])]
    input_raw = read_func().strip().split()
    A = {}
    for i in range(N):
        if int(input_raw[i]) not in A:
            A[int(input_raw[i])] = 1
        else:
            A[int(input_raw[i])] += 1
    A_val = A.values()

    A_val.sort(reverse = True)

    count = 0L
    for i in range(K, len(A_val)):
        count += A_val[i]
    print count


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

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

C = Counter(A)
v = list(C.values())
v = sorted(v)

div = len(v)
ans = 0
if div <= K:
    ans = 0
else:
    ans += sum(v[0:div-K])
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

from collections import Counter, defaultdict

II = lambda: int(input())
MI = lambda: map(int, input().split())


def main():
    N, K = MI()
    C = Counter(MI())
    return N - sum((c for _, c in C.most_common(K)))


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

Submission status: Accepted

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

ans = 0
for i,(k,v) in enumerate(ctr.most_common()):
    if i < K: continue
    ans += v
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    from collections import Counter

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

    for index, (key, ai) in enumerate(sorted(Counter(a).items(), key=lambda x: x[1], reverse=True), 1):
        if index > k:
            ans += ai

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    from collections import Counter

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

    for index, (key, value) in enumerate(sorted(Counter(a).items(), key=lambda x: x[1], reverse=True), 1):
        if index > k:
            ans += value

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

N, K = map(int, raw_input().split())
A = map(int, raw_input().split())

num_A = collections.Counter(A)
res = 0
for i, (k, v) in enumerate(sorted(num_A.items(), key=lambda x: x[1])):
  if len(num_A) - K < i + 1:
    break
  res += v
print res

Submission status: Accepted

Solution code:
import collections


N, K = map(int, input().split(' '))
A = input().split(' ')
counter_A = collections.Counter(A)
diversity = len(counter_A) - K
ans = 0
if diversity <= 0:
    print(ans)
    exit()
for item in counter_A.most_common()[-1*diversity:]:
    ans += item[1]
print(ans)

Submission status: Accepted

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

count = {}

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

v = list(sorted(count.values(), key=lambda x: -x))

ret = 0
for i in range(K, len(v)):
    ret += v[i]
print(ret)

Submission status: Accepted

Solution code:
import collections 
def inpl(): return [int(i) for i in input().split()]

N, K = inpl()
A = list(sorted(collections.Counter(inpl()).values()))
if len(A) <= K:
    print(0)
else:
    print(sum(A[:len(A)-K]))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
lis = list(map(int,input().split()))
li = [0 for i in range(n)]
for i in range(n):li[lis[i]-1] += 1
li.sort(reverse = True)
ans = 0
for k in range(m):
    ans += li[k]
print(n-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_list = list(c.values())
values_list.sort()
ans = 0
for i in range(len(values_list)-K):
    ans += values_list[i]
print(ans)

Submission status: Accepted

Solution code:
import bisect
N, K = map(int, input().split())
a=list(map(int, input().split()))
nlis=[0]*200001
s=0
for i in range(N):
    if nlis[a[i]]==0:
        s+=1
    nlis[a[i]]+=1
nlis.sort(reverse=True)

print(sum(nlis[K:]))

Submission status: Accepted

Solution code:
import bisect
N, K = map(int, input().split())
a=list(map(int, input().split()))
nlis=[0]*200001
s=0
for i in range(N):
    if nlis[a[i]]==0:
        s+=1
    nlis[a[i]]+=1
nlis.sort()
l=bisect.bisect_left(nlis, 1)

print(sum(nlis[l:l+s-K]))

Submission status: Accepted

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

D = {}

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

list = list(D.items())

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

count = 0

l = len(list) - K
i = 0
while (l >= 1):
    count += list[i][1]
    i += 1
    l -= 1

print(count)

Submission status: Accepted

Solution code:
from collections import Counter

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

ctr = Counter(xs)

cnts = sorted(ctr.values())

ans = n - sum(cnts[-k:])

print(ans)

Submission status: Accepted

Solution code:
import collections
n,k = map(int,input().split())
a = list(map(int,input().split()))
d = collections.Counter(a)
v = list(d.values())
v.sort()
v.reverse()
if len(v) <= k:
  print(0)
else:
  print(n-sum(v[:k]))

Submission status: Accepted

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

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

L.append(c)

L = sorted(L)

n = len(L)

ans = 0

if n <= K:
	print(0)
else:
	for i in range(n - K):
		ans += L[i]
	print(ans)

Submission status: Accepted

Solution code:
import collections

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

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
list_a = list(map(int, input().split()))
dict_a = {}
for i in range(n):
    try:
        dict_a[list_a[i]] += 1
    except KeyError:
        dict_a[list_a[i]] = 1
list_val = []
for value in dict_a.values():
    list_val.append(value)
list_val.sort()
list_val.reverse()
ans = 0
for i in range(k,len(list_val)):
    ans += list_val[i]
print(ans)

Submission status: Accepted

Solution code:
# cook your dish here
n,kk = map(int,input().split())	
a = list(map(int,input().split()))
a.sort()

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

ans.sort()
ans = ans[::-1]

if len(ans)<=kk:

    print(0)
else:
    ass = 0
    for i in range(kk,len(ans)):
        ass += ans[i]
    print(ass)

Submission status: Accepted

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

count_dict = {}
for i in range(len(A)):
	if A[i] in count_dict.keys():
		count_dict[A[i]] += 1
	else:
		count_dict[A[i]] = 1

if len(count_dict.keys()) <= K:
	print(0)
else:
	need = len(count_dict.keys()) - K
	temp = sorted(count_dict.values())
	res = 0
	for i in range(need):
		res += temp[i]
	print(res)

Submission status: Accepted

Solution code:
'''input
5 2
1 1 2 2 5
'''
n,k = map(int,raw_input().split())
arr = map(int,raw_input().split())
ind = [0]*(n+1)
for i in range(n):
	ind[arr[i]] += 1
ind.sort()
aa = 0
for i in range(n+1):
	if ind[i] > 0:
		aa += 1
if aa <= k:
	print "0"
else:
	ans = 0
	for i in range(n):
		if aa == k:
			break
		if ind[i] > 0:
			ans += ind[i]
			aa -= 1
	print ans

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n,k = [int(x) for x in input().split()]

d = {}
for x in input().split():
    x = int(x)
    if x not in d:
        d[x] = 0
    d[x] += 1

cnt = 0
ret = 0
for kk,v in sorted(d.items(), key=lambda x:-x[1]):
    cnt += 1
    if cnt>k:
        ret += v
print(ret)

Submission status: Accepted

Solution code:
from collections import Counter
import sys

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

if k>=len(c):
    print(0)
    sys.exit()
d = c.values()
print(sum(sorted(d)[:len(c)-k]))

Submission status: Accepted

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

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

b = {}

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

c = []
for i in b:
    c.append(b[i])
c.sort()
c.reverse()

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

Submission status: Accepted

Solution code:
from collections import Counter

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

ACounter=Counter(A)
Acommon=ACounter.most_common()

count=len(Acommon)
answer=0
index=-1

while(count>K):
  answer+=Acommon[index][1]
  count-=1
  index-=1
print(answer)

Submission status: Accepted

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

N, K = reads()
A = reads()

freq = defaultdict(lambda: 0)
for a in A:
  freq[a] += 1
ans = N - sum(sorted(freq.values())[-K:])
print(ans)

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
from collections import defaultdict
d = defaultdict(int)
for ele in a:
  d[ele] += 1
print(sum(sorted(d.values(), reverse=True)[k:]))

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

Solution code:
from collections import defaultdict
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
dd = defaultdict(int)
for s in A:
    dd[s]+=1
i=0
ans = 0
for k,v in sorted(dd.items(), key=lambda x: -x[1]):
    i+=1
    ans +=v
    if i==K:
        break
print(N-ans)

Submission status: Accepted

Solution code:
from collections import Counter
from operator import itemgetter

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

c = dict(Counter(a))
if len(c) <= k:
	print(0)
else:
	hindo = sorted(c.items(), key=itemgetter(1))
	ans = 0
	for i in range(len(hindo) - k):
		ans += hindo[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))
ans = 0
dic = {}
for i in range(n):
    dic[a[i]] = 0
for i in range(n):
    dic[a[i]] += 1
ans = 0
for i in sorted(dic.items(), key = lambda x:x[1]):
    if s <= k:
        break
    ans += i[1]
    s -= 1
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
dic = {}
count = 0
for i in a:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1
v = len(dic.items())
for i in sorted(dic.values()):
    if v <= k:
        break
    v -= 1
    count += i
print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

C = defaultdict(int)

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

for k,v in sorted(C.items(),key=lambda x:x[1],reverse=True)[:K]:
    N -= v
        
print(N)

Submission status: Accepted

Solution code:
import math
import sys
from collections import Counter
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()

    
    array_result.append(data.split(" "))

    flg = 1

    try:


        while flg:

            data = input()

            array_temp = []

            if(data != ""):
                
                array_result.append(data.split(" "))

                flg = 1

            else:

                flg = 0
    finally:


        return array_result





arr_data = getinputdata()

n = int(arr_data[0][0])
k = int(arr_data[0][1])

arr=arr_data[1]


list01=Counter(arr)

#print(list01,len(list01))

#print(list01)

cnt=0
if len(list01) > k:
    
    for key,val in sorted(list01.items(),key=lambda x: x[1]):

#        print(key,val,len(list01))

        cnt+=list01.pop(key)
        
        
        if len(list01)==k:
            
            break
        

print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter
def inpl(): return list(map(int, input().split()))

N, K = inpl()
A = inpl()
C = Counter(A)

#type(C)
print(sum(sorted(C.values(),  reverse=True)[K:]))
#sorted(C.values(),  reverse=True)[K:]

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

num = {}
for x in a:
    if x in num:
        num[x] += 1
    else:
        num[x] = 1

nums = []
for x in num:
    nums.append(num[x])
nums = sorted(nums)

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

print(res)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
cnt = [0] * (N+1)  # cnt[1] から cnt[N] まで使う

for i in range(N):
    cnt[A[i]] += 1

shurui = 0
for n in range(1, N+1):
    if cnt[n] > 0:
        shurui += 1

if shurui <= K:
    print(0)
else:
    cnt.sort(reverse=True)
    print(sum(cnt[K:shurui]))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
d = [0 for _ in range(200001)]

for i in map(int, input().split()):
    d[i] += 1

s = sorted(d)

ans = 0
for i in range(200001-k):
    ans += s[i]

print(ans)

Submission status: Accepted

Solution code:
lis = list(map(int, input().split()))
lis2 = input().split()
n = lis[0]
k = lis[1]
d = {}

for i in range(n):
    if lis2[i] not in d.keys():
       d[lis2[i]] = 1
    else:
        d[lis2[i]] += 1 
        
if len(list(d.values())) <= k:
    print(0)
else:
    a = list(d.values())
    a.sort()
    a.reverse()
    swap = 0
    for i in range(k,len(a)):
        swap += a[i]
    print(swap)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
d=[0 for i in range(200001)]
for i in map(int,input().split()):
    d[i]+=1
d=sorted(d)
ans=0
for i in range(200001-k):
    ans+=d[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def i2():
 return [int(i) for i in input().split()]
[n,k]=i2()
a=i2()
from collections import Counter
c = Counter(a)
cv=c.values()
print(sum(sorted(list(cv))[:len(cv)-k]))

Submission status: Accepted

Solution code:
from collections import OrderedDict

temp=input()
n,k=list(map(int,temp.split(" ")))
co=OrderedDict()
ans=[]
temp=input()
nums=list(map(int,temp.split(" ")))
for i in nums:
  if i in co:
	  co[i]+=1
  else:
	  co[i]=1
for i in co:
  ans.append(co[i])
kinds=len(ans)
dif=kinds-k
if dif<=0:
    print(0)
else:
    ans.sort()
    print(sum(ans[:dif]))

Submission status: Accepted

Solution code:
import heapq

n,k=map(int,raw_input().split())
a=map(int,raw_input().split())



d={}
for i in a:
    if i in d:
        d[i]+=1
    else:
        d[i]=1
l=d.values()
div=max(0,len(l)-k)
if div==0:
    print 0
    exit()


heapq.heapify(l)

ans=0
while div>0:
    ans+=heapq.heappop(l)
    div-=1
print ans

Submission status: Accepted

Solution code:
import numpy as np
N, K = map(int, input().split())
a = list(map(int, input().split()))

def uniq(a):
    d = {}
    for a_ in a:
        if a_ not in d:
            d[a_] = 1
        else:
            d[a_] += 1
    return d.keys(), d.values()

def solve():
    u, c = uniq(a)
    k = len(u) - K
    if len(u) > K:
        print(sum(sorted(c)[:k]))
    else:
        print(0)

solve()

Submission status: Accepted

Solution code:
from collections import Counter
import numpy as np

N,K=[int(_) for _ in input().split()]
a_lst=[int(_) for _ in input().split()]

counter=Counter(a_lst)
v_arr=np.zeros(len(counter)).astype(int)
for i, (k, v) in enumerate(counter.items()):
    v_arr[i]=v
v_arr=np.sort(v_arr)
total=v_arr.sum()
top=v_arr[-K:].sum()
print(total-top)

Submission status: Accepted

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

from collections import Counter
from operator import itemgetter


A_cnt = Counter(A)
# print(A_cnt)
# print([v for k, v in sorted(A_cnt.items(), key = itemgetter(1))[:K]])
print(N - sum([v for k, v in sorted(A_cnt.items(), key = itemgetter(1), reverse = True)[:K]])\
)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


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

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

cnt = {}

for a in As:
    cnt.setdefault(a, 0)
    cnt[a] += 1

c = len(cnt)
ans = 0

for k, v in sorted(cnt.items(), key=lambda x:x[1]):
    if c <= K:
        break
    c -= 1
    ans += v

print(ans)

Submission status: Accepted

Solution code:
import collections

if __name__ == '__main__':
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    ele_count = len(set(A))
    # print(ele_count)

    counter = collections.Counter(A)
    ans = 0
    for ele in counter.most_common()[:-(ele_count - K + 1):-1]:
        ans += ele[1]

    print(ans)

Submission status: Accepted

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

from collections import defaultdict

d = defaultdict(lambda: 0)

for elem in a:
    d[elem] += 1
    
lst = list(d.items())
lst.sort(key=lambda x: -x[1])

i = k
res = 0

while True:
    try:
        now = lst[i]
    except IndexError:
        break
    res += now[1]
    i += 1

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

a = [int(x) for x in input().split()]

d = defaultdict(int)

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

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

m = len(e)

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

# dictionary で種類と数値をとっておく
d = defaultdict(int)
for a in al:
  d[a] += 1

# 種類が少ないものから色を変えていくと最小の変えなければいけないボールの数がわかる
ans = 0
if len(d.keys()) > k:
  # 色を塗り替えないといけないので数えなければ…
  v = list(d.values())
  v.sort()
  ans = sum(v[:len(d.keys()) - k])
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8


if __name__ == '__main__':
  n,k = map(int,input().split())
  a = list(map(int,input().split()))
  s = sorted(a)
  tmp = s[0]
  count = 0 
  dif_a = [1]
  for i in range(1,n):
    if s[i] == tmp:
      dif_a[count] += 1
    else:
      tmp = s[i] 
      count += 1
      dif_a.append(1)

  ds = sorted(dif_a)
  kind = count + 1
  remove = 0
  for i in range(len(ds)-k):
    remove += ds[i]
  print(remove)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce
from operator import xor
INF = float("inf")
sys.setrecursionlimit(10**7)

# 4近傍（右, 下, 左, 上）
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ks=[]
ele=0
for i in range(max(a)+1): ks.append(0)
for m in a:
  if ks[m]==0: ele+=1
  ks[m]+=1
if ele>k:
  ks.sort()
  p=sum(ks)
  for i in range(len(ks)-k,len(ks)):
      p-=ks[i]
  print(p)
else:
  print(0)

Submission status: Accepted

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

for a in list(map(int, input().split())):
    v[int(a)] += 1

v = sorted(v, reverse=True)
if 0 in v:
    v = v[:v.index(0)]
v = list(reversed(v))

ans = 0
for i in range(len(v)):
    if len(v) - i <= K:
        break
    ans += v[i]

print(ans)

Submission status: Accepted

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