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

Solution (Python):

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

    count = 1
    num = 0
    dic = {}
    for a in A:
        if num == a:
            count += 1

        elif num > 0:
            dic.update({num:count})
            count = 1

        num = a

    dic.update({num:count})

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

    count = 0
    keys = len(dic.keys())
    for i in range(keys-K):
        count += sort_dic[i][1]

    print (count)


if __name__ == "__main__":
    
    
    main()

Solution (Python):
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = [0] * (N + 1)
    MAX = 9999999
    for a in A:
        B[a] += 1
    B.sort(reverse=True)
    s = 0
    for i in range(0, K):
        s += B[i]
    return sum(B) - s


print(main())

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
dict = {}
for i in range(N):
    if(A[i] in dict):
        dict[A[i]] += 1
    else:
        dict[A[i]] = 1
if(len(dict) <= K):
    print(0)
else:
    l = sorted(dict.values())
    ans = 0
    for i in range(len(dict) - K):
        ans += l[i]
    print(ans)

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

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

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

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

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
from collections import Counter
data=Counter(a)
data_i=sorted(data.items(),key=lambda x:x[1])
count=0
set_a=list(set(a))
if k<len(set_a):
    for i in range(len(set_a)-k):
        count+=data_i[i][1]
    print(count)
else:
    print(0)

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

num_dic = {}
for num in A:
    num_dic.setdefault(num, 0)
    num_dic[num] += 1

if len(num_dic.keys()) <= K:
    print(0)
    exit()

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

Solution (Python):


import sys

u"""
最初にdictにいれ、key=number, value=count
それを元に (count, number)のリストをつくり、昇順にソート
リストの長さがK以下になるまでリストをたどる
"""

n, k = [int(e) for e in raw_input().split()]
al = [int(e) for e in raw_input().split()]

d = {}
for a in al:
    d[a] = d.get(a, 0) + 1


l = []
for dk, dv in d.iteritems():
    
    l.append(dv)
l.sort(reverse=True)


b = 0
for e in l[k:]:
    b += e
    











    
sys.stdout.write('%d\n' % b)
sys.stdout.flush()

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

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

sorted_d = sorted(d.values(),reverse=True)
sums = sum(sorted_d[k:])
print(sums)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
from collections import Counter
data=Counter(a)
ans=[]
for i in data.values():
    ans.append(i)
ans.sort()
ans.reverse()
print(n-sum(ans[0:k]))

Solution (Python):
from collections import Counter
N, K = list(map(int, input().split()))
l = list(sorted(Counter(map(int, input().split())).values()))

ans = 0
w = len(l)
for v in l:
    if w <= K:
        print(ans)
        break
    w -= 1
    ans += v

Solution (Python):
from collections import Counter
n,k=map(int,input().split())
a=list(map(int,input().split()))
counter=Counter(a)
ans=[]
for i in counter.values():
    ans.append(i)
ans.sort()
ans.reverse()
v=sum(ans[0:k])
print(n-v)

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

b = [(a[0], 1)]
for i in range(len(a) - 1):
    if a[i] == a[i + 1]:
        x, y = b[-1]
        b[-1] = (x, y + 1)
    else:
        b.append((a[i + 1], 1))
b.sort(key=lambda x: x[1], reverse=True)
if len(b) <= k:
    print(0)
else:
    print(sum([x[1] for x in b[k:]]))

Solution (Python):
from collections import Counter
n,k=map(int,input().split())
cnt=sorted(Counter(map(int,input().split())).values())
count=0
p=len(cnt)
if p<=k:
    print(count)
else:
    for c in cnt:
        if p>k:
            p-=1
            count+=c
        else:break
    print(count)

Solution (Python):
from collections import Counter
N, K = map(int, input().split())
cnt = list(sorted(Counter(map(int, input().split())).values()))
num = len(cnt)
ans = 0
for v in cnt:
	if num <= K:
		break
	ans += v
	num -= 1
print(ans)

Solution (Python):

"""
https://beta.atcoder.jp/contests/abc081/tasks/arc086_a

"""
import sys
from sys import stdin
from collections import Counter
input = stdin.readline


def solve(A, K):
    ans = 0

    c = Counter(A)
    divs = c.most_common()
    divs.reverse()

    diff = len(divs) - K
    for d in divs[:diff]:
        ans += d[1]
    return ans


