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 -*-

def zehnpaard():
    from collections import Counter

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

def main():
    """WA"""
    N, K = list(map(int, input().split()))
    a_list = list(map(int, input().split()))

    import collections
    c_dict = collections.Counter(a_list)
    diff = len(c_dict) - K
    if diff <= 0:
        print(0)
        exit()

    # diff個消す。カウント数が少ないやつを。
    ans = 0
    c_list = sorted(c_dict.items(), key=lambda x: x[1])
    for i in range(diff):
        ans += c_list[i][1]
    print(ans)

def main2():
    N, K = list(map(int, input().split()))
    a_list = list(map(int, input().split()))

    import collections
    c_dict = collections.Counter(a_list)
    print(sum(value for key, value in c_dict.most_common()[K:]))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

def zehnpaard():
    from collections import Counter

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

def main():
    """WA"""
    N, K = list(map(int, input().split()))
    a_list = list(map(int, input().split()))

    import collections
    c_dict = collections.Counter(a_list)
    diff = len(c_dict) - K
    if diff <= 0:
        print(0)
        exit()

    # diff個消す。カウント数が少ないやつを。
    ans = 0
    c_list = sorted(c_dict.items(), key=lambda x: x[1])
    for i in range(diff):
        ans += c_list[i][1]
    print(ans)

def main2():
    N, K = list(map(int, input().split()))
    a_list = list(map(int, input().split()))

    import collections
    c_dict = collections.Counter(a_list)
    print(sum(value for key, value in c_dict.most_common()[K:]))

if __name__ == "__main__":
    main2()

Submission status: Accepted

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

a_list = list(map(int, input().split()))
a_set = set(a_list)
if len(a_set) <= K:
    print(0)
else:
    a_map = {}
    for a in a_list:
        if a in a_map:
            a_map[a] += 1
        else:
            a_map[a] = 1
    margin = len(a_set) - K
    a_map = sorted(a_map.items(), key=lambda x:x[1])
    count = 0
    for i in range(margin):
        count += a_map[i][1]
    print(count)

Submission status: Accepted

Solution code:
from collections import Counter
n,k =map(int, input().split())
a=list(map(int, input().split()))
count=Counter(a)
ans=0
for l,(i,j) in enumerate(count.most_common()):
    if l<k:continue
    ans+=j
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
import collections
c = collections.Counter(A)
print(N - sum([pair[1] for pair in c.most_common(K)]))

Submission status: Accepted

Solution code:
import collections

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

cnt_list = []

c = collections.Counter(A)
values, counts = zip(*c.most_common())
counts = list(counts)
counts.reverse()

answer=0

if len(counts) > K:
    for i in range(len(counts)-K):
        answer += counts[i]
        
print(answer)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
CA = Counter(A).most_common(K)
S = 0
for i in range(len(CA)):
    S += CA[i][1]
print(N-S)

Submission status: Accepted

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

ball_dict = {}
uni_A = list(set(A))

if len(uni_A) <= k:
    print(0)

else:
    for uni_a in uni_A:
        ball_dict[uni_a] = 0

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

    val = sorted(ball_dict.values())

    # print(val)

    print(sum(val[:-k]))

Submission status: Accepted

Solution code:
#!/usr/bin/python3

from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def main():
    N, K = map(int, input().split())
    c = Counter(list(map(int, input().split())))
    total = len(c)
    ans = 0
    for v in sorted(c.values()):
        if total - K > 0:
            ans += v
            total -= 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import collections
import numpy as np
N, K =map(int,input().split())
a=list(map(int,input().split()))
c = collections.Counter(a)
c_=c.most_common()
c__=np.array(c_)
ans=0
for i in range (K,c__.shape[0]):
    ans+=c__[i][1]
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
dic = {}
for i in a:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1
dic = sorted(dic.items(), key=lambda x: x[1])
#while len(dic) > k:
#    ans += dic[0][1]    
#    del dic[0]
for i in range(len(dic)-k):
    ans += dic[i][1]
print(ans)

Submission status: Accepted

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

Cn = [0] * (N+1)
Uq = set()
for a in A:
  Cn[a] += 1
  Uq.add(a)

