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 defaultdict

n, k, *a = map(int, open(0).read().split())
d = defaultdict(int)
for b in a:
    d[b] += 1

v = sorted(d.values())[::-1]

c = 0
for i in range(min(k, len(v))):
    c += v[i]

print(n - c)

Submission status: Accepted

Solution code:
from collections import Counter


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


N, K = LI()
A = LI()
A = Counter(A)
A = sorted(A.items(), key=lambda x: x[1])
ans = 0

if len(A) > K:
    L = len(A)-K
    for i in range(L):
        ans += A[i][1]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
C = {}
for i in range(N):
    if A[i] not in C:
        C[A[i]] = 0
    C[A[i]] += 1
C = sorted(list(C.items()),key=lambda x:x[1],reverse=True)
if len(C)<=K:
    print(0)
else:
    cnt = 0
    for i in range(K,len(C)):
        cnt += C[i][1]
    print(cnt)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

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

from collections import Counter

c = Counter(a)
need = len(c) - k
ans = 0

for i in sorted(c.values()):
    if need <= 0:
        break
    ans += i
    need -= 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
L = list(map(int, input().split()))
S = len(set(L))
C = Counter(L).most_common()[::-1]
ans = 0
for c in C:
    if S <= K:
        break
    ans += c[1]
    S -= 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n, k, *a = map(int, open(0).read().split())
d = defaultdict(int)

for b in a:
    d[b] += 1

k = len(d) - k
ans = 0
for v in sorted(d.values()):
    if k <= 0:
        break
    ans += v
    k -= 1
print(ans)

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
import sys
sys.setrecursionlimit(10 ** 6)

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:]
c = 0
for i in range(len(e)):
    c += e[i][1]
print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)

N,K = mint()
A = lint()
d = dict()
for a in A:
    d.setdefault(a,0)
    d[a] += 1
l = []
for k in d:
    l.append(d[k])
l.sort(reverse=True)
print(N-sum(l[:K]))

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
A = list(map(int, input().split()))
d = Counter(A)
B = list(d.values())
B.sort()
L = len(B)
print(sum(B[:max(L-K,0)]))

Submission status: Accepted

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

a = sorted(a)
shurui = 0

for i in a:
    if a_num[i] == 0:
        shurui += 1
    a_num[i] += 1

a_num = sorted(a_num)
ans = 0
i = 0

while shurui > k:
    if a_num[i] != 0:
        ans += a_num[i]
        shurui -= 1
    i += 1

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
data=[0]*n
for i in a:
  data[i-1]+=1
data.sort()
m=n-k
if data.count(0)>=m:
  print(0)
else:
  ans=0
  res=data.count(0)
  while res<m:
    ans+=data[res]
    res+=1
  print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
As = list(map(int,input().split()))
nc=[0]*(200000+1)
for a in As:
    nc[a] += 1
setA = set(As)
kind = len(setA)
if kind - K <= 0:
    print(0)
else:
    nc.sort()
    c = 0
    for i in range(200000+1):
        if nc[i] == 0:
            continue
        c += nc[i]
        kind -= 1
        if kind == K:
            break
    print(c)

Submission status: Accepted

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

cc=collections.Counter(A)
tt=list(cc.values())
tt.sort()
print(sum(tt[:len(tt)-K]))

Submission status: Accepted

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

A = collections.Counter(A)
A = sorted(A.items(), key=lambda x:x[1])
cnt = A[:len(A)-K]

ans = []
for c in cnt:
    ans.append(c[1])
    
print(sum(ans))

Submission status: Accepted

Solution code:
import heapq
N,K = map(int,input().split())
Alist = list(map(int,input().split()))
Ball = dict()
for i in Alist:
    if i not in Ball:
        Ball[i] = 1
    else:
        Ball[i] += 1
Balllist = []
for key, value in Ball.items():
    Balllist.append(value)
heapq.heapify(Balllist)
size = len(Balllist)
Answer = 0
for _ in range(10**7):
    if size <= K:
        break
    Answer += heapq.heappop(Balllist)
    size -= 1
print(Answer)

Submission status: Accepted

Solution code:
#24 C - Not so Diverse AC
import collections 
N,K = map(int,input().split())
A = list(map(int,input().split()))

cnt = collections.Counter(A)