def main(args):
    N, K = map(int, input().split())
    A = [int(x) for x in input().split()]
    ans = solve(A, K)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
n, k = (int(i) for i in input().split())
 
a = [int(i) for i in input().split()]
 
dic = {}
 
for i in range(n):
    if a[i] in dic:
        dic[a[i]] += 1
    else:
        dic[a[i]] = 1
 
ans_num= len(dic)-k
ans = 0
 
 
if ans_num <= 0:
    ans *= 0 
else:
    sort = sorted(dic.items(), key=lambda x:x[1])
    for i in range(ans_num):
        ans += sort[i][1]
 
print(ans)

Solution (Python):
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
j = Counter(a)
le = sorted(j.items(), key=lambda i : i[1])
times = len(j) - k
si = 0
for i in range(times):
    item, value = le[i]
    si += value
print(si)

Solution (Python):
from collections import defaultdict
N, K = map(int, input().split())
A = list(map(int, input().split()))
d = defaultdict(lambda :0)
for a in A:
    d[a] += 1
key_list = list(d.keys())
value_list = list(d.values())
value_list.sort()
tmp_num = len(value_list)

del_num = tmp_num - K
counter = 0
if del_num > 0:
    for v in value_list[0:del_num]:
        counter += v
print(counter)

Solution (Python):
from collections import Counter

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

counter = Counter(A)
x = len(counter) - K 
if x <= 0:
    print(0)
else:
    ans = 0
    least_common = counter.most_common()[-x:]
    for a, c in least_common:
        ans += c
    print(ans)

Solution (Python):
N,K = map(int,input().split(' '))
L=[0]*N
A = list(map(int,input().split(' ')))
for a in A:
    L[a-1]+=1
L.sort()
print(sum(L[:N-K]))

Solution (Python):
N,K=map(int,input().split())
from collections import Counter
S=list(map(int,input().split()))
C=dict(Counter(S))

a=sorted(C.items(), key=lambda x:x[1])
num=len(C)-K
count=0
b=[]
for key in C:
    b.append(C[key])
c=sorted(b)
for i in range(num):
    count+=c[i]
print(count)

Solution (Python):
N, K = map(int, input().split())
a = list(map(int, input().split()))
dic = {}

for elem in a:
    if elem not in dic:
        dic[elem] = 1
    else:
        dic[elem] += 1

kosuu_list = []
for key in dic:
    kosuu_list.append(dic[key])
kosuu_list_asc = sorted(kosuu_list)

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

Solution (Python):
def solve(N, K, A):
    numbers = {}
    for a in A:
        numbers[a] = numbers.get(a, 0) + 1
    if len(numbers) <= K:
        return 0
    return N - sum(sorted(numbers.values(), key=lambda x: -x)[:K])
N, K = map(int, raw_input().split())
A = map(int, raw_input().split())
print solve(N, K, A)

Solution (Python):


def foo(xs, k):
    d = {}
    for x in xs:
        d[x] = d.get(x, 0) + 1

    ys = sorted(d.items(), key=lambda x: x[1])
    return sum(map(lambda x:x[1], ys[:-k])) 


if __name__ == "__main__":
    n, k = list(map(int, input().split()))
    xs = list(map(int, input().split()))
    print(foo(xs, k))

Solution (Python):
N,K = map(int,input().split())
A_list = list(map(int,input().split()))
counter = [0]*(N+1)

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

zeronashi = [i for i in counter if i != 0]
zeronashi.sort()

length = len(zeronashi)
x = length - K
y = 0
if x < 1:
    print(0)
else:
    for i in range(x):
        y += zeronashi[i]
    print(y)

Solution (Python):
from collections import Counter

n, k = map(int, input().split())
a = map(int, input().split())
s = 0
for w, c in Counter(a).most_common(k):
  s += c
print(n-s)

Solution (Python):
import math

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


A_unique = {}
 
for i in range(0,len(A)):
    if A[i] in A_unique:
        A_unique[A[i]] += 1
    else:
        A_unique[A[i]] = 1
v = list(A_unique.values())
v.sort() 
A_length = len(A_unique.keys()) 
d = 0
n = 0

for k in v:
    if  A_length - n <= K:
        print(d)
        break
    else:
        d += k 
        n += 1

