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:
N, K = map(int, input().split())
As = [int(i) for i in input().split()]

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


diff = len(d) - K
ans = 0
if diff > 0:
    new_d = sorted(d.items(), key=lambda x: x[1])
    # 辞書がリスト[(タプル),(タプル),(タプル)]の形になる
    for x, y in new_d[:diff]:
        ans += y
    print(ans)
else:
    print('0')

Submission status: Accepted

Solution code:
import collections

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

num = len(set(a))
if num <= k:
    print(0)
else:
    num = num - k
    c = collections.Counter(a).most_common()
    ans = 0
    for i in reversed(c):
        if num > 0:
            ans += i[1]
        else:
            break
        num -= 1
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

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

obj, cnt = zip(*collections.Counter(values).most_common()[::-1])
cnt = [i for i in cnt]

N_diff = max(0, len(cnt) - K)
ans = sum(cnt[: N_diff])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#虚無うめ
n,k=map(int,input().split())
lists=list(map(int,input().split()))
import collections
lists=collections.Counter(lists).most_common()
lists=lists[::-1]
if len(lists)<=k:
    print(0)
else:
    c=0
    for i in range(len(lists)-k):
        c+=lists[i][1]
    print(c)

Submission status: Accepted

Solution code:
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
from collections import defaultdict
dd = defaultdict(int)

N,K=MI()
A=LI()

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

for k,v in dd.items():
    li.append(v)
    
li.sort(reverse = True)

print(N - sum(li[:min(K,N)]))

Submission status: Accepted

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

for n in A:
    if n not in kind:
        kind[n] = 0
    kind[n] += 1
diff = len(kind) - int(K)
if diff > 0:
    values = sorted(kind.values())
    print(sum(values[:diff]))
else:
    print(0)

Submission status: Accepted

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

dic=Counter(num)
d=sorted(dic.items(),key=lambda x:x[1])
l=len(d)
if l<=k:
  ans=0
else:
  ans=sum([d[i][1] for i in range(l-k)])
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

print(sum(sorted(c.values(), reverse=True)[k:]))

Submission status: Accepted

Solution code:
from collections import Counter

n, k = map(int, input().split())
b_list = list(map(int, input().split()))
mycounter = Counter(b_list)

ans = n

for i in mycounter.most_common(k):
  ans -= i[1]
print(ans)

Submission status: Accepted

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

checker = dict()

for a in ass:
    checker.setdefault(a, 0)
    checker[a] += 1
    
ss = sorted(checker.values())
c = len(ss)
ans = 0

for s in ss:
    if c <= k:
        break
    else:
        ans += s
        c -= 1
print(ans)

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
    
    
d = sorted(d.values())
if len(d) > k:
  ans = 0
  for i in range(len(d)-k):
    ans += d[i]
  print(ans)
else:
  print(0)

Submission status: Accepted

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

count=0
values=sorted(B.values())
for i in range(len(B)-K):
    count+=values[i]
    

print(count)

Submission status: Accepted

Solution code:
import numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=np.zeros(N)
for i in A:
  B[i-1]+=1
C=sorted([i for i in B if i != 0])
ans=0
for i in range(len(C)-K):
  ans+=C[i]
print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

list_num = [0 for i in range(N+1)]
for i in range(N) :
    list_num[list_A[i]] += 1

list_num.sort(reverse = True)

ans = N
for i in range(K) :
    ans -= list_num[i]

if ans > 0 :
    print(ans)
else :
    print(0)

Submission status: Accepted

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

import collections
c = collections.Counter(a)
m = c.most_common()
ans = 0
if len(m)<=k:
    print(0)
else:
    for i in range(k, len(m)):
        ans += m[i][1]
    print(ans)

Submission status: Accepted

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

c=collections.Counter(a)
num_kind=len(set(a)) #種類の数
diff = num_kind-k #変更すべき数
values, counts = zip(*c.most_common()[::-1]) #少ない順
print(sum(counts[:diff]))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = [int(x) for x in input().split()]
d = {}
ans_list = []
ans = 0

