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 sys import stdin
def main():
    #入力
    readline=stdin.readline
    n,k=map(int,readline().split())
    a=list(map(int,readline().split()))

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

    d=list(d.items())
    d.sort(key=lambda x:x[1])
    res=0
    for i in range(len(d)-k):
        res+=d[i][1]

    print(res)
    
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
import collections
C = collections.Counter(A)
D = sorted(C.items(), key=lambda x: x[1])
B = len(set(A)) -K
result = 0
for a in range(B):
    result += D[a][1]

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
from collections import Counter


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N, K = list(map(int,_in[0].split(' ')))  # type:list(int)
    A_arr = list(map(int,_in[1].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    counter = Counter(A_arr)
    counter = list(counter.values())
    counter.sort(reverse=True)
    cnt = sum(counter[K:])
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
d=[0]*N
ans=0
for i in range(N):
    d[a[i]-1]+=1
d.sort(reverse=True)
if len(d)>K:
    ans =sum(d[K:])
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    # 整数 1 つ
    # n = int(input())
    # 整数複数個
    n, k = map(int, input().split())
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    A = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    # A = [list(map(int, input().split())) for i in range(H)]

    Unique = list(set(A))
    Dic = {x:0 for x in set(A)}

    if len(set(A)) <= k:
        print(0)
    else:
        for x in A:
            Dic[x] += 1
        Dicsort = sorted(Dic.items(), key=lambda x: x[1])

        cnt = 0
        for i in range(len(set(A)) - k):
            cnt += Dicsort[i][1]
        print(cnt)
resolve()

Submission status: Accepted

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

Ad = {x:0 for x in set(A)}
if len(set(A)) <= K:
    print(0)
else:
    count = 0
    for a in A:
        Ad[a] += 1
    Ad = sorted(Ad.items(), key=lambda x:x[1])
    for i in range(len(set(A))-K):
        count += Ad[i][1]
    print(count)

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] = 0
    d[i] += 1
l = []
for i in d.keys():
    l.append(d[i])
l = sorted(l)
print(sum(l[:len(l)-k]))

Submission status: Accepted

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

from collections import Counter
# N = int(input())
# S = input()
N, K = map(int, input().split())
A = list(map(int, input().split()))

def main():
    cnt_a = Counter(A).most_common()[::-1]
    task = len(cnt_a) - K
    if task <= 0:
        print(0)
        exit()
    ans = 0
    for i, v in enumerate(cnt_a, 1):
        ans += v[1]
        if i == task:
            print(ans)
            exit()

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()))
ans=len(a)
a=dict(Counter(a))
count=0
a = sorted(a.items(), key=lambda x:x[1], reverse=True)
for i,j in a:
  count+=1
  ans-=j
  if count==k:
    break
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)
B = C.most_common()[::-1]
l =  len(B)
ans = 0
for i in range(N):
  if l > K:
    ans += B[i][1]
    l -= 1
  else:
    break
print(ans)

Submission status: Accepted

Solution code:
import sys
import os
import math
import bisect
import collections
import itertools

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def f(A, B, x):
    return math.floor(A * x / B) - A * math.floor(x / B)


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    A = collections.Counter(il())
    cnt = sorted([v for v in A.values()])

    ret = 0
    for i in range(len(cnt) - K):
        ret += cnt[i]

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
if len(list(set(a)))<=k:
  print(0)
else:
  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])
  m=len(d)
  cnt=0
  for i in range((len(list(set(a))))-k):
    cnt+=d[i][1]
  print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

#【註】N 個のボールと K 種類の整数
N, K = map(int, input().split())

#【註】ボールに書かれている数 A_1...A_N
A = list(map(int, input().split()))

#【註】何種類あるか？を set で取得
C = list(set(A))

# 数字の種類数
B = len(C) 
# 元々の種類と目的の種類の差
x = B - K

#-----
#【註】「文字：回数」という辞書を使って、ループを最小限にします。
d = {}

#【解説】A のそれぞれの要素について
for i in A:
    #【解説】もし辞書に既に登録されていれば、その値（出現回数）に 1 を足す
    if i in d:
        d[i] += 1
    #【解説】そうでなければ、辞書にキーを登録して値（出現回数）を 1 にする
    else:
        d[i] = 1

#【解説】辞書を値でソートすると、出現回数が小さい順に並ぶ
d = sorted(d.items(), key=lambda x:x[1])

ans = 0
count = 0

if x<=0:
    print('0')
