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):
N,K = map(int,input().split())
l = list(map(int,input().split()))
d = dict()
for a in l:
   if a in d:
      d[a] += 1
   else:
      d[a] = 1
v = list(d.values())
if len(v) <= K:
   print(0)
else:
   v.sort()
   print(sum(v[:-K]))

Solution (Python):
from collections import Counter

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

    cnt = Counter(A).most_common()[::-1]
    l = len(cnt)
    if l <= K:
        print(0)
    else:
        x = l - K
        ball = 0
        for i, j in cnt[:x]:
            ball += j
        print(ball)

main()

Solution (Python):
N , K = list(map(int,input().split()))
li = list(map(int,input().split()))
typ = []
li.sort()
count = 0 
for i in range(N - 1): 
    count += 1
    if li[i] != li[i + 1]: 
        typ.append(count)
        count = 0 
if li[N - 1] != li[N - 2]: 
    typ.append(1)
else:
    typ.append(count + 1)
typ.sort()
count = 0 
if len(typ) > K:
    for i in range(len(typ) - K): 
        count += typ[i]
print(count)

Solution (Python):
a = list(map(int,input().split()))
n = a[0]
k = a[1]
b = list(map(int,input().split()))
c = [0 for x in range(0,len(b))]


for num in b:
    c[num-1] = c[num-1]+1

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

Solution (Python):



"""arc086_a"""

import sys
import pprint
import re

from collections import Counter

from functools import reduce

import itertools
pp = pprint.PrettyPrinter(indent=2)


def main():
    """Main function."""
    n, k = list(map(int, input().split(' ')))
    as_ = list(map(int, input().split(' ')))

    counter = Counter(as_)

    occ_list = sorted(counter.most_common(), key=lambda t: t[1], reverse=True)
    if len(occ_list) <= k:
        print(0)
    elif len(occ_list) == k + 1:
        print(occ_list[-1][1])
    else:
        print(reduce(lambda t1, t2: (0, t1[1] + t2[1]), occ_list[k:])[1])



if __name__ == '__main__':
    sys.exit(main())

Solution (Python):
n,k = input().split()
list = input().rstrip().split()
dict = {}

for i in range(int(n)):
    if list[i] in dict:
        dict[list[i]] +=1
    else:
        dict[list[i]] = 1
count = 0
if len(dict) > int(k):
    count = int(n)
    max_val = max(dict.values())
    keys_of_max_val = [key for key in dict if dict[key] == max_val]
    if len(keys_of_max_val) > int(k):
        count = int(n)- int(k)*max_val
    else:
        work = 0
        for keys, vals in sorted(dict.items(), key=lambda x: -x[1]):
            count -= vals
            work +=1
            if work == int(k):
                break
print(count)

Solution (Python):
N,K = list(map(int, input().split()))
a = list(map(int, input().split()))
b = [0 for i in range(200001)]
ans=0
count=0
num = [0 for i in range(200001)]
for i in range(N):
    b[a[i]] += 1

for i in range(200001):
    if(b[i] >= 1):
        count+=1
        num[b[i]]+=1

for i in range(200000):
    if(count-K) > 0:
        ans += min(num[i+1],count-K)*(i+1)
        count -= num[i+1]

print(ans)

Solution (Python):
nk = list(map(int,input().split()))
ls = list(map(int,input().split()))

d = dict()

for i in ls:
    if i in d:
        d[i] += 1
    else:
        d[i] = 1
       
l = list(d.values())
l.sort(reverse = True)

num = sum(l[nk[1]:])

print(num)

Solution (Python):
n, k = map(int, input().split())
nums = list(map(int, input().split()))
def judge(n, k, nums):
  original = nums
  types = set(nums)
  diff = len(types) - k
  count = []
  temp_count = 0
  temp_char = nums[0]
  ans = 0
  nums.sort()

  if len(types) <= k:
    return 0

  for i in range(n):
    if nums[i] == temp_char:
      temp_count += 1
    else:
      temp_char = nums[i]
      if not temp_count == 0:
        count.append(temp_count)
      temp_count = 1

    if i == n-1:
      if not temp_count == 0:
        count.append(temp_count)
        
  count.sort()
  
  return sum(count[:diff])

