CodeEff/ECCO huggingface dataset:
# Original solution
"""
案件を報酬を受け取るまでにかかる時間で降順ソートしておく。
"""
class segTree:
    def __init__(self,n,identity):
        if bin(n).count("1") == 1:
            self.leaves = 2**(n.bit_length()-1)
        else:
            self.leaves = 2**n.bit_length()
        self.identity = identity
        self.tree = [[self.identity,-1] for i in range(self.leaves*2)]
        for i in range(self.leaves,self.leaves*2):
            self.tree[i] = [self.identity,i-self.leaves]

    def merge(self,x,y):
        #ここにマージ処理を書く
        if x[0] < y[0]:
            return x[::1]
        else:
            return y[::1]

    def update(self,i,x):
        i = i+self.leaves
        self.tree[i][0] = x
        i >>= 1
        while i > 0:
            self.tree[i] = self.merge(self.tree[i*2],self.tree[i*2+1])
            i >>= 1

    def query(self,l,r):
        #[l:r)のクエリ結果を返す
        l = l + self.leaves
        r = r + self.leaves
        res = [self.identity,-1]
        while l < r:
            if l&1:
                res = self.merge(res,self.tree[l])
                l += 1
            if r&1:
                r -= 1
                res = self.merge(res,self.tree[r])
            l >>= 1
            r >>= 1
        return res

N,M = list(map(int,input().split()))
M += 1
sgt = segTree(N,float("INF"))
AB = [list(map(int,input().split())) for _ in range(N)]
AB.sort(key=lambda x: -x[0])

today = 1
for i in range(N):
    a,b = AB[i]
    due = M-a
    #M日までに報酬を受け取るためには、何日までに働かなくてはいけないかを求める。

    if today <= due:
        #今日の日付がdue以下であれば、今日その仕事をこなす
        sgt.update(today-1,b)
        today += 1
    else:
        if due < 1:
            continue
        #今日の日付がdueを過ぎてしまっている場合は、due以前の労働で報酬がb以下の仕事のうち、報酬がもっとも低い仕事を塗り替える。
        tmp = sgt.query(0,due)
        if tmp[0] < b:
            sgt.update(tmp[1],b)
        
ans = 0
for i in range(N):
    if sgt.tree[i+sgt.leaves][0] != float("INF"):
        ans += sgt.tree[i+sgt.leaves][0]
print(ans)




# Optimized solution
"""
Aの値が大きいものからheapqにぶち込んでいけばよい。
"""
from heapq import heappop,heappush
N,M = list(map(int,input().split()))
AB = [list(map(int,input().split())) for _ in range(N)]
AB.sort(reverse=True)
que = []
for a,b in AB:
    if M >= a:
        heappush(que,b)
        M -= 1
    elif que:
        if que[0] < b:
            heappop(que)
            heappush(que,b)
print((sum(que)))
# Original solution
import sys
readline = sys.stdin.readline

# 逆順に見ていく
# A_iの値が小さい順にheapqに追加していく

N,M = list(map(int,readline().split()))
from collections import defaultdict
tasks = defaultdict(list)
for i in range(N):
  a,b = list(map(int,readline().split()))
  tasks[a].append(b)
  
import heapq
q = []
heapq.heapify(q)
ans = 0
for i in range(1,M + 1):
  for task in tasks[i]:
    heapq.heappush(q,-task)
  if q:
    t = heapq.heappop(q)
    ans += (-t)

print(ans)

# Optimized solution
import sys
readline = sys.stdin.readline

N,M = list(map(int,readline().split()))
from collections import defaultdict
tasks = defaultdict(list)
for i in range(N):
  a,b = list(map(int,readline().split()))
  tasks[a].append(b)
  
import heapq as hq
q = []
ans = 0
for i in range(1, M + 1):
  newtask = tasks[i]
  for t in newtask:
    hq.heappush(q,-t)
  if q:
    ans += -hq.heappop(q)

print(ans)
# Original solution
from collections import defaultdict
import heapq
N,M=list(map(int,input().split()))
x=defaultdict(list)
for i in range(N):
    a,b=list(map(int,input().split()))
    x[a].append(-b)
y=[]
an=0
for i in range(M+1):
    y+=x[i]
    heapq.heapify(y)
    try:
        an-=heapq.heappop(y)
    except:
        pass
print(an)

# Optimized solution
import heapq
N,M=list(map(int,input().split()))
x={}
for i in range(M+1):
    x[i]=[]