for aa in a:
    if aa not in d:
        d[aa] = 0
    d[aa] += 1

for v in d.values():
    ans_list.append(v)

ans_list.sort()

for i in range(len(ans_list) - k):
    ans += ans_list[i]

print(ans)

Submission status: Accepted

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

stay = 0
for k,v in c.most_common(k):
    stay += v
print(n-stay)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
A = [int(i) for i in input().split()]
c = Counter(A)
if len(c) > K:
    v = list(c.values())
    v.sort(reverse=True)
    print(sum(v[K:]))
else:
    print(0)

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

Solution code:
from collections import Counter

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

tmp = Counter(A).most_common()[::-1]
ans = 0
for i in range(len(tmp)-K):
    ans += tmp[i][1]

print(ans)

Submission status: Accepted

Solution code:
import collections
N,K=map(int,input().split())
A=list(map(int,input().split()))
c=collections.Counter(A)
cm=c.most_common()
t=len(cm)
j=-1
ans=0
while(t>K):
    ans+=cm[j][1]
    t-=1
    j-=1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n, k = map(int,input().split())
a = list(map(int,input().split()))
ctr = Counter(a)
stay = 0
for k,v in ctr.most_common(k):
    stay += v
print(n - stay)

Submission status: Accepted

Solution code:
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce

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

Submission status: Accepted

Solution code:
from collections import Counter # 重複個数のカウント: Counter(num_list)

def main():
    n, k = map(int, input().split())
    num_list = list(map(int, input().split()))

    count_list = Counter(num_list)

    tmp_list = []

    for v in count_list.values():
        tmp_list.append(v)

    tmp_list.sort()
    
    count = 0
    for i in range(len(tmp_list)-k):
        count += tmp_list[i]

    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

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


#----------
N,K = list(map(int, input().rstrip().split()))
A_list = list(map(int, input().rstrip().split()))
#----------
balls={}

for num in A_list:
    if str(num) not in balls:
        balls[str(num)] = 1
    else:
        balls[str(num)] += 1

# 辞書ballsを昇順にソート
balls_list = sorted(balls.items(),key=lambda x:x[1])

result = 0
if len(balls) > K:
    for i in range(len(balls) - K):
        result += balls_list[i][1]

print(result)

Submission status: Accepted

Solution code:
def main():
    n, target = map(int, input().split())
    count = {}
    numbers = list(map(int, input().split()))
    for num in numbers:
        if num in count:
            count[num] += 1
        else:
            count[num] = 1
    count = sorted(count.values())
    print(sum(count[:-target]) if len(count) > target else 0)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

d = sorted(d.items(), key=lambda  x:x[1])
ans = 0
if len(d) <= k:
    print(ans)
else:
    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()))
cnt = Counter(a)
cnt_sorted = sorted(cnt.items(), key=lambda x: x[1])
num = len(cnt.keys()) - k
ans = 0
for i in range(num):
    ans += cnt_sorted[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()))

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

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
A = [int(x) for x in input().split()]
d = {}
for a in A:
  if a in d:
    d[a] +=1
  else:
    d[a] =1
    
d =sorted(d.values())
if len(d) > k:
  ans = 0
  for i in range(len(d)-k):
    ans += d[i]
  print(ans)
else:
  print(0)

Submission status: Accepted

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

num = [0] * 200005
kind = 0
for i in a:
    if num[i] == 0:
        kind += 1
    num[i] += 1
num.sort()
ans = 0
for i in range(200005):
    if kind <= k:
        break
    if num[i] == 0:
        continue
    ans += num[i]
    kind -= 1
print(ans)

Submission status: Accepted

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

#print(lis)

ans = 0
for i in range(len(lis)-k):
    ans += lis[i]

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
dic = {}
B = []
for a in A:
  if a in dic:
    dic[a] += 1
  else:
    dic[a] = 1
for v in dic.values():
  B.append(v)
B.sort()
if len(B) <= K:
  print(0)
else:
  print(sum(B[:-K]))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a2=sorted(a)