diff = len(cnt.keys()) - K
# 個数が少ないボールを diff 種類書き換える
values = sorted(list(cnt.values()),reverse = False)
ans = values[:diff]
print(sum(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).most_common()

if len(C) <= K:
    print(0)
    exit()
ans = 0
for c in C[len(C) - 1:K - 1:-1]:
    ans += c[1]
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
ins = lambda: input().rstrip()
ini = lambda: int(input().rstrip())
inm = lambda: map(int, input().split())
inl = lambda: list(map(int, input().split()))
out = lambda x: print('\n'.join(map(str, x)))

n, k = inm()
a = inl()
if len(set(a)) <= 2:
    print(0)
else:
    b = {}
    for i in a:
        if i not in b:
            b[i] = 0
        b[i] += 1
    b = list(map(lambda x: x[1], sorted(b.items(), key=lambda x: x[1])))
    print(sum(b[:len(set(a))-k]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
ins = lambda: input().rstrip()
ini = lambda: int(input().rstrip())
inm = lambda: map(int, input().split())
inl = lambda: list(map(int, input().split()))
out = lambda x: print('\n'.join(map(str, x)))

n, k = inm()
a = inl()
if len(set(a)) <= 2:
    print(0)
else:
    b = {}
    for i in a:
        if i not in b:
            b[i] = 0
        b[i] += 1
    b = sorted(b.items(), key=lambda x: x[1])
    ans = 0
    for i in range(len(set(a))-k):
        ans += b[i][1]
    print(ans)

Submission status: Accepted

Solution code:
import collections

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

d = dict(collections.Counter(a))
d = sorted(d.values())

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

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n,k = LI()
    a = LI()
    dic = collections.Counter(a)
    dlst = sorted(dic.items(),key=lambda x: x[1])
    cnt = len(dic)-k
    ans = 0

    for i in range(cnt):
        ans += dlst[i][1]
    print(ans)
        
main()

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/arc086/tasks/arc086_a

from collections import Counter

n, k = [int(i) for i in input().split()]
A = Counter([int(i) for i in input().split()])

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

ans = 0
res = len(A) - k
if res < 1:
    print(0)
else:
    for i in range(res):
        _, cnt = A[i]
        ans += cnt
    print(ans)

Submission status: Accepted

Solution code:
import collections
a = input().split()
N = int(a[0])
b = int(a[1])
c = [int(s) for s in input().split()]
d = collections.Counter(c)
e = len(d)
f = []
for myvalue in d.values():
    f.append(myvalue)
g = sorted(f)
h = int(0)
if b >= int(e):
    print("0")
else:
    for i in range(int(e) - b):
        h = h + g[i]
    print(h)

Submission status: Accepted

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

b = len(set(a))
c = Counter(a)
c = list(sorted(c.values(), reverse = True))

if b <= k:
    print(0)
else:
    d = b - k
    print(sum(c[b-d:]))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
x = len(set(A))
if x <= K:
    print(0)
    exit()
import collections
C = collections.Counter(A)
x -= K
result = 0
for c in C.most_common()[::-1]:
    if x == 0:
        print(result)
        exit()
    result += c[1]
    x -= 1

Submission status: Accepted

Solution code:
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
from collections import Counter
B = list(dict(Counter(A)).values())
M = len(B)
if M<=K:
    print(0)
else:
    B.sort()
    print(sum(B[0:(M-K)]))

Submission status: Accepted

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

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

v = sorted(list(d.values()))[::-1]

print(sum(v[k:]))

Submission status: Accepted

Solution code:
import heapq
from collections import Counter
N, K = map(int, input().split())
A = list(map(int, input().split()))
l = Counter(A)
#print(l)
dif = len(l) - K
if dif <= 0:
    print(0)
    exit()
l = list(l.values())
heapq.heapify(l)
#print(l)
ans = 0
for i in range(dif):
    ans += heapq.heappop(l)
print(ans)

Submission status: Accepted

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

D=[0]*(2*10**5+1)

t=len(set(A))-K
if t<=0:
    print(0)
else:
    for a in A:
        D[a]+=1
    print(sum(sorted([d for d in D if 0<d])[:t]))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
kosu = [0]*n
kaburi = []

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

for j in range(n):
    if kosu[j] != 0:
        kaburi.append(kosu[j])

kaburi.sort()
print(sum(kaburi[:len(kaburi)-k]))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
d = {}
for i in range(N):
    if A[i] not in d:
        d[A[i]] = 1
    else:
        d[A[i]] += 1
if len(d) <= K:
    print(0)
else:
    d=sorted(d.items(), key=lambda x:x[1])
    print(sum([d[i][1] for i in range(len(d)-K)]))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
p = [0 for _ in range(N)]
for i in range(N):
    p[A[i]-1] += 1
q = [p[i] for i in range(N) if p[i] != 0]
q.sort()
w = len(q)
print(sum(q[:w-K]))

Submission status: Accepted

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

for b in a:
    if b in balls:
        balls[b] += 1
    else:
        balls[b] = 1

ballcnts = list(balls.values())
ballcnts.sort()

req_delete = len(ballcnts) - k
if req_delete <= 0: print(0)
else: print(sum(ballcnts[:req_delete]))

Submission status: Accepted

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

c = collections.Counter(A)
ans = 0
d=c.most_common()
len_A=len(set(A))
if(len_A<=K):
    print(ans)
else:
    for i in range(len_A-K):
        ans +=d[-1*(i+1)][1]
    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)

print(sum(list(map(lambda x:x[1],ca.most_common()[k:]))))

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

li = [0] * (N + 1)

#  li[i] = j  i:ボールの数 j:種類

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

ans = 0
k = len(dic)

for i in range(N):
    if li[i] > 0:
        if k - li[i] >= K:
            ans += i * li[i]
            k -= li[i]
        else:
            ans += i * (k - K)
            k -= li[i]

    if k <= K:
        break

print(ans)

Submission status: Accepted

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    cnt = 0
    pre_n = a[0]
    cnt_kind = []
    for i in a:
        if pre_n == i:
            cnt += 1
            pre_n = i
        else:
            cnt_kind.append(cnt)
            cnt = 1
            pre_n = i

    cnt_kind.append(cnt)
    cnt_kind.sort()
    if len(cnt_kind) <= k:
        print(0)
    else:
        print(sum(cnt_kind[:len(cnt_kind)-k]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
A = [int(i) for i in input().split()]
D = {}
for i in A:
    if i in D:
        D[i] += 1
    else:
        D[i] = 1
B = list(D.values())
B.sort(reverse=True)
print(N-sum(B[:min(len(B), K)]))

Submission status: Accepted

Solution code:
import collections

N,K,*A = map(int, open(0).read().split())

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

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
C = {}
for i in range(N):
    a = A[i]
    if a not in C:
        C[a] = 0
    C[a] += 1
C = sorted(list(C.items()),key=lambda x:x[1],reverse=True)
if len(C)<=K:
    print(0)
else:
    cnt = 0
    for i in range(K,len(C)):
        cnt += C[i][1]
    print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import *

N, K = map(int, input().split())
A = list(map(int, input().split()))
cnt = Counter(A)
vs = list(cnt.values())
vs.sort()
print(sum(vs[:max(0, len(vs)-K)]))

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
d = {}
u = 0

for i in range(n):
    if a[i] in d:
        d[a[i]] += 1
    else:
        d[a[i]] = 1
        u += 1

if u == k:
    print(0)
else:
    vals = sorted(d.values(), reverse=True)
    print(sum(vals[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)

items = []
for key, value in c.items():
    items.append((key, value))
items.sort(key=lambda x: x[1])

ans = 0
for key, value in items[:max(0, len(items)-k)]:
    ans+=value
print(ans)

Submission status: Accepted

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

c = collections.Counter(a).most_common()[::-1]
p = len(c) - k
if p <= 0:
  print(0)
else:
  print(sum(list(map(lambda x:x[1], c[:p]))))

Submission status: Accepted

Solution code:
from collections import Counter
n, k = map(int, input().split())
a = Counter([int(x) for x in input().split()])
all_balls = len(a)

result = 0
for _, x in reversed(a.most_common()):
  if k >= all_balls:
    break
  result += x
  all_balls -= 1

print(result)

Submission status: Accepted

Solution code:
from collections import Counter

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

B = list(Counter(A).values())
B.sort(reverse=True)

print(sum(B[K:]))

Submission status: Accepted

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

C = collections.Counter(A)

if len(C) <= K:
    print(0)
else:
    ret = 0
    for c in C.most_common()[K:]:
        ret += c[1]
    print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	C
# CreatedDate:  2020-07-29 17:00:49 +0900
# LastModified: 2020-07-29 17:12:06 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd
from collections import Counter


def main():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    a_cnt = sorted(list(map(list, Counter(A).items())), reverse = True, key = lambda x: x[1])
    init_kind = len(set(A))
    ans = 0
    while len(a_cnt) > k:
        _, num = a_cnt.pop()
        ans += num
    print(ans)




if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
N,K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
ca = Counter(A)
count = [[k, ca[k]] for k in ca]
count.sort(key = lambda x: x[1])
tn = 0
for i in range(len(count) - K):
    tn += count[i][1]
print(tn)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(A)

print(sum(sorted(c.values())[:len(c) - k]))

Submission status: Accepted

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

a1 = sorted(a)
a2 = []
count = 1
for i in range(n - 1):
    if a1[i] == a1[i + 1]:
        count += 1
    else:
        a2.append(count)
        count = 1
a2.append(count)
l = sorted(a2)
print(sum(l[:len(l) - k]))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

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

from collections import Counter

d = Counter(a)
d = sorted(d.values())

L = len(d)
if L >= k:
    print(sum(d[:L-k]))
else:
    print(0)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

N, K = map(int, sys.stdin.readline().split())
A = list(map(int, sys.stdin.readline().split()))

counter = Counter(A)
counter = sorted(counter.items(), key = lambda x: x[1])

if len(counter) <= K:
    print(0)
else:
    ans = 0
    needs = len(counter) - K
    for key, count in counter:
        needs -= 1
        ans += count

        if needs == 0:
            break

    print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline

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

b=[]
for i in d.values():
    b.append(i)

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

Submission status: Accepted

Solution code:
import collections
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = collections.Counter(a)
l = list(l.values())
l.sort(reverse = True)
print(sum(l[k:]))

Submission status: Accepted

Solution code:
import math
import sys
from collections import Counter
readline = sys.stdin.readline


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

n, k, *a = map(int, open(0).read().split())
c = Counter(a)
print(sum(sorted(c.values())[:len(c) - k]))

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
lsa = list(map(int,input().split()))
ls = [0 for i in range(2 * (10 ** 5))]
for i in range(n):
    ls[lsa[i] - 1] += 1
ls2 = [i for i in ls if i != 0]
ls2.sort()

ans = 0

if len(ls2) > k:
    t = len(ls2) - k
    ls3 = [ls2[i] for i in range(t)]
    ans += sum(ls3)

print(ans)

Submission status: Accepted

Solution code:
import collections
N,K = map(int,input().split())
A = list(map(int,input().split()))
B = collections.Counter(A)
if len(B) <= 2:
    print(0)
else:
    C = list(B.values())
    C.sort()
    print(sum(C[:-K]))

Submission status: Accepted

Solution code:
import collections

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

balls = collections.Counter(A)

num = []
for x in balls.values():
    num.append(x)

num.sort()

d = len(num) - k

if d <= 0:
    print(0)
    exit()

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

print(ans)

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a

# Union Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# 約数生成
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors


inf = 10 ** 18
mod = 10 ** 9 + 7

n,k = lr()
a = lr()
dic = collections.Counter(a)
dic_list = list(dic.items())
dic_list.sort(key=lambda x:x[1], reverse=True)
ans = 0
if len(dic_list) <= k:
    print(ans)
else:
    for _,num in dic_list[k:]:
        ans+=num
    print(ans)

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a

# Union Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# 約数生成
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors


inf = 10 ** 18
mod = 10 ** 9 + 7

n,k = lr()
a = lr()
dic = collections.Counter(a)
dic_list = list(dic.items())
dic_list.sort(key=lambda x:x[1], reverse=True)
ans = 0
if len(dic_list) <= k:
    print(ans)
else:
    for _,num in dic_list[k:]:
        ans+=num
    print(ans)

Submission status: Accepted

Solution code:
import math
import sys
import collections
n,k = map(int, input().split())
A = list(map(int, input().split()))
count=len(A)
Adel=list(set(A))
ans=len(Adel)-k
#print(Adel)
if ans<0:
    ans=0
    print(ans)
    sys.exit()
newA=collections.Counter(A)
count=list(newA.values())
count.sort()
#print(count)
ans=sum(count[:-k])
print(ans)