Problem:


Score : 300 points

Problem Statement

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

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

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

Constraints

1 \leq K \leq N \leq 200000

1 \leq A_i \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

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

Sample Input 1

5 2
1 1 2 2 5

Sample Output 1

1

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

Sample Input 2

4 4
1 1 2 2

Sample Output 2

0

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

Sample Input 3

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

Sample Output 3

3



Sample input:
5 2
1 1 2 2 5


Sample output:
1

Submission status: Accepted

Solution code:
import collections

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

#数える
a = collections.Counter(a)

#ソートする
a_sort = sorted(a.items(), key=lambda x:x[1])

#必要な分だけ足す
out = 0
if len(a_sort) <= k:
    print(0)
else:
    for i in range(len(a_sort)-k):
        out += a_sort[i][1]
    print(out)

Submission status: Accepted

Solution code:
N,K=map(int, input().split())
A=list(map(int, input().split()))
ball={}
result=0
for i in range(N):
  if A[i] in ball.keys():
    ball[A[i]]+=1
  else:
    ball[A[i]]=1
sorted_ball = sorted(list(ball.values()))
if len(sorted_ball) > K:
  for i in range(len(sorted_ball)-K):
    result+=sorted_ball[i]
print(result)

Submission status: Accepted

Solution code:
from collections import Counter

n,k=map(int,input().split())
a=list(map(int,input().split()))
b=sorted(Counter(a).values())

print(sum(b[:(len(b)-k)]))

Submission status: Accepted

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

dic={}

for i in range(n):
    if not a[i] in dic:
        dic[a[i]]=0
    dic[a[i]]+=1

keys=list(dic.keys())
list=[]

for i in range(len(keys)):
    list.append(dic[keys[i]])

list.sort(reverse=True)
count=0
for i in range(k,len(list)):
    count+=list[i]
print(count)

Submission status: Accepted

Solution code:
k,n=map(int,input().split())
a = list(map(int,input().split()))
a=sorted(a)
d=[]
cnt=1
for i in range(k-1):
  if a[i]==a[i+1]:
    cnt+=1
  else:
    d.append(cnt)
    cnt=1
d.append(cnt)
d=sorted(d)
ans=0
for i in range(len(d)-n):
  ans+=d[i]
print(ans)

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:
        dic[i] += 1
    else:
        dic[i] = 1

lis = []

for i in dic:

    lis.append(dic[i])

lis.sort()

ans = 0

num = len(lis) - K
if num < 0:
    num = 0

for i in range(num):
    ans += lis[i]

print (ans)

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

Submission status: Accepted

Solution code:
import bisect
n,k = map(int,input().split())
a = [int(_) for _ in input().split()]

a.sort()

b = [0]*(len(set(a)))

ind = 0
for i in set(a):
    b[ind] = bisect.bisect_right(a,i)-bisect.bisect_left(a,i)
    ind += 1

b.sort()

print(sum(b[:max(0,len(b)-k)]))

Submission status: Accepted

Solution code:
from collections import Counter

N, K = map(int, input().split())
*A, = map(int, input().split())
lst = sorted(Counter(A).values())
print(sum(lst[:len(lst)-K]))

Submission status: Accepted

Solution code:
from collections import Counter

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

a = Counter(A).most_common()

L = []
if K < len(a):
    need = len(a) - K
    for i in range(1, need+1):
        L.append(a[-i][1])

print(sum(L))

Submission status: Accepted

Solution code:
import collections

n,k = map(int,input().split())
lst = list(map(int,input().split()))
lst_com = collections.Counter(lst).most_common()
if len(lst_com)<=k:
    print(0)
    exit()
ans=n
for i in range(k):
    ans -= lst_com[i][1]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

dd=sorted(list(d.values()))

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

print(ans)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

c = collections.Counter(l)
c = c.most_common()
cnt = 0
if len(c) - K > 0:
    for i in range(1 , len(c) - K + 1):
        cnt += c[-i][1]

print(cnt)

Submission status: Accepted

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


c=collections.Counter(A)

a=(c.most_common()[K:])

ans=0
if a==[]:
  print(ans)
else:
  for i in range(len(a)):
    ans+=a[i][1]
  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()])
values,counts=zip(*A.most_common())
a=len(values)
#print(counts)
#print(a)
if k<a:
    ans=0
    for i in range(k,a):