else:
    #【解説】辞書からキーと値を取り出し
    for key, value in d:
        #【解説】取り出した玉の数に value を足す
        ans = ans + value
        #【解説】何種類取り出したか、の値に 1 を足す
        count = count + 1

        #【解説】もし x 種類取り出しが完了したら ans を出力
        if count == x:
            print(ans)
            break

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections

n,k = map(int,input().split())
a = list(map(int,input().split()))
c = collections.Counter(a)
item = []
for key,value in c.items():
    item.append([value,key])
item.sort()
cnt = len(item)
i = 0
ans = 0
while True:
    if cnt <= k:
        break
    ans += item[i][0]
    i+=1
    cnt -= 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N,K = map(int,input().split())
A = np.array(input().split(), dtype=np.int32)
counter = np.bincount(A)
counter.sort()
counter = counter[::-1] 
no_change = counter[:K].sum()
answer = N - no_change
print(answer)

Submission status: Accepted

Solution code:
import collections
n,k = map(int,input().split())
a = list(map(int,input().split()))
c = collections.Counter(a)
values, counts = zip(*c.most_common())
print(sum(counts[k:]))

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
INIT_VAL = 0
def DD2(d1,d2): return [[INIT_VAL]*d2 for _ in range(d1)]
def DD3(d1,d2,d3): return [DD2(d2,d3) for _ in range(d1)]
## math ##
def to_bin(x: int) -> str: return format(x, 'b') # rev => int(res, 2)
def to_oct(x: int) -> str: return format(x, 'o') # rev => int(res, 8)
def to_hex(x: int) -> str: return format(x, 'x') # rev => int(res, 16)
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]
## libs ##
from itertools import accumulate as acc
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left

#======================================================#

def main():
    n, k = MII()
    aa = Counter(MII())
    c = aa.most_common()[::-1][:len(aa)-k]
    print(sum([ci[1] for ci in c]))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

import collections
ball = collections.Counter(ball)
ball = list(ball.values())
ball = sorted(ball)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
lis = list(map(int,input().split()))
dic = {}
for i in range(1,N+1):
    dic[i-1] = 0
for i in lis:
    dic[i-1] += 1

lis2 = list(dic.values())
lis2.sort()
print(N - sum(lis2[N-K:]))

Submission status: Accepted

Solution code:
import collections
N,K = map(int,input().split())
a = list(map(int,input().split()))
c = collections.Counter(a)
c_most = c.most_common()
if len(c) <= K:
    print(0)
else:
    print(sum([c_most[-i][1] for i in range(1,len(c)-K+1)]))

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

    L = sorted(list(L.items()), key= lambda x : x[1], reverse = True)
    
    cnt = 0
    typ = 0

    for i in range(len(L)):
        typ += 1
        if typ > K:
            pair = L[i]
            cnt += pair[1]
    
    print(cnt)
if __name__ == "__main__":
    main()

Submission status: Accepted

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

c=[0]*(n+1)
for i in range(n):
    c[a[i]]+=1
c.sort(reverse=True)
csum=0
for i in range(k):
    csum+=c[i]
print(n-csum)

Submission status: Accepted

Solution code:

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

set_l = set(l)
cnt = k - len(set_l)
d = [0] * 200001
for j in l:
    d[j] += 1
print(sum(sorted(d)[:-k]))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
tmp = [0]*N
tmp_2 = [0]*N
Cnt=0
result = 0
for i in range(N):
  tmp[A[i]-1]+= 1
  tmp_2[A[i]-1] = 1

m = sum(tmp_2)-K
if m>0:
	tmp = sorted(tmp)
	for i in range(N):
		if tmp[i]!=0:
			result += tmp[i]
			Cnt +=1
		if Cnt == m:
			break
    
print(result)

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))
L1 = Counter(L)
L2 = L1.most_common()
L2 = sorted(L2,key = lambda x:x[1])

sum1 =0
if s-k <= 0:
    print(0)
    exit()
else:
    for i in range(s-k):
        sum1 += L2[i][1]
print(sum1)

Submission status: Accepted

Solution code:
from collections import Counter
n,k = map(int,input().split())
a_ls = list(map(int, input().split()))
c = Counter(a_ls)
value_ls = list(c.values())
value_ls.sort(reverse=True)
ans = 0
for i in range(k, len(value_ls)):
    ans += value_ls[i]
print(ans)

Submission status: Accepted

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

c=[0]*(n+1)
for i in range(n):
    c[a[i]]+=1
c.sort(reverse=True)
csum=0
for i in range(k):
    csum+=c[i]
print(n-csum)

Submission status: Accepted

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

d = {}
for a in A:
    d.setdefault(a, 0)
    d[a] += 1
t = sorted(d.values())
print(sum(t[:-K]))

Submission status: Accepted

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

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