Solution (Python):

from collections import Counter

N, K = list(map(int, input().split()))
Al = sorted(Counter(list(map(int, input().split()))).values())

print(sum(Al[0:len(Al)-K]))

Solution (Python):


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

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

res = 0
num = dict()

key = 0

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

if key - k <= 0:
    print("0")
    exit(0)

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

for i in range(key - k):
    res += num2[i][1]

print(res)

Solution (Python):



import collections

N, K = map(int, raw_input().split())
A = map(int, raw_input().split())
A_dict = collections.Counter(A)
A_common = A_dict.most_common(K)

if len(A_dict) <= K:
  result = 0
else:
  result = N
  for i in range(K): result -= A_common[i][1]

print result

Solution (Python):

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

d = dict()
for elem in A:
    if elem not in d:
        d[elem] = 1
    else:
        d[elem] = d[elem] + 1

std = sorted(d.values())
if len(std) <= K:
    print(0)
else:
    print(sum(std[0:len(std)-K]))

Solution (Python):
from collections import Counter
N,K=map(int,input().split())
a=list(map(int,input().split()))

cnt_sum=0

counter = Counter(a).most_common()

for i in range(K):
  try:
    cnt_sum+=counter[i][1]
  except:
    break
answer=N-cnt_sum

print(answer)

Solution (Python):





counter = 0

condition = list(map(int, input().split()))
A_list = list(map(int, input().split()))

zero_line = [0 for _ in range(200001)]

for i in A_list:
    if zero_line[i] == 0:
        counter += 1
    zero_line[i] += 1

zero_line.sort()
zero_line.reverse()

if counter <= condition[1]:
    print(0)
else:
    print(sum(zero_line[condition[1] :counter]))

Solution (Python):
N,K = map(int,input().split())
a = list(map(int,input().split()))
ball = {}
i = 0
num = 0
change = 0
for n in range(len(a)):
    if a[n] in ball:
        ball[a[n]] = ball[a[n]] + 1
    else:
        ball[a[n]] = 1

num = len(ball)
s = sorted(ball.items(),key=lambda ball: ball[1])
while K < num:
    num -= 1
    change += s[i][1]
    i += 1

print(change)

Solution (Python):
import heapq

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

T = [0] * (N + 1)

for a in A:
    T[a] += 1

ns = []
for t in T:
    if t > 0:
        heapq.heappush(ns, t)
ans = 0
while len(ns) > K:
    ans += heapq.heappop(ns)
print(ans)

Solution (Python):
from collections import Counter

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

lis = list(map(str,input().split()))

counter = Counter(lis)

num = len(counter)

a =  sorted(counter.values())

Ans = 0

for i in a:
	if num <= K:
		break
	Ans += i
	num -= 1
	
print(Ans)

Solution (Python):
from collections import defaultdict

parameter = list(map(int, input().split()))
n, k = parameter[0], parameter[1]

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

numbers = defaultdict(lambda: 0)
for b in balls:
    numbers[b] += 1

k = min(k, len(numbers))

sort_number = sorted(numbers.items(), key=lambda x:x[1], reverse=True)
answer = n - sum([sort_number[i][1] for i in range(k)])
print(answer)

Solution (Python):
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

l = sorted(d.items(), key=lambda p: p[1])
x = len(l)
ans = 0
for e in l:
    if x <= K:
        break
    x -= 1
    ans += e[1]
print(ans)

Solution (Python):
N,K,*A=map(int,open(0).read().split());B={}
for a in A:B[a]=B.get(a,0)+1
print(sum(sorted(B.values())[:-K]))

Solution (Python):
N,K = [int(x) for x in input().split()]
alist = [0]+ sorted([int(x) for x in input().split()])
countlist =[]
prev =0
count =0
while len(alist)>0:
	a = alist.pop()
	if a == prev:
		count+=1
	else:
		countlist.append(count)
		count = 1
		prev = a
countlist.sort()
countlist.reverse()
print(sum(countlist[K:]))

Solution (Python):
N, K = map(int, input().split(' '))
A = map(int, input().split(' '))
bucket = {}

for i in A:
    if i not in bucket.keys():
        bucket[i] = 1
    else:
        bucket[i] += 1

if len(bucket.keys()) <= K:
    print('0')
