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: Time Limit Exceeded

Solution code:
from collections import Counter
N,K = [int(i) for i in input().split()] 
A = [int(i) for i in input().split()]
counter = Counter(A)
ans = 0
counter_list = counter.most_common()
if K < len(counter_list):
    while (K!=len(counter_list)):
        ans += counter_list[-1][1]
        counter_list = counter_list[:-1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8                                                                  

from collections import defaultdict

N, K = map(int, raw_input().split())
As = map(int, raw_input().split())

dic = defaultdict(lambda: 0)

for a in As:
    dic[a] += 1

count = 0
while True:
    if len(dic) <= K:
        break
    dekey = min(dic, key=(lambda x: dic[x]))
    count += dic[dekey]
    del dic[dekey]

print count

Submission status: Time Limit Exceeded

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

freq = {}
a = [int(i) for i in input().split()]

for i in a:
    if i not in freq:
        freq[i] = 1
    else:
        freq[i] += 1

total = 0
while len(freq.keys()) > k:
    mn = (0,2000001)
    for i in freq.keys():
        if freq[i] < mn[1]:
            mn = (i,freq[i])
    total += mn[1]
    del freq[mn[0]]
print(total)

Submission status: Time Limit Exceeded

Solution code:
import collections
n, k = map(int, raw_input().split())
num = map(int, raw_input().split())
n = int()
num = collections.Counter(num)
while True:
    if len(num) <= k:
        print n
        break
    t, v = sorted(num.items(), key=lambda x:x[1])[0]
    del num[t]
    n += v

Submission status: Time Limit Exceeded

Solution code:
import collections
n, k = map(int, raw_input().split())
num = map(int, raw_input().split())
n = int()
num = collections.Counter(num)
while True:
    if len(num) <= k:
        print n
        break
    t, v = sorted(num.items(), key=lambda x:x[1])[0]
    del num[t]
    n += v

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
st=input().split()
N=int(st[0]);K=int(st[1])
S=set([])
st=input().split()
P=[0]*(N+1)
for i in range(N):
    k=int(st[i])
    S.add(k)
    P[k]+=1
out=0
Q=list(sorted(np.array(P)))
while(True):
    if Q[0]==0:
        Q.remove(0)
    else:
        break
for i in range(max([len(S)-K,0])):
    out+=Q[i]
print(out)

Submission status: Time Limit Exceeded

Solution code:
list_ = [0]
n, min_count = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
kind = 1
tmp = a[0]
for i in range(n):
    if(tmp != a[i]):
        kind += 1
        tmp = a[i]
        list_.append(0)
    list_[kind - 1] += 1
ans = 0
for i in range(kind - min_count):
    min_idx = list_.index(min(list_))
    ans += list_[min_idx]
    list_[min_idx] = 200001
print(ans)

Submission status: Time Limit Exceeded

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

d = sorted(d.items(), key=lambda x: x[1])
count = 0
while len(d) > k:
    count += d[0][1]
    d.pop(0)
print (count)

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools
import heapq
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect, bisect_left, bisect_right
from collections import defaultdict, deque


# d = defaultdict(lambda: 0)
# setrecursionlimit(10**7)
# inf = float("inf")


##### stdin ####
def LM(t, r): return list(map(t, r))
def R(): return stdin.readline()
def RS(): return R().split()
def I(): return int(R())
def F(): return float(R())
def LI(): return LM(int,RS())
def LF(): return LM(float,RS())
def ONE_SL(): return list(input())
def ONE_IL(): return LM(int, ONE_SL())
def ALL_I(): return map(int, stdin)
def ALL_IL(): return LM(int,stdin)

##### tools #####
def ap(f): return f.append
def pll(li): print('\n'.join(LM(str,li)))
def pljoin(li, s): print(s.join(li))



##### main #####		
def main():
	N,K = LI()
	A = LI()
	set_A = set(A)
	d = {}
	for a in set_A:
		d[a] = A.count(a)

	ans=0
	max_v,max_k = max((v,k) for (k,v) in d.items())
	while len(d)>K:
		min_v,min_k = min((v,k) for (k,v) in d.items())
		d[max_k] += min_v
		ans += min_v
		del d[min_k]

	print(ans)


if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools
import heapq
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect, bisect_left, bisect_right
from collections import defaultdict, deque


# d = defaultdict(lambda: 0)
# setrecursionlimit(10**7)
# inf = float("inf")


##### stdin ####
def LM(t, r): return list(map(t, r))
def R(): return stdin.readline()
def RS(): return R().split()
def I(): return int(R())
def F(): return float(R())
def LI(): return LM(int,RS())
def LF(): return LM(float,RS())
def ONE_SL(): return list(input())
def ONE_IL(): return LM(int, ONE_SL())
def ALL_I(): return map(int, stdin)
def ALL_IL(): return LM(int,stdin)

##### tools #####
def ap(f): return f.append
def pll(li): print('\n'.join(LM(str,li)))
def pljoin(li, s): print(s.join(li))



##### main #####		
def main():
	N,K = LI()
	A = LI()
	set_A = set(A)
	d = {}
	for a in set_A:
		d[a] = A.count(a)

	ans=0
	while len(d)>K:
		min_v,min_k = min((v,k) for (k,v) in d.items())
		max_v,max_k = max((v,k) for (k,v) in d.items())
		d[max_k] += min_v
		ans += min_v
		del d[min_k]

	print(ans)


if __name__ == '__main__':
	main()

Submission status: Memory Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
a=[0]*10**8
cnt=0
for i in range(N):
    if a[A[i]]==0:
        cnt+=1
    a[A[i]]+=1
a.sort(reverse=True)
sum=0
for i in range(cnt-K):
    sum+=a[cnt-i-1]

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
a_list = list(map(int, input().split()))
counter_list = [0]*N
for i in a_list:
    counter_list[i-1] += 1

counter_list.sort(reverse=True)

ans = N - sum(counter_list[:K])    

print(ans)

Submission status: Accepted

Solution code:
import collections as c
n,k = map(int,input().split())
x = list(map(int,input().split()))
y = c.Counter(x)
s = []
for i in y.items():
    s.append([i[1],i[0]])
s.sort()
ans = 0
for i in range(len(s)-k):
    ans += s[i][0]
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

N,K = map(int,input().split())
A = list(map(int,input().split()))
nums = [0]*N

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

nums.sort()

nums = deque(nums)

while True:
  if nums[0] == 0:
    nums.popleft()
  else:
    break
nums = list(nums)
if len(nums) <= K:
  print(0)
else:
  print(sum(nums[:len(nums)-K]))

Submission status: Accepted

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

dic={}
for i in A:
    if i not in dic.keys():
        dic[i]=1
    else:
        dic[i]+=1
dic=sorted(dic.items(),key=lambda x:x[1],reverse=True)
k=0
ans=0
for key,value in dic:
    if k>=K:
        ans+=value
    k+=1
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import Counter
import sys
sys.setrecursionlimit(10**6)


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

c = Counter(a)

c = list(c.most_common())
# c = c.most_common()
len_c = len(c)
# print(len_c)
# print(c)
ans = 0
while(len_c-k > 0):
    # print(len_c-k)
    # print()
    # print(k)
    i, j = c.pop()
    len_c -= 1
    ans += j
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 23 15:50:08 2020

@author: liang
"""

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

for a in A:
    d[a-1] += 1
    
d.sort(reverse=True)

ans = N - sum(d[:K])

print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
L = [0] * n
ans = 0

for a in A:
  L[a-1] += 1
L = sorted(L)
for i in range(n-k):
  ans += L[i]

print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
D = dict.fromkeys(range(1,n+1),0)
ans = 0
for a in A:
  D[a] += 1
D = sorted(D.items(), key=lambda x:x[1])
for i in range(n-k):
  ans += D[i][1]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter as CC

N, K = map(int, input().split())
A = list(map(int, input().split()))
B = CC(A)
C = sorted(B.values())
print(sum(C[:max(0, len(C) - K)]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

num = Counter(s)


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

else:
    cnt = len(num) - k
    ans = 0
    i = 0
    for v in sorted(num.values()):
        if cnt == i:
            break
        ans += v
        i += 1

    print(ans)

Submission status: Accepted

Solution code:
import math
from math import gcd,pi,sqrt
INF = float("inf")
MOD = 10**9 + 7

import sys
sys.setrecursionlimit(10**6)
import itertools
import bisect
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n,k = i_map()
    a = i_list()
    dic = {}
    for i in a:
        dic[i] = dic.get(i,0) + 1

    l = len(dic.keys())

    if l <= k:
        print(0)
        exit()
    l = l - k
    ans = 0
    cnt = 0

    for i in sorted(dic.values()):
        ans += i
        cnt += 1
        if cnt == l:
            break
    print(ans)







if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n,k= map(int,input().split())
Xl = list(map(int,input().split()))
from collections import Counter
Xdic = Counter(Xl)
valuelist = sorted(list(Xdic.values()), reverse=True)
print(sum(valuelist[k:]))

Submission status: Accepted

Solution code:
from collections import Counter

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

L = Counter(A)

ans = N
for a, cnt in L.most_common(K):
  ans -= cnt

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

d={}

for n in range(N):
    if d.get(A[n])==None:
        d[A[n]]=1
    else:
        d[A[n]]+=1

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

if K>=len(d):
    print(0)
else:
    cnt=0
    ans=0
    for n in range(len(dn)):
        ans+=dn[n][1]
        cnt+=1
        if cnt==len(d)-K:
            break
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

kouho = CA.most_common()[::-1]
ans = 0
idx = 0
while idx < len(CA) - k:
    key, val = kouho[idx]
    ans += val
    idx += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

main()

Submission status: Accepted

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

Submission status: Accepted

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

a=collections.Counter(a)
key=list(a.values())
key.sort()
ans=sum(key[:len(key)-k])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter, deque, defaultdict
n, k = map(int, input().split())
A = list(map(int, input().split()))
t = defaultdict(int)
for x in A:
    t[x]+=1
B = list(t.values())
B.sort()
if(len(B)<=k):
    print(0)
    exit()

ans = sum(B[:len(B)-k])
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter, deque, defaultdict
n, k = map(int, input().split())
A = list(map(int, input().split()))
t = defaultdict(int)
for x in A:
    t[x]+=1
B = []
for x in t.values():
    B.append(x)
B.sort()
if(len(B)<=k):
    print(0)
    exit()
    
ans = 0
for i in range(0, len(B)):
    ans+=B[i]
    if len(B)-(i+1) <= k:
        break
print(ans)

Submission status: Accepted

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

a = collections.Counter(a)
key = list(a.values())
#print(key)
key.sort()
#print(key)
ans = sum(key[:len(key)-k])
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
X=[0 for _ in range(N+1)]
for a in A:
    X[a]+=1
X.sort(reverse=True)
print(N-sum(X[0:K]))

Submission status: Accepted

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

a_dict = {}

for i in range(N):
    if a[i] not in a_dict.keys():
        a_dict[a[i]] = 1
    else:
        a_dict[a[i]] += 1

ans = 0
if len(a_dict) <= K:
    print(ans)
else:
    sort_a_dict_list = sorted(a_dict.items(), key=lambda x:x[1])

    for j in range(len(a_dict) - K):
        ans += sort_a_dict_list[j][1]
    print(ans)

Submission status: Accepted

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

kind = {}
for a in an:
  if not a in kind:
    kind[a] = 1
  else :
    kind[a] += 1

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

if len(sortedKind) > k:
  count = 0
  for check in sortedKind[:len(sortedKind)-k]:
    count += check[1]
  print(count)
else :
  print(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

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

values,counts = zip(*collections.Counter(A).most_common())
ans = 0

B = counts[K:]

for i in B:
    ans += i
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
import collections
c = collections.Counter(a)
li = []
ans = 0
for v,num in  c.items():
    li.append((v,num))
ll = sorted(li,key=lambda x: x[1],reverse=True)

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

print(ans)

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.keys():
    dic[a] += 1
  else:
    dic[a] = 1
    
dic = sorted(dic.items(), key = lambda x:x[1], reverse=True)

ans = 0
for (i,j) in dic[K:]:
  ans += j
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

count = {}
for val in A:
    if val in count:
        count[val] += 1
    else:
        count[val] = 1

ans = 0
diff = len(set(A)) - K
if diff > 0:
    count_sorted = sorted(count.items(), key=lambda x:x[1])
    for k,v in count_sorted:
        ans += v
        diff -= 1
        if diff == 0:
            break

print(ans)

Submission status: Accepted

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

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

dic_sort=sorted(dic.values(),reverse=True)

print(sum(dic_sort[K:]))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
from collections import Counter
s = list(map(int,input().split()))
num = Counter(s)
if len(num) <= k:
    print(0)
else:
    n = len(num) - k
    a = 0
    ans = 0
    for i in sorted(num.values()):
        if a == n:
            break
        
        ans += i
        a += 1
        
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
 
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
# N=O(10^5)なので、O(N)で解く
# ABC-155-C-Poll の類題
# 方針：各数字の出現回数を数える
 
# Counter(リスト) は辞書型のサブクラスであり、キーに要素・値に出現回数という形式
# Counter(リスト).most_common() は(要素, 出現回数)というタプルを出現回数順に並べたリスト
A = Counter(A).most_common()
 
# 出現回数の大きい方からK種類はそのままにしておく。これによって、変えない個数が求まる。
# 変える個数は、N-(変えない個数)
s = 0
for i in range(min(K, len(A))):
  s += A[i][1]
  
print(N-s)

Submission status: Accepted

Solution code:
from collections import Counter

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

# N=O(10^5)なので、O(N)で解く
# ABC-155-C-Poll の類題
# 方針：各数字の出現回数を数える

# Counter(リスト) は辞書型のサブクラスであり、キーに要素・値に出現回数という形式
# Counter(リスト).most_common() は(要素, 出現回数)というタプルを出現回数順に並べたリスト
A = Counter(A).most_common()

# 出現回数の大きい方からK個はそのままにしておく(=変えない個数）
# 変える個数は、N-(変えない個数)
s = 0
for i in range(min(K, len(A))):
  s += A[i][1]
  
print(N-s)

Submission status: Accepted

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

# N=O(10^5)なので、O(N)で解く
# ABC-155-C-Poll の類題

# 各数字の出現回数を数える
dic = {}
for i in range(N):
  a = A[i]
  if a not in dic: dic[a] = 1
  else: dic[a] += 1

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

# dicは [(5, 1), (3, 2), (2, 2), (4, 2), (1, 3)] のようになっている
# dicのvalueにおいて、前からlen(dic)-K個の和を取る
ans = 0
for i in range(max(len(dic)-K, 0)):
  ans += dic[i][1]
  
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(N + 1)]

for a in A:
    B[a] += 1
nums = [b for b in B if b > 0]
nums.sort()
n = len(nums)
print(0 if n<=K else sum(nums[:n - K]))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [0 for i in range(N + 1)]

for a in A:
    B[a] += 1
nums = [b for b in B if b > 0]
nums.sort()
n = len(nums)
print(0 if n<=K else sum(nums[:n - K]))

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(), reverse=True,key=lambda x:x[1])

sum=0
for i in range(min(K,len(cnt))):
    sum+=cnt_sorted[i][1]

print(N-sum)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
num_map = dict()
for i in range(n):
    if a[i] not in num_map:
        num_map[a[i]] = 1
    else:
        num_map[a[i]] += 1

rest_key_num = max(0, len(num_map) - k)
values = sorted(list(num_map.values()))
res = 0
for j in range(rest_key_num):
    res += values[j]
print(res)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
x=[0]*(2*10**5+1)
ans=0
for i in range(n):
    x[a[i]]+=1
z=[x[i] for i in range(2*10**5+1) if x[i]!=0]
z=sorted(z)
for i in range(max(len(z)-k,0)):
    ans+=z[i]
print(ans)

Submission status: Accepted

Solution code:
def count_list(lst):
    count_lst = [0] * (max(lst) + 2) #0からmax(lst)+1までの個数のリスト

    for i in lst:
        count_lst[i] += 1

    return count_lst


def main():
    n, k = map(int, input().split())
    a_lst = list(map(int, input().split()))
    new_lst = list(set(a_lst))
    types = len(new_lst)

    if types <= k:
        print(0)
    else:
        erase = types - k
        count_lst = count_list(a_lst)
        count_lst.sort()
        count = 0

        tmp = 0
        for i in count_lst:
            if i != 0:
                count += i
                tmp += 1

            if tmp == erase:
                break

        print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
import collections
a = collections.Counter(A)
b = a.most_common()
ans = 0
if len(b) > K:
    for i in range(K,len(b)):
        ans += b[i][1]
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(N+1)]
 
for a in A:
  B[a] += 1
nums = [b for b in B if b > 0]
nums.sort()
n = len(nums)
if n <= K:
  print(0)
else:
  print(sum(nums[:n-K]))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
from collections import Counter
ac=sorted(list(Counter(a).values()))
le=len(ac)
if le<=k:
  print(0)
else:
  count=0
  for i in range(le-k):
    count+=ac[i]
  print(count)

Submission status: Accepted

Solution code:
# vim: fileencoding=utf-8


def main():
    n, k = map(int, input().split())
    li = [0] * n
    a = list(map(int, input().split()))
    for i in a:
        li[i - 1] += 1

    li.sort(reverse=True)
    print(sum(li[k:]))


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

sum = 0
c = Counter(a).most_common()
if K > len(c):
    print(0)
    exit()
for i in range(K):
    sum += c[i][1]
print(len(a) - sum)

Submission status: Accepted

Solution code:
import collections
N ,K = map(int,input().split())
lsA = list(map(int,input().split()))
counterA = collections.Counter(lsA)
valu = list(counterA.values())
valu.sort()
kind = len(set(lsA))
if kind <= K:
    ans = 0
else:
    ans = sum(valu[:kind - K])
print(ans)

Submission status: Accepted

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

a.sort()

times=[0]*(n+1)
for i in a:
    times[i]+=1
times.sort()


for i in range(n+1):
    if times[i]!=0:
        j=i
        break

ans=0
if len(times)>k-j:
    for i in range(j,len(times)-k):
        ans+=times[i]
print(ans)

Submission status: Accepted

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

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

replace_num = len(num_dic.keys()) - K
replace_num = 0 if replace_num < 0 else replace_num
ans = 0
for i, taple in enumerate(sorted(num_dic.items(), key = lambda x:x[1])):
  if i == replace_num:
    break
  ans += taple[1]

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
input = lambda: sys.stdin.readline().rstrip()

def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a_cnt = sorted(list(Counter(a).values()))
    s = set(a)
    t = max(0, len(s) - k)
    print(sum(a_cnt[0:t]))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint
from statistics import mean,median,mode

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

def MI():
    return map(int,input().split())

def RI():
    return list(map(int,input().split()))

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input()) for _ in range(n) ]

ddict = defaultdict(lambda: 0)
# ddict = defaultdict(lambda: 1)
# ddict = defaultdict(lambda: int())
# ddict = defaultdict(lambda: list())
# ddict = defaultdict(lambda: float())

n,k = MI()
a = RI()

for i in range(n):
    ddict[a[i]] += 1

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

e = d[k:]
ans = 0
for i in range(len(e)):
    ans += e[i][1]
print(ans)

Submission status: Accepted

Solution code:
import bisect as bs
N, K = (int(x) for x in input().split())
A = sorted([int(x) for x in input().split()])
f = lambda X, x: bs.bisect_right(X,x)-bs.bisect_left(X,x)
cnt = sorted([f(A,a) for a in set(A)],reverse=True)
vrt = len(cnt)
print(sum(cnt[K:]) if vrt > K else 0)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A_dict = dict()
for i in range(N):
    if A_dict.get(A[i]) is None:
        A_dict[A[i]] = 1
    else:
        A_dict[A[i]] += 1
A_dict_len = len(A_dict.keys())
ans = 0
A_dict_sorted = sorted(A_dict.items(), key=lambda a: a[1])
#print(A_dict_sorted)
for i in range(max(0, A_dict_len - K)):
    ans += A_dict_sorted[i][1]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import *
n,m=map(int,input().split())
l=list(map(int,input().split()))
k=len(set(l))
if(k<=m):
    print(0)
else:
    d=defaultdict(int)
    for i in l:
        d[i]+=1
    f=list(d.values())
    f.sort()
    g=k-m
    c=0
    for i in range(g):
        c+=f[i]
    print(c)

Submission status: Accepted

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

dic={}

for i in a_list:
    if i in dic:
        dic[i]+=1
    else:
        dic[i]=1

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

ans=0

if len(dic)>k:
    num=len(dic)-k
    for i in range(num):
        ans+=dic2[i][1]

    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

a_cnt = collections.Counter(a)
#print(a_cnt)
a_sort = sorted(a_cnt.values(),reverse=True)
#print(a_sort)
#print(a_sort[k:])
print(sum(a_sort[k:]))

Submission status: Accepted

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

X = [0] * N

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

X = sorted(X, reverse=True)

S = sum(X[:K])

ans = N - S

print(ans)

Submission status: Accepted

Solution code:
import collections
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
count=0
b=len(collections.Counter(a))
c=collections.Counter(a).most_common()
#print(b)
for i in range(min(k,b)):
    count+=c[i][1]
#    print(count)
ans=len(a)-count
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

B = [0] * (N + 1)
for i in range(N):
    B[A[i]] += 1
B.sort(reverse=True)
cnt = 0
# 利用可能な種類数がK個なのでN-Kで答えになる
for i in range(K):
    cnt += B[i]
print(N - cnt)

Submission status: Accepted

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

B = [0]*(N+1)
for i in range(N):
    B[A[i]] += 1
B.sort(reverse = True)
cnt = 0
for i in range(K):
     cnt += B[i]
print(N - cnt)