nonzero = sorted([i for i in num if i!=0])

sumR = max(0, len(nonzero) - k)
ans = sum(nonzero[:sumR])
print(ans)

Submission status: Accepted

Solution code:
def main():
    N,K=map(int,input().split())
    A=[int(x) for x in input().split()]
    
    dictionary={}
    for num in A:
        if num in dictionary:
            dictionary[num]+=1
        else:
            dictionary[num]=1
    
    ans=0  
    i=0
    
    if len(dictionary)<=K:
        ans=0
    else:
        for value in sorted(dictionary.values()):
            if (i==len(dictionary)-K):
                break
            else:
                ans+=value
                i+=1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

a.sort()
num = [0] * n
c = 0
t = 0
for i in range(n-1):
    if a[i] != a[i+1]:
        c += 1
        num[a[i]-1] += c
        t += 1
        c = 0
    else:
        c += 1

s = sum(num)
t += 1
num[a[i+1]-1] = n - s
num.sort()

ans = 0
for i in range(n):
    if num[i] == 0:
        continue
    if t <= k:
        print(ans)
        break
    ans += num[i]
    t -= 1

Submission status: Accepted

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

d = {}
for t in a:
    if t in d:
        d[t]+=1
    else:
        d[t]=1
ans = 0
if len(d) <= k:
    print(ans)
else:
    dl = sorted(list(zip(d.values(), d.keys())))
    for i in range(len(dl)-k):
        ans += dl[i][0]
    print(ans)

Submission status: Accepted

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

adic=defaultdict(int)
for a in alist:
  adic[a]+=1
#print(adic)

if len(adic)<=K:
  print(0)
else:
  freq_list=[]
  for k,v in adic.items():
    freq_list.append(v)
  freq_list.sort(reverse=True)
  #print(freq_list)
  print(sum(freq_list[K:]))

Submission status: Accepted

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

written = set(ball)
count = [0 for _ in range(max(written))]

if len(written) > k:
    for i in ball:
        count[i - 1] += 1
rewrite = [c for c in count if c > 0]
rewrite.sort()
print(sum(rewrite[: len(written) - k]))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
ball_count = [0 for _ in range(N)]
for ball in list(map(int, input().split())):
    ball_count[ball-1] += 1

ball_count.sort()
ball_count.reverse()
answer = 0
# 配列の要素で0でないものがK個以下になるまで要素の小さいものを書き換えていきcountを加えていく。
# 種類を減らすためには全てを０にしないとおけない

print(sum(ball_count[K:]))

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()
cnt = len(c)
i = 1
ans = 0
while cnt > k:
    cnt -= 1
    ans += c[-i][1]
    i += 1

print(ans)

Submission status: Accepted

Solution code:
import collections
import sys
sys.setrecursionlimit(2000000)
# import math
# import itertools
# import statistics
# import numpy as np
#n = int(input())
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
a_count = collections.Counter(a)
a_list = list(a_count.items())
a_sorted = sorted(a_list, key=lambda x: x[1])
# print(a_sorted)
count = 0
for i in range(len(a_sorted)-k):
    count += a_sorted[i][1]

print(count)

Submission status: Accepted

Solution code:
import collections as col

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

L= len(set(A))
A = col.Counter(A)

Aval = A.values()
Aval = sorted(Aval)

ans = 0

if L <= K:
    print(0)
    
else:
    for i in range(L-K):
        ans += Aval[i]
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


N, K = map(int, input().split())
counter = Counter(int(x) for x in input().split())

# 書き換えが必要な種類
i = len(counter) - K

if i < 1:
  print(0)
else:
  t = [(v, k) for k, v in counter.items()]
  t.sort()
  print(sum(v for v, k in t[:i]))

Submission status: Accepted

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

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

C = list(set(A))

B = len(C) 
x = B - K

d = {}

for i in A:
    if i in d:
        d[i] += 1
    else:
        d[i] = 1

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

ans = 0
count = 0

if x<=0:
    print('0')
else:
    for key, value in d:
        ans = ans + value
        count = count + 1
        if count == x:
            print(ans)
            break

Submission status: Accepted

Solution code:
import collections

x, y = map(int,input().split())
li = list(map(int,input().split()))
c = collections.Counter(li)
ans = 0

count = c.most_common()
if len(count) > y:
    for i in range(y,len(count)):
        ans += count[i][1]

print(ans)

Submission status: Accepted

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

d = {}

for key in A:
    if key in d:
        d[key] += 1
        continue
    d[key] = 1

count = 0
d = sorted(d.items(), key=lambda x: x[1])
ans = 0
for key,data in d:
    if count<(len(A_set)-K):
        ans = ans + data
    count = count + 1