else:
    turn_over = 0
    turn_num = len(bucket.keys())
    for i in sorted(bucket.values()):
        turn_over += i
        turn_num -= 1
        if turn_num <= K:
            print(turn_over)
            break

Solution (Python):
n, k = (int(i) for i in input().split())
 
a = [int(i) for i in input().split()]
 
dic = {}
 
for i in range(n):
    if a[i] in dic:
        dic[a[i]] += 1
    else:
        dic[a[i]] = 1
 
ans_num= len(dic)-k
ans = 0
 
 
if ans_num <= 0:
    ans *= 0 
else:
    sort = sorted(dic.items(), key=lambda x:x[1])
    for i in range(ans_num):
        ans += sort[i][1]
 
print(ans)

Solution (Python):
n, k = (int(i) for i in input().split())

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

dic = {}

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

ans_num= len(dic)-k
ans = 0


if ans_num <= 0:
    ans *= 0 
else:
    sort = sorted(dic.items(), key=lambda x:x[1])
    for i in range(ans_num):
        ans += sort[i][1]

print(ans)

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

def make_group(list):
    list.sort()
    group = []
    g_count = 0
    prev = list[0]
    for i in range(N):
        current = list[i]
        if prev != current:
            group.append(g_count)
            g_count = 0
        g_count += 1
        prev = current
        if i == N - 1:
            group.append(g_count)
    return sorted(group)

def calc_moving_count(group):
    group_length = len(group)
    length = max(group_length - K, 0)
    should_move_group = group[:length]
    count = sum(should_move_group)
    return count

def main():
    group = make_group(A)
    count = calc_moving_count(group)
    print(count)

if __name__  == '__main__':
    main()

Solution (Python):

"""
Created on Wed Dec 20 12:35:48 2017

"""
 
from collections import Counter

NKList = list(map(int,input().split()))
N = NKList[0]
K = NKList[1]

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

countDict = Counter(AList)
soretedList = sorted(countDict.values())

l = len(soretedList)

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

Solution (Python):

import collections

n,k=map(int,raw_input().split())
a=map(int,raw_input().split())
cnt=collections.Counter(a)
l=[]
for k1,v in cnt.items():
 l.append(v)
l2=sorted(l,reverse=True)
temp=0
for i in xrange(min(k,len(l2))):
 temp+=l2[i]
print n-temp

Solution (Python):
from collections import defaultdict


def get_cnt(n_dic, stop):
    cnt = 0
    for i, item in enumerate(sorted(n_dic.items(), key=lambda x: x[1])):
        cnt += item[1]
        if i+1 == stop:
            break
    return cnt


def main():
    n, k = map(int, input().split())

    n_dic = defaultdict(lambda: 0)
    for x in input().split():
        n_dic[int(x)] += 1

    stop = len(n_dic) - k
    if stop > 0:
        cnt = get_cnt(n_dic, stop)
    else:
        cnt = 0
    print(cnt)
        

if __name__ == '__main__':
    main()

Solution (Python):

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

num = [0] * 200001
for a in A:
    num[a] += 1

num = list(filter(lambda x: x != 0, num))
num.sort()
print(sum(num[:len(num) - K]) if (len(num) > K) else 0)

Solution (Python):
from collections import Counter


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

    c = Counter(A)

    ans = 0
    n_type = len(c)
    for count in sorted(c.values()):
        if n_type <= K:
            break
        ans += count
        n_type -= 1

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
N, K = map(int, input().split(' '))
A = Counter(map(int, input().split(' ')))
if len(A) <= K:
    print(0)
else:
    print(N - sum([
        v for _, v in sorted(A.items(), key=lambda x: x[1], reverse=True)[:K]
    ]))

Solution (Python):

from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(set(a))
c = Counter(a)
d = sorted(c.items(), key = lambda tmp: tmp[1])
ans = 0
if len(b) > k:
    e = len(b)
    for tmp in range(len(d)):
        if e == k:
            print(ans)
            break
        else:
            ans += (d[tmp])[1]
            e -= 1
else:
    print(ans)

Solution (Python):
from collections import Counter

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

print(sum(sorted(list(Counter(a).values()),reverse=True)[k:]))

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