for i in range(N):
    a,b=list(map(int,input().split()))
    if a>M:
        continue
    x[a].append(-b)
y=[]
an=0
for i in range(M+1):
    for n in x[i]:
        heapq.heappush(y,n)
    try:
        an-=heapq.heappop(y)
    except:
        pass
print(an)
# Original solution
import heapq
n,m=list(map(int,input().split()))
c=[[] for i in range(m+1)]
for i in range(n):
    a,b=list(map(int,input().split()))
    if a<=m:
        c[a].append(-b)
d=[]
heapq.heapify(d)
ans=0
for i in range(1,m+1):
    for j in range(len(c[i])):
        heapq.heappush(d,c[i][j])
    if d!=[]:
        ans+=heapq.heappop(d) 
print((-ans))   

# Optimized solution
import heapq
n,m=list(map(int,input().split()))
c=[[] for i in range(m+1)]
for i in range(n):
    a,b=list(map(int,input().split()))
    if a<=m:
        c[a].append(-b)
d=[]
heapq.heapify(d)
ans=0
for i in range(1,m+1):
    for j in c[i]:
        heapq.heappush(d,j)
    if d!=[]:
        ans+=heapq.heappop(d) 
print((-ans))
# Original solution
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush, heapify
import math
import itertools
import random
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
INF = float('inf')
def inputInt(): return int(eval(input()))
def inputMap(): return list(map(int, input().split()))
def inputList(): return list(map(int, input().split()))
 
def main():
    N,M = inputMap()
    ab = []
    for i in range(N):
        a,b = inputMap()
        if a > M:
            continue
        ab.append((a,b))
        
    ab = sorted(ab, key=lambda x:(x[0],-x[1]))
    pq = []
    heapify(pq)
    
    ans = 0
    for i in range(1,M+1):
        ind = -1
        for j,val in enumerate(ab):
            a,b = val
            ind = -1
            if a > i:
                break
            if i == a:
                heappush(pq, -b)
                ind = j
                
        if len(pq) > 0:
            tmp = heappop(pq) * (-1)
            ans += tmp
            
        if ind != -1:
            ab = ab[ind+1:]
    
    print(ans)
    
if __name__ == "__main__":
	main()


# Optimized solution
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush, heapify
import math
import itertools
import random
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
INF = float('inf')
def inputInt(): return int(eval(input()))
def inputMap(): return list(map(int, input().split()))
def inputList(): return list(map(int, input().split()))
 
def main():
    N,M = inputMap()
    ab = []
    heapify(ab)
    for i in range(N):
        a,b = inputMap()
        if a > M:
            continue
        heappush(ab, (a,b))
    
    ans = 0
    pq = []
    heapify(pq)
    for i in range(1,M+1):
        while True:
            if len(ab) == 0 or ab[0][0] != i:
                break
            a,b = heappop(ab)
            heappush(pq, -b)
            
        if len(pq) > 0:
            ans = ans + (heappop(pq) * (-1))
        
    
    print(ans)
    
if __name__ == "__main__":
	main()
# Original solution
n, m = list(map(int, input().split()))
work = [tuple(map(int, input().split())) for _ in range(n)]
work.sort(key = lambda x: (x[1], -x[0]), reverse = True)
do = [0] * (m+1)
money = 0
for a, b in work:
    if a > m:
        continue
    while do[a] != 0:
        a += 1
        if a > m:
            break
    if a > m:
        continue
    do[a] = 1
    money += b
print(money)

# Optimized solution
import bisect as bi
n, m = list(map(int, input().split()))
work = [tuple(map(int, input().split())) for _ in range(n)]
work.sort(key = lambda x: (x[1], -x[0]), reverse = True)
ava = list(range(0, m+1))
money = 0
for a, b in work:
    if a > m:
        continue
    l = bi.bisect_left(ava, a)
    if l >= len(ava):
        continue
    del ava[l]
    money += b
print(money)
# Original solution
import sys
from heapq import heappush, heappop
input = sys.stdin.readline

def main():
    N, M = list(map(int, input().split()))
    jobs = {}
    for _ in range(N):
        A, B = list(map(int, input().split()))
        if A > M:
            continue
        if A in list(jobs.keys()):
            jobs[A] = jobs[A] + [-B]
        else:
            jobs[A] = [-B]

    profit = 0
    days = list(jobs.keys())
    potential_jobs = []
    
    # M-1日後から今日まで遡る
    for elappsed_day in range(1, M+1):
        if elappsed_day in days:
            for job in jobs[elappsed_day]:
                heappush(potential_jobs, job)
        if len(potential_jobs) > 0:
            profit += heappop(potential_jobs)
            
    print((-profit))

