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:
def not_so_diverse(N: int, K: int, A: int) -> int:
    freq = [0] * N
    for a in A:
        freq[a - 1] += 1

    return sum(sorted(freq, reverse=True)[K:])


if __name__ == "__main__":
    N, K = map(int, input().split())
    A = [int(s) for s in input().split()]
    ans = not_so_diverse(N, K, A)
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

def main():
    n, k = map(int, input().split())
    a = [int(s) for s in input().split()]
    print(solve(n, k, a))

def solve(n, k, a):
    counter = Counter(a)
    s = 0
    for _, m in counter.most_common(k):
        s += m

    return n - s

main()

Submission status: Accepted

Solution code:
import collections

n, k = map(int, input().split())
l = list(map(int, (input().split())))
count = 0
c = collections.Counter(l)
c = c.most_common()[::-1]


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

Submission status: Accepted

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

T = [0]*N
for a in A:
  T[a] += 1
T.sort()
print sum(T[:N-K])

Submission status: Accepted

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

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

    num = []
    for key in d:
        num.append(d[key])

    if len(num) <= K:
        ans = 0
    else:
        num = sorted(num, key=lambda x: -x)
        ans = sum(num[K:])

    print(ans)


if __name__ == '__main__':
    main()

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()]
cnt=dict(Counter(a))
if len(cnt)<=k:
    print(0)
else:
    value=list(reversed(sorted(cnt.values())))
    print(sum(value[k:]))

Submission status: Accepted

Solution code:
n, k=map(int, input().split())
a=list(map(int, input().split()))
ans=0
d={}
for ai in a:
    if ai in d:
        d[ai]+=1
    else:
        d[ai]=1
cnt=sorted(list(d.values()), reverse=True)
print(sum(cnt[k:]))

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
now = A[0]
num = 1
l = []
for i in range(1,N):
    if now != A[i]:
        l.append(num)
        now = A[i]
        num = 1
    else:
        num += 1
l.append(num)
l.sort()
if len(l) <= K:
    print(0)
else:
    print(sum(l[0:len(l)-K]))

Submission status: Accepted

Solution code:
#081-C
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return list(input())
n,k=IL()
A=IL()
Ad={}
for a in A:
    if a in Ad:
        Ad[a]+=1
    else:
        Ad[a]=1
Add=sorted(Ad.items(), key=lambda x: x[1])
ans=0
for i in range(len(Add)-k):
    ans+=Add[i][1]
print(ans)

Submission status: Accepted

Solution code:
n, k = (int(i) for i in input().split())
a = [int(i)-1 for i in input().split()]

import collections
c = collections.Counter(a)
values, counts = zip(*c.most_common(k))
print(n-sum(counts))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
k2num = {}
for k in map(int, input().split()):
    if k in k2num:
        k2num[k] += 1
        continue
    k2num[k] = 1

ans = 0
num_ball = sorted(k2num.values(), key=lambda x: x)
for i, num in enumerate(num_ball):
    if len(num_ball)-i <= K:
        break
    ans += num
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
A = Counter(list(map(int, input().split())))
A = sorted(A.items(), key=lambda x: x[1], reverse=True)
print(sum([a[1] for a in A[K:]]))

Submission status: Accepted

Solution code:
# 入力
[N,K] = list(map(int, input().split()))
A = list(map(int, input().split()))

# 処理
D = {}
for i in range(N):  # 各整数の個数を数える
  if A[i] in D:
    D[A[i]] += 1
  else:
    D[A[i]] = 1
    
L = list(D.values())[:]  # Dの値集合をコピー．
# D.values()はリストでないので，listで型変換する．

L.sort()  # Lをソート

ans = 0   # 答え
for i in range(len(L)-K):
  ans += L[i]

# 出力
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
A = list(map(int, input().split()))
c = list(Counter(A).most_common())
c = c[::-1]
num = len(c)
i = 0
ans = 0
while K<num:
    num -= 1
    ans += c[i][1]    
    i += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

