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()))
c = Counter(A)
cnt = 0
ans = 0
m = len(c)-k
if m < 0:
    m = 0
for k, v in sorted(c.items(), key=lambda x: x[1]):
    if cnt == m:
        break
    ans += v
    cnt += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    n, k = map(int, input().split())
    As = map(int, input().split())
    ans = 0
    for num, count in Counter(As).most_common()[::-1][:-k]:
        ans += count
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

val = sorted(A.values())
print(sum(val[:len(val) - 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)
d = c.most_common()

ans = N
for i in range(K):
  if i == len(d):
    break
  ans -= d[i][1]
print(ans)

Submission status: Accepted

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

count = Counter(a)
t = len(count)
ans = 0
for i in sorted(count.values()):
  if t <= k: break
  ans += i
  t -= 1
print(ans)

Submission status: Accepted

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

a_list=[int(i) for i in input().split()]

a_dic={}
for i in range(n):
    if a_list[i] not in a_dic:
        a_dic[a_list[i]]=1
    else:
        a_dic[a_list[i]]+=1
        


if len(a_dic)<=k:
    print(0)
else:
    #値を取り出す
    a_value=list(a_dic.values())
    
    a_value.sort(reverse=True)
    
    print(sum(a_value[k:]))

Submission status: Accepted

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

d = dict()
for v in a:
    if v not in d:
        d[v] = 1
    else:
        d[v] += 1

l = []
for v in d.values():
    l.append(v)
l.sort()

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

Submission status: Accepted

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

import io
import sys
import math

def solve(n, k, a_lst):
    # implement process
    a_set = set(a_lst)
    ak = len(a_set)
    diff = ak - k
    if diff < 1:
        return 0
    
    #ソート済み配列を検査することで、count処理を線形にする
    a_lst.sort()
    tmp = a_lst[0]
    cnt = 0
    cnt_lst = []
    for i in range(len(a_lst)):
        if tmp != a_lst[i]:
            tmp = a_lst[i]
            cnt_lst.append(cnt)
            cnt = 0
        cnt += 1
    cnt_lst.append(cnt)
    cnt_lst.sort()
    return sum(cnt_lst[0:diff])

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

    # process
    ans = str( solve(n, k, a_lst) )
    
    # output
    print(ans)
    return ans
        
### DEBUG I/O ###
_DEB = 0   # 1:ON / 0:OFF

_INPUT = """\
5 2
1 1 2 2 5
"""
_EXPECTED = """\
0
"""

def logd(str):
    """usage:
    if _DEB: logd(f"{str}")
    """
    if _DEB: print(f"[deb] {str}")

### MAIN ###
if __name__ == "__main__":
    if _DEB:
        sys.stdin = io.StringIO(_INPUT)
        print("!! Debug Mode !!")

    ans = main()

    if _DEB:
        print()
        if _EXPECTED.strip() == ans.strip(): print("!! Success !!")
        else: print(f"!! Failed... !!\nANSWER:   {ans}\nExpected: {_EXPECTED}")

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, k = map(int, input().split())
a = sorted(list(map(int, input().split())))

b = [[a[0],1]]
cnt = 0

for i in a[1:] :
    if b[cnt][0] == i:
        b[cnt][1] += 1
    else:
        b.append([i, 1])
        cnt += 1

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

ans = 0
for i, j in b_sorted[: num - k]:
    ans += j

print(ans)

Submission status: Accepted

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

a_list=[int(i) for i in input().split()]
#print(a_list)

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

#print(dic)
#print(len(dic))


#書き換えなくてよい    
if len(dic)<=k:
    print(0)
else:
    ans=sorted(dic.items(),key=lambda x:x[1],reverse=True)
    b_list=ans[k:]
    ans_sum=0
    for i in b_list:
        ans_sum+=i[1]
    print(ans_sum)

Submission status: Accepted

Solution code:
# Problem: https://atcoder.jp/contests/abc081/tasks/arc086_a
# Python 3rd Try

import sys
from collections import Counter
# import heapq,copy
# import pprint
# from collections import deque
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solver(ball_count, limit_num, ball_write_nums):
    result = 0
    total_count = Counter(ball_write_nums)
    count_list = list(total_count.values())
    count_list.sort()
    change_num = len(count_list) - limit_num
    for j in range(0, change_num, +1):
        result += count_list[j]
    return result


if __name__ == "__main__":
    K, N = MI()
    SI = LI()
    print("{}".format(solver(K, N, SI)))

Submission status: Accepted

Solution code:
from collections import defaultdict

n,k=map(int,input().split())
l=list(map(int,input().split()))
a=defaultdict(int)
for i in l:
	a[i] += 1
a=list(a.items())
if len(a) <= k:
	print(0)
else:
	ans = 0
	a.sort(key = lambda x: x[1])
	for i in range(len(a) - k):
		ans += a[i][1]
	print(ans)

Submission status: Accepted

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

dic = {}
for i in li:
  if i in dic:
    dic[i] += 1
  else:
    dic[i] = 1

val = sorted(dic.values())

ans = 0
if k > len(val):
  print(ans)
else:
  a = len(val) - k
  ans = sum(val[:a])
  print(ans)

Submission status: Accepted

Solution code:
#辞書型でリトライ
N,K = map(int, input().split(' '))
A = input().split(' ')

dic = {}
for a in range(len(A)):
    tmp = A[a]
    if not tmp in dic:
        dic[tmp] = 1
    else:
        dic[tmp] += 1

values = sorted(dic.values())
ans = 0
if len(values)<=K:
    pass
else:
    tmp = len(values)-K
    values = values[:tmp]
    ans = sum(values)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(a)
c = dict(sorted(c.items(), key=lambda x:x[1], reverse=True))

ans = sum(list(c.values())[k:])
print(ans)

Submission status: Accepted

Solution code:
# arc086_a.py
import collections
N, K = map(int, input().split())
A = list(map(int, input().split()))
C = collections.Counter(A)
C = sorted(C.values())

lenght = len(C)
# print(lenght, K, C)
print(sum(C[:(lenght-K)]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
from collections import defaultdict
dic = defaultdict(int)
for a in A:
    dic[a] += 1
sort_dic = sorted(dic.items(), key = lambda x:x[1])
n = len(sort_dic)
if n<=K: print(0);exit()
ans = 0
for i in range(n-K):
    ans += sort_dic[i][1]
print(ans)

Submission status: Accepted

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

a.sort()
ans = 0
a.append(-1)
x = []
tmp = 0
for i in range(n):
  if a[i] != a[i+1]:
    tmp += 1
    x.append(tmp)
    tmp = 0
  else:
    tmp += 1
    
x.sort(reverse=True)
print(sum(x[k:]))

Submission status: Accepted

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

count = [0]*(n+1)

for i in range(n):
    count[lis[i]] += 1

cnt = sorted(count)
nlis = []
for i in range(n+1):
    if cnt[i] != 0:
        nlis.append(cnt[i])
if len(nlis) <= k:
    print(0)
    exit()

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

Submission status: Accepted

Solution code:
def ra(a): #圧縮された要素を非保持
    ll,l=[],1
    for i in range(len(a)-1):
        if a[i]==a[i+1]:
            l+=1
        else:
            ll.append(l)
            l=1
    ll.append(l)
    return ll
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=sorted(ra(sorted(a)))
print(sum(b)-sum(b[-k:]))

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

av = list(sorted(d.values()))
ks = 0
idx = 0
print(sum(av[0:-k]))

Submission status: Accepted

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

from collections import Counter
ca = Counter(a)

# 個数が少ないものを何個削れば良いか数える
c = sorted(ca.values())
print(sum(c[:-k]))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
li = [0]*10**6
for i in range(N):
    li[A[i]] += 1
# print(li)
li.sort(reverse=True)
# print(li)
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()))
AD = sorted(list(Counter(A).values()))
print(sum(AD[:len(AD) - k]))

Submission status: Accepted

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

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

ans = 0
t = len(d.keys())
if t <= k:
  print(0)
else:
  p = t-k
  for i in sorted(d.values()):
    if p == 0: break
    ans += i
    p -= 1
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# Problem: https://atcoder.jp/contests/abc081/tasks/arc086_a
# Python 2nd Try

import sys
from collections import Counter
# import heapq,copy
# from collections import deque
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solver(givenBall, selectKind, ball_num):
    result = 0
    summary_Ball = Counter(ball_num)
    # print("{}".format(list(summary_Ball.values())))
    sortedCount = sorted(list(summary_Ball.values()))
    # print(sortedCount)
    ballAllKind = len(sortedCount)
    changeNumberKind = ballAllKind - selectKind
    if changeNumberKind <= 0:
        result = 0
    else:
        for j in range(0, changeNumberKind, +1):
            result += sortedCount[j]
    return result


if __name__ == "__main__":
    N, K = MI()
    Ai = LI()
    print("{}".format(solver(N, K, Ai)))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
sa=set(a)
nums=len(sa)
if nums<=k:
    print(0)
else:
    num=[0]*n
    for i in range(n):
        num[a[i]-1]+=1
    num.sort()
    ans=0
    for i in range(n-k):
        ans+=num[i]
    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)
ca = sorted(ca.items(), key=lambda x:x[1], reverse=True)

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

print(N-ans)

Submission status: Accepted

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

import collections
c = collections.Counter(a)

v,cts = zip(*c.most_common())
x = [int(i) for i in cts]
print(sum(x[k:]))

Submission status: Accepted

Solution code:
# Problem: atcoder.jp/contests/abc081/tasks/arc086_a
# Python 1st Try

import sys
# from collections import defaultdict
import collections as col
# import pprint
# import heapq,copy
# from collections import deque
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solver(ALLNUM, KIND, ball_numbers):
    result = 0
    # prp = pprint.pprint
    counter_result = col.Counter(ball_numbers)
    ball_num_len = len(counter_result)
    counter_result_sorted = sorted(counter_result.items(), key=lambda x: x[1])
    # prp(counter_result_sorted)
    if ball_num_len <= KIND:
        return 0
    reduce_kind = ball_num_len - KIND
    # prp(reduce_kind)
    for _, ballCount in counter_result_sorted:
        result += ballCount
        reduce_kind = reduce_kind - 1
        # prp(reduce_kind)
        if reduce_kind <= 0:
            break
    return result


if __name__ == "__main__":
    N, K = MI()
    Ai = list(map(int, sys.stdin.readline().split()))
    # print(Ai)
    print("{}".format(solver(N, K, Ai)))

Submission status: Accepted

Solution code:
import collections
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
col_A = collections.Counter(A)
lis_col_A=[]
for key,val in col_A.items():
    lis_col_A.append([key,val])
slcA = sorted(lis_col_A,key = lambda x:-x[1])
sumA = 0
for i in range(K,len(slcA)):
    sumA += slcA[i][1]
print(sumA)

Submission status: Accepted

Solution code:
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
result = 0
cnt = Counter(a)
cnt_set = set(cnt.items())
cnt_sort = sorted(cnt.items(), key=lambda x:x[1])
for i in range(len(cnt_set) - k):
    result += cnt_sort[i][1]

print(result)

Submission status: Accepted

Solution code:
import sys
import math
import numpy as np
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(map(int,input().split()))
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

N,K = I()
A = l()
c = Counter(A)
n = len(list(c))
if n <= K:
    print(0)
else:
    v = list(c.values())
    v.sort()
    print(sum(v[:n-K]))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
dp=[0]*(2*(10**5)+1)
for i in range(N):
  dp[A[i]]+=1
dp.sort(reverse=True)
bns=sum(dp[K:])
print(bns)

Submission status: Accepted

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

d=[0]*n
for i in range(n):
    d[a[i]-1]+=1

d.sort(reverse=True)

sm=0
for i in range(k):
    if i>=n:
        break
    sm+=d[i]

print(n-sm)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

def input():
    return sys.stdin.readline()[:-1]

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

def ii():
    return int(input())

def main():
    N, K = mi()
    A = list(mi())
    c = list(Counter(A).values())
    c.sort()
    print(sum(c[:-K]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

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

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

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ball=[0]*n
s=set()
for i in range(n):
    ball[a[i]-1]+=1
    s.add(a[i]-1)
kind=len(s)
ans=0
ball.sort()
for i in range(n):
    if ball[i]==0:
        continue
    if kind>k:
        kind-=1
        ans+=ball[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import Counter

n, k, *a = map(int, read().split())
c = Counter(a)

l = list(c.items())
l.sort(key=lambda x: x[1])

ans = 0
for i in range(len(set(a)) - k):
    ans += l[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()))
ans = Counter(a).most_common(k)
count = 0
for i in ans:
    count += i[1]
print(n-count)

Submission status: Accepted

Solution code:
import collections

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

check=collections.Counter(A).most_common()
ans=0
for i in range(K,len(check)):
  ans+=check[i][1]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
A = Counter(list(map(int,input().split()))).most_common()
repeat = len(set(A))-K
ans = 0
num = 0

while num < repeat:
    ans += A.pop(-1)[1]
    num += 1

print(ans)

Submission status: Accepted

Solution code:
import collections
a,b=map(int,input().split())
n=list(map(int,input().split()))
n=collections.Counter(n)
m,o=zip(*n.most_common())
o=list(o)
print(sum(o)-sum(o[:b]))

Submission status: Accepted

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

Submission status: Accepted

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

count={}

for i in range(N):
  if A[i] not in count:
    count[A[i]]=1
  else:
    count[A[i]]+=1

if(len(count)<=K):
  print(0)
else:
  x=len(count)-K #減らさなければならない種類
  answer=0
  sorted_count=sorted(count.items(),key=lambda x:x[1])
  for i in range(x):
    answer+=int(sorted_count[i][1])
  
  print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n,k = map(int,input().split())
ls = list(map(int,input().split()))
p = len(list(set(ls)))
if p <= k:
    print(0)
    sys.exit()
di=[float("inf")]*n
for i in range(n):
    if di[ls[i]-1] == float("inf"):
        di[ls[i]-1] = 1
    else:
        di[ls[i]-1] += 1
di.sort(reverse=True)
q = 0 
while True:
    s = di.pop()
    p -= 1
    q += s
    if p <= k:
        print(q)
        sys.exit()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
A = tuple(map(int, input().split()))
d = dict()
for a in A:
    d.setdefault(a, 0)
    d[a] += 1

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

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

Submission status: Accepted

Solution code:
from collections import Counter

n, k = map(int, input().split())
a = input().split()
d = sorted(Counter(a).values())
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n, k = [int(x) for x in input().split()]
a_list = [int(x) for x in input().split()]
d = defaultdict(int)
for a in a_list:
    d[a] += 1
print(sum(sorted(d.values(), reverse=True)[k:]))

Submission status: Accepted

Solution code:
from collections import defaultdict
n, k = [int(x) for x in input().split()]
a_list = [int(x) for x in input().split()]
d = defaultdict(int)
for a in a_list:
    d[a] += 1
print(sum(sorted(d.values())[:-k]))

Submission status: Accepted

Solution code:
from collections import Counter

n, k = map(int, input().split())
a = input().split()
c = sorted(Counter(a).values(), reverse=True)

print(max(0, sum(c[k:])))

Submission status: Accepted

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

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:
from collections import Counter
import sys

N, K = map(int, input().split())
A = list(input().split())
ball = [int(A[i]) for i in range(N)]
kinds = len(set(ball))
Cou = list(Counter(ball).most_common())
dele = [int(Cou[i][1]) for i in range(len(Cou) - kinds + K, len(Cou))]
print(sum(dele))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
count = [0]*max(A)
for i in A:
  count[i-1] += 1
print(sum(sorted(count,reverse=True)[K:]))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
L = [0] * 200001
for i in range(N):
  L[A[i]] += 1
L.sort(reverse = True)
ans = 0
for i in range(K):
  ans += L[i]
print(N-ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A = list(map(int,input().split()))
table = [0]*200001
ans = N
for i in range(N):
	table[A[i]] += 1
table.sort(reverse=True)
for i in range(K):
	ans -= table[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N,K,*a = map(int, open(0).read().split())
c = Counter(a)
v = sorted([x for x in c.values()])
if len(v) - K > 0:
    print(sum(v[:len(v)-K]))
else:
    print(0)

Submission status: Accepted

Solution code:
from collections import Counter
from operator import itemgetter

N,K = list(map(int,input().split()))
A = list(Counter(map(int,input().split())).items())
A = sorted(A,key=itemgetter(1),reverse = True)

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
from collections import defaultdict
d = defaultdict(int)
num_list = list(map(int,input().split()))
for tmp in num_list:
	d[tmp] += 1
    
v_list = sorted(list(d.values()) ,reverse=True)

print(n - sum(v_list[:k]))

Submission status: Accepted

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

D_list = sorted(D.items(), key=lambda x: x[1])
N = len(D_list)
ANS = 0
for t in D_list:
    if N <= K:
        break
    ANS += t[1]
    N -= 1

print(ANS)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
if len(set(a)) == k:
    print(0)
    exit()

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

s = sorted(d.items(), key=lambda x:x[1])
ans = 0
for j in range(len(s) - k):
    ans += s[j][1]

print(ans)

Submission status: Accepted

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

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


import collections

c = collections.Counter(al)

l = len(c)
cc = sorted(c.items(), key=lambda x:x[1])

res = 0

if l <= k:
    pass

else:
    for i in range(l-k):
       res += cc[i][1] 

print(res)

Submission status: Accepted

Solution code:
from collections import OrderedDict

N, K =map(int, input().split())
a = list(map(int, input().split()))
p = OrderedDict()
for i in a:
  if i not in p:
    p[i] = 1
  else:
    p[i] +=1
#print(p)
p_rev = OrderedDict(
    sorted(p.items(), key=lambda x: x[1])
)
#print(p_rev)
#print(len(p_rev))
count = 0
sum = 0
for i in p_rev.values():
  if len(p_rev) - count > K:
    sum += i
    count += 1
  else:
    break
print(sum)

Submission status: Accepted

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

ans = 0
c = collections.Counter(A_list)
if K >= len(c):
    ans = 0
else:
    foo = len(c)-K
    bar_list = list(c.values())
    bar_list.sort()
    for i in range(foo):
        ans += bar_list[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

base = np.zeros(n)
for i in range(n):
  base[a[i]-1] += 1
  
seen = np.where(base>0,1,0)
num = np.sum(seen) - k

if num <= 0:
  ans = 0
elif num > 0:
  base.sort()
  count = 0
  ans = 0
  for i in range(n):
    if base[i] != 0:
      ans += base[i]
      count += 1
      
    if count == num:
      break
    
print(int(ans))

Submission status: Accepted

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

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

b = collections.Counter(a)
b = sorted(b.items(), key=lambda x:x[1])
c = set(a)

if len(c) <= k:
    print(0)
else:
    ans = 0
    b = b[:len(b)-k]
    for i in b:
        ans +=i[1]
        
    print(ans)

Submission status: Accepted

Solution code:
import collections

n, k = map(int,input().split())
a = input().split()
a = sorted(collections.Counter(a).values(), reverse=True)
s = sum(a[:k])

print(0 if s == n else n - s)

Submission status: Accepted

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

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

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

tmp = 0

for i in range(min(k, len(d_sorted))):
    tmp += d_sorted[i][1]

print(n - tmp)

Submission status: Accepted

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

Submission status: Accepted

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

d = sorted(d.items(), key=lambda x:x[1], reverse=True)
ans = 0
if len(d) <= k:
    print(0)
else:
    for i in range(len(d) - k):
        ans += d[-i-1][1]
    print(ans)

Submission status: Accepted

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

b=sorted(c(a).items(),key=lambda x:x[1])
kind=len(c(a))
i=0
ans=0
while k<kind:
    ans+=b[i][1]
    kind-=1
    i+=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
n,k = map(int, input().split())
a = list(map(int, input().split()))
l = collections.Counter(a)
p = list(l.values())
p.sort()
ans = 0
for i in range(len(p)-k):
    ans += p[i]
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:
import collections

N, K = map(int, input().split())
A = sorted(collections.Counter(list(map(int, input().split()))).items(), key=lambda x: x[1], reverse = True)
if len(A) <= K:
    print(0)
else:
    print(sum([x[1] for x in A[K:]]))

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N, K = MAP()
A = LIST()
cnt = sorted(list(Counter(A).values()), reverse=True)
print(sum(cnt[K:]))

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N, K = MAP()
A = LIST()

C = Counter(A)

n = sorted(list(C.values()))
print(sum(n[:len(n)-K]))