if __name__ == "__main__":
    main()


# Optimized solution
import sys
from heapq import heappush, heappop
input = sys.stdin.readline

def main():
    N, M = list(map(int, input().split()))
    jobs = {}
    for _ in range(N):
        A, B = list(map(int, input().split()))
        if A > M:
            continue
        if A in list(jobs.keys()):
            jobs[A] += [-B]
        else:
            jobs[A] = [-B]

    profit = 0
    days = list(jobs.keys())
    potential_jobs = []
    
    # M-1日後から今日まで遡る
    for elappsed_day in range(1, M+1):
        if elappsed_day in days:
            for job in jobs[elappsed_day]:
                heappush(potential_jobs, job)
        if len(potential_jobs) > 0:
            profit += heappop(potential_jobs)
            
    print((-profit))

if __name__ == "__main__":
    main()
# Original solution
# ABC137 D - Summer Vacation
import heapq as hp
n,m=list(map(int,input().split()))
AB=[[] for i in range(m+1)]
l=[]
for i in range(n):
    a,b=list(map(int,input().split()))
    if a<=m:
        AB[a].append(-b)
ans=0
hp.heapify(l)
for ab in AB:
    for x in ab:
        hp.heappush(l,x)
    if len(l)>0:
        ans-=hp.heappop(l)
print(ans)



# Optimized solution
import heapq as hp
n,m=list(map(int,input().split()))
AB=[[] for i in range(m+1)]

for i in range(n):
  a,b=list(map(int,input().split()))
  if a<=m:
    AB[a].append(-b)

ans=0
l=[]
hp.heapify(l)
for ab in AB:
  for t in ab:
    hp.heappush(l,t)
  if len(l)>0:
    ans-=hp.heappop(l)
print(ans)
# Original solution
import heapq
n,m=list(map(int, input().split()))
a=[list(map(int, input().split())) for i in range(n)]
a.sort(key=lambda x: x[1],reverse=True)
x=[1 for i in range(m)]
ans=0
count=0
q=[]

for i in range(1,m+1):
    for j in range(n):
        if a[j][0]==i: 
            heapq.heappush(q,-a[j][1])
    
    if len(q)!=0:
        ans+=-heapq.heappop(q)
        
            

print(ans)

        


        
        
    
    
        



# Optimized solution
import heapq
n,m=list(map(int, input().split()))
a=[list(map(int, input().split())) for i in range(n)]
a.sort()
x=[1 for i in range(m)]
ans=0
q=[]
cnt=0
for i in range(1,m+1):
    for j in range(cnt,n):
        if a[j][0]==i:
            heapq.heappush(q,-a[j][1])
            cnt+=1
        else:
            break
 
    if len(q)!=0:
        ans+=-heapq.heappop(q)
        
            

print(ans)
# Original solution
from collections import deque
import sys
def input():
    return sys.stdin.readline()[:-1]

N, M = list(map(int, input().split()))
C = {}
for _ in range(N):
    A,B = list(map(int, input().split()))
    if not A in C:
        C[A] = []
    C[A].append(B)
for i in C:
    C[i] = deque(sorted(C[i])[::-1])

answer = 0
tmp = deque()

for i in range(1, M+1):
    if i in C:
        tmp2 = tmp
        tmp = deque()
        right = C[i]
        while len(tmp2) != 0 and len(right) != 0:   # line7
            if tmp2[0] > right[0]:                  # line8
                tmp.append(tmp2.popleft())           # line9
            else:                                   # line10
                tmp.append(right.popleft())          # line11
        if len(tmp2) != 0:                          # line12
            tmp+=tmp2                      # line13
        else:                       # line14
            tmp+=right
    if tmp != deque():
        answer += tmp[0]
        tmp.popleft()
print(answer)

# Optimized solution
import heapq
N, M = list(map(int, input().split()))
C = {}
for _ in range(N):
    A,B = list(map(int, input().split()))
    if A in C:
        C[A].append(B)
    else:
        C[A] = []
        C[A].append(B)
for i in C:
    C[i] = sorted(C[i])[::-1]

answer = 0
tmp = []
heapq.heapify(tmp)