Solution (Python):
[N, K] = input().split(' ')
N = int(N)
K = int(K)
As = input().split(' ')
As = [int(a) for a in As]
nums = {}
for a in As:
    if a in nums:
        nums[a] += 1
    else:
        nums[a] = 1

S = len(nums)

if S <= K:
    print(0)
else:
    xs = [v for v in nums.values()]
    xs.sort()
    print(sum(xs[0:S-K]))

Solution (Python):
_, k = map(int, raw_input().strip().split())
dictYes = {}
a = map(int, raw_input().strip().split())
for value in a:
    if value not in dictYes:
        dictYes[value] = 0
    dictYes[value] += 1
j = []
for value in dictYes:
    j.append((dictYes[value], value))
if len(j) <= k:
    print 0
else:
    j.sort()
    total = 0
    for val in j[:len(j)-k]:
        total += val[0]
    print total

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

Solution (Python):
from collections import Counter
n, k = list(map(int, input().split()))
a = Counter(list(map(int, input().split())))
b = sum(sorted(a.values(), reverse=True)[:k])
print(n-b)

Solution (Python):
n, k = list(map(int, input().split()))
b = list(map(int, input().split()))
a = [0 for i in range(n)]
m = set(b)
ans = 0
for l in range(n):
    a[b[l]-1] += 1
c = [item for item in a if item != 0]
c.sort(reverse=True)
balls = sum(c[0:k])
print(n-balls)

Solution (Python):
import numpy as np
from collections import Counter
import collections
a = list(map(int,input().split()))
N = a[0]
K = a[1]
d = np.array([])
b = list(map(int,input().split()))
li = list(set(b))
c = np.array(b)
l = Counter(b)
h = len(li) -K
Num = 0
if K >= len(li):
  print(0)
  exit()
d = np.append(d,list(l.values()))
d = np.sort(d)
for i in range(h):
  Num += d[i]
print(int(Num))

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

A_input = input()
A = A_input.split()
A_sort = sorted(A)

B=[]
B.append(1)
j=0

for i in range(1,N):
    if(A_sort[i]==A_sort[i-1]):
        B[j]+=1
    else:
        B.append(1)
        j+=1

B_sort = sorted(B)
R=0

if(j+1<=K):
    R = 0
else:
    for i in range(0, j+1-K):
        R += B_sort[i]

print(R)

Solution (Python):
from collections import Counter

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

A_count = Counter(A)

A_sort = sorted(A_count.values())[::-1]
print(sum(A_sort[K:]))

Solution (Python):


from collections import Counter


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


loop_count = 0
count = 0
for key, value in sorted(c.items(), key=lambda x:x[1], reverse=True):
    if loop_count >= k:
        count += value
    loop_count += 1


print(count)

Solution (Python):

from collections import Counter

n, k = list(map(int,input().split()))
a = list(map(int,input().split()))
counter = Counter(a)
if(k-len(counter)>=0):
    print(0)
else:
    ans = 0
    r = counter.most_common()
    for i in range(len(counter)-k):
        ans += r[len(r)-i-1][1]
    print(ans)

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

B = [0] * N

for i in A:
	B[i-1]+=1
	
B = sorted(B)[::-1]

ans = sum(B[K:])
print(ans)

Solution (Python):
from collections import Counter
N, K = map(int, raw_input().split())
A = map(int, raw_input().split())

cnt = Counter()
for num in A:
    cnt[str(num)] += 1

values = sorted(cnt.values())
ans = sum(values[:max(len(values)-K, 0)])

print ans

Solution (Python):
N,K=map(int,input().split())
B = [0] * (N)
for a in map(int,input().split()):
  B[a-1] += 1
B=sorted(B)[::-1]
print(sum(B[K:]))

Solution (Python):
a = [int(i) for i in input().split()]
ball = [int(i) for i in input().split()]
num, kind = a[0], a[1]
ball.sort()
count = []
c = 0 
ans = 0

s = ball[0]
for i in range(num):
    if s == ball[i]:
        c += 1
    else:
        s = ball[i]
        count.append(c)
        c = 1
count.append(c)
count.sort()

for i in range(len(count)-kind):
    ans += count[i]

print(ans)

Solution (Python):
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce
from operator import xor
INF = float("inf")
sys.setrecursionlimit(10**7)


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    c = Counter(A)
    v = list(sorted(c.values()))
    print(sum(v[:-K]))