print(judge(n, k, nums))

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
counter = [] 
x = 0
count = 1
while x < n-1:
	for i in range(n-x-1):
		if a[x] == a[x+1+i]:
			count += 1
		else:
			break
	counter.append(count)
	x = x+1+i
	count = 1
if a[n-2] != a[n-1]:
	counter.append(1)
counter.sort()

y = len(counter)
l = y
answer = 0
for i in range(y): 
	if l > k:
		answer += counter[i]
		l = l - 1
	else:
		break
print(answer)

Solution (Python):
import math
import itertools
import heapq
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect, bisect_left, bisect_right
from collections import defaultdict, deque








def LM(t, r): return list(map(t, r))
def R(): return stdin.readline()
def RS(): return R().split()
def I(): return int(R())
def F(): return float(R())
def LI(): return LM(int,RS())
def LF(): return LM(float,RS())
def ONE_SL(): return list(input())
def ONE_IL(): return LM(int, ONE_SL())
def ALL_I(): return map(int, stdin)
def ALL_IL(): return LM(int,stdin)


def ap(f): return f.append
def pll(li): print('\n'.join(LM(str,li)))
def pljoin(li, s): print(s.join(li))




def main():
	N,K = LI()
	A = LI()

	cnt_d=defaultdict(lambda: 0)
	for i in A:
		cnt_d[i] += 1
	
	sorted_d = sorted(cnt_d.items(), key=lambda x: x[1])
	
	ans=0
	i=0
	n = len(cnt_d)
	while n>K:
		min_k,min_v = sorted_d[i]
		i+=1
		ans+=min_v
		n-=1

	print(ans)


if __name__ == '__main__':
	main()

Solution (Python):
nk = [int(x) for x in input().split()]
n = nk[0]
k = nk[1]
a = [int(x) for x in input().split()]

many = 1000000
cnt = [many for x in range(200001)]
kind = 0
for ai in a:
  if cnt[ai] == many:
    cnt[ai] = 1
    kind += 1
  else:
    cnt[ai] += 1

cnt.sort()
res = 0
if kind > k:
  for i in range(kind - k):
    res += cnt[i]

print(res)

Solution (Python):


data1=input()
data2=input()
n=data1.split(" ")
a=data2.split(" ")
c={}
count = 0
for i in range(int(n[0])):
    if a[i] in c:c[a[i]] += 1
    else:c[a[i]] =1
c = sorted(c.items(), key = lambda x:x[1])
for i in range(len(c)-int(n[1])):
    count += c[i][1]
print(count)

Solution (Python):
import numpy as np

n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
d = {}
for i in range(n):
    if a[i] in d:
        d[a[i]] += 1
    else:
        d[a[i]] = 1
d = sorted(d.items(), key=lambda x: x[1])
num = len(d) - k
d = np.array(d)
count = np.sum(d[:num, 1])
print (count)

Solution (Python):
from collections import Counter

N, K = map(int, input().split())
A = list(map(int, input().split()))
counter = dict(Counter(A))
counter_list = list(counter.values())
counter_list.sort()

ans = 0


z = len(counter_list)
for c in counter_list:
    if z > K:
        z -= 1
        ans += c
    else:
        print(ans)
        break

Solution (Python):
from collections import Counter

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

c=Counter(input().split())

l=len(c)

if(l<=k):
    print(0)
else:
    print(sum(v for key,v in c.most_common()[k:]))

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

kosuu = [0 for i in range(N+1)]
for i in range(N):
    kosuu[A[i]] = kosuu[A[i]] + 1
