Problem:


Score : 300 points

Problem Statement

Takahashi has N balls. Initially, an integer A_i is written on the i-th ball.

He would like to rewrite the integer on some balls so that there are at most K different integers written on the N balls.

Find the minimum number of balls that Takahashi needs to rewrite the integers on them.

Constraints

1 \leq K \leq N \leq 200000

1 \leq A_i \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of balls that Takahashi needs to rewrite the integers on them.

Sample Input 1

5 2
1 1 2 2 5

Sample Output 1

1

For example, if we rewrite the integer on the fifth ball to 2, there are two different integers written on the balls: 1 and 2.
On the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print 1.

Sample Input 2

4 4
1 1 2 2

Sample Output 2

0

Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.

Sample Input 3

10 3
5 1 3 2 4 1 1 2 3 4

Sample Output 3

3



Sample input:
5 2
1 1 2 2 5


Sample output:
1

Submission status: Accepted

Solution code:
from collections import Counter

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

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
cnt=len(set(a))
from collections import Counter
data=Counter(a)
data=sorted(data.items(),key=lambda x:-x[1])
data_cnt=len(data)#ボールの種類数
ans=0
if cnt<=k:
    print(0)
    
else:
    for i in range(k,data_cnt):
        ans+=data[i][1]
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
cnt = Counter(map(int, input().split()))
tot = 0
k = []
for i in cnt.values():
    k.append(i)
k.sort()
for i in range(len(cnt)-K):
    tot += k[i]
print(tot)

Submission status: Accepted

Solution code:
from collections import Counter
from collections import OrderedDict
N, K = map(int, input().split())
cnt = Counter(map(int, input().split()))
tot = 0
if len(cnt) <= K:
    print(0)
else:
    k = []
    for i in cnt.values():
        k.append(i)
    k.sort()
    for i in range(len(cnt)-K):
        tot += k[i]
    print(tot)

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
num=[]
seta=set(a)
if len(seta)-k <0:
    print(0)
else:
    for x in seta:
        num.append(c[x])
    num.sort()
    for i in range(len(seta)-k):
        ans+=num[i]
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N, K = [int(elem) for elem in input().split(' ')]
A_list = [int(elem) for elem in input().split(' ')]
assert len(A_list) == N

counter = Counter(A_list)
current_num_categories = len(counter)

count = 0
for i, num in enumerate(sorted(counter.values()), start=1):
    if i <= current_num_categories - K:
        count += num
    else:
        break

print(count)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
al = list(map(int,input().split()))
dics = {}
 
for i in range(n):
    if al[i] in dics:
        dics[al[i]] = dics[al[i]] + 1
    else:
        dics.update({al[i]:1})
        
counts = len(dics.values())
kesu = 0
sortv = list(sorted(list((dics.values()))))

i = counts
j = 0
while i > k:
    kesu = kesu + sortv[j]
    i = i - 1
    j = j + 1
print(kesu)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
a = [int(i) for i in input().split()]
s = len(set(a))
c = Counter(a)
L = sorted(c.items(), key=lambda x: x[1])

cnt = 0
i = 0
while s > K:
    cnt += L[i][1]
    s -= 1
    i += 1
print(cnt)

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

if len(C)<=K:
    print(0)
    exit(0)

ans = 0
for k in range(K,len(C)):
    ans += C[k][1]
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)

if len(A_dict) <= K:
    print(0)
else:
    print(N-sum(dict(A_dict.most_common(K)).values()))

Submission status: Accepted

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

a.sort()
x, y = 1, 1
y_lst = []

for i in range(n-1):
    if a[i] == a[i+1]:
        y += 1
    else:
        x += 1
        y_lst.append(y)
        y = 1

    if i == len(a)-2:
            y_lst.append(y)

y_lst.sort()
t = x - k
if t <= 0:
    print(0)
else:
    print(sum(y_lst[:t]))

Submission status: Accepted

Solution code:
from collections import Counter
N,K = map(int,input().split())
AC = Counter(list(map(int,input().split()))).most_common()[::-1]
C = 0
if len(AC) > K:
    for i in range(len(AC)-K):
        k,v = AC[i]
        C += v