print(ans)

Submission status: Accepted

Solution code:
import sys
import collections
N, K = map(int, sys.stdin.readline().split(" "))
A = list(map(int, sys.stdin.readline().split(" ")))
C = sorted(list(collections.Counter(A).values()), reverse = True)
print(sum(C[K:]))

Submission status: Accepted

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

Submission status: Accepted

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

a_dic = {}

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

a_val = sorted(a_dic.values(),reverse = True)

print(sum(a_val[k:]))

Submission status: Accepted

Solution code:
from sys import stdin
import sys

[N, K] = list(map(int, stdin.readline().rstrip().split()))
A = list(map(int, stdin.readline().rstrip().split()))

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


count=0
num_of_items = len(h)

if num_of_items <= K:
    print(0)
    sys.exit()

for i in sorted(h.values()):
    if num_of_items > K:
        count += i
        num_of_items -= 1

print(count)

Submission status: Accepted

Solution code:
N,K=map(int, input().split())
A=list(map(int, input().split()))
A.sort()
cnt=0
l=[]
l.append([A[0],0])
for i in range(len(A)):
  if A[i]==l[cnt][0]:
    l[cnt][1]+=1
  else:
    cnt+=1
    l.append([A[i],1])
if len(l) <= K:
  print(0)
else:
  l.sort(key=lambda x: x[1])
  sum=0
  for i in range(len(l)-K):
    sum+=l[i][1]
  print(sum)

Submission status: Accepted

Solution code:
import collections
N,K = map(int,(input().split()))
a = list(map(int,(input().split())))
f = collections.Counter(a)
c = f.most_common()[::-1]
ans = 0
for key in c[:max(0,len(c)-K)]:
    ans += key[1]
print(ans)

Submission status: Accepted

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

A_dict = {}
for item in A:
    if item in A_dict:
        A_dict[item] += 1
    else:
        A_dict[item] = 1

A_dict_sorted = sorted(A_dict.items(), key=lambda x:x[1])
result = 0
for i in range(len(A_dict)-K):
    result += A_dict_sorted[i][1]

print(result)

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('c', c)
c = sorted(c.values())
# print('c', c)

ans = sum(c[:-K])
print(ans)

Submission status: Accepted

Solution code:
import io

nim,mike=map(int,input().split())

A=list(map(int,input().split()))
from collections import Counter
A=Counter(A)
qwe=nim
for i in A.most_common(mike):
  qwe-=i[1]
print(qwe)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, k = list(map(int, readline().split()))
    a = list(map(int, readline().split()))

    from collections import Counter

    counter = Counter(a)
    u = len(counter)
    lcc = counter.most_common()[::-1]
    x = u - k

    ans = 0
    for i in range(x):
        ans += lcc[i][1]

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int,input().split())
A = list(map(int,input().split()))
count = Counter(A)
value = list(count.values())
value.sort()
ans = sum(value[:-K])
print(ans)

Submission status: Accepted

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

from collections import Counter
from operator import itemgetter


ac = Counter(a)
aci = list(ac.items())
aci = sorted(aci,key=itemgetter(1),reverse = False)  
#print(aci)

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = list(map(int,input().split()))
dict = {}
for i in a:
	if i in dict:
		dict[i] += 1
	else:
		dict[i] = 1
dict2 = sorted(dict.items(), key=lambda x:x[1], reverse=True)
c = 0
flg = 0
d = 0
for i in dict2:
	if c >= k:
		flg = 1
	if flg == 1:
		d += i[1]
	c += 1
print (d)

Submission status: Accepted

Solution code:
from collections import Counter
n, k, *a = map(int, open(0).read().split())
_, c = zip(*Counter(a).most_common())
print(n - sum(c[:k]))

Submission status: Accepted

Solution code:
def resolve():
    N, K = list(map(int, input().split(" ")))
    A = list(map(int, input().split(" ")))
    d = {}
    for a in A:
        if a in d:
            d[a] += 1
        else:
            d[a] = 1
    d = sorted(list(d.items()), key=lambda x: x[1], reverse=True)
    res = 0
    while len(d) > K:
        num, cnt = d.pop()
        res += cnt
    print(res)


if '__main__' == __name__:
    resolve()

Submission status: Accepted

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

d = {}

for a in A:
    if a in d:
        d[a] += 1
    else:
        d[a] = 1

l = sorted(d.values(), reverse=True)

if len(l) <= K:
    print(0)
else:
    print(sum(l[K:]))

Submission status: Accepted

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

dic = {}

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

ks = list(dic.keys())
vs = list(dic.values())