for i in range(M+1):
    if i in C:
        right = C[i]
        for j in right:
            heapq.heappush(tmp,-1*j)
        
    if tmp != []:
        answer += -1*heapq.heappop(tmp)
print(answer)
# Original solution
# ABC-137-D
import heapq
import bisect
N, M = list(map(int, input().split()))
AB = []#Aでソートをかけて利用する。
BA = []#Bでソートをかけてheapqで利用する。
heapq.heapify(BA)

for _ in range(N):
    A, B = list(map(int, input().split()))
    AB.append((A, B))    
AB = sorted(AB)

ans = 0
check_M = 1
while check_M <= M:
    r = bisect.bisect_right(AB, (check_M+1, 0))
    for A, B in AB[:r]:
        heapq.heappush(BA, (-1*B, A))
    try:
        max_B = heapq.heappop(BA)
    except:
        check_M += 1
        AB = AB[r:]
        continue
    ans += (-1) * max_B[0]
    check_M += 1
    AB = AB[r:]

print(ans)

# Optimized solution
# ABC-137-D
import heapq

N, M = list(map(int, input().split()))
day_value = [[] for _ in range(M+1)]

for _ in range(N):
    A, B = list(map(int, input().split()))
    if A <= M:
        day_value[A].append(B)

ans = 0
check_M = 1
BA = []
heapq.heapify(BA)

while check_M <= M:
    for item in day_value[check_M]:
        heapq.heappush(BA, -1*item)
    try:
        max_B = heapq.heappop(BA)
    except:
        check_M += 1
        continue
    ans += (-1) * max_B
    check_M += 1
print(ans)
# Original solution
import sys
import heapq
input = sys.stdin.readline

n, m = list(map(int, input().split()))
sum = 0
# Aiが最小のものから順に選ぶ
# 重複するときは最大金額を選び残りはAiを+1する

# Ai>Mのものは最初から除外
# 受け取りにA日かかるということはM-A+1日目までに受けなければならない
# index = M-A(day)
# element = B(reword)

schedule = [[] for _ in range(m+1)]
for _ in range(n):
    day, reword = list(map(int, input().split()))
    if day > m:
        continue
    schedule[m-day+1].append(reword)

for i in range(m, 0, -1):
    if not schedule[i]:
        del schedule[i]
        continue
    max_reword = heapq.nlargest(1, schedule[i])
    sum += max_reword[0]
    schedule[i].remove(max_reword[0])
    if schedule[i]:
        schedule[i-1].extend(schedule[i])
    del schedule[i]

print(sum)


# Optimized solution
import sys
from heapq import heappop, heappush
input = sys.stdin.readline

n, m = list(map(int, input().split()))
# Ai>Mのものは最初から除外
# Aiが最小のものから順に選ぶ

# index = A(day-1), element = B(reword)
schedule = [[] for _ in range(m)]
for _ in range(n):
    day, reword = list(map(int, input().split()))
    if day > m:
        continue
    schedule[day-1].append(reword)

hq = []
sum = 0

for i in range(m):
    while schedule[i]:
        heappush(hq, -schedule[i][-1])
        del schedule[i][-1]

    if hq:
        sum -= heappop(hq)

print(sum)
# Original solution
def dfs(l, r):
    if (l, r) in cache:
        return cache[(l, r)]
    if l == r:
        return 0
    return min(dfs(l, m) + dfs(m + 1, r) + L[l][1] * L[m][0] * L[m + 1][1] * L[m + 1][0] for m in range(l, r))
    
cache = {}
L = [list(map(int, input().split())) for _ in range(eval(input()))]
print(dfs(0, len(L) - 1))

# Optimized solution
def dfs(l, r):
    if l == r:
        return 0
    if (l, r) in cache:
        return cache[(l, r)]
    cache[(l, r)] = min(dfs(l, m) + dfs(m + 1, r) + L[l][0] * L[m][1] * L[m + 1][0] * L[r][1] for m in range(l, r))
    return cache[(l, r)]

cache = {}
L = [list(map(int, input().split())) for _ in range(eval(input()))]
print(dfs(0, len(L) - 1))
# Original solution
from functools import reduce
def f(a,b): return [Card[a][0], Card[b][1]]
n = int(input())
Card = [[] for _ in [0]*n]
Cost = {}
for i in range(n):
  Card[i] = list(map(int, input().split()))
  Cost[(i, i)] = 0