if __name__ == '__main__':
    main()

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
mp={}
for e in a:
    if e in mp:
        mp[e]+=1
    else:
        mp[e]=1
l=len(mp)
ans=0
for e,cnt in sorted(mp.items(),key=lambda x:x[1]):
    if l <= k:
        break
    ans+=cnt
    l-=1
print(ans)

Solution (Python):
def c_not_so_diverse(N, K, A):
    from collections import Counter
    count = Counter(A)  

    if len(count) <= K:
        
        return 0
    else:
        count = sorted(count.items(), key=lambda x: x[1], reverse=True)
        count = count[K:]  
        ans = 0
        for l in count:
            ans += l[1]
    return ans

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

Solution (Python):
n, k=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
d={i:0 for i in set(a)}
for i in a: d[i]+=1
print(n-sum([t[1] for t in sorted(d.items(), key=lambda x:-x[1])][:k]))

Solution (Python):
from collections import Counter
import numpy as np

n,k = input().split()
n = int(n)
k = int(k)
a_list = input().split()
count = np.array(list(Counter(a_list).values()))
count.sort()
count = count[::-1]
print(count[k:].sum())

Solution (Python):
n, k = map(int, input().split())
a =list(map(int, input().split()))
ball = 0
count = [0]*(n+1)
for i in range(n):
    count[a[i]] += 1

count.sort(reverse=True)
for i in range(k):
    ball += count[i]

print(n - ball)

Solution (Python):
N, K = map(int, input().split())
l = list(map(int, input().split()))
h = [0] * (N + 1)
for i in range(N):
    h[l[i]] += 1
h.sort()
print(sum(h[:-K]))

Solution (Python):
N, K = map(int, input().split())
B = {}
for i in input().split():
    if i in B:
        B[i] += 1
    else:
        B[i] = 1
print(sum(sorted(B.values())[:-K]))

Solution (Python):
N,K=map(int,input().split())
B = [0] * (N)
for a in map(int,input().split()):
  B[a-1] += 1
B=sorted(B)[::-1]
print(sum(B[K:]))

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = {i:0 for i in set(A)}
for a in A:
    B[a] += 1
print(sum(sorted(B.values())[:-K]))

Solution (Python):
from collections import Counter

N,K=map(int,input().split())
A=list(map(int,input().split()))
count=0
S=set(A)
c=Counter(A)
L=c.most_common(len(S))
 
x=len(S)-1
for _ in range(len(S)-K):
    count=count+int(L[x][1])
    x=x-1
 
print(count)

Solution (Python):
n,k = [int(x) for x in input().split()]
s = {}

for i in input().split():
    if(i in s):
        s[i] += 1
    else:
        s[i] = 1


t = list(s.values())
t.sort()
a = sum(t[0:-k])

print(a)

Solution (Python):
n, k = map(int, input().split())
a_l = list(map(int, input().split()))
           
import sys
import numpy as np

def g():
    m_l = np.array([0]*(n+1))

    r = 0
    for a in a_l:
        if m_l[a] == 0: r += 1
        m_l[a] += 1

    if r <= k:
        print(0)
        sys.exit(0)

    mn = 0
    while True:
        ma = np.ma.masked_equal(m_l, 0, copy=False)
        a = np.where(m_l == ma.min())[0]

        for i in a:
            mn += m_l[i]
            r -= 1
            m_l[i] = 0

            if r <= k: return mn
        
print(g())

Solution (Python):
N,K=map(int,input().split())
A=[0]*N
A=list(map(int,input().split()))
Anum={}
for i in range(N):
    if A[i] in Anum:
        Anum[A[i]]+=1
    else:
        Anum[A[i]]=1
if len(Anum)<=K:
    print("0")
else:
    ans=0
    i=0
    for k, v in sorted(Anum.items(), key=lambda x: x[1]):
        if i==len(Anum)-K:
            break
        ans+=v
        i+=1
    print(ans)

Solution (Python):
from collections import Counter
N, K = list(map(int,input().split()))
listA = Counter(input().split())

listA = list(listA.values())
listA.sort()
times = len(listA) - K

num = 0

if times <= 0:
    num = 0
else:
    for i in range(times):
        num += listA[i]

print(num)

Solution (Python):
from collections import Counter