kindnum=[]
kind=1
now=a2[0]
ren=1
for i in range(1,n):
    if now!=a2[i]:
        kindnum.append(ren)
        kind+=1
        now=a2[i]
        ren=1
        continue
    ren+=1
kindnum.append(ren)
kindnum2=sorted(kindnum)
if kind<=k:
    print(0)
else:
    ans=0
    for i in range(kind-k):
        ans+=kindnum2[i]
    print(ans)

Submission status: Accepted

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

d = sorted(d.values())

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

Submission status: Accepted

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

from collections import defaultdict
d = defaultdict(lambda: 0)

for i in a:
  d[str(i)] += 1
  
ds = sorted(d.values(), reverse = True)
print(sum(ds[k:]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
hash = [0 for i in range(n+1)]

if len(set(a))<=k:
  print(0)
  exit()
else:
  ans=0
  for i in a:
    hash[i]+=1
  hash.sort()
  L=hash
  #何種類減らすかカウントするx
  x = len(set(a)) - k
  j=0
  m=0
  while m!=x:
    if L[j]==0:
      j+=1
      continue
    else:
      ans+=L[j]
      j+=1
      m+=1
  print(ans)

Submission status: Accepted

Solution code:
def keycount(li):
    from collections import defaultdict
    count = defaultdict(int)
    for i in li:
        count[i] += 1
    dst = sorted([i for k, i in count.items()])
    return dst
num, shurui = map(int, input().split())
ball = input().split()
ballset = set(ball)
if shurui < len(ballset):
    count = len(ballset) - shurui
    dst = sum(keycount(ball)[:count])
else:
    dst = 0
print(dst)

Submission status: Accepted

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

A_dict = {x:0 for x in A_set}
for i in A:
    A_dict[i] += 1
A_dict = sorted(A_dict.items(), key=lambda x:x[1])

ans = 0
if len(A_set) > K:
    for i in range(len(A_set) - K):
        ans += A_dict[i][1]
        
print(ans)

Submission status: Accepted

Solution code:
import collections
import numpy

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

A = collections.Counter(A)
C=A.most_common()

if len(C)> K:
	dels=len(C) - K
	delsList=numpy.array(C[-dels:])
	ans=sum(delsList)[1]
else :
	ans=0

print(ans)

Submission status: Accepted

Solution code:
N, K = [int(x) for x in input().split()]
A_lst = [int(x) for x in input().split()]
dict = {}
for i in A_lst:
    if not i in dict.keys():
        dict[i] = 1
    else:
        dict[i] += 1

val_lst = list(dict.values())
val_lst.sort()
k = len(val_lst)
cnt = 0

for i in val_lst:
    if k <= K:
        break
    else:
        k -= 1
        cnt += i

print(cnt)

Submission status: Accepted

Solution code:
N, K = [int(x) for x in input().split()]
A_lst = [int(x) for x in input().split()]
dict = {}
for i in A_lst:
    if not i in dict.keys():
        dict[i] = 1
    else:
        dict[i] += 1

lst = []
for i in dict.keys():
    lst.append(dict[i])
lst.sort()
k = len(lst)
cnt = 0

for i in lst:
    if k <= K:
        break
    else:
        k -= 1
        cnt += i

print(cnt)

Submission status: Accepted

Solution code:
N, K = [int(x) for x in input().split()]
A_lst = [int(x) for x in input().split()]
dict = {}
for i in A_lst:
    if not i in dict:
        dict[i] = 1
    else:
        dict[i] += 1

lst = []
for i in dict.keys():
    lst.append(dict[i])
lst.sort()
k = len(lst)
cnt = 0

for i in lst:
    if k <= K:
        break
    else:
        k -= 1
        cnt += i

print(cnt)

Submission status: Accepted

Solution code:
N, K = [int(x) for x in input().split()]
A_lst = [int(x) for x in input().split()]
dict = {}
for i in A_lst:
    if not i in dict:
        dict[i] = 1
    else:
        dict[i] += 1

lst = []
for i in dict.keys():
    lst.append(dict[i])
lst.sort()
k = len(lst)
cnt = 0
i = 0

while k > K:
    k -= 1
    cnt += lst[i]
    i += 1

print(cnt)

Submission status: Accepted

Solution code:
import collections

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

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

a_count=collections.Counter(a)

aco=sorted(a_count.values())
ans=0
#for i in range(len(a_count)-k):

for i in range(len(aco)-k):
    ans+=aco[i]
    

print(ans)

Submission status: Accepted

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

arr = [0] * 200000
for i in range(len(a)):
    arr[a[i]] += 1

arr.sort()
arr.reverse()

s = 0
for i in range(k):
    s += arr[i]
    
print(n-s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
from operator import itemgetter
import numpy as np

N, K = map(int, input().split())
A = list(map(int, input().split()))
c = np.array(sorted(Counter(A).items(), key=itemgetter(1), reverse=True))
a_to_replace = c[K:]
print(sum(a_to_replace[:,1]))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
d = {}
for x in map(int, input().split()):
    n = d.get(x, 0) + 1
    d[x] = n

cnt = len(d)
ret = 0
for k, v in sorted(d.items(), key=lambda item: item[1]):
    if K >= cnt:
        print(ret)
        exit()

    ret += v
    cnt -= 1

Submission status: Accepted

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

a.sort()

count = 1
num_count = 1
num_count_list = list()
for i in range(1, n):
    if(a[i] == a[i - 1]):
        num_count += 1
    else:
        num_count_list.append(num_count)
        num_count = 1
        count += 1

num_count_list.append(num_count)

num_count_list.sort()
result = 0
if(count > k):
    loop = count - k

    for i in range(loop):
        result += num_count_list[i]

print(result)

Submission status: Accepted

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

def solve(N: int, K: int, A: "List[int]"):
    r = 0

    c = collections.Counter(A).most_common()
    while K < len(c):
        _, cnt = c.pop()
        r+=cnt
    return r


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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections as c

n,k = map(int,input().split())
l = list(map(int,input().split()))
count = c.Counter(l)
ans = 0
check = 0
right = len(count)-k
if right <= 0:
    print(0)
    exit()
for i,j in count.most_common()[::-1]:
    ans += j
    check += 1
    if check == right:
        print(ans)
        exit()

Submission status: Accepted

Solution code:
n, k = input().split()
n, k = int(n), int(k)
balls = input().split()
dic = {}
for x in balls:
	if x not in dic:
		dic[x] = 1
	else:
		dic[x] += 1
change = 0
qnt = list(dic.values())
qnt.sort()
dif = len(qnt) - k
for x in range(0, dif):
	change += qnt[x]
print(change)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
count = 0
x = dict()
ans = 0
for i in range(N):
    if A[i] in x:
        x[A[i]] += 1
    else:
        x[A[i]] = 1
        count += 1
l = []
if count <= K:
    print(0)
else:
    for idx in x:
        l.append(x[idx])
    l.sort()
    for i in range(count-K):
        ans += l[i]

    print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N,K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
import collections
c = collections.Counter(A)
li = list(c.values())
li.sort()
li.reverse()
ans = 0
for i in range(K,len(li)) :
    ans += li[i]
print(ans)

Submission status: Accepted

Solution code:
import math
import sys
import numpy as np
import collections

#n, a, b= map(int, input().split())
n, k  = map(int, input().split())
#s = list(str(input()))
#n = int(input())
a = list(map(int, input().split()))

a1 = collections.Counter(a)
num = [0]*len(a1)
counter = 0

for i in a1.keys():
  num[counter] = a1[i]
  counter = counter + 1

num.sort()
tot = 0
for i in range(len(num)-k):
  tot = tot + num[i]

print(tot)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
nList = list(map(int,input().split()))
nSet = list(set(nList))
nDic = {}

for i in nList:
  if not(i in nDic.keys()):
    nDic[i] = 1
  else:
    nDic[i] += 1

countList = sorted(nDic.values())
print(sum(countList[:(len(nSet)-k)]))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt_dict = {}
for i in a:
    if i in cnt_dict:
        cnt_dict[i] += 1
    else:
        cnt_dict[i] = 1
cnt_dict_sorted = sorted(cnt_dict.items(), key=lambda x: x[1])
ans = 0
if len(cnt_dict_sorted) <= k:
    ans = 0
else:
    tmp = len(cnt_dict_sorted) - k
    for j in range(tmp):
        ans += cnt_dict_sorted[j][1]
print(ans)

Submission status: Accepted

Solution code:
import collections

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

ans=0
A_=set(A)
if len(A_)<=K:
    pass
elif len(A_)>K:
    r=len(A_)-K
    A_Cnt=collections.Counter(A)
    A_Cnt_Sort=A_Cnt.most_common()[::-1]
    for a in A_Cnt_Sort[:r]:
        ans+=a[-1]

print(ans)

Submission status: Accepted

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

AS = list(map(int,input().split()))
balls = {}

for a in AS:
  if a in balls:
    balls[a] += 1
  else:
    balls[a] = 1

dic2 = sorted(balls.items(), key=lambda x:x[1])
amount = len(dic2) - k

count = 0
for i in range(amount):
  count += dic2[i][1]
print(count)

Submission status: Accepted

Solution code:
from collections import Counter
n,k = map(int,input().split())
a = Counter(map(int, input().split()))
cnt = 0
for v in sorted(a.values())[:len(a)-k]:
    cnt += v
print(cnt)

Submission status: Accepted

Solution code:
import sys
from collections import deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n,k = map(int,readline().split())
lst1 = list(map(int,readline().split()))

dic1 = dict()

for i in lst1:
    if i in dic1:
        dic1[i] += 1
    else:
        dic1[i] = 1

values = list(dic1.values())

values.sort()

values = deque(values)

ans = 0
while k < len(values):
    ans += values.popleft()

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
li = {}
for i in A:
    c = li.setdefault(i,0)
    li[i] = c + 1
    # print(c)
w = len(li.keys()) - K
# print(w)
# print(len(li.keys()))
ans = sorted(li.values())
# print(ans)
if w > 0:
    print(sum(ans[:w]))
else:
    print(0)

Submission status: Accepted

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

import collections
c = collections.Counter(l)
c2=[i for i in c.items()]
c2.sort(key=lambda x: x[1])
ans=0
for i in range(len(c2)-k):
    ans+=c2[i][1]

print(ans)

Submission status: Accepted

Solution code:
N, K = [int(n) for n in input().split()]
A = [int(n) for n in input().split()]
o = {}
for k in A:
    c = o.setdefault(k, 0)
    o[k] = c + 1
w = len(o.keys()) - K
ans = sorted(o.values())
print(sum(ans[:w]) if w > 0 else 0)

Submission status: Accepted

Solution code:
import collections
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=collections.Counter(a)
li=c.most_common()
if len(li)<=k:
  print(0)
  exit()
li=li[k:]
ans=0
for i in li:
  ans+=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)
cs = sorted(c.items(), key=lambda x:x[1])
cnt = 0
for i in range(len(cs)-k):
    cnt += cs[i][1]
print(cnt)

Submission status: Accepted

Solution code:
import collections
n,k=map(int,input().split())
a=list(map(int,input().split()))
a_counter=collections.Counter(a)
a_count=a_counter.most_common()
answer = 0
# print(a_counter)
for i in range(k, len(a_count)):
    # print(a_count[i])
    answer += a_count[i][1]

print(answer)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
cnt=[0 for i in range(N)]
for a in A:
  cnt[a-1]+=1
knd=[]
for i,c in enumerate(cnt):
  if c!=0:
    knd.append((i+1,c))
knd.sort(key=lambda x: x[1],reverse=True)
ans=0
while len(knd)>K:
  j,d=knd.pop()
  ans+=d
print(ans)

Submission status: Accepted

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

A_list = collections.Counter(A)
ok = A_list.most_common()[0:K]

#print(ok)
ans2 = 0
for i in range(len(ok)):
  ans2 += ok[i][1]
  
print(N - ans2)

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 i in a:
  d[i]+=1
if len(d)<=k:
  print(0)
else:
  print(sum(sorted(list(d.values()))[:len(d)-k]))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
numberlist = dict()
for i in range(N):
    if A[i] in numberlist:
        numberlist[A[i]] += 1
    else:
        numberlist[A[i]] = 1
numberlist = sorted(numberlist.items(), key=lambda x: x[1])
#print(numberlist)
ans = 0
index = 0
lenlist = len(numberlist)
while(1):
    if index >= lenlist - K:
        break
    ans += numberlist[index][1]
    index += 1
print(ans)

Submission status: Accepted

Solution code:
def solve(n, k, A):
    d = {}
    for a in A:
        d[a] = d.get(a, 0) + 1
    return sum(sorted(d.values())[:len(d) - k])

_n, _k = map(int, input().split())
_A = map(int, input().split())
print(solve(_n, _k, _A))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = list(map(int, input().split()))
s = dict()
for x in a:
  if x in s:
    s[x] += 1
  else:
    s[x] = 1
s_l = sorted(s.values())[0:max(0, len(s)-k)]
print(sum(s_l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter

input = sys.stdin.readline


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

    C = Counter(A)

    ans = 0
    if len(C) <= K:
        print(0)
        return
    else:
        mc = C.most_common()
        for i in range(len(C) - K):
            ans += mc[-i - 1][1]

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

cnt = {}

for i in range(N):
    cnt.setdefault(A[i], 0)
    cnt[A[i]] += 1

cnt_val = sorted(cnt.values())
ans = sum(cnt_val[:len(cnt_val) - K])

print(ans)

Submission status: Accepted

Solution code:
dic = dict()

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

for ball in balls:
  if ball in dic:
    dic[ball] += 1
  else:
    dic[ball] = 1
    
nums = list(dic.values())
if len(nums) <= k:
  print(0)
else:
  print(sum(sorted(nums, reverse = True)[k:]))

Submission status: Accepted

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

dic = dict()
for a in A:
    if dic.get(a) is not None:
        dic[a] += 1
    else:
        dic[a] = 1
sorted_dic = sorted(dic.items(), key=lambda x:x[1], reverse=True)

ans = 0
for ind, (k, v) in enumerate(sorted_dic):
    if ind < K:
        continue
    else:
        ans += v
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

print(N - sum(x[1] for x in Counter(A).most_common(K)))

Submission status: Accepted

Solution code:
import collections

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

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

count = 0
for i in range(len(c) - K):
    count += c[i][1]
print(count)

Submission status: Accepted

Solution code:
# 2020/01/20
# https://atcoder.jp/contests/abc081/tasks/arc086_a
# counterによってC = [(A_l, c_l),..]を求めてソート、小さい順にK-len(C)を取り出す

from collections import Counter

def solve():
    N,K = map(int, input().split())
    A = list(map(int, input().split()))
    counter = Counter(A)
    L = len(counter)
    ret = 0
    if L > K:
        l = counter.most_common()
        # print(L,l,l[K:])
        ret += sum(map(lambda x:x[1],l[K:]))
    
    print(ret)
    
solve()

Submission status: Accepted

Solution code:
from collections import Counter
import heapq

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

C = Counter(A)
print(N - sum(heapq.nlargest(K, C.values())))

Submission status: Accepted

Solution code:
from collections import Counter

N, K = map(int, input().split())
A = [int(i) for i in input().split()]
cnt = sorted(Counter(A).values())
print(sum(cnt[:-K]))

Submission status: Accepted

Solution code:
from collections import defaultdict
N, K = map(int, input().split())
nums = [int(i) for i in input().split()]

num_freq = defaultdict(int)
for num in nums:
    num_freq[num] += 1

_sorted_nums = [[i] * j for i,
                j in sorted(num_freq.items(), key=lambda x:x[1])]
sorted_nums = []
for i in _sorted_nums:
    sorted_nums += i

borders = []
for i in range(1, N):
    if sorted_nums[i-1] != sorted_nums[i]:
        borders.append(i)

if len(num_freq) <= K:
    print(0)
else:
    print(borders[-K])