seikosuu = [x for x in kosuu if x > 0]
seikosuu.sort()
l = len(seikosuu)
ans=sum(seikosuu[0:l-K])
print(ans)

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

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

for i in a:
    if i not in freq:
        freq[i] = 1
    else:
        freq[i] += 1
        
l = sorted(freq.values())
total = 0
index = 0

while len(l) - index > k:
    total += l[index]
    index += 1
print(total)

Solution (Python):


from collections import defaultdict

N, K = map(int, raw_input().split())
As = map(int, raw_input().split())

dic = defaultdict(lambda: 0)

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

tar = len(dic) - K

count = 0
if tar <= 0:
    print count
else:
    for i, (k, v) in enumerate(sorted(dic.items(), key=lambda x: x[1])):
        count += v
        if i+1 == tar:
            print count
            break

Solution (Python):
def main():
    from collections import Counter
    n,k=[int(i) for i in input().split()]
    c=Counter([int(i) for i in input().split()])
    key=sorted(c,key=lambda x:c[x])
    ans=0
    if len(c)-k <=0:
        print("0")
    else:
        for i in key[:len(c)-k]:
            ans+=c[i]
        print(ans)

if __name__=='__main__':
    main()

Solution (Python):



from collections import Counter

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

b = len(a) - k

amount = 0
if b != 0:
    for i,v in enumerate(a.most_common()):
        if i+1 > k : amount += v[1]
    print(amount)

else:
    print(0)

Solution (Python):
import numpy as np
st=input().split()
N=int(st[0]);K=int(st[1])
S=set([])
st=input().split()
P=[0]*(N+1)
Q=[0]*(N+1)
for i in range(N):
    k=int(st[i])
    S.add(k)
    P[k-1]+=1
    Q[P[k-1]-1]-=1
    Q[P[k-1]]+=1
out=0
change=0
count=0
if len(S)<=K:
    print(0)
else:
    for i in range(1,N+1):
        flag=1
        for j in range(Q[i]):
            change+=i
            count+=1
            if count==max([len(S)-K,0]):
                flag=0
                break
        if flag==0:
            break
    print(change)

Solution (Python):
N, K = [int(i) for i in input().split(' ')]
A = [int(i) for i in input().split(' ')]
dict = {i:0 for i in range(1,N+1)}
for i in range(N):
 dict[A[i]] += 1
print(N-sum(sorted(dict.values())[-K:]))

Solution (Python):
if __name__=="__main__":
        l1 = list(map(int,input().split(" ")))
        l2 = list(map(int,input().split(" ")))
        l = {i:0 for i in range(200001)}
        N = l1[0]
        K = l1[1]
        for i in range(N):
            l[l2[i]]+=1
        dict = sorted(l.values())
        condition = lambda t:t!=0
        fliter_dict = list(filter(condition,dict))
        if len(fliter_dict)<=K:
            print(0)
        else:
            print(sum(fliter_dict[0:(len(fliter_dict)-K)]))

Solution (Python):
n,k=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
count=1
a.sort()

num=[]
count_kakikae=0

for i in range(1,n):
    if a[i]==a[i-1]:
        count=count+1
    else:
        num.append(count)
        count=1
    if i==n-1:
        num.append(count)



num.sort()

if len(num)-k>0:
    for i in range(len(num)-k):
        count_kakikae=count_kakikae+num[i]

print(count_kakikae)

Solution (Python):
from collections import Counter
N, K = (int(x) for x in input().split())
A = [int(x) for x in input().split()]

c = Counter(A)
minors = c.most_common()[K:]
print(sum([elem[1] for elem in minors]))

Solution (Python):
from collections import Counter
N,K = [int(i) for i in input().split()] 
A = [int(i) for i in input().split()]
counter = Counter(A)
ans = 0
counter_list = counter.most_common()[::-1]
if K < len(counter_list):
    for a in counter_list[:len(counter_list)-K]:
        ans += a[1]
print(ans)