for i in range(1, n):
  for j in range(0, n-i):
    a = j+i
    Cost[(j, a)] = min([reduce(lambda a,b: a*b, f(j, k) + f(k+1, a)) + Cost[(j, k)] + Cost[(k+1, a)] for k in range(j, j+i)])
print(Cost[0, n-1])

# Optimized solution
from functools import reduce
n = int(input())
Card = [[] for _ in [0]*n]
Cost = {}
for i in range(n):
  Card[i] = list(map(int, input().split()))
  Cost[(i, i)] = 0
for i in range(1, n):
  for j in range(0, n-i):
    a = j+i
    Cost[(j, a)] = min([reduce(lambda a,b: a*b, [Card[j][0],Card[k][1],Card[k+1][0],Card[a][1]]) + Cost[(j, k)] + Cost[(k+1, a)] for k in range(j, j+i)])
print(Cost[0, n-1])
# Original solution
from functools import reduce
n = int(input())
Card = [[] for _ in [0]*n]
Cost = {}
for i in range(n):
  Card[i] = list(map(int, input().split()))
  Cost[(i, i)] = 0
for i in range(1, n):
  for j in range(0, n-i):
    a = j+i
    Cost[(j, a)] = min([reduce(lambda a,b: a*b, [Card[j][0],Card[k][1],Card[k+1][0],Card[a][1]]) + Cost[(j, k)] + Cost[(k+1, a)] for k in range(j, j+i)])
print(Cost[0, n-1])

# Optimized solution
n = int(input())
Card = [[] for _ in [0]*n]
Cost = {}
for i in range(n):
  Card[i] = list(map(int, input().split()))
  Cost[(i, i)] = 0
for i in range(1, n):
  for j in range(0, n-i):
    a = j+i
    Cost[(j, a)] = min([Card[j][0] * Card[k][1] * Card[k+1][0] * Card[a][1] + Cost[(j, k)] + Cost[(k+1, a)] for k in range(j, j+i)])
print(Cost[0, n-1])
# Original solution
from collections import Counter

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

dic = Counter(l)
ans = 0
kv = list(dic.items())
kv = sorted(kv, key=lambda x: x[1])
while len(kv) > k:
    ans += kv[0][1]
    del kv[0]

print(ans)

# Optimized solution
from collections import Counter

n, k = list(map(int, input().split()))
dic = Counter(list(map(int, input().split())))
kv = list(dic.items())
kv = sorted(kv, key=lambda x: x[1])
ans = 0
cnt = 0
while len(kv)-cnt > k:
    ans += kv[cnt][1]
    cnt+=1
print(ans)
# Original solution
# ARC086C - Not so Diverse
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 list(dic.values())], reverse=True)
ans = 0
for i in range(len(cnt)):
    if k > 0:
        k -= 1
        continue
    ans += cnt[i]
print(ans)

# Optimized solution
# ARC086C - Not so Diverse (ABC081C)
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 list(dic.values())], reverse=True)
print((n - sum(cnt[:k])))
# Original solution
# ARC086C - Not so Diverse (ABC081C)
from collections import Counter


def main():
    _, k, *a = list(map(int, open(0).read().split()))
    print((sum(sorted(list(Counter(a).values()), reverse=True)[k:])))


if __name__ == "__main__":
    main()

# Optimized solution
# ABC081C - Not so Diverse (ARC086C)
from collections import Counter


def main():
    n, k = list(map(int, input().rstrip().split()))
    A = sorted(list(Counter(input().split()).values()), reverse=True)
    print((sum(A[k:])))


if __name__ == "__main__":
    main()
# Original solution
import collections
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = collections.Counter(a)
cnt = 0
c = b.most_common()[::-1]
while len(c) > k:
	cnt += c[0][1]
	c.pop(0)
print (cnt)

# Optimized solution
import collections
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = collections.Counter(a)
cnt = 0
c = b.most_common()
s = 0
for i in range(len(c)):
	cnt += 1
	if cnt > k:
		for j in range(i,len(c)):
			s += c[j][1]
		break
print (s)
# Original solution
n, k = list(map(int, input().split()))
L = list(map(int, input().split()))
d = {}
for a in L:
    if a not in d:
        d[a] = 1
    else:
        d[a] += 1
l = []
for v in list(d.values()):
    l.append(v)

if len(l) <= k:
    print((0))