print(C)

Submission status: Accepted

Solution code:
from collections import Counter

_, k = map(int, input().split())
balls = Counter(map(int, input().split()))
print(sum(sorted(balls.values(), reverse=True)[k:]))

Submission status: Accepted

Solution code:
import collections

N,K = map(int,input().split())
A = [0]*N
A = list(map(int,input().split()))
c = collections.Counter(A)
c_list = list(c.values())
c_list.sort()
c_list_inv = [i for i in c_list[::-1]]
c_list_sum = [i for i in c_list_inv[K::1]]
print(sum(c_list_sum))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
dic = defaultdict(int)
for i in range(len(a)):
    dic[a[i]] += 1
counts = sorted(dic.values())
print(sum(counts[:-k]))

Submission status: Accepted

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

values = list(sorted(dic.values()))

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

Submission status: Accepted

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

    num = [0 for _ in range(200000)]
    s = 0
    for A in A:
        num[A-1] += 1

    count = 0
    num.sort()
    for i in range(200000-K):
        count += num[i]
    return count

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
d,ans={},0
for i in list(map(int,input().split())):
    d[i]=d.get(i,0)+1
s=sorted(d.values())
for i in range(len(s)-K):
    ans+=s[i]
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = input().split()
b = dict()
for i in a:
    b[i] = b.get(i, 0) + 1
print(sum(sorted(b.values())[:-k]))

Submission status: Accepted

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

d = dict()
for i in a:
    d[i] = d.get(i, 0) + 1
d_sorted = sorted(d.values())
knd = len(set(a))##種類数
print(sum(d_sorted[:knd - k]))

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(a)

remaining = n - sum([x[1] for x in c.most_common(k)])

print(remaining)

Submission status: Accepted

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

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    from collections import Counter

    ball_count, kind_count = map(int, input().split())
    ball_numbers = Counter(list(map(int, input().split())))
    sorted_ball_numbers = sorted(ball_numbers.values())
    print(sum(sorted_ball_numbers[:-kind_count]))

Submission status: Accepted

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

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    from collections import Counter

    ball_count, kind_count = map(int, input().split())
    ball_numbers = Counter(list(map(int, input().split())))
    sorted_ball_numbers = sorted(ball_numbers.items(), key=lambda x: x[1])
    ball_number_key_count = len(ball_numbers.keys())

    need_rewritten_count = 0
    diff_count = ball_number_key_count - kind_count

    if diff_count > 0:
        for i, (key, value) in enumerate(sorted_ball_numbers):
            if diff_count > i:
                need_rewritten_count += value

    print(need_rewritten_count)

Submission status: Accepted

Solution code:
from collections import defaultdict
n, k = map(int, input().split())
a = list(map(int, input().split()))
dic = defaultdict(int)
for i in range(n):
    dic[a[i]] += 1

s = sorted(dic.values())
if len(dic) <= k:
    print(0)
else:
    num = 0
    for i in range(len(dic) - k):
        num += s[i]
    print(num)

Submission status: Accepted

Solution code:
from collections import Counter
from functools import reduce

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

count = Counter(A)

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

L = sorted(list(count.items()), key=lambda x: x[1], reverse=True)
ans = sum([n[1] for n in L[K:]])
print(ans)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().strip()

n,k = na()
a = na()

dic = {}

for num in a:
    if num in dic:
        dic[num] += 1
    else:
        dic[num] = 1

if len(dic) <= k:
    print(0)
else:
    dic = sorted(dic.items(), key=lambda x:x[1], reverse=True)
    ans = 0
    for i in dic[k:]:
        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()))

counter = Counter(a)

lis = counter.most_common()[::-1]
individual = len(lis)

cnt = 0
ind = 0
while individual > K:
    cnt += lis[ind][1]
    ind += 1
    individual -= 1

print(cnt)

Submission status: Accepted

Solution code:
n,k = list(map(int,input().split()))
num = list(map(int,input().split()))
l = [0 for _ in range(200001)]
for i in range(n):
    l[ num[i] ] +=1
