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:
__DEUBG__ = 0


from collections import Counter

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

balls = Counter(balls)
b2 = sorted(balls.items(), key=lambda item: item[1], reverse=True)

uniq_num = len(balls)

if __DEUBG__:
    print('\n'*2)
    print(len(balls))
    print(balls)
    print(b2)

if uniq_num <= K:
    print(0)
else:
    total = 0
    for i in range(uniq_num-K):
        tmp = b2.pop()
        if __DEUBG__:
            print(tmp)
        total += tmp[-1]
    print(total)

Submission status: Accepted

Solution code:
from collections import Counter
n, k = map(int, input().split())
a = map(int, input().split())
ca = Counter(a)
print(sum(sorted([w for q, w in ca.items()])[:-k]))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = map(int, input().split())
ball_count = {}
for num in a:
    if num in ball_count:
        ball_count[num] += 1
    else:
        ball_count[num] = 1
ball_kinds = len(ball_count.keys())
ball_count_list = sorted(ball_count.values(), reverse=True)
ans = 0
for _ in range(ball_kinds - k):
    ans += ball_count_list.pop()

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n, k = map(int, input().split())
L = list(map(int, input().split()))
cnt = Counter(L)
if len(cnt.keys()) <= k:
  print(0)
else:
  rewrite_groups = len(cnt.keys()) - k
  print(sum(sorted(cnt.values())[:rewrite_groups]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import collections

input = sys.stdin.readline

N, K = map(int, input().split())
A = list(map(int, input().split()))
li = []
ans = 0

B = set(A)

cnt = len(B)

#for x in B:
#    a = A.count(x)
#    li.append(a)

li = list(collections.Counter(A).values())
li = sorted(li)

if cnt - K > 0:
    for x in range(cnt - K):
        ans += li[x]
    print(ans)

else :
    print(ans)

Submission status: Accepted

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

import collections
A_tuple = collections.Counter(A).most_common()[::-1]

if len(A_tuple) < K:
    print(0)
else:
    ans = 0
    for i in range(len(A_tuple)-K):
        ans += A_tuple[i][1]
    print(ans)

Submission status: Accepted

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

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

num = list(d.values())
num.sort(reverse=True)
print(sum(num[K:]))

Submission status: Accepted

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

for i in a:
  if i not in dict:
    dict[i] = 1
  else:
    dict[i] += 1

kind = len(dict)-k
if kind <=0:
  print(0)
  exit()

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

for i in range(kind):
  sum += dic[i][1]
  
print(sum)

Submission status: Accepted

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

import collections
c = collections.Counter(A)
c = c.most_common()

m = len(set(A))

if m > k:
    for i in range(m-k):
        ans += c[-1-i][1]
        
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
def main():
    n, k = map(int, input().split())
    a = tuple(map(int, input().split()))

    col = max(0, len(set(a)) - k)
    if col == 0:
        print(0)
        sys.exit()

    ac = Counter(a)
    acl = ac.most_common()[::-1]
    acll = [ace[1] for ace in acl[:col]]

    r = sum(acll)
    print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    N, K = list(map(int, input().split(' ')))
    A = list(map(int, input().split(' ')))
    counter = Counter(A)
    n_kind = len(counter.keys())
    if n_kind <= K:
        print(0)
    else:
        counts = list(counter.values())
        counts.sort()
        print(sum(counts[:(n_kind - K)]))



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
def solve(n, k, a_list):

    counter = collections.Counter()

    for i in a_list:
        counter[i] += 1

    tmp = sorted(counter.values(), reverse=True)
    ans = n - sum(tmp[:k])
    
    return ans

if __name__ == "__main__":
    n, k = [int(i) for i in input().split()]
    a_list = [int(i) for i in input().split()]
    print(solve(n, k, a_list))

Submission status: Accepted

Solution code:
import collections
def solve(n, k, a_list):

    counter = collections.Counter()

    for i in a_list:
        counter[i] += 1

    key_num = len(counter.keys())
    if key_num > k:
        tmp = sorted(counter.items(), key=lambda x: x[1])
        ans = sum([v for k, v in tmp[: key_num - k]])
    else:
        ans = 0

    return ans

if __name__ == "__main__":
    n, k = [int(i) for i in input().split()]
    a_list = [int(i) for i in input().split()]
    print(solve(n, k, a_list))

Submission status: Accepted

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

from collections import Counter

AL=Counter(A)
AL= sorted(AL.items(), key=lambda x:x[1])
n=len(set(A))-K

sum=0
for i in range(n):
  sum+=AL[i][1]
print(sum)

Submission status: Accepted

Solution code:
# coding: utf-8
# hello worldと表示する
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n,k=MI()
lis=LI()
cou=Counter(lis)
u=cou.most_common()
#print(u)
ans=0
for i in range(len(u)-k):
    j=len(u)-i-1
    #print(j)
    ans+=u[j][1]
    #print(cou.most_common()[j][1])
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n,k=map(int,input().split())
l=[int(i) for i in input().split()]
c=Counter(l).most_common()
if len(c)<=k:
    ans=0
else:
    sum=0
    for i in range(0,k):
        sum+=c[i][1]
        ans=n-sum

print(ans)

Submission status: Accepted

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

nndict = {}
nnlist = []
for i in range(1, N+1):
  if ndict[i] >= 1:
    if ndict[i] not in nndict:
      nndict[ndict[i]] = 1
      nnlist.append(ndict[i])
    else:
      nndict[ndict[i]] += 1
      
nnlist.sort(reverse=True)


ki = K
for i in range(len(nnlist)):
  if ki > nndict[nnlist[i]]:
    ki -= nndict[nnlist[i]]
  else:
    nndict[nnlist[i]] -= ki
    inow = i
    break
else:
  print(0)
  exit()

ans = 0
for i in range(i, len(nnlist)):
  ans += nndict[nnlist[i]] * nnlist[i]
print(ans)

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

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

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  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

#####MakeDivisors######
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)
  return divisors

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

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

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n = n//a
    count += 1
  if a**count == n:
    return count
  else:
    return count+1

#############
# Main Code #
#############

N,K = IL()
A = IL()
d = DD(int)
for a in A:
  d[a] += 1
  
d = [(d[k])for k in d]
d.sort()
n = len(d)
P(sum(d[:max(0,n-K)]))

Submission status: Accepted

Solution code:
# 解説版

from collections import defaultdict
N, K = map(int, input().split())
balls = defaultdict(int)
for b in input().split():
    balls[b] += 1
balls = sorted(balls.values())
print(N - sum(balls[-K:]))

Submission status: Accepted

Solution code:
from collections import defaultdict
N, K = map(int, input().split())
balls = defaultdict(int)
for b in input().split():
    balls[b] += 1
types = len(balls.keys())
balls = sorted(balls.values(), reverse=True)
ans = 0
while types > K:
    ans += balls.pop()
    types -= 1
print(ans)

Submission status: Accepted

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

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

c = Counter(a).most_common()
ans = 0
tmp = 0
for i,cnt in c:
    tmp += cnt
    k-=1
    if k == 0:
        ans = n-tmp
        break
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)
values,counts=zip(*a.most_common())
l=len(values)
ans=0
i=0
while l>k:
  ans=ans+counts[len(values)-i-1]
  i=i+1
  l=l-1

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
X = dict()
for i in A:
    if i in X:
        X[i] += 1
    else:
        X[i] = 1