N,K=map(int,input().split())
A=list(map(int,input().split()))
count=0
S=set(A)
c=Counter(A)
L=c.most_common(len(S))

x=len(S)-1
for _ in range(len(S)-K):
    count=count+int(L[x][1])
    x=x-1

print(count)

Solution (Python):
from collections import Counter
N,K=map(int,input().split())
AS=[int(x) for x in input().split()]
nc=Counter(AS)
nckeys=len(nc)
sa=nckeys-K
if sa>0:
    ndch=[x[1] for x in nc.most_common()]
    ndch=sorted(ndch)[:sa]
    hw=sum(ndch)
else:
    hw=0
print(hw)

Solution (Python):
def main():
    N, K = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]
    
    n_num = len(list(set(A) ) )


    nums = [0]* max(A)

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

    
    count = 0
    sub = n_num - K
    for num in nums:
        if(sub <= 0):
            break
        
        if(num > 0):
            count += num
            sub -= 1
    
    print(count)
if __name__=="__main__":
    main()

Solution (Python):
import bisect
Input = map(int,raw_input().split(' '))
N = Input[0]
K = Input[1]
A = map(int,raw_input().split(' '))
A = sorted(A)
count = []
i = 0
while i < len(A):
	count.append(bisect.bisect_right(A,A[i]) - bisect.bisect_left(A,A[i]))
	i = bisect.bisect_right(A,A[i])
count = sorted(count)
dim = len(count)
rslt = 0
if dim <= K:
	print rslt
else:
	rslt += sum(count[:dim-K])
	print rslt

Solution (Python):
from collections import Counter

N,K = list(map(int,input().split()))
lis = list(map(int,input().split()))
num = len(set(lis)) 
if num <= K:
    print (0)
else:
    c = Counter(lis)
    sum = 0
    for tuple in c.most_common()[:-num+K-1:-1]:
        sum += tuple[1]
    print (sum)

Solution (Python):
import collections

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


test = collections.Counter(A)
test_key = list(test.keys())

temp = []
for i in test_key:
    temp.append(int(test[str(i)]))
tyouka = K - len(temp)
temp = sorted(temp)

ans = 0 
if tyouka >=0:
    print(0)
else:
    tyouka = -tyouka
    for i in range(tyouka):
        ans += int(temp[i])
    print(ans)

Solution (Python):
N, K = input().split()
A = input().split()

dic = {}
for a in A:
    if a in dic:
        dic[a] += 1
    else:
        dic[a] = 1

li  = [0] * len(dic)
i   = 0
for d in dic:
    li[i] = dic[d]
    i += 1

total = 0
li.sort()
if len(li) > int(K):
    total += sum(li[:len(li)-int(K)])

print(total)

Solution (Python):
import collections

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

N = int(N)
K = int(K)
P = []

for k ,v in collections.Counter(A).items():
    P.append(v)

P.sort()

Del = len(P) - K
t = 0

if Del < 1:
    Del = 0
else:
    for i in range(Del):
        t += int(P[i])

print(t)

Solution (Python):
N , K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
A.append(0)
cnt = []
i_cnt = 1
for i in range(N):
    if A[i] == A[i + 1]:
        i_cnt += 1
    else:
        cnt.append(i_cnt)
        i_cnt = 1
cnt.sort()
result = 0
for i in range(len(cnt) - K):
    result += cnt[i]
print(result)

Solution (Python):
dict={}
cnt=0
n,k=[int(i) for i in input().split()]
a=input().split()
for i in a:
  if i in dict:
    dict[i]+=1
  else:
    dict[i]=1
dict=sorted(dict.items(), key=lambda x: x[1])
for i in range(0,len(dict)-k):
  cnt+=dict[i][1]
print(cnt)

Solution (Python):




def test(n, k, a):
    count = 0
    dict = {}
    for i in range(n):
        a[i] = int(a[i])
        if a[i] in dict:
            dict[a[i]] += 1
        else:
            dict[a[i]] = 1



    if len(dict) > k:
        num = 0
        for ke, v in sorted(dict.items(), key=lambda x:x[1]):
            if num < (len(dict) - k):
                count += v
                num += 1
            else:
                return count
    else:
        return 0

if __name__ == "__main__":
    nk = input().split(" ")
    a = input().split(" ")
    print(test(int(nk[0]), int(nk[1]), a))