ddict = defaultdict(int)
li_a = list(input().split())

for a in li_a:
    ddict[a] += 1

counter = 0
for w in sorted(ddict, key=lambda x: ddict[x], reverse=True)[k:]:
    counter += ddict[w]

print(counter)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    A = map(int, input().split())
    cnt = {}
    for a in A:
        if a not in cnt:
            cnt[a] = 0
        cnt[a] += 1
    n = max(len(cnt) - K, 0)
    x = list(cnt.values())
    x.sort()
    print(sum(x[:n]))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import collections
import sys

n,m = map(int,input().split())

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

a.sort()

l = collections.Counter()

for i in a:
    l[i] += 1

co = 0
su = 0
l = l.most_common(m)
for i in l:
    if co < m:
        co += 1
        n -= i[1]

print(n)

Submission status: Accepted

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

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

l=len(set(A))

mycounter=Counter(A)
X=mycounter.most_common()[::-1]
cnt=0
if l<=k:
    print(0)
else:
    for i in range(l-k):
        cnt+=X[i][1]
    print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
now = a[0]
leng = 1
b = []
for i in a[1:]:
  if i == now:
    leng += 1
  else:
    b.append(leng)
    leng = 1
  now = i
b.append(leng)
b.sort()
len_b = len(b)
if len_b > k:
  print(sum(b[:len_b - k]))
else:
  print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
N,K = map(int,input().split())
A = list(map(int,input().split()))
a = collections.Counter(A)
ans = sorted(list(a.values()),reverse=True)
print(sum(ans[K:]))

Submission status: Accepted

Solution code:
from collections import Counter
N,K=map(int,input().split())
a = list(map(int,input().split()))
d=Counter(a)
ans=sum(sorted(d.values(),reverse=1)[: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()))
d=Counter(a)
d=sorted(d.items(),key=lambda x:x[1])
ans=0
for i in range(len(d)-K):
        ans+=d[i][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)
count=0
if len(c)<=K:
    print(0)
else:
    x=c.most_common()[K:]
    for a in x:
        count+=a[1]
    print(count)

Submission status: Accepted

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

Submission status: Accepted

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

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

LD = list(dic.values())

L = sorted(LD)

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
args = list(map(int, input().split()))
ballset = set(args)
balls = {}
for ball in ballset:
    balls[ball] = 0
for i in args:
    balls[i] += 1
count = len(ballset)
if count <= K:
    print (0)
else:
    print (sum(sorted(balls.values())[0:max(count-K, 0)]))

Submission status: Accepted

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

numAppeared = [0] * (N+1)

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

numAppeared.sort( reverse=True )

# print( sum( numAppeared[i] for i in range(K,N) ) )
print( sum( numAppeared[K:N] ) )

Submission status: Accepted

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

numAppeared = [0] * (N+1)

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

numAppeared.sort( reverse=True )

print( sum( numAppeared[i] for i in range(K,N) ) )

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
nums=list(input().split())
used_balls=0
dict={}
for n in nums:
  if n not in dict.keys():
    dict[n]=1
  else:
    dict[n]+=1
tmp=0
for d in sorted(dict.items(),key=lambda x: x[1],reverse=True):
  tmp+=1
  if tmp<=K:
    used_balls+=d[1]
  else:
    break
    
print(N-used_balls)

Submission status: Accepted

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

c = Counter(a)
c = sorted(c.items(), key=lambda x:x[1])
res = 0
for _, i in c[:len(c)-k]:
    res += i
print(res)

Submission status: Accepted

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

B = [0 for i in range(n+1)]

for i in range(n) :
    B[A[i]] += 1

B.sort(reverse=1)