Y = [] 
for i in X.keys():
    Y.append(X[i])
Y.sort()
#print(Y)
if len(Y) <= K:
    ans = 0
else:
    ans = sum(Y[:len(Y)-K])
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)
count = c.most_common()[::-1]

n = len(count)-K
cnt = 0
for i in range(n):
    cnt += count[i][1]
print(cnt)

Submission status: Accepted

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

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()
cnt=0
for i in range(len(a)):
  if i>k-1:
    cnt+=a[i][1]
print(cnt)

Submission status: Accepted

Solution code:
# coding: utf-8
import re
import math
from collections import defaultdict
from collections import deque
import collections
from fractions import Fraction
import itertools
from copy import deepcopy
import random
import time
import os
import queue
import sys
import datetime
from functools import lru_cache
#@lru_cache(maxsize=None)
readline=sys.stdin.readline
sys.setrecursionlimit(2000000)
#import numpy as np
alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_pow(a,b):#aはbの累乗数か
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def get_primes(n,type="int"):
    if n==0:
        if type=="int":return []
        else:return [False]
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=""):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def pm(x):
    return x//abs(x)
def inputintlist():
    return list(map(int,input().split()))
######################################################################################################


N,K=inputintlist()
A=inputintlist()
D=[]
for a,b in collections.Counter(A).items():
    D.append([a,b])
