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 as dd
N, K = map(int, input().split())
A = list(map(int, input().split()))
D = dd(int)
for i in range(N):
  D[A[i]] += 1
j = len(D)-K
ans = 0
for i in sorted(D.values()):
  if j <= 0: break
  ans += i
  j -= 1
print (ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
A = list(map(str, input().split()))
A.sort()
A_set = list(set(A))
A_set.sort()
c = Counter(A)
if len(A_set) <= K:
    print(0)
    exit()
lists = []
for i in range(len(A_set)):
    lists.append(c[str(A_set[i])])
cnt = 0
lists.sort()
for i in range(len(lists) - K):
    cnt += lists[i]
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

A = Counter(A).most_common()

count = 0

if len(A) > k:
    for i in range(len(A)-k):
        count += A[-1-i][1]

print(count)

Submission status: Accepted

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

from collections import Counter

def main(N,K,A):
    change = max(len(set(A))-K,0)
    ans = 0
    A_count = Counter(A)
    for a in A_count.most_common()[:-change-1:-1]:
        ans += a[1]
    print(ans)
    
if __name__ == '__main__':
    N, K = map(int, input().split())
    A = [int(a) for a in input().split()]
    main(N,K,A)

Submission status: Accepted

Solution code:
def cc081():
    import sys
    from collections import Counter

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

    dan = Counter(an)

    l = len(dan)
    if l <= k:
        print(0)
        sys.exit()

    l -= k
    cnt = 0
    for ke,va in dan.most_common()[::-1]:
        cnt += va
        l -= 1
        if l <= 0:
            break

    print(cnt)

cc081()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
n, k = map(int, input().split())
a = collections.Counter(list(map(int, input().split())))
a = sorted(a.items(), key=lambda x: x[1])

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
#print(A)
S = set(A)
#print(len(S))
if len(S) <= K:
  print(0)
else:
  ch = len(S) - K
  INF = 10**7
  X = [INF] * (N + 1)
  for i in range(N):
    if X[A[i]] == INF:
      X[A[i]] = 1
    else:
      X[A[i]] += 1 
  X.sort()
  print(sum(X[:ch]))

Submission status: Accepted

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

Submission status: Accepted

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

seta = set(a)
cntK = len(seta)
cnta = [0] * cntK
if cntK <= K:
    print(0)
    sys.exit()

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

dtmp = sorted(d.items(), key=lambda x: x[1])
cnt = 0
for i in range(cntK - K):
    cnt += dtmp[i][1]
print(cnt)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

input = sys.stdin.readline

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

t=0

a = Counter(A)
total=0
n = len(a)
if n-K > 0:
    for i, j in zip(range(n-K),a.most_common()[::-1]):
        total +=j[1]
    print(total)
else:print(0)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(A)
l = len(c)

if l <= K:
  print("0")
else:
  times = 0
  value = 0

  for k,v in sorted(c.items(), key=lambda x: -x[1]):
    if times >=K:
      break
    value += v
    times += 1
  
  print(N-value)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(A)
ans = 0
size = len(c.keys())

if size > K:
    temp = size - K
    ans += sum(v for _, v in sorted(c.items(), key=lambda x: x[1])[:temp])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    N, K = map(int, input().split())
    targets = list(map(str, input().split()))
    targets_dict = {}

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

    need_reduce = len(targets_dict) - K

    if need_reduce <= 0:
        print(0)
    else:
        ans = 0
        targets_dict = sorted(targets_dict.items(), key=lambda x: x[1])
        for i in range(need_reduce):
            ans += targets_dict[i][1]
        print(ans)
       
resolve()

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] in a_dic.keys():
        a_dic[a[i]] += 1
    else:
        a_dic[a[i]] = 1

lis = sorted(list(a_dic.values()))
res = 0
if k >= len(lis):
    print(res)
else:
    for i in range(len(lis)-k):
        res += lis[i]
    print(res)

Submission status: Accepted

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

c = collections.Counter(A)
c.most_common()
values, counts = zip(*c.most_common())
L = list(counts)
L.sort()

ans = 0

for i in range(len(c)-K):
    ans += L[i]

if len(c) <= K:
    print(0)
else:
    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)