Solution (Python):
N,K=map(int,raw_input().split(' '))
A=map(int,raw_input().split(' '))
A.sort()

t=[float('inf')]*N
t[0]=0
prev=A[0]
i=0
for a in A:
  if a==prev:
    t[i]+=1
  else:
    i+=1
    t[i]=1
  prev=a

d=len(set(A))
if d<=K:
  print 0
else:
  t.sort()
  s=0
  for i in range(d-K):
    s+=t[i]
  print s

Solution (Python):
import collections

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

C = list(collections.Counter(A).values())
count = 0

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

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

from collections import Counter
import numpy as np
c = Counter(A)

no = len(c.most_common())
numlist = []
for label, num in c.most_common():
    numlist.append(num)

diff = no - K
ans = 0
if(diff > 0):
    ans = np.sum(numlist[-diff:])
    
print(ans)

Solution (Python):
import collections
n, k = map(int, raw_input().split())
num = map(int, raw_input().split())
n = int()
num = collections.Counter(num)
for t, v in sorted(num.items(), key=lambda x:x[1])[0:-k]:
    n += v
print n

Solution (Python):
import collections
import sys
N,K=map(int,raw_input().split())
a=[]
a=collections.Counter( map(int,raw_input().split()) )
b=sorted(a.items(), key=lambda x:x[1])

if len(b)=="K":
	print 0
	sys.exit()
else:
	sum=0
	for i in range(len(b)-K):
		sum+=b[i][1]
	else:
		print sum

Solution (Python):
import numpy as np

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

A = list(map(int, input().split()))
A.sort()
key = A[0]

B = []
cnt = 1

for i in range(1,N):
    if key == A[i]:
        cnt += 1
    else:
        key = A[i]
        B.append(cnt)
        cnt = 1

B.append(cnt)

B.sort(reverse=True)

print(sum(B[K:]))

Solution (Python):
list_ = [0]
n, min_count = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
kind = 1
tmp = a[0]
for i in range(n):
    if(tmp != a[i]):
        kind += 1
        tmp = a[i]
        list_.append(0)
    list_[kind - 1] += 1
ans = 0
list_.sort()
idx = 0
for i in range(kind - min_count):
    ans += list_[idx]
    idx += 1
print(ans)

Solution (Python):
import collections
from collections import  OrderedDict

num = [int(x) for x in input().split()]
val = input().split()

count_dict = collections.Counter(val)

cant = 0

if len(count_dict) <= num[1]:
    print(0)
else:
    sort_dict = OrderedDict(sorted(count_dict.items(), key=lambda x:x[1], reverse= True))
    value_dict = sort_dict.values()
    print(sum(list(value_dict)[num[1]:]))

Solution (Python):
n,k = map(int,raw_input().split(" "))
a = raw_input().split(" ")

dic = {}
for x in a:
  if x not in dic:
    dic[x] = 1
  else:
    dic[x] += 1
z = len(dic)
if z <= k:
  print 0
  exit()
ans = 0
d = sorted(dic.items(), key=lambda x: x[1])
for a in range(z-k):
  ans += int(d[a][1])
print ans

Solution (Python):
n, k = map(int, input().split())
a = list(map(str, input().split()))
dis = len(set(a)) - k
if dis > 0:
    dic = {}
    for i in range(n):
        if a[i] in dic:
            dic[a[i]] += 1
        else:
            dic[a[i]] = 1
    dic_v = sorted(dic.values())
    print(sum(dic_v[:dis]))
else:
    print(0)

Solution (Python):


import sys

def main(argv):
    line = sys.stdin.readline()
    while line:
        N, K = line.rstrip("\n").split(" ", 2)
        N = int(N)
        K = int(K)
        A = map(
          lambda a: int(a), sys.stdin.readline().rstrip("\n").split(" "))
        
        histogram = {}
        for a in A:
            if a not in histogram:
                histogram[a] = 0
            histogram[a] += 1

        if K >= len(histogram):
            print 0
        else:
            diff = len(histogram) - K
            print sum(sorted(histogram.values())[0:diff])

        line = sys.stdin.readline()