D.sort(key=lambda x:x[1])
llen=len(D)
ans=0
i=0
while llen>K:
    ans+=D[i][1]
    llen-=1
    i+=1
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
memo = [0] * N
ans = 0

for a in A:
    memo[a-1] += 1
    
memo.sort()

for i in memo[:N-K]:
    ans += i

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# AtCoder
from collections import Counter
N, K = map(int, input().split())
A = list(map(int, input().split()))
c = Counter(A)
k = sorted(c.most_common(), reverse=True, key=lambda x: x[1])[K:]

ans = 0
for e in k:
    ans += e[1]
print(ans)

Submission status: Accepted

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

for i in a:
    if i in ball:
        ball[i] += 1
    else:
        ball[i] = 1

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

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

Submission status: Accepted

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

    res = 0
    balls = {}
    bucket = [[] for _ in range(n+1)]

    for ball in A:
        if ball in balls:
            balls[ball] += 1
        else:
            balls[ball] = 1
    
    for key, value in balls.items():
        bucket[value].append(key)

    for i in range(len(bucket)):
        for j in range(len(bucket[i])):
            if bucket[i] != [] and len(balls) > k:
                res += i
                balls.pop(bucket[i][j])

    print(res)

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

Submission status: Accepted

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

Submission status: Accepted

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

D = {}
for a in A:
    if a not in D:
        D[a] = 1
    else:
        D[a] += 1
E = sorted(D.values())
c = 0
i = 0
l = len(D)
while l > K:
    c += E[i]
    l -= 1
    i += 1
print(c)

Submission status: Accepted

Solution code:
from collections import defaultdict

N, K = map(int, input().strip().split(" "))

d = defaultdict(int)

for i in [int(i) for i in input().strip().split(" ")]:
    d[i] += 1

if len(d.keys()) <= K:
    print(0)
else:
    cnt = len(d.keys()) - K
    n = 0
    for v in sorted(d.values())[:cnt]:
        n += v
    print(n)

Submission status: Accepted

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

count = [0] * n

for x in range(n):
    count[a[x]-1] += 1

count.sort()
length = len(count)

print(sum(count[:length - k]))

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(alis)

jun = c.most_common()[::-1]

total = len(jun)
rem = total - k


ret = 0
if rem < 0:
    print(0)
else:
    for i in range(rem):
        ret += jun[i][1]
    print(ret)

Submission status: Accepted

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

Submission status: Accepted

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

#n,k = 10,3
#a = [5,1,3,2,4,1,1,2,3,4]

count = [0] * (n+1)
for i in a:
  count[i] += 1
count2 = []
for i in count:
  if i:
    count2.append(i)
count2 = sorted(count2)
target = len(count2) - k 
target = max(target,0)


ans = 0
if target:
  for i in range(target):
    ans += count2[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N, K = map(int, input().split(' '))
balls = tuple(map(int, input().split(' ')))

counter = Counter(balls)
counts = sorted(counter.values(), reverse=True)

print(sum(counts[K:]))

Submission status: Accepted

Solution code:
from collections import Counter

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

ans = 0
if k < len(Counter(a).keys()):
    kv = Counter(a).most_common()
    for i in range(1, len(Counter(a).keys()) - k + 1):
        ans += kv[-i][1]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
c = Counter(A)
diff = len(c.keys()) - K
if diff<=0:
    print(0)
else:
    c_s = sorted(c.items(), key=lambda x: x[1])
    cnt=0
    for _, value in c_s:
        cnt += value
        diff -= 1
        if diff==0:
            break
    print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import Counter
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,K=map(int,input().split())
    A=list(map(int,input().split()))
    c=Counter(A)
    m=c.most_common()[::-1]
    ans=0
    for _,n in m[:-K]:
        ans+=n
    print(ans)
    

if __name__ == '__main__':
    main()

Submission status: Accepted

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

a=[int(i) for i in input().split()]
d=Counter(a)
a=sorted(list(d.items()),key=lambda x:x[1],reverse=True)
if len(a)<=k:
    print(0)
else:
    ans=0
    for i in range(k,len(a)):
        ans+=a[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()))