c = sorted(c.values())
print(sum(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()))
S = Counter(A)

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,k=map(int,input().split())
    from collections import Counter
    C=Counter(map(int,input().split()))
    l=len(C)
    if l<=k:
        print(0)
        return
    v=sorted(list(C.values()))
    print(sum(v[:l-k]))
resolve()

Submission status: Accepted

Solution code:
from collections import Counter
N,K = map(int,input().split())
A = Counter(list(map(int,input().split())))
v = list(sorted(A.values(),reverse=True))
print(sum(v[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 = A.most_common()

M = len(A) # 数の種類
ans = 0

for i in range(len(A)-1, -1, -1):
    if M <= K:
        print(ans)
        break
    else:
        M -= 1
        ans += A[i][1]
else:
    print(ans)

Submission status: Accepted

Solution code:
import copy
import collections

def calc(k, balls):
    dif = len(set(copy.copy(balls))) - int(k)
    change_count = 0

    if dif <= 0:
        return change_count
    else:
         c = collections.Counter(balls)
         most_common = c.most_common()[::-1]
         for x in most_common:
             change_count = change_count +  x[1]
             dif = dif - 1
             if dif <= 0 :
                break
         return change_count


_ = input().split()
k = _[1]
balls = sorted(input().split())

print(calc(k, balls))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
array = list(map(int, input().split()))
dic = {}
ans = 0
for item in array:
    if item in dic:
        dic[item] += 1
    else:
        dic[item] = 1
dic_sorted = sorted(dic.items(), key=lambda x:x[1])
count = len(dic)
i = 0
while K < count:
    count -= 1
    ans += dic_sorted[i][1]
    i += 1
print(ans)

Submission status: Accepted

Solution code:
# ABC081C - Not so Diverse (ARC086C)
from collections import Counter


def main():
    N, K, *A = open(0).read().split()
    C, K = list(Counter(A).values()), int(K)
    C.sort(reverse=1)
    ans = sum(C[K:])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC081C - Not so Diverse (ARC086C)
from collections import Counter


def main():
    N, K, *A = map(int, open(0).read().split())
    C = list(Counter(A).values())
    C.sort(reverse=1)
    ans = sum(C[K:])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = sorted(list(map(int, input().split())))
b = set(a)
c = []
x = 1
ans = 0

if(len(b) > k):
    for i in range(1,n):
        if(a[i]!=a[i-1]):
            c.append(x)
            x = 1
        else:
            x += 1
    c.append(x)
    c.sort()
    for i in range(len(b) - k):
        ans += c[i]
        
print(ans)

Submission status: Accepted

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

A_set = list(set(A))
if len(A_set) <= K:
  print(0)
else: # len(A_set) > K:
  dictionary = {}
  for n in A_set:
    dictionary[n] = 0
  for m in A:
    dictionary[m] += 1
  
  dict_val = list(dictionary.values())
  dict_val.sort(reverse = True)
  ans = N - sum(dict_val[:K])
  print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N,K = map(int,input().split())
A = Counter(list(map(int,input().split())))
nums = sorted(A.values(), reverse= True)
num = len(nums)
ans = 0

while num > K:
  ans += nums[-1]
  del nums[-1]
  num -= 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import collections
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
c = collections.Counter(a)
values, counts = zip(*c.most_common(k))
res = n - sum(counts) 
print(res if res >=0 else 0)

Submission status: Accepted

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

from collections import Counter

ac = Counter(A)
cnt = 0

if len(ac) > K:
    for a in ac.most_common()[K:]:
        cnt += a[1]
        
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

N, K = map(int, input().split())
A = Counter(list(map(int, input().split())))
L = max(0, len(A.keys()) - K)

print(sum(sorted(A.values())[:L]))

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
a_list=list(map(int,input().split()))
b=[0]*n
for i in a_list:
    b[i-1]+=1
b.sort()
ans=0
for i in range(n-k):
    ans+=b[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 = Counter(A)
ans = 0

ball = []

for i in c:
    ball.append(c[i])

ball.sort(reverse=True)
print(sum(ball[K:]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

n,k=map(int,input().split())
a=input().split()
x=len(list(set(a)))
n=x-k if x-k>=0 else 0
print(sum(sorted(Counter(a).values())[:n]))

Submission status: Accepted

Solution code:
from collections import Counter

n,k=map(int,input().split())
a=input().split()
x=len(list(set(a)))
n=x-k if x-k>=0 else 0

counter=Counter(a)
l=sorted(counter.values())
print(sum(l[:n]))

Submission status: Accepted

Solution code:
from collections import Counter
N,K = map(int, input().strip().split(' '))
counter_A = Counter(list(map(int, input().strip().split(' '))))
print(N-sum([v for k,v in counter_A.most_common(K)]))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    
    N,K = map(int,input().split())
    A = list(map(int,input().split()))
    from collections import defaultdict
    B = defaultdict(int)
    for i in A:
        B[i] += 1
    C = []
    for i in B:
        C.append(B[i])
    C = sorted(C)
    if len(C) <= K:
        ans = 0
    else:
        ans = 0
        for i in range(len(C)-K):
            ans += C[i]
    print(ans)
    
    
main()

Submission status: Accepted

Solution code:
from collections import Counter
n,k=map(int,input().split())
S=Counter(list(map(int,input().split()))).most_common()
if len(S)<=k:
  print(0)
else:
  print(sum(i[1] for i in S[k:]))

Submission status: Accepted

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

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

if __name__ == '__main__':
  main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np
import collections

N,K = map(int, input().split())
As = input().split()
for i in range(N):
  As[i] = int(As[i])
As = np.array(As)

As_c = collections.Counter(As)

ans = 0
counter = 0
for a in sorted(As_c.values()):
  if counter < len(As_c) - K:
    ans += a 
    counter += 1
  else:
    break
print(int(ans))

Submission status: Accepted

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

import collections

if U > K:
  c = collections.Counter(A)
  d = c.most_common(K)
  print(N-(sum(x[1] for x in d)))
else:
  print(0)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(As)
# print(c)
common = c.most_common(k)
# print(common)

common_sum = sum([item[1] for item in common])
print(n - common_sum)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(As)
common = c.most_common(K)

common_sum = sum([item[1] for item in common])

print(N-common_sum)

Submission status: Accepted

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

a = sorted(a)
count = [0] * n
for i in range(n):
    count[a[i] - 1] += 1
index = sorted(range(len(count)), key=lambda j: count[j], reverse=True)
ans = 0
for i in range(k):
    ans += count[index[i]]
ans = sum(count) - ans
print(ans)

Submission status: Accepted

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

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

a = {}

for i in a_input:
  if i in a:
    a[i] += 1
  else:
    a.setdefault(i, 1)
    
need_changes = len(a) - k

a = sorted(a.items(), key=lambda x:x[1])
a_values = [a[i][1] for i in range(len(a))]

count = 0
ans = 0
if need_changes > 0:
  for i in a_values:
    ans += i
    count += 1
    if count == need_changes:
      break
      
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n,k=map(int,input().split())
A=list(map(int,input().split()))
s=len(set(A))
C=Counter(A).most_common()
if s>k:
	for i in range(k):
		n-=C[i][1]
	print(n)
else:
	print(0)

Submission status: Accepted

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

from collections import Counter

c = Counter(a)
d = c.most_common()

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

else:
    ans = 0
    while len(d) > k:
        item = d.pop()
        ans += item[1]
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n, k = map(int, input().split())
arr = map(int, input().split())
c = Counter(arr)
a = 0
l = len(c.keys()) - k
if 0 < l:
    for _, v in sorted(c.items(), key=lambda x: x[1])[:l]:
        a += v
print(a)

Submission status: Accepted

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

from collections import Counter

ac = Counter(A)

cnt = 0

if len(ac) > K:
    for a in ac.most_common()[K:]:
        cnt += a[1]
    
print(cnt)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    
    d = {}
    for Ai in A:
        if Ai in d.keys():
            d[Ai] += 1
        else:
            d[Ai] = 1
    
    #d_sorted = sorted(d.items(), key=lambda x: x[1])
    
    kosuu = [item[1] for item in d.items()]
    kosuu.sort()
    shuruisuu = len(kosuu)
    
    ans = 0
    cnt = 0
    while K < shuruisuu:
        ans += kosuu[cnt]
        shuruisuu -= 1
        cnt += 1
    
    print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from fractions import gcd
# import queue
from collections import Counter
# from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""


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

C = Counter(L)
X = [x for x in C.items()]

X.sort(key=lambda x : x[1])

kind = len(X)
cnt = 0
idx = 0
while True:
    if kind <= K:
        print(cnt)
        exit()
    kind -= 1
    cnt += X[idx][1]
    idx += 1

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
D=sorted(d.items(), key=lambda x:x[1], reverse=True)
if len(set(A))>k:
    for i in range(k):
        n-=D[i][1]
    print(n)
else:
    print(0)

Submission status: Accepted

Solution code:
from collections import Counter
n,k=map(int,input().split())
a=Counter([int(i) for i in input().split()])
a=sorted(a.items(),key=lambda x:x[1])

li=[]
for i,j in a:
    li.append(j)

ans=0
if k>=len(a):
    ans=0
else:
    for i in range(len(a)-k):
        ans+=li[i]
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)
flag = 0
ans = 0
for b in B.most_common()[::-1]:
    if flag == max(0, len(B)-K):
        break
    ans += b[1]
    flag += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = list(Counter(A).values())
c.sort()

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

Submission status: Accepted

Solution code:
import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
# from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""

N,K = map(int, sys.stdin.readline().split())
list_num = list(map(int, sys.stdin.readline().split()))
list_tmp = [0] * N
# print(list_tmp)

for num in list_num:
    # print(num)
    list_tmp[num-1] += 1

# print(list_tmp)
# print([i for i in list_tmp if i > 0])
list_tmp2 = [i for i in list_tmp if i > 0]

if(len(list_tmp2) <= K):
    print(0)
else:
    list_tmp2.sort()
    # print(list_tmp2)
    cnt = 0
    for i in range(len(list_tmp2) - K):
        cnt += list_tmp2[i]
    print(cnt)

Submission status: Accepted

Solution code:
from collections import*;_,k,*a=map(int,open(0).read().split());c=sorted(Counter(a).values());print(sum(c[:-min(len(c),k)]))

Submission status: Accepted

Solution code:
# sys.stdin.readline
import collections
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N, K = map(int, input().split())
        A = map(int, input().split())
        A_c = collections.Counter(A)

        ans = 0
        for i, c in enumerate(A_c.most_common()[::-1]):
            if len(A_c) - i <= K:
                break
            else:
                ans += c[1]
        print(ans)


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Accepted

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

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

    dic = {}
    for t in a:
        if t in dic:
            dic[t] += 1
        else:
            dic[t] = 1
    dic = sorted(dic.items(), key=lambda x: -x[1])
    
    if len(dic) > K:
        ans = 0
        for i in range(K, len(dic)):
            ans += dic[i][1]
        print(ans)
    else:
        print(0)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import*
n,k=[int(i) for i in input().split()]
l=input().split()
a=Counter(l)
ca=a.most_common()
val=len(set(l))
s=0
if val < k:
	k=val
for i in range(k):
	s +=ca[i][1]
print(n-s)

Submission status: Accepted

Solution code:
from collections import Counter
n,k=map(int,input().split())
A=sorted(Counter(list(map(int,input().split()))).most_common(),key=lambda x:x[1],reverse=True)
print(n-sum([A[i][1] for i in range(min(k,len(A)))]))

Submission status: Accepted

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

A = [int(_) for _ in input().split()]
from collections import Counter

counter = Counter(A)
vals = sorted(counter.values())

ans = 0
for i in range(0, len(set(A)) - K):
    ans += vals[i]

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = input().split()
D = {}

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

ans = 0
if len(D) > K:
	D = sorted(D.values(),reverse = True)
	ans = sum(D[K:])

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = input().split()
D = {}

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

ans = 0
if len(D) > K:
	D = sorted(D.values())
	for i in range(len(D) - K):
		ans += D[i]

print(ans)

Submission status: Accepted

Solution code:
import collections
N, K = map(int, input().split())
a = list(map(int,input().split()))
def v_c(l):
    values, counts = zip(*c.most_common())
    return values, counts
c = collections.Counter(a)
ans = 0
if len(c) > K:
    dif = len(c) - K
    values, counts = zip(*c.most_common())
    for i in range(len(c)-dif, len(c)):
        ans += counts[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
A_sort = sorted(C, reverse=True)

print(sum(A_sort[K:]))

Submission status: Accepted

Solution code:
from collections import defaultdict


def resolve():
    n, k = list(map(int, input().split()))
    a = list(map(int, input().split()))
    d = defaultdict(int)
    for ai in a:
        d[ai] += 1

    if len(d) <= k:
        print("0")
    else:
        dd = sorted(d.values())
        ans = sum([v for v in dd[:len(d) - k]])
        print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

from collections import Counter

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

# 何種類減らす必要がある？
remain = len(set(A))-K
#print(remain)

c = Counter(A)
c = c.most_common()[::-1]
#print(c)

count = 0
s = c[0:remain]
#print(s)
for _, num in s:
    count += num

print(count)

Submission status: Accepted

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

N,K = II()
A = III()

x = defaultdict(int)

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

num = len(x.keys())

if num<=K:
    print(0)
else:
    x2 = list(x.values())
    x2.sort()
    n = num-K
    ans = 0
    for i in range(n):
        ans += x2[i]
    print(ans)

Submission status: Accepted

Solution code:
import sys
import  math
from statistics import *
from collections import *
from operator import itemgetter
"""
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

n, k = na()
aaa = na()
c = Counter(aaa)
if len(c.keys()) <= k:
    print(0)
else:
    vvv = sorted(c.values())
    print(sum(vvv[:-k]))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc081/tasks/arc086_a
# C - Not so Diverse
import collections
N, K = list(map(int, input().split()))
a_list = list(map(int, input().split()))

c = collections.Counter(a_list)

# 数がすくないやつから使う
c_list = sorted([v for k, v in c.items()])
ans = sum(c_list[:-K])
print(ans)

Submission status: Accepted

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

tmp = [0] * N

for i in A:
    tmp[i-1] += 1

tmp = sorted(tmp)

print(sum(tmp[0:N-K]))

Submission status: Accepted

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

num = len(set(a))
ans = 0

if num <= k:
    print(ans)
    exit()
else:
    dict_a = {}
    for i in a:
        if i not in dict_a:
            dict_a[i] = 1
        else:
            dict_a[i] += 1
    a_values = [v for v in dict_a.values()]
    a_values.sort()
    i = 0
    while num > k:
        ans += a_values[i]
        num -= 1
        i += 1

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
alist = list(map(int, input().split()))
d = {}
for a in alist:
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
print(sum(list(sorted(d[k] for k in d))[:-k]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 数の少ないものから消すと考える
n, k = map(int, input().split())
arr = list(map(int, input().split()))
d = {}
for a in arr:
  if a not in d:
    d[a] = 1
  else:
    d[a] += 1

t = len(d)

d = list(sorted(d.items(), key = lambda x : x[1]))
if t <= k:
  print(0)
else:
  count = 0
  for di in d[:t-k]:
    count += di[1]
  print(count)

Submission status: Accepted

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

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,k = LI()
a = LI()
c = list(Counter(a).items())
c.sort(key = itemgetter(1))
m = len(c)
ans = 0
for i,j in c[:m-k]:
    ans += j
print(ans)

Submission status: Accepted

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

cli = []
for i in range(nk[0]+1):
	cli.append(0)

for i in range(nk[0]):
	cli[a[i]] += 1

c = sorted(cli, reverse = True)

m = 0
for i in range(nk[1]):
	m += c[i]

ans = nk[0] - m
if ans < 0:
	ans = 0
	
print(ans)

Submission status: Accepted

Solution code:
import collections

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
a = collections.Counter(A)
asort = sorted(a.items(), key = lambda x:x[1])

ans = 0
l = len(asort)
for j in range(len(asort)):
    if l <= K:
        break
    else:
        l -= 1
        ans += asort[j][1]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n,k = map(int,input().split())
a = [i for i in input().split()]
cnt = Counter(a)
li = list(cnt.values())
li.sort()
ll = len(li)
if ll > k:
    print(sum(li[:ll-k]))
else:
    print(0)

Submission status: Accepted

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