else:
    ans = float('inf')
    for i in range(2 ** len(l)):
        b = [0] * len(l)
        for j in range(len(l)):
            if ((i >> j) & 1):
                b[j] = 1
        if b.count(1) <= k:
            cnt = 0
            for m in range(len(l)):
                if b[m] == 0:
                    cnt += l[m]
            ans = min(ans, cnt)
    print(ans)


# Optimized solution
n, k = list(map(int, input().split()))
L = list(map(int, input().split()))
d = {}
for a in L:
    if a not in d:
        d[a] = 1
    else:
        d[a] += 1
l = []
for v in list(d.values()):
    l.append(v)
l.sort(reverse=True)
print((n - sum(l[:k])))
# Original solution
import os,re,sys,operator,math
from collections import Counter,deque
from operator import itemgetter
from itertools import accumulate,combinations,groupby
from sys import stdin,setrecursionlimit
from copy import deepcopy
import heapq

setrecursionlimit(10**6)
n,k=list(map(int,stdin.readline().rstrip().split()))
a=[int(i) for i in stdin.readline().rstrip().split()]
t=deque(Counter(a).most_common())
ans=0
while len(t)>k:
    now=t.pop()
    ans+=now[1]
print(ans)

# Optimized solution

from collections import Counter
n,k=list(map(int,input().split()))
a=[int(i) for i in input().split()]

c=Counter(a).most_common()
c.sort(key=lambda x: x[1])
#print(c)
l=len(c)
print((sum(c[i][1] for i in range(l-k))))
# Original solution
import collections

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

c = collections.Counter(a)
vs = []
for v in list(c.values()):
  vs.append(v)
vs.sort()

i = 0
if len(vs) <= k:
  print(i)
  exit()
else:
  while len(vs) > k:
    i += vs[0]
    vs = vs[1:]
  else:
    print(i)
    exit()
    
    

# Optimized solution
import collections

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

c = collections.Counter(a)
vs = []
for v in list(c.values()):
  vs.append(v)
vs.sort(reverse=True)

i = 0
if len(vs) <= k:
  print(i)
  exit()
else:
  while len(vs) > k:
    i += vs[-1]
    vs.pop()
  else:
    print(i)
    exit()
# Original solution
from collections import Counter

n, m = list(map(int, input().split()))
dic = Counter(list(map(int, input().split())))
lis = sorted(list(dic.items()), key=lambda x: x[1], reverse=True)

res = 0
for i, x in enumerate(lis):
    if i < m:
        continue
    else:
       res += x[1]
print(res)


# Optimized solution
from collections import Counter

n, m = list(map(int, input().split()))
dic = Counter(list(map(int, input().split())))
lis = sorted(list(dic.items()), key=lambda x: x[1], reverse=True)

res = 0
for i in range(min(m, len(lis))):
    res += lis[i][1]
print((n - res))
# Original solution
N, K=list(map(int, input().split()))
A=list(map(int, input().split()))
sa=set(A)
if len(sa)<=K:
  print((0))
else:
  # 書き換える要素の種類数
  sub=len(sa)-K
  # 少ないやつを書き換えたい
  a=list(sa)
  
  numl=[]
  for i in a:
    numl.append(A.count(i))
  
  numl.sort()
  print((sum(numl[:sub])))
  
  

# Optimized solution
N, K=list(map(int, input().split()))
A=list(map(int, input().split()))
sa=set(A)
if len(sa)<=K:
  print((0))
else:
  # 書き換える要素の種類数
  sub=len(sa)-K
  # 少ないやつを書き換えたい
  A.sort()
  A.append(A[-1])
  a=[0]*len(sa)
  j=0
  for i in range(N):
    a[j]+=1
    if A[i]!=A[i+1]:
      j+=1
  
  a.sort()
  print((sum(a[:sub])))
# Original solution
n, k = list(map(int,input().split()))
a = list(map(int,input().split()))

aset = set(a)
count = []
n = len(aset)
for i in aset:
    count.append([i,a.count(i)])
if n <= k:
    print((0))
else:
    c = n - k
    count.sort(key=lambda x: x[1])
    ans = 0
    for i in range(c):
        ans += count[i][1]
    print(ans)

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

a.sort()
count = []
c = 1
for i in range(n-1):
    if a[i] != a[i+1]:
        count.append(c)
        c = 1
    else:
        c += 1
    if i == n - 2:
        count.append(c)

l = len(count)
#print(a)
#print(count)
c = 0
if l <= k:
    print((0))
else:
    count.sort()
    for i in range(l):
        l -= 1
        c += count[i]
        if l <= k:
            print(c)
            quit()