d = Counter(A)
dic = sorted(d.items(), key=lambda x:x[1])
if len(dic)<=K:
    print(0)
else:
    ans = 0
    cnt = len(dic)
    for key,value in dic:
        if cnt-1==K:
            ans += value
            print(ans)
            break
        else:
            ans += value
            cnt -= 1

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

dic=sorted(d.items(), key=lambda x:x[1])
if len(dic)<=K:
    print(0)
else:
    ans=0
    cnt=len(dic)
    for key,value in dic:
        if cnt-1==K:
            ans+=value
            print(ans)
            break
        else:
            ans+=value
            cnt-=1

Submission status: Accepted

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

dict = {}

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

countlist = sorted([a for a in dict.values()], reverse=True)

print(sum(countlist[K:]))

Submission status: Accepted

Solution code:
import collections
N,K = map(int,input().split())
A = list(map(int,input().split()))
A = collections.Counter(A)
res = 0
cnt = A.most_common()
if len(cnt) > K:
    for i in range(K,len(cnt)):
        res += cnt[i][1]
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
N,K=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
C=Counter(A)
C=list(C.values())
C.sort()
print sum(C[:-1*K])

Submission status: Accepted

Solution code:
from collections import Counter

def resolve():
	n, k = map(int, input().split())
	a = list(map(int, input().split()))
	c = Counter(a)
	lc = len(c.keys())
	if lc > k:
		v = 0
		sc = sorted(c.values())
		for i in range(lc-k):
			v += sc[i]
		print(v)
	else:
		print(0)
resolve()

Submission status: Accepted

Solution code:
#複数の整数入力を受け付ける関数
def datain(num):

    data = []
    iscnt = True

    while iscnt:
        for val in map(int,input().split()):
            data.append(val)
            if len(data) == num:
                iscnt = False
                break
        
    return data

INF = 10**8
data1 = datain(2)
data2 = datain(data1[0])
count = [INF for i in range(data1[0]+1)]
num = 0
ans = 0

for val in data2:
    if count[val] == INF:
        count[val] = 0
        num += 1
    count[val] += 1

count.sort()
#print(count);print(num)

if num >= data1[1]:
    for val in count[0:(num-data1[1])]:
        ans += val

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)]

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

        cnt = 0
        for i in range(len(Dic) - 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()))
cnt = [0] * (n+1)
for i in range(n):
    cnt[a[i]] += 1
cntl = sorted(cnt)
print(sum(cntl[:-k]))

Submission status: Accepted

Solution code:
ma = lambda :map(int,input().split())
n,k = ma()
A = list(ma())
import collections
co = collections.Counter(A)
l = sorted(list(co.values()))

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

Submission status: Accepted

Solution code:
def main():
    from collections import Counter

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    from collections import Counter

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    from collections import Counter

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter


n, k  = map(int, input().split())
m = Counter(input().split())
print(sum(sorted(m.values(), reverse=True)[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).most_common()

if len(d) > K:
    print(sum(v for k, v in d[-(len(d) - K):]))
else:
    print(0)

Submission status: Accepted

Solution code:
import collections

n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt = collections.Counter(a)
ans = 0
t=cnt.most_common()
if len(t) <= k:
    print(0)
    exit()

for i in range(k):
    ans += t[i][1]
print(n - ans)

Submission status: Accepted

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

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

A.sort()

cnt = []

if N ==1:
    print(1)
    exit()

tmp = 1
for i in range(1,len(A)):
    if (A[i-1] == A[i]):
        tmp+=1
    else:
        cnt.append(tmp)
        tmp=1

    if (i == (len(A)-1)):
        cnt.append(tmp)
    
cnt.sort()

#print(cnt)

if N > K:
    print(sum(cnt[:(len(cnt)-K)]))
else:
    print(0)

Submission status: Accepted

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

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.items(), key=lambda x:x[1])
l = len(d)

cnt = 0
for _, j in d:
    if l > k:
        l -= 1
        cnt += j
    else:
        break


print(cnt)

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.items(), key=lambda x:x[1])
l = len(d)

cnt = 0
for _, j in d:
    if l > k:
        l -= 1
        cnt += j

print(cnt)

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)]

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

        cnt = 0
        for j in range(len(Dic) - k):
            cnt += Dicsort[j][1]

        print(cnt)
resolve()