count=0
a = []
for i in l:
    if(i>0):
        count+=1
        a.append(i)
a.sort()
ans = 0
for i in range(count-k):
    ans += a[i]
print(ans)

Submission status: Accepted

Solution code:
import collections
n,k = (int(i) for i in input().split())
x = [int(i) for i in input().split()]
y = set(x)
if len(y)<=k:
	print(0)
else:
	answer = 0
	c = collections.Counter(x).most_common()
	for j in range(-1,-(len(y)-k+1),-1):
		answer += c[j][1]
	print(answer)

Submission status: Accepted

Solution code:
import sys
n,k=map(int,input().split())
a=sorted(list(map(int,input().split())))
s=set(a)
if len(s)<=k:
    print(0)
    sys.exit()
lst=[0]*n
ind=0
lst[0]=1
for i in range(n-1):
    if a[i+1]==a[i]:
        lst[ind]+=1
    else:
        ind+=1
        lst[ind]+=1
lst.sort()
ans,cou=0,0
for i in range(n):
    if lst[i]!=0 and cou<len(s)-k:
        ans+=lst[i]
        cou+=1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

dd=defaultdict(int)
for a in arr:
    dd[a]+=1

brr=sorted(dd.values())
ans=0
for k in range(len(brr)-k):
    ans+=brr[k]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

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

Submission status: Accepted

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

numlist = {}
for i in A:
    if i in numlist:
        numlist[i] += 1
    else:
        numlist[i] = 1
numbers = numlist.values()
numbers = sorted(numbers)

change = 0
vari = len(numbers)
for i in range(vari):
    if K >= vari - i:
        break
    change += numbers[i]

print(change)

Submission status: Accepted

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

Submission status: Accepted

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

countup.sort()
result = 0
f = list(filter(lambda x: x != 0, countup))

for i, v in enumerate(f):
 if i >= len(f) - k:
    print(result)
    break
 result += v

Submission status: Accepted

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

ans=[]
count=1

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

ans=sorted(ans,reverse=True)

if K >= len(ans):
    print(0)