print(sum(B[k:]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Mar  1 01:07:09 2019

@author: yuta
"""
import collections

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

"""
TLE解法
def main(nk,a):
    k = nk[1]
    a_uni = list(set(a)) #重複なし数列
    a_uni_num = [0]*len(a_uni) #各数字の重複個数
    for i in range(len(a_uni)):
        for j in range(len(a)):
            if a[j] == a_uni[i]:
                a_uni_num[i] += 1
    a_uni_num_sort = sorted(a_uni_num) #重複個数を昇順で並び替え
    red_num = len(a_uni) - k #減らす種類の数
    print(sum(a_uni_num_sort[:red_num]))

main(NK,A)
"""
def main(nk,a):
    k = nk[1]
    values,counts = zip(*collections.Counter(a).most_common())
    print(sum(counts)-sum(counts[:k]))
main(NK,A)

Submission status: Accepted

Solution code:
import collections
n,k = map(int, input().split())
num = list(map(int, input().split()))
cns = collections.Counter(num)
f = [cns[i] for i in cns]
f.sort(reverse=True)
print(sum(f[k::]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import collections
n,k = map(int, input().split())
num = list(map(int, input().split()))
cns = collections.Counter(num)
f = [cns[i] for i in cns]
f.sort(reverse=True)
print(sum(f[k::]))

Submission status: Accepted

Solution code:
import collections
n,k=map(int,input().split())
a=list(map(str,input().split()))
a_c=collections.Counter(a)
l=sorted(a_c.values())
ans=0
for i in range(len(l)-k):
    ans+=l[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
A = Counter([int(i) for i in input().split()]).most_common()
cnt = 0
for a in A[K:]:
    cnt += a[1]
print(cnt)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
l = list(map(int, input().split()))
cnt = [0 for _ in range(N)]
for i in l:
    cnt[i - 1] += 1
cnt.sort(reverse = True)
total = 0
for i in range(K):
    total += cnt[i]
print(N - total)

Submission status: Accepted

Solution code:
#ABC081C
#ABC082C
N,K=map(int,input().split())
A=list(map(int,input().split()))
sA=set(A)
ans=0
d={}
for s in sA:
	d[s]=0
for a in A:
	d[a]+=1
d=sorted(d.items(),key=lambda x:x[1])
for i in range(len(sA)-K):
	ans+=d[i][1]
print(ans)

Submission status: Accepted

Solution code:
import collections

n,k = (int(i) for i in input().split())
c = collections.Counter([int(i) for i in input().split()])
l = sorted(c.values())
if len(l) > k:
  print(sum(l[:len(l)-k]))
else:
  print(0)

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
l = list(map(int, input().split()))
c = [0 for i in range(N)]
for i in l:
    c[i - 1] += 1
c.sort(reverse = True)
total = 0
for i in range(K):
    total += c[i]
print(N - total)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
l = list(map(int, input().split()))
cnt = [0 for i in range(N)]
for i in l:
    cnt[i - 1] += 1
cnt.sort(reverse = True)
total = 0
for i in range(K):
    total += cnt[i]
print(N - total)

Submission status: Accepted

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

from collections import Counter

most2 = Counter(A).most_common(K)

count = 0
for m in most2:
    count = count + m[1]

print(N-count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
set_a = set(a)
ans = 0
i = -1
c = Counter(a).most_common() # (出現回数の最も少ない数字,出現回数)
while len(set_a) > k:
    set_a.remove(c[i][0])
    ans += c[i][1]
    i-=1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n,k=map(int,input().split())
a=Counter(list(map(int,input().split()))).most_common()
print(sum(map(lambda x:x[1],a[k:])))

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import numpy as np
import random


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

count_dic = {}

for i in range(len(a)):
    if a[i] in count_dic.keys():
        count_dic[a[i]] += 1
    else:
        count_dic[a[i]] = 1
# print(type(count_dic[1]))

# print(count_dic)
count_list = sorted(count_dic.items(),key=lambda x: x[1],reverse=True)

ans = 0
# print(len(count_list))
while len(count_list)>k:
    # print(count_list)
    delte_tuple = count_list[-1]
    add_tuple = count_list[0]
    count_list[0] = tuple([add_tuple[0],add_tuple[1]+delte_tuple[1]])
    ans += delte_tuple[1]
    del count_list[-1]

print(ans)

Submission status: Accepted

Solution code:
_, k = map(int, input().split())
a = list(map(int, input().split()))
r = len(set(a))
d = {}
tot = 0
for i in a:
    if i in d:
        d[i] += 1
    else:
        d[i] = 1
l = sorted(d.values())
if r > k:
    tot += sum(l[0:r-k])
print(tot)

Submission status: Accepted

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

# you have n balls.  i-th ball is written Ai
# we want for a kind of numbers to be lessor or equal to K by re-writing ball number Ai
# calculate minimum count

# 方針
# 何種類あるかを調べる : num_cnt
# その中でもっとも個数の少ないものから順に書き換える : num_cnt - K 
# ただし num_cnt > k の場合にのみこの操作をする

num = [0]*max(A)
cnt = [0]*max(A)

# count sequence
for a in A:
    if num[a-1] == 0:
        num[a-1] = 1

    cnt[a-1] += 1

kind = sum(num)

if kind > K:
    new_cnt = sorted([e for e in cnt if e != 0])
    res = sum(new_cnt[:(kind-K)])
else:
    res = 0

print(res)

Submission status: Accepted

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

print(sum(sorted(d.values())[:-K]))

Submission status: Accepted

Solution code:
import collections
import numpy as np
import sys
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,K = IL()
l = IL()
setl = set(l)

if len(setl) <= K:
	print(0)
else:
	cl = collections.Counter(l)
	ans = 0
	for i in cl.most_common()[K:]:
		ans += i[1]
	print(ans)

Submission status: Accepted

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

kind = dict(collections.Counter(ball))

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = [int(j) for j in input().split()]
count = [0] * N
for a in A:
  count[a - 1] += 1

count.sort(reverse=True)
print(sum(count[K:]))

Submission status: Accepted

Solution code:
import collections
from operator import itemgetter
N,K = map(int,input().split())
ball = [int(j) for j in input().split()]

kind = dict(collections.Counter(ball))

if len(kind) <= K:
    print(0)
else:
    skind = sorted(kind.items(),key=itemgetter(1))
    sumNum = 0
    for i in range(len(skind)-K):
        sumNum += skind[i][1]
    print(sumNum)

Submission status: Accepted

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = map(int,input().split())|	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = list(map(int,input().split()))	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import collections
import math

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

c = collections.Counter(A)

now = len(c)
ans = 0
for _, i in c.most_common()[::-1]:
  if now <= K:
    break
  ans += i
  now -= 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
from collections import Counter
C=Counter(A)
ans=0
l=len(C)
ans=0
if l<=k:
    print(ans)
else:
    B=[]
    for i in C.keys():
        B.append([i,C[i]])
    B=sorted(B,key=lambda x:x[1])
    for i in range(l-k):
        ans+=B[i][1]
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n,k=map(int,input().split())
lst=list(map(int,input().split()))
new_lst=[]
result=0
if len(list(set(lst)))<=k:
    print(result)
    exit()
else:
    c=Counter(lst).most_common()
    for u in range(k,len(c)):
        new_lst.append(c[u][1])
    print(sum(new_lst))

Submission status: Accepted

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

count = Counter(A)
res = 0
i = 0
for k,v in count.most_common():
    if K>i:
        i+=1
        continue
    res += v
print(res)

Submission status: Accepted

Solution code:
from collections import Counter
n,k=map(int,input().split())
A=Counter(list(map(int,input().split()))).most_common()
diff=len(A)-k
A=A[::-1]
if diff==0:
  print(0)
else:
  print(sum(list(map(lambda x:x[1],A[:diff]))))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
d = {}
for i in list(map(int,input().split())):
    if i in d.keys():
        d[i] += 1
    else:
        d[i] = 1
l = []
for k,v in sorted(d.items(), key=lambda x: x[1]):
    l.append(v)

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

print(ans)

Submission status: Accepted

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

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

print(sum(sorted(d.values())[:-K]))

Submission status: Accepted

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

d = {}
for a in A:
  if(a not in d):
    d[a] = 1
  else:
    d[a] += 1
if(len(d) <= K):
  print(0)
else:
  tmp = sorted(d.items(), key = lambda x: -x[1])[K:]
  print(sum(t[1] for t in tmp))

Submission status: Accepted

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
N,K = map(int, input().split())
a = list(map(int, input().split()))

from collections import Counter
from functools import reduce
d = Counter(a)

# print(d.items())
# print(d.most_common()[::-1])
s =  reduce( lambda x,y:x+y, map(lambda x:x[1], d.most_common()[::-1][:len(d.most_common())-K]))\
  if len(d.most_common())-K >0 else 0
print(s)

Submission status: Accepted

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

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

A.sort()

i = 0
counter = -1
pnum = -1
data = []

while i < N:
    if pnum != A[i]:
        data.append(1)
        pnum = A[i]
        counter += 1
    else:
        data[counter] += 1
    i += 1

data.sort()

print sum(data[:counter-K+1])

Submission status: Accepted

Solution code:
from collections import Counter

n, k = map(int, input().split())
c = sorted(Counter(input().split()).values())
print(sum(c[:max(0, len(c)-k)]))

Submission status: Accepted

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

lis.sort()
lis.append(241543903)
num = []
score = 1
kind = 1
ans = 0

for j in range(nk[0]):
    if lis[j] == lis[j+1]:
        score += 1
    else:
        num.append(score)
        score = 1
        kind += 1

kind -= 1
num.sort()

if nk[1] >= kind:
    print(0)
else:
    for k in range(kind - nk[1]):
        ans += num[k]
    print(ans)

Submission status: Accepted

Solution code:
import collections

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

def solve():
  c = list(collections.Counter(a).values())
  c.sort()
  if(len(c)<=k):
    print(0)
  else:
    print(sum(c[:len(c)-k]))

solve()

Submission status: Accepted

Solution code:
from collections import Counter


def solve(N, K, As):
    c = Counter(As)
    if len(c) <= K:
        return 0
    m = sorted([(v, k) for (k, v) in c.items()], reverse=True)
    return sum([n[0] for n in m[K:]])


if __name__ == "__main__":
    N, K = tuple(map(int, input().split(" ")))
    As = list(map(int, input().split(" ")))
    print(solve(N, K, As))

Submission status: Accepted

Solution code:
import collections
n,k = (int(i) for i in input().split())
a = list(int(i) for i in input().split())
c = collections.Counter(a)
values,counts = zip(*c.most_common())
if len(counts) >= k:
 ans = sum(counts[k:])
else:
 ans = 0

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
from collections import Counter

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

c = Counter(a)
values, counts = zip(*c.most_common())

print(np.sum(np.sort(counts)[::-1][k:]))

Submission status: Accepted

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

ans = 0
for i in range(variety - K):
    ans += series[i]
print(str(ans))

Submission status: Accepted

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

a = sorted(a)
b =[]
tmp = 1
for i in range(0,n-1):
  if a[i]==a[i+1]:
    tmp +=1
  else:
    b.append(tmp)
    tmp = 1

if a[n-2]==a[n-1]:
  b.append(tmp)
elif a[n-2]!=a[n-1]:
  b.append(tmp)
  
b = sorted(b)
if len(b)<=k:
  print(0)
else:
  print(sum(b[:len(b)-k]))

Submission status: Accepted

Solution code:
from collections import Counter
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N, K = map(int, input().split())
A = list(map(int, input().split()))

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

Submission status: Accepted

Solution code:
from collections import Counter

n,k = map(int,input().split())
n_lst = list(map(int,input().split()))
cnt_lst = Counter(n_lst).most_common()
ans = 0

for i in range(k,len(cnt_lst)):
    ans += cnt_lst[i][1]
print(ans)

Submission status: Accepted

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

from collections import defaultdict
d = defaultdict(int)

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

l = sorted(d.values())
M = len(d.keys())
if M<=K:
    print(0)
else:
    print(sum(l[:M-K]))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
L = list(map(int,input().split()))
l = []
import collections as coll
c = coll.Counter(L)
for x in c.items():
    tmp = (x[1],x[0])
    l.append(tmp)
l=sorted(l)
ans = 0
for i in range(len(l)-K):
    ans += l[i][0]
print(ans)

Submission status: Accepted

Solution code:
(n, k) = map(int, input().split())
count_dict = {}
for i in input().split():
    a = int(i)
    if not a in count_dict:
        count_dict[a] = 1
    else:
        count_dict[a] += 1
count = sorted(count_dict.items(), key=lambda x: x[1])
sum = 0
for i in range(len(count) - k):
    sum += count[i][1]
print(sum)

Submission status: Accepted

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

    import collections
    count = collections.Counter(A)
    pq = sorted([(v, k) for k, v in count.items()], key=lambda x: x[0])
    ans = 0

    if len(count) <= K:
        return 0

    for v, k in pq:
        ans += v
        del count[k]
        if len(count) <= K:
            return ans
    return -1


print(solve())

Submission status: Accepted

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

ar = []
tmp = -1
for a in li:
    if a != tmp:
        tmp = a
        ar.append(1)
    else:
        ar[-1] += 1
ar.sort()

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

Submission status: Accepted

Solution code:
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
count = 0
for i, v in enumerate(sorted(B.values())):
    if len(B) - i <= K:
        break
    count += v
print(count)

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

cnt = 0
for i in range(len(d)):
    if i >= k:
        cnt += d[i][1]
print(cnt)

Submission status: Accepted

Solution code:
import collections
N,K = map(int,input().split())
A = list(map(int,input().split()))
a = collections.Counter(A)
b = a.most_common()
L = len(b)
i = K
count = 0
while i < L:
    count+=b[i][1]
    i+=1
print(count)

Submission status: Accepted

Solution code:
from collections import Counter

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

count = 0
if len(A) > k:
    for i in range(len(A) - k):
        count += A[k + i][1]
        
print(count)

Submission status: Accepted

Solution code:
import collections
def ma():return map(int, input().split())#a,b,c,d or n,k
def lma():return list(map(int, input().split()))#x or y
a,b=ma()
x = lma()
c = collections.Counter(x)
k = c.most_common()
if b >= len(c):
    print(0)
else:
    s=0
    for i in range(b,len(c)):
        s+=k[i][1]
    print(s)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
A = input().split()
A_dict = sorted(Counter(A).items(), key=lambda x: x[1])
must_dec = len(A_dict) - K
ans = 0
for i in range(must_dec):
    ans += A_dict[i][1]
print(ans)

Submission status: Accepted

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

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

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)
C = list(sorted(C.items(), key=lambda x: x[1]))

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

from collections import Counter
C = list(Counter(A).most_common())

Ans = 0
for i in range(len(C)-K):
    Ans += C[-1-i][1]

print(Ans)

Submission status: Accepted

Solution code:
# _*_ coding:utf-8 _*_
#  Atcoder_Beginnger_Contest081-C
#  TODO https://atcoder.jp/contests/abc081/tasks/arc086_a
import collections as col

def solveProblem(neededBallKind,BallsList):
	AsListCounter = col.Counter(BallsList)
	allBallKind = len(AsListCounter)
	sortCounter = sorted(AsListCounter.items(),reverse=False,key=lambda x:x[1])
	replaceBallCount = 0
	replaceBallKind = allBallKind-neededBallKind
	if replaceBallKind <= 0:
		answer = 0
	else:
		for k,v in sortCounter:
			replaceBallCount = replaceBallCount + v
			replaceBallKind = replaceBallKind - 1
			if replaceBallKind <= 0:
				break
		answer = replaceBallCount
	return answer

if __name__ == '__main__':
	_,K = map(int,input().strip().split(' '))
	AsList = list(map(int,input().strip().split(' ')))
	solution = solveProblem(K,AsList)
	print("{}".format(solution))