Cn.sort(reverse=True)

if len(Uq) <= K:
  print(0)
else:
  print(sum(Cn[K:]))

Submission status: Accepted

Solution code:
from collections import Counter
from operator import itemgetter

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

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

Submission status: Accepted

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(10000)


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


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


# --------------------------------------------

dp = None


def main():
    N, K = li_input()
    A = li_input()

    D = collections.defaultdict(lambda: 0)
    C = []

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

    for v in D.values():
        C.append(v)

    C.sort(reverse=True)

    print(sum(C[K:]))


main()

Submission status: Accepted

Solution code:
# vim: set fileencoding=utf-8 :
from collections import Counter

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

Submission status: Accepted

Solution code:
# vim: set fileencoding=utf-8 :
from collections import Counter

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

Submission status: Accepted

Solution code:
# vim: set fileencoding=utf-8 :
from collections import Counter

n, k = map(int, raw_input().split())
a = map(int, raw_input().split())
c = Counter(a)
v = sorted(c.values())
print(sum(v[:-k]))

Submission status: Accepted

Solution code:
from collections import Counter

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

a.sort()
c = Counter(a)
m = len(c)
min = c.most_common()[::-1]
p = 0
ans = 0
while(m > K):
  ans += min[p][1]
  m -= 1
  p += 1
    
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, raw_input().split())
A = map(int, raw_input().split())
C = Counter(A)
v = sorted(C.values())
print sum(v[:-K])

Submission status: Accepted

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

import itertools
import collections
cnt = collections.Counter(A)
print(sum(itertools.islice(sorted(cnt.values()), max(0, len(cnt) - K))))

Submission status: Accepted

Solution code:
import itertools
n,k = map(int,input().split())
a = sorted(list(map(int,input().split())))
c = []
g = itertools.groupby(a)
for m,v in g:
    c.append(len(list(v)))
c = sorted(c)
print(sum(c[:-k]))

Submission status: Accepted

Solution code:
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

N,K = inpl()
aa = inpl()
ed = set()
dd = defaultdict(int)

for a in aa:
	ed.add(a)
	dd[a] += 1

CNTs = []
for i in ed:
	CNTs.append(dd[i])

CNTs.sort()
L = len(CNTs)
ans = 0
for i in range(L-K):
	ans += CNTs[i]

print(ans)

Submission status: Accepted

Solution code:
import collections

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
from collections import OrderedDict
count=0
N,K=map(int,input().split())
ball=list(map(str,input().split()))
c=sorted(collections.Counter(ball).values())
n=len(c)-K
if n==0:
    print(count)
else:
    for i in range(n):
        count+=c[i]
    print(count)

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt = Counter(A)

vl = list(cnt.values())
vl.sort(reverse=True)

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

Submission status: Accepted

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

d = {}

for i in range(N):
    d[A[i]] = 0

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

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

c = len(d)
ret = 0
for i in range(N):
    if c <= K:
        break
    c -= 1
    ret += dd[i][1]
print(ret)

Submission status: Accepted

Solution code:
import bisect
n,k = map(int,input().split())
a= list(map(int,input().split()))
a.sort()
c = []
d = 0

while d <= n-1:
    s = bisect.bisect_right(a,a[d])
    c.append(s-d)
    d = s
c.sort()

#print(c)

l = len(c)
d = c[:l-k:]

#print(d)

print(sum(d))

Submission status: Accepted

Solution code:
N,K=map(int,input().split(" "))
A=input()
A=A.rstrip().split(" ")
list1=[0]*(N+1)
for i in range(N):
    list1[int(A[i])-1]+=1
list1.sort(reverse=True)
#print(list1)

for j in range(K):
    list1[j]=0
    
print(sum(list1))

Submission status: Accepted

Solution code:
from collections import Counter

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

keys = [li[0] for li in A.most_common()][K:]
ans = sum(A[key] for key in keys)
print(ans)

Submission status: Accepted

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

a = tuple(map(int, input().split(" ")))

a_count = dict()
for a_i in a:
	if a_i not in a_count:
		a_count[a_i] = 0
	a_count[a_i] += 1

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