#        print(counts[i])
        ans+=counts[i]
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
# たくさんあるものから優先していきのこらせる
import numpy as np
N,K = map(int,input().split())
A = np.array(input().split(), dtype=np.int32)
counter = np.bincount(A)
counter.sort()
counter = counter[::-1] # desc order
no_change = counter[:K].sum()
answer = N - no_change
print(answer)

Submission status: Accepted

Solution code:
nk = input().split(" ");
n = int(nk[0]);
k = int(nk[1]);
a = input().split(" ");
num = 0;
for i in range(n) :
	a[i] = int(a[i]);
bucket = [0]*max(a);
for i in range(n) :
	bucket[a[i]-1] += 1;
	if bucket[a[i]-1] == 1 :
		num += 1;
bucket.sort();
ans = 0;
j = 0;
for i in range(max(a)) :
	if j >= num -k :
		break;
	if bucket[i] > 0 :
		ans += bucket[i];
		j += 1;
print(ans);

Submission status: Accepted

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


def solve(N: int, K: int, A: "List[int]"):
    C = collections.Counter(A)
    M = len(C) - K
    if M <= 0:
        print(0)
    else:
        print(sum([c[1] for c in reversed(C.most_common())][0:M]))


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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]"
    solve(N, K, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
from collections import Counter
counters = Counter(a)
b = sorted(counters.items(), key=lambda x: x[1])
left = len(b) - k
index = 0
ans = 0
while left > 0:
    ans += b[index][1]
    index += 1
    left -= 1
print(ans)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(a_list)
c = c.most_common()

changenum = len(c) - k
if changenum <= 0:
    print(0)
    exit()

changecount=0
for changeidx in range(changenum):
    index = -1 - changeidx
    changecount += c[index][1]
print(changecount)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
from collections import defaultdict
dic = defaultdict(lambda : 0)
for a in input().split():
    a = int(a)
    dic[a] += 1
diff = len(dic.keys()) - k
if n == k or diff <= 0:
    print(0)
else:
    count = sum(sorted(dic.values())[:diff])
    print(count)

Submission status: Accepted

Solution code:
from collections import Counter
n, k = map(int, input().split())
a = Counter(list(map(int, input().split())))
if k > len(a):
    print(0)
    exit()
else:
    print(sum(sorted(list(a.values()), reverse=True)[k:]))

Submission status: Accepted

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

a.sort()
key = 0
cnt = 0
b = []
for i in range(n):
    if a[i] != key:
        key = a[i]
        b.append(cnt)
        cnt = 1
    else:
        cnt += 1
b.append(cnt)
del b[0]

b.sort()
if len(b) < k:
    print(0)
else:
    print(sum(b[:len(b)-k]))

Submission status: Accepted

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

    id2count = {}
    for x in A:
        if x not in id2count:
            id2count[x] = 1
        else:
            id2count[x] += 1

    sorted_items = sorted(id2count.items(), key=lambda x: x[1])
    total = 0
    for (key, count) in sorted_items[:len(sorted_items)-int(K)]:
        total += count
    print(total)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

from collections import Counter

cnt = Counter(a)

value = sorted(cnt.values(),reverse=True)

flg = 0
if len(value)<=k:
    print(0)
else:
    for i in range(k):
        flg += value[i]

    print(n-flg)

Submission status: Accepted

Solution code:
from collections import Counter
N,K = map(int,input().split())
A = Counter(list(map(int,input().split())))
V = []
for v in A.values():
    V.append(v)
V.sort(reverse = 1)
print(sum(V[K:]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, raw_input().split())
a_arr = map(int, raw_input().split())

a_dic = {}

for idx in range(0, N):
  if str(a_arr[idx]) in a_dic:
    a_dic[str(a_arr[idx])] += 1
  else:
    a_dic[str(a_arr[idx])] = 1

a_len = len(a_dic.keys())

# https://qiita.com/shizuma/items/40f1fe4702608db40ac3
counter = 0
sum_val = 0
for k, v in sorted(a_dic.items(), key=lambda x: x[1]):
  if a_len - counter <= K:
    break
  sum_val += v
  counter += 1

print(sum_val)

# https://atcoder.jp/contests/abc081/tasks/arc086_a

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

a_count = Counter(a)

a_c = []
for i in a_count:
    a_c.append([int(i),a_count[i]])

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

ans = 0
if k >= len(a_c_sort):
    print(0)
else:
    for i in range(len(a_c_sort)-k):
        ans += a_c_sort[i][1]
    print(ans)

Submission status: Accepted

Solution code:
import collections

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

a = collections.Counter(A)

del_num = len(a)-K
print (sum(sorted(a.values())[:del_num]))

Submission status: Accepted

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

dic = {}

for a in A:
    if a in dic:
        dic[a] += 1
    else:
        dic[a] = 1
temp = list(dic.values())
ans = 0
temp.sort()
for i in range(len(temp)-k):
    ans += temp[i]
    
print(ans)

Submission status: Accepted

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

dic = dict()
for i in A:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] =1
dic_sort = sorted(dic.items(), key = lambda x: x[1])

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

print (ans)


# print (dic_sort)

Submission status: Accepted

Solution code:
# ABC081C - Not so Diverse (ARC086C)
n, k = list(map(int, input().rstrip().split()))
lst = sorted(list(map(int, input().rstrip().split())))
dic = {}
for i in lst:
    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

cnt = sorted([i for i in dic.values()], reverse=True)
ans = 0
for i in range(len(cnt)):
    if k > 0:
        k -= 1
        continue
    ans += cnt[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N,K=map(int,input().split())
A=list(map(int,input().split()))
C=defaultdict(int)
B=[]
for a in A:
    C[a]+=1
ans=0
for i,v in C.items():
    B.append(v)
B.sort(reverse=True)
p=0
for i in range(len(B)):
    if i<K:
        continue
    ans+=B[i]
print(ans)

Submission status: Accepted

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

a_set = set(a)
a_counts = {}

for elem in a:
    if not elem in a_counts:
        a_counts[elem] = 0
    a_counts[elem] += 1

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

ball_to_replace = len(a_set) - k

result = 0
if ball_to_replace > 0:
    for i in range(ball_to_replace):
        result += a_counts[i][1]
print(result)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A1 = set(A)
AN = {}
for a in A:
    if not a in AN:
        AN[a] = 0
    AN[a] += 1
AN = sorted(AN.items(), key=lambda x: x[1])
ans = 0
K1 = len(A1) - K
if K1 > 0:
    for k in range(K1):
        ans += AN[k][1]
    print(ans)
else:
    print('0')

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,m=LI()
  l=LI()

  l.sort()
  a=l[0]
  l2=[]
  c=1
  for x in l[1:]:
    if a==x:
      c+=1
    else:
      l2.append([a,c])
      c=1
      a=x
  l2.append([a,c])

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

  ans=0
  for i in range(len(l2)-m):
    ans+=l2[i][1]

  return ans

print(main())

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial
from heapq import heappop, heappush, heappushpop
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    from collections import Counter
    N, K = MI()
    A = LI()
    c = Counter(A).most_common()
    c = [j for i, j in c]
    ans=0
    for val in c[K:]:
        ans += val
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial
from heapq import heappop, heappush, heappushpop
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    from collections import Counter
    N, K = MI()
    A = LI()
    c = Counter(A)
    c = [j for i, j in c.items()]
    c.sort()
    change_num = len(c) - K
    ans=0
    for C in c:
        if change_num <= 0:
            break
        ans += C
        change_num -= 1
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

def main():
    num, k = map(int, input().split())
    data = list(map(int, input().split()))
    dic = defaultdict(int)
    for i in range(num):
        dic[data[i]] += 1

    dic = sorted(dic.items(), key=lambda x: -x[1])
    ans = 0
    for i in range(min(k, len(dic)), len(dic)):
        ans += dic[i][1]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
countlist = []
tmp = 0
for i in range(0, N):
	if tmp != A[i]:
		countlist.append([A[i], 0])
		tmp = A[i]
	countlist[-1][1] += 1
countlist.sort(key = lambda v: v[1])
sum = 0
for i in range(0, len(countlist)-K):
	sum += countlist[i][1]
print(sum)

Submission status: Accepted

Solution code:
from collections import Counter


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


keep = 0
for _, val in ctr_A.most_common(k):
    keep += val


print(n - keep)

Submission status: Accepted

Solution code:
from collections import Counter


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


set_A = set(lst_A)
cnt_A = sorted(Counter(lst_A).items(), key=lambda x:x[1])
num_change = len(set_A) - k
ans = 0
if num_change > 0:
    for i in range(num_change):
        ans += cnt_A[i][1]


print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

l = sorted(d.values())
print(sum(l[:len(l) - k]))

Submission status: Accepted

Solution code:
N,K = list(map(int,input().split(" ")))
d={}
for i in input().split(" "):
    if i in d:
        d[i]+=1
    else:
        d[i]=1
print(sum(sorted(d.values())[:len(d.keys())-K:]))

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(a)

arr = c.most_common()[k:]

print(sum(x[1] for x in arr))

Submission status: Accepted

Solution code:
from collections import Counter

n, k = map(int, input().split())
arr = Counter(input().split())
chenge = len(arr) - k
count = 0
for k, v in arr.most_common()[::-1]:
  if chenge > 0:
    count += v
    chenge -= 1
print(count)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(a)

arr = c.most_common()[k:]

print(sum(x[1] for x in arr))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
S=set(a)
dic={i:0 for i in S}
for i in a:
    dic[i]+=1
cnt=0
l=sorted(dic.values())
if len(l)>k:cnt+=sum(l[:len(l)-k])
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

TMP = [0] * N

for i in A:
    TMP[i-1] += 1

filter_TMP = sorted([i for i in TMP if i > 0], reverse=True)

max_ball = 0
cnt = 1
for i in filter_TMP:
    if cnt <= K:
        max_ball += i
        cnt += 1
    else:
        break
    

print(N - max_ball)

Submission status: Accepted

Solution code:
n,k,*a=map(int,open(0).read().split())
d={}
for i in a:d[i]=d.get(i,0)+1
print(sum(sorted(d.values())[:-k]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

count_list = [ 0 for i in range(n) ]
for i in num_list:
	count_list[i-1] += 1
count_list.sort(reverse = True)
print(n-sum(count_list[:k]))

Submission status: Accepted

Solution code:
from collections import Counter as ct
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
count=ct(A)
div=len(count)
if div<=K:print(0)
else:print(sum([i[1] for i in sorted(count.items(),key= lambda x:x[1])[:div-K]]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import Counter
import heapq
INF = float("inf")


def solve(N: int, K: int, A: "List[int]"):
    counter = Counter(A)
    # print(counter)
    values = sorted(counter.values())
    # print(values)
    if len(values) < K:
        print(0)
    else:
        print(sum(values[:len(values)-K]))
    return


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]"
    solve(N, K, A)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,k=LI()
  a=LI()

  a.sort()
  b=[]

  _a=a[0]
  c=1
  for x in a[1:]:
    if _a==x:
      c+=1
    else:
      b.append([_a,c])
      _a=x
      c=1
  b.append([_a,c])

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

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

  return ans

print(main())

Submission status: Accepted

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

Submission status: Accepted

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

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

max_n = 200000

ball_counts = [0]*max_n

for ball in balls:
    ball_counts[ball-1] += 1


counts = []

for n in ball_counts:
    if n !=0:
        counts.append(n)
        
counts = sorted(counts)

print(sum(counts[0:len(counts)-k]))

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())[:-K]))

Submission status: Accepted

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

c = Counter(A).most_common()
c.sort(key=lambda x: -x[1])
ans = 0
while len(c) > K:
    ans += c.pop()[1]
print(ans)

Submission status: Accepted

Solution code:
[n, k] = [int(x) for x in input().split()]
num_list = [int(x) for x in input().split()]
num_dict = {}
for num in num_list:
    num_dict[num] = num_dict.get(num, 0) + 1
sorted_count = sorted(num_dict.values())
if len(sorted_count) - k > 0:
    print(sum(sorted_count[:len(sorted_count)-k]))
else:
    print(0)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
nums = list(map(int,input().split()))
nums.sort()
dif = []
cnt=[]
for i in range(N-1):
  if nums[i] != nums[i+1]:
    dif.append(i+1) #玉の番号の変わり目を記録する
if len(dif)==0:
  print(0)
else:
  for i in range(len(dif)+1):
    if i == 0:
      cnt.append(dif[0])
    if i == len(dif):
      cnt.append(N-dif[-1])
      break
    cnt.append(dif[i]-dif[i-1])
  cnt.sort(reverse=True)#それぞれの球が何個ずつあるかのリスト作成
  n = 0
  if len(cnt)<=K:
    print(0)
  else:
    for i in range(K):
      n+=cnt[i]
    print(N-n)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
dic=dict({})
for a in A:
    if a in dic:
        dic[a]+=1
    else:
        dic[a]=1
print(sum(sorted(dic.values())[:-K]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 2019/05/20
# AtCoder Beginner Contest 081 - C

# 1. 集計
# 2. ソート
# 3. 集計テーブルの長さ<=kまで加算(小さい方から)

from operator import itemgetter

def numcount(in_list):
    wkdict = dict()
    for i in range(len(in_list)):
        if wkdict.get(in_list[i]) == None:
            wkdict[in_list[i]] = 1
        else:
            wkdict[in_list[i]] += 1
    numlist = list()
    for x in wkdict.keys():
        wlst = list()
        wlst.append(x)
        wlst.append(wkdict[x])
        numlist.append(wlst)
    return numlist

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

# 1.
acnt = numcount(a)

# 2.
acnt.sort(key=itemgetter(1))

# 3
idx = 0
acm = 0
while(len(acnt) - idx > k):
    acm += acnt[idx][1]
    idx += 1

# Output
print(acm)

Submission status: Accepted

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

nlist = [0] * N
for i in A:
    nlist[i-1] += 1

nlist.sort(reverse = True)
cnt = 0
for i in range(K,N):
    if nlist[i] == 0: break
    cnt += nlist[i]

print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt_dict = Counter(A)
cnt_common = cnt_dict.most_common()

if len(cnt_common) <= K:
  ans = 0
else:
  n_asis = 0
  for i in range(K):
    n_asis += cnt_common[i][1]
  ans = N - n_asis

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


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

    c = Counter()
    for a in A: c[a] += 1

    oc = sorted(c.items(), key=lambda x: x[1])
    res = 0
    for i in range(len(oc)-K):
        res += oc[i][1]

    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list( map(int, input().split()) ) #ここでN個受け取る

var = [0] * (N+1)


for i in A:
    var[i] += 1
    
var = sorted(var,  reverse = True )


count = 0
for j in range( K, N+1 ):
    if var[j] == 0:
        break
    
    if var[j] != 0:
        count += var[j]
        
print(count)

Submission status: Accepted

Solution code:
from collections import Counter

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

C = Counter(A)

for i,c in enumerate(sorted(C.items(), key=lambda x: -x[1])):
	total += c[1]
	if i == K-1:
		break
print(N-total)

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.values())
print(sum(a[:len(a) - k]))

Submission status: Accepted

Solution code:
from functools import reduce
import math
import collections
def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)

    # N の逆元
    # N_inv = pow(N,MOD-2,MOD)

    # nCr
    # Nの階乗 * rの階乗の逆元 * n-rの階乗の逆元
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")

    # 大きな素数
    MOD = 10**9+7
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N, K = (int(_) for _ in input().split())
    A = list(int(_) for _ in input().split())
    d = collections.defaultdict(int)
    for a in A:
        d[a] +=1
    d = sorted(d.items(),key=lambda x:x[1])
    V = []
    for k,v in d:
        V.append(v)
    ans = 0
    for i in range(len(V)-K):
        ans += V[i]
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(A)
d = sorted(c.values())
l = len(d)
print(sum(d[0:l-K]))

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
A = Counter([int(_) for _ in input().split()])
print(sum(A.values()) - sum(dict(A.most_common(K)).values()))

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(A)
d = sorted(c.values())
l = len(d)
print(sum(d[0:l-K]))

Submission status: Accepted

Solution code:
import collections


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


main()

Submission status: Accepted

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

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

keys = sorted(m, key=lambda x:m[x])

msum = 0
for i in range(len(m) - K):
	msum += m[keys[i]]

print(msum)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
A = Counter(list(map(int, input().split()))).most_common()
ans = 0
while len(A) > K:
	ans += A[-1][1]
	del A[-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()
l = len(A)
ans = 0
while l > K:
	ans += A[-1][1]
	del A[-1]
	l = len(A)
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()
l = len(A)
ans = 0
for _ in range(N):
	if l > K:
		ans += A[-1][1]
		del A[-1]
		l = len(A)
	else:
		print(ans)
		exit()

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
A = list(map(int, input().split()))
C = Counter(A)
D = []
for c in C:
    D.append(C[c])
D.sort(reverse=True)
print(sum(D[K:]))