vs.sort()

B = (len(ks))
cnt = 0

for i in range(len(vs)):
    if B > K:
        B -= 1
        cnt += vs[i]
    else:
        break

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def I(): 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 LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h,w): return [[i for j in range(w)] for k in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**5+10)
input = sys.stdin.readline

N,K = MI()
a = LI()

d = {}
for i in range(N):
    d[a[i]] = d.get(a[i],0)+1
num = list(d.values())
num.sort()
n = len(num)
ans = 0
for i in range(n-K):
    ans += num[i]
print(ans)

Submission status: Accepted

Solution code:
import collections

n,k=map(int,input().split())
a=list(map(int,input().split()))
a=collections.Counter(a)
A=a.most_common()[::-1]
x=len(A)-k

if x<=0:
  print("0")
else:
  ans=0
  for i in range(x):
    ans+=A[i][1]
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

N, K = map(int, input().split())
A = list(map(int, input().split()))
d = defaultdict(int)
for a in A:
    d[a] += 1
kind = list(d.values())
kind = sorted(kind, reverse=True)
print(sum(kind[K:]))

Submission status: Accepted

Solution code:
import collections

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

AC = collections.Counter(A)

if K >= len(AC):
    print(0)
    exit(0)

delete_item_num = len(AC)-K
ans = 0
cnt = 0
for k, v in sorted(AC.items(), key=lambda x: x[1]):
    cnt += 1
    ans += v
    if cnt >= delete_item_num:
        print(ans)
        exit(0)

Submission status: Accepted

Solution code:
from collections import Counter
m,k=map(int,input().split())
A=list(map(int,input().split()))
d=Counter(A)
l=d.most_common()[::-1]
ans=0
i=0
while len(d)>k:
  ans+=l[i][1]
  del d[l[i][0]]
  i+=1
print(ans)

Submission status: Accepted

Solution code:
import collections
n,k = map(int,input().split())
a = list(map(int,input().split()))
a = collections.Counter(a)
a = list(a.values())
a.sort()
if len(a) <= k:
    print(0)
else:
    print(sum(a[0:len(a)-k]))

Submission status: Accepted

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

import collections
A_c = collections.Counter(A)
A_A = A_c.most_common()

ans = 0
for i in range(len(A_A)):
  if i >K-1:
    ans += list(A_A[i])[1]

if len(A_A)<=K:
  print(0)
else:
  print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

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

def main():
    c = Counter(a)
    res = sum(sorted(c.values())[:-k])
    print(res)

if __name__ ==  '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt = defaultdict(int)
for i in range(n):
    cnt[a[i]] += 1

cnt = sorted(cnt.items(), key = lambda x:x[1])
ans = 0
for i in range(len(cnt) - k):
    ans += cnt[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()))

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

if len(C) <= K:
  print(0)
else:
  print(sum([c[1] for c in C[:len(C)-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)
alt = sorted(cnt.values(), reverse=True)
print(sum(alt[k:]))

Submission status: Accepted

Solution code:
import collections
#aa = collections.Counter(a) # list to list

def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
    n,k = readInts()
    A = readInts()
    A_qu = collections.Counter(A)
    ALL = []
    for i in A_qu:
        ALL.append(A_qu[i])
    ALL = sorted(ALL)
    ans = 0
    for i in range(len(ALL) - k):
        ans += ALL[i]
    print(ans)
if __name__ == '__main__':
  main()

Submission status: Accepted

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

aa = collections.Counter(a)
sub = []
for i in aa:
    sub.append(aa[i])

sub.sort()
ans = 0
for i in range(len(sub) - k):
    ans += sub[i]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n, k = map(int, input().split())
ns = list(map(int, input().split()))
dic = Counter(ns)
result = 0
len_dic = len(dic)
for key, value in sorted(dic.items(), key=lambda x: x[1]):
    if len_dic <= k:
        break
    len_dic -= 1
    result += value
print(result)

Submission status: Accepted

Solution code:
url = "https://atcoder.jp/contests/abc081/tasks/arc086_a"
import copy
def main():
    n, m = list(map(int, input().split()))
    li = list(map(int, input().split()))
    dic = {}

    for v in li:
        dic.setdefault(v, 0)
        dic[v] += 1

    if len(dic) <= m:
        print(0)
        exit()

    count = 0
    vals = sorted(list(dic.values()), reverse=True)
    for i in range(len(dic) - m):
        count += vals.pop(-1)



    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
count = [0] * N
for i in range(N):
  count[a[i]-1] += 1
count.sort(reverse = True)
ans = N
for i in range(k):
  ans -= count[i]
print(ans)