Submission status: Accepted

Solution code:
N,K=map(int,raw_input().split())
d={}
l=map(int,raw_input().split())
for i in l:
  if not i in d:
    d[i]=1
  else:
    d[i]+=1
c=0
e=sorted(d.items(),key=lambda x:x[1])[:len(d.items())-K]
for i in e:
  c+=i[1]
print(c)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
a = [int(x) for x in input().split()]
d = {}

for i in a :
    if i in d.keys() :
        d[i] += 1
    else :
        d[i] = 1
    
    
sb = sorted(d.items(), key = lambda x : x[1])
types = len(sb)
cnt = 0

for k in sb :
    if types > K :
        types -= 1
        cnt += k[1]
    else: 
        break
        
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
#from collections import Counter

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

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

    freq = sorted(dic.values())

    cnt = 0
    for i in range(len(a_set) - k):
        cnt += freq[i]
        #cnt += c.most_common()[::-1][i][1]

    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

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

c.sort()

y = len(c) - k

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

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

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

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

ans = sorted(ans)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

ret = {}

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

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

Submission status: Accepted

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

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

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))
ans=0
i = 0
r = sorted(A2.items(),key=lambda x:-x[1])
for k,v in r:
    i+=1
    ans+=v
    if i == K:
        break
print(N-ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import collections

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

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

Submission status: Accepted

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

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

A.sort()

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


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

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

sum_count=0
ans=0

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

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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()]
set_a = set(a)

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

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

Solution code:
from collections import Counter

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

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

Submission status: Accepted

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

from collections import Counter 

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

# 各数値ごとに数を集計
num_dict = Counter(num_list)
# 数の多い順でソート
num_tpl = num_dict.most_common()

# 種類が少ない時は書き換え0回
if len(num_tpl) <= K:
    print(0)
    exit()
else:
    sum1 = 0
    # 残していい数を集計
    for i in range(K):
        sum1 += num_tpl[i][1]
    # 全体の数 - 残す数で、書き換える数を出す
    print(N - sum1)

Submission status: Accepted

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

from collections import Counter 

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

# 各数値ごとに数を集計
num_dict = Counter(num_list)
# 数の少ない順でソート
num_tpl = num_dict.most_common()[::-1]

ans = 0
for i in range(len(num_tpl)):
    # 種類数がK以下なら終了
    if len(num_tpl) - i <= K:
        print(ans)
        exit()
    # 数が少ない要素から、答えに追加していく
    ans += num_tpl[i][1]

Submission status: Accepted

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

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

queue = Queue.PriorityQueue()

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

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

print ans

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
a = list(map(int,input().split()))
dictionary = {}
for number in a:
    if number not in dictionary:
        dictionary[number] = 1
    else:
        dictionary[number] += 1
sortedDictionary = sorted(dictionary.items(), key=lambda x: x[1])
#print(sortedDictionary)
kind = len(sortedDictionary)
ans = 0
index = 0
while kind > K:
    ans += sortedDictionary[index][1]
    kind -= 1
    index +=1
print(ans)

Submission status: Accepted

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

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

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

c = Counter()

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

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

print(altCounter)

Submission status: Accepted

Solution code:
backet = {}
# get
first_input = input().split(" ")
N = int(first_input[0])
K = int(first_input[1])

data_input = input().split(" ")

# sort
data_input.sort()

# unique
unique = set(data_input)


# create backet
for n in unique:
    backet[n] = 0

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

if kind <= K:
    print(0)

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

Submission status: Accepted

Solution code:
from collections import Counter

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

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

Submission status: Accepted

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

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

print(sum(c[K:]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print sum

Submission status: Accepted

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

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

kind_num = len(sort_tuple_list)
remove_counter = 0

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

print(remove_counter)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import collections

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import os
import sys

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

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

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

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

tmp = len(d)
ans = 0


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

print(ans)
exit(0)

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()]
c = Counter(a)
cc = c.most_common()
cv = []
ans = 0

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter


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

sort_count = sorted(a.values())


total = 0
l = len(sort_count)

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