else:
    print(sum(ans[K:]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
kind=[]
count=1
for i in range(len(a)-1):
  if(a[i]==a[i+1]):count+=1
  else:
    kind.append(count)
    count=1
kind.append(count)
kind.sort()
change=0
for i in range(k):
  if(len(kind)!=0):change+=kind.pop()
print(n-change)

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:]

# Set 2 dimension list
def dim2input(N):
    li = []
    for _ in range(N):
        li.append(list(map(int, input())))
    return li

""" input template
S = input()
N = int(input())
L = list(map(int, input().split()))
a, b = list(map(int, input().split()))
SL = list(input())
"""

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

dp = None

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

    D = collections.defaultdict(lambda: 0)

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

    L = []
    for v in D.values():
        L.append(v)

    L.sort(reverse=True)

    i = 0
    S = 0
    U = min(K, len(L))
    while i < U:
        S += L[i]
        i += 1

    print(sum(L) - S)


main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N, K = map(int, input().rstrip().split())
ball = {}
for i in input().rstrip().split():
    if i not in ball:
        ball[i] = 1
    else:
        ball[i] += 1
ball_sort = sorted(ball.items(), key = lambda x: x[1])
if len(ball_sort) > K:
    cnt = 0
    for i in range(len(ball_sort) - K):
        cnt += ball_sort[i][1]
    print(cnt)
else:
    print(0)

Submission status: Accepted

Solution code:
import collections as col
n,k = map(int,input().split())
a   = list(map(int,input().split()))
count_dict = col.Counter(a)
cdmc       = count_dict.most_common(k)
sum_cdmc = 0

for k,v in cdmc:
    sum_cdmc += v
print(n - sum_cdmc)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import collections

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

count_rmv = 0
count_int = collections.Counter(A).most_common()
num_int_type = len(count_int)

while num_int_type > K:
    num_int_type -= 1
    count_rmv += count_int[num_int_type][1]

print(count_rmv)

Submission status: Accepted

Solution code:

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

    count = 1
    num = 0
    dic = {}
    for a in A:
        if num == a:
            count += 1

        elif num > 0:
            dic.update({num:count})
            count = 1

        num = a

    dic.update({num:count})

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

    count = 0
    keys = len(dic.keys())
    for i in range(keys-K):
        count += sort_dic[i][1]

    print (count)


if __name__ == "__main__":
    # global stime
    # stime = time.clock()
    main()

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = [0] * (N + 1)
    MAX = 9999999
    for a in A:
        B[a] += 1
    B.sort(reverse=True)
    s = 0
    for i in range(0, K):
        s += B[i]
    return sum(B) - s


print(main())

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
dict = {}
for i in range(N):
    if(A[i] in dict):
        dict[A[i]] += 1
    else:
        dict[A[i]] = 1
if(len(dict) <= K):
    print(0)
else:
    l = sorted(dict.values())
    ans = 0
    for i in range(len(dict) - K):
        ans += l[i]
    print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

num_dic = {}
for num in A:
    num_dic.setdefault(num, 0)
    num_dic[num] += 1

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

else:
    change = 0
    num_dic = sorted(num_dic.items(), key=lambda x: x[1])
    for i in range(len(num_dic) - K):
        change += num_dic[i][1]
    print(change)
    exit()

Submission status: Accepted

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

import sys

u"""
最初にdictにいれ、key=number, value=count
それを元に (count, number)のリストをつくり、昇順にソート
リストの長さがK以下になるまでリストをたどる
"""

n, k = [int(e) for e in raw_input().split()]
al = [int(e) for e in raw_input().split()]

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

l = []
for dk, dv in d.iteritems():
    # l.append((dv, dk))
    l.append(dv)
l.sort(reverse=True)
# print l

b = 0
for e in l[k:]:
    b += e
    
# i = 0
# b = 0
# ll  = len(l)
# llc = ll
# while llc > k:
#     if i >= ll:
#         break
#     c = l[i]
#     llc -= c
#     b += c
#     i += 1
    
sys.stdout.write('%d\n' % b)
sys.stdout.flush()

Submission status: Accepted

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

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

sorted_d = sorted(d.values(),reverse=True)
sums = sum(sorted_d[k:])
print(sums)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
from collections import Counter
data=Counter(a)
ans=[]
for i in data.values():
    ans.append(i)
ans.sort()
ans.reverse()
print(n-sum(ans[0:k]))

Submission status: Accepted

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

ans = 0
w = len(l)
for v in l:
    if w <= K:
        print(ans)
        break
    w -= 1
    ans += v

Submission status: Accepted

Solution code:
from collections import Counter
n,k=map(int,input().split())
a=list(map(int,input().split()))
counter=Counter(a)
ans=[]
for i in counter.values():
    ans.append(i)
ans.sort()
ans.reverse()
v=sum(ans[0:k])
print(n-v)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import Counter
n,k=map(int,input().split())
cnt=sorted(Counter(map(int,input().split())).values())
count=0
p=len(cnt)
if p<=k:
    print(count)
else:
    for c in cnt:
        if p>k:
            p-=1
            count+=c
        else:break
    print(count)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
cnt = list(sorted(Counter(map(int, input().split())).values()))
num = len(cnt)
ans = 0
for v in cnt:
	if num <= K:
		break
	ans += v
	num -= 1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
https://beta.atcoder.jp/contests/abc081/tasks/arc086_a

"""
import sys
from sys import stdin
from collections import Counter
input = stdin.readline


def solve(A, K):
    ans = 0

    c = Counter(A)
    divs = c.most_common()
    divs.reverse()

    diff = len(divs) - K
    for d in divs[:diff]:
        ans += d[1]
    return ans


def main(args):
    N, K = map(int, input().split())
    A = [int(x) for x in input().split()]
    ans = solve(A, K)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
n, k = (int(i) for i in input().split())
 
a = [int(i) for i in input().split()]
 
dic = {}
 
for i in range(n):
    if a[i] in dic:
        dic[a[i]] += 1
    else:
        dic[a[i]] = 1
 
ans_num= len(dic)-k
ans = 0
 
 
if ans_num <= 0:
    ans *= 0 
else:
    sort = sorted(dic.items(), key=lambda x:x[1])
    for i in range(ans_num):
        ans += sort[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()))
j = Counter(a)
le = sorted(j.items(), key=lambda i : i[1])
times = len(j) - k
si = 0
for i in range(times):
    item, value = le[i]
    si += value
print(si)

Submission status: Accepted

Solution code:
from collections import defaultdict
N, K = map(int, input().split())
A = list(map(int, input().split()))
d = defaultdict(lambda :0)
for a in A:
    d[a] += 1
key_list = list(d.keys())
value_list = list(d.values())
value_list.sort()
tmp_num = len(value_list)

del_num = tmp_num - K
counter = 0
if del_num > 0:
    for v in value_list[0:del_num]:
        counter += v
print(counter)

Submission status: Accepted

Solution code:
from collections import Counter

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

counter = Counter(A)
x = len(counter) - K 
if x <= 0:
    print(0)
else:
    ans = 0
    least_common = counter.most_common()[-x:]
    for a, c in least_common:
        ans += c
    print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split(' '))
L=[0]*N
A = list(map(int,input().split(' ')))
for a in A:
    L[a-1]+=1
L.sort()
print(sum(L[:N-K]))

Submission status: Accepted

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

a=sorted(C.items(), key=lambda x:x[1])
num=len(C)-K
count=0
b=[]
for key in C:
    b.append(C[key])
c=sorted(b)
for i in range(num):
    count+=c[i]
print(count)

Submission status: Accepted

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

for elem in a:
    if elem not in dic:
        dic[elem] = 1
    else:
        dic[elem] += 1

kosuu_list = []
for key in dic:
    kosuu_list.append(dic[key])
kosuu_list_asc = sorted(kosuu_list)

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

Submission status: Accepted

Solution code:
def solve(N, K, A):
    numbers = {}
    for a in A:
        numbers[a] = numbers.get(a, 0) + 1
    if len(numbers) <= K:
        return 0
    return N - sum(sorted(numbers.values(), key=lambda x: -x)[:K])
N, K = map(int, raw_input().split())
A = map(int, raw_input().split())
print solve(N, K, A)

Submission status: Accepted

Solution code:


def foo(xs, k):
    d = {}
    for x in xs:
        d[x] = d.get(x, 0) + 1

    ys = sorted(d.items(), key=lambda x: x[1])
    return sum(map(lambda x:x[1], ys[:-k])) 


if __name__ == "__main__":
    n, k = list(map(int, input().split()))
    xs = list(map(int, input().split()))
    print(foo(xs, k))

Submission status: Accepted

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

for i in A_list:
    counter[i] += 1

zeronashi = [i for i in counter if i != 0]
zeronashi.sort()

length = len(zeronashi)
x = length - K
y = 0
if x < 1:
    print(0)
else:
    for i in range(x):
        y += zeronashi[i]
    print(y)

Submission status: Accepted

Solution code:
from collections import Counter

n, k = map(int, input().split())
a = map(int, input().split())
s = 0
for w, c in Counter(a).most_common(k):
  s += c
print(n-s)

Submission status: Accepted

Solution code:
import math

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

#keyはA[i]、valueは登場回数
A_unique = {}
 
for i in range(0,len(A)):
    if A[i] in A_unique:
        A_unique[A[i]] += 1
    else:
        A_unique[A[i]] = 1
v = list(A_unique.values())
v.sort() #登場回数が少ない順に書き換えていく
A_length = len(A_unique.keys()) #元々のAに含まれる数字の種類
d = 0
n = 0

for k in v:
    if  A_length - n <= K:
        print(d)
        break
    else:
        d += k #d個を書き換え
        n += 1 #数字が1種類減る

Submission status: Accepted

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

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

print(sum(Al[0:len(Al)-K]))

Submission status: Accepted

Solution code:


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

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

res = 0
num = dict()

key = 0

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

if key - k <= 0:
    print("0")
    exit(0)

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

for i in range(key - k):
    res += num2[i][1]

print(res)

Submission status: Accepted

Solution code:
# C - Not so Diverse
# coding: utf-8

import collections

N, K = map(int, raw_input().split())
A = map(int, raw_input().split())
A_dict = collections.Counter(A)
A_common = A_dict.most_common(K)

if len(A_dict) <= K:
  result = 0
else:
  result = N
  for i in range(K): result -= A_common[i][1]

print result

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A = list(map(int, input().split()))

d = dict()
for elem in A:
    if elem not in d:
        d[elem] = 1
    else:
        d[elem] = d[elem] + 1

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

Submission status: Accepted

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

cnt_sum=0

counter = Counter(a).most_common()

for i in range(K):
  try:
    cnt_sum+=counter[i][1]
  except:
    break
answer=N-cnt_sum

print(answer)

Submission status: Accepted

Solution code:
#N個のボールを持ち，K個以下の整数にする
#順当に考えるならば，種類が少ないやつから変更していけばいい
#番号の列をリストで表現
#押し付けきれるとは限らない

counter = 0

condition = list(map(int, input().split()))
A_list = list(map(int, input().split()))

zero_line = [0 for _ in range(200001)]

for i in A_list:
    if zero_line[i] == 0:
        counter += 1
    zero_line[i] += 1

zero_line.sort()
zero_line.reverse()

if counter <= condition[1]:
    print(0)
else:
    print(sum(zero_line[condition[1] :counter]))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
ball = {}
i = 0
num = 0
change = 0
for n in range(len(a)):
    if a[n] in ball:
        ball[a[n]] = ball[a[n]] + 1
    else:
        ball[a[n]] = 1

num = len(ball)
s = sorted(ball.items(),key=lambda ball: ball[1])
while K < num:
    num -= 1
    change += s[i][1]
    i += 1

print(change)

Submission status: Accepted

Solution code:
import heapq

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

T = [0] * (N + 1)

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

ns = []
for t in T:
    if t > 0:
        heapq.heappush(ns, t)
ans = 0
while len(ns) > K:
    ans += heapq.heappop(ns)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

lis = list(map(str,input().split()))

counter = Counter(lis)

num = len(counter)

a =  sorted(counter.values())

Ans = 0

for i in a:
	if num <= K:
		break
	Ans += i
	num -= 1
	
print(Ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

parameter = list(map(int, input().split()))
n, k = parameter[0], parameter[1]

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

numbers = defaultdict(lambda: 0)
for b in balls:
    numbers[b] += 1

k = min(k, len(numbers))

sort_number = sorted(numbers.items(), key=lambda x:x[1], reverse=True)
answer = n - sum([sort_number[i][1] for i in range(k)])
print(answer)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

l = sorted(d.items(), key=lambda p: p[1])
x = len(l)
ans = 0
for e in l:
    if x <= K:
        break
    x -= 1
    ans += e[1]
print(ans)

Submission status: Accepted

Solution code:
N,K,*A=map(int,open(0).read().split());B={}
for a in A:B[a]=B.get(a,0)+1
print(sum(sorted(B.values())[:-K]))

Submission status: Accepted

Solution code:
N,K = [int(x) for x in input().split()]
alist = [0]+ sorted([int(x) for x in input().split()])
countlist =[]
prev =0
count =0
while len(alist)>0:
	a = alist.pop()
	if a == prev:
		count+=1
	else:
		countlist.append(count)
		count = 1
		prev = a
countlist.sort()
countlist.reverse()
print(sum(countlist[K:]))

Submission status: Accepted

Solution code:
N, K = map(int, input().split(' '))
A = map(int, input().split(' '))
bucket = {}

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

if len(bucket.keys()) <= K:
    print('0')
else:
    turn_over = 0
    turn_num = len(bucket.keys())
    for i in sorted(bucket.values()):
        turn_over += i
        turn_num -= 1
        if turn_num <= K:
            print(turn_over)
            break