if __name__ == "__main__":
    main(sys.argv)

Solution (Python):
from collections import OrderedDict, deque, Counter

def solve():
    N, K = list(map(int, input().split(" ")))
    A = list(map(int, input().split(" ")))
    counter = Counter(A)
    target = counter.most_common()
    l = len(target) - K
    target.reverse()
    ans = target[:l]
    summation = 0
    for word, cnt in ans:
        summation += cnt

    print(summation)


solve()

Solution (Python):

import sys
from collections import Counter

N, K = sys.stdin.readline().split()
K = int(K)
line = sys.stdin.readline()
A = map(int, line.strip().split())
D = Counter(A)
diff = len(D) - K

if diff <= 0:
    print(0)
else:
    print( sum( i[1] for i in D.most_common()[-1*diff:]) )

Solution (Python):
from collections import Counter
n,s=[int(i) for i in input().split(' ')]
a=[int(i) for i in input().split(' ')]

b=[]
counter = Counter(a)
for word, cnt in counter.most_common():
    b.append(int(cnt))
    
b.reverse()

x=0
if len(b)-s > 0:
  for i in range(len(b)-s):
    x+=b[i]
print(x)

Solution (Python):
import sys

line = sys.stdin.readline().strip()
N, K = [int(x) for x in line.split()]

line = sys.stdin.readline().strip()
A = [int(x) for x in line.split()]

X = {}
for i in A:
    X[i] = X.get(i, 0) + 1

vs = sorted(list(X.values()))

r = 0

if len(vs) < K:
    r = 0
else:
    r = sum(vs[:-K])

print(r)

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

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

L = list(d.values())

if len(L) <= K:
    print(0)
else:
    L.sort()
    print(sum(L[:-K]))

Solution (Python):
import sys
readline = sys.stdin.readline

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

d = {}
for a in A:
    if str(a) in d:
        d[str(a)] += 1
    else:
        d[str(a)] = 1

ans = 0
count = 1
for k, v in sorted(d.items(), key=lambda x: -x[1]):
    if count > K:
        ans += v
    count += 1        
print(ans)

Solution (Python):




 
s = input().split()
N = int(s[0])
K = int(s[1])
s = input().split()
s = [int(i) for i in s]

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

L = len(dic)
if K >= L : 
    print(0)
else :
    okikae = L - K
    x = sorted(dic.items(), key=lambda x: x[1])
    rst = 0
    for i in range(okikae) : 
        rst += x[i][1]
    print(rst)

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

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


sorted_freq=sorted(freq)


count=0
for i in range(1,N-K+1):
    count+=sorted_freq[i]
print(count)

Solution (Python):
import collections

n, k = map(int, input().split())
a = list(map(int, input().split()))
c = collections.Counter(a)
c = sorted(c.items(), key=lambda k: k[1],
           reverse=True)

size = len(c)
if size > k:
    ans = sum([v for _, v in c[k:]])
    print(ans)

else:
    print(0)

Solution (Python):
from collections import Counter

N, K = map(int, input().split())
A = map(int, input().split())
C = Counter(A)
if len(C.keys()) <= K:
    print(0)
else:
    n = len(C.keys()) - K
    print(sum(map(lambda x:x[1],C.most_common()[-n:])))

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

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

cnts = sorted(d.values())
print(sum(cnts[:-k]))

Solution (Python):
[n, k] = map(int, raw_input().split())
xs = map(int, raw_input().split())
d = dict()
for x in xs:
    if x in d:
        d[x] += 1
    else:
        d[x] = 1
v = d.values()
v.sort()
if len(v) <= k:
    r = []
else:
    r = v[:len(v)-k]
print sum(r)

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

ans = 0
num = {}
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):
	ans += num2[i][1]

print(ans)