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:
n, k = map(int, input().split())
a = list(map(int, input().split()))
d = dict()
for x in a:
    try: d[x] += 1
    except: d[x] = 1
l = sorted(list(d.values()))
if len(l) <= k: print(0)
else: print(sum([l[i] for i in range(len(l) - k)]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *


d = defaultdict(int)
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
for i in range(n):
    d[a[i]] += 1
count = 0
size = len(d)
for i, v in sorted(d.items(),key=lambda x:x[1]):
    if size <= k:
        break
    count += v
    size -= 1
print(count)

Submission status: Accepted

Solution code:
N,K = list(map(int,input().split()))
data = list(map(int,input().split()))
s = {}
ans = 0
for i in data:
    if i not in s:
        s[i] = 1
    else:
        s[i] += 1

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

Submission status: Accepted

Solution code:
from collections import Counter
n,k=map(int,input().split())
a=Counter(input().split())
ans=0
keys,counts=zip(*a.most_common())
for num,(key,count) in enumerate(zip(keys,counts)):
    if int(num)>k-1:ans+=count
print(ans)

Submission status: Accepted

Solution code:
import sys
# A,B,C = [int(n) for n in input().split()]
N,K = [int(n) for n in input().split()]
# S = str(input())
# T = str(input())
bucket = [0]*(N+1)
A = [int(n) for n in input().split()]
dic = {}
for a in A:
    if not(a in dic):
        dic[a]=1
    else:
        dic[a]+=1
val = sorted(dic.values())
# print(val)
rc = len(val) - K
ans = 0
for r in range(rc):
    ans += val[r]
print(ans if ans >=0 else 0)

Submission status: Accepted

Solution code:
from collections import Counter

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

x = Counter(A).most_common()[::-1]
count = [x[i][1] for i in range(len(x)-K)]
print(int(sum(count)))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
D=dict()
for i in input().split():
    if i in D:
        D[i]+=1
    else:
        D[i]=1
L=[]
for i in D:
    L.append(D[i])
L.sort()
print(sum(L[:-K]))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
D=dict()
for i in input().split():
    if i in D:
        D[i]+=1
    else:
        D[i]=1
L=[]
for i in D:
    L.append(D[i])
L.sort()
print(sum(L[:-K]))

Submission status: Accepted

Solution code:
import collections

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

num = collections.Counter(A).most_common()[::-1]
ans = 0

if len(num) > K:
    for i in range(len(num) - K):
        ans += num[i][1]
print(ans)

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


N, K = il()
A = il()
d = {}
c = 0
for a in A:
    try:
        d[a] += 1
    except KeyError:
        d[a] = 1
k = K - len(d)
if k >= 0:
    print(0)
    exit()
sd = sorted(d.items(), reverse=True, key=lambda x: x[1])
s = 0
for _, v in sd[k:]:
    s += v
print(s)

Submission status: Accepted

Solution code:
N, K = map(int, input().strip().split())
mdict = {}
for a in input().strip().split():
  a = int(a)
  if a in mdict:
    mdict[a] += 1
  else:
    mdict[a] = 1

total = 0
for i, (k, v) in enumerate(sorted(mdict.items(), key=lambda x: -x[1])):
  if i >= K:
    total += v

print(total)

Submission status: Accepted

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

import collections

arr2 = collections.Counter(arr)

sm = list(arr2.items())

sm.sort(key=lambda x : x[1],reverse=True)

ans = 0

if len(sm) > k:
    for j in sm[k:]:
        ans += j[1]

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split(" "))
A = input().split(" ")
A = [int(i) for i in A]
A = sorted(A)
D = [0] * (N+1)
for i in range(len(A)):
    D[A[i]] += 1

flag = 0
for i in range(len(D)):
    if D[i] > 0:
        flag += 1
if flag <= K:
    print(0)
else:
    DIF = flag - K
    D = sorted(D)
    E = []
    for i in range(len(D)):
        if D[i] > 0:
            E.append(D[i])
    ans = 0
    for i in range(DIF):
        ans += E[i]
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
arr = [0 for _ in range(n+1)]

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

tmp = sorted(arr)[n-k+1:n+1]
print(n-sum(tmp))

Submission status: Accepted

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

def main():
    sa = input().split(' ')
    N = int(sa[0])
    K = int(sa[1])
    sa = input().split(' ')
    A = [int(s) for s in sa]

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

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

    r = 0
    if K < len(items):
        n = 0
        for k in range(K):
            n += items[k][1]
        r = N - n

    print(r)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    a_count = Counter(a)
    if len(a_count) <= k:
        print(0)
    else:
        val = list(a_count.values())
        val.sort()
        print(sum(val[:-k]))


abc081_c()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = [int(i) for i in input().split()]
d = {}
for n in A:
    if n in d.keys():
        d[n] += 1
    else:
        d[n] = 1
if len(d.keys()) <= K:
    print(0)
else:
    val = []
    for k, v in sorted(d.items(), key=lambda x: -x[1]):
        val.append(v)
    ans = 0
    for i in val[K:]:
        ans += i
    print(ans)

Submission status: Accepted

Solution code:
from sys import stderr, setrecursionlimit
from functools import reduce
from operator import add
from collections import Counter
setrecursionlimit(2147483647)
def f(n=1):
    if n == 1:
        return [int(j) for j in input().split()]
    res = []
    for i in range(n):
        res.append([int(j) for j in input().split()])
    return res
def debug(*x, sep=" ", end="\n"):
    for item in x:
        stderr.write(str(item))
        stderr.write(sep)
    stderr.write(end)

n, k = f()
a = Counter(f())

print(n - sum(map(lambda x:x[1], a.most_common(k))))

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    n, k = map(int, input().split())
    aa = list(map(int, input().split()))
    counts = Counter(aa)
    if len(counts) <= k:
        print(0)
        return
    res = sum(sorted(counts.values())[:len(counts) - k])
    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()))
q = Counter(a).most_common()
count = 0
i = 0
if len(q) < k:
    print(0)
    exit()
while i < k:
    count += q[i][1]
    i += 1
print(n-count)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a_list = list(map(int, input().split()))
num_file = {}
for i in a_list:
    num_file[i] = 0
for i in a_list:
    num_file[i] += 1
if len(num_file) <= k:
    print(0)
else:
    new_list = sorted(num_file.items(), key = lambda x: x[1])
    ans = 0
    count = 0
    for key, value in new_list:
        ans += value
        count += 1
        if len(new_list)- count == k:
            print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
dict = {}
for Ai in A:
    if Ai in dict:
        dict[Ai] += 1
    else:
        dict[Ai] = 1
count = 0
if len(dict) > K:
    count = sum(sorted(dict.values())[:len(dict) - K])
print(count)

Submission status: Accepted

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

summary = {}
for a in A:
    if a not in summary.keys():
        summary[a] = 1
    else:
        summary[a] += 1
s_list = list(summary.values())
s_list.sort()
res = 0
i = 0
while len(s_list)-i > K:
    res += s_list[i]
    i += 1
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
count = {}
for i in a:
    if i not in count:
        count[i] = 1
    else:
        count[i] += 1
b = []
for i, j in sorted(count.items(), key=lambda x: x[1]):
    b.append(j)
print(sum(b[0:len(b)-k]))

Submission status: Accepted

Solution code:
# Not so Diverse
from collections import Counter
n, k = map(int, input().split())
a = map(int, input().split())
count = Counter(a)
c = sorted(count, key=lambda x: count[x], reverse=True)
ans = 0
while len(c) - k > 0:
    ans += count[c.pop()]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

def cnt_uniq(li):
    return Counter(li).values()

uniq = sorted(cnt_uniq(a), reverse = True)[k:]
print(sum(uniq))

Submission status: Accepted

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

for i in map(int, input().split()):
    cnt[i-1] += 1

ans = 0
for i in sorted(cnt)[:N - K]:
    ans += i

print(ans)

Submission status: Accepted

Solution code:
import collections


def calc(A, K):
    dist = {}
    tmp = collections.Counter(A)
    type_ = len(tmp)

    if type_ <= K:
        return 0

    for a, count in tmp.items():
        if count not in dist.keys():
            dist[count] = [a]
        else:
            dist[count].append(a)

    rewrite = 0
    i = 0
    for c in sorted(dist.keys()):
        for _ in dist[c]:
            i += 1
            rewrite += c
            if i == type_ - K:
                return rewrite


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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

table = {}
for _ in input().split():
    i = int(_)
    if i in table.keys():
        table[i] += 1
    else:
        table[i] = 1
nums = sorted(table.values())
print(sum(nums[0:len(nums)-K]))

Submission status: Accepted

Solution code:
import collections
N,K = [int(i) for i in input().split() ]
balls = [int(i) for i in input().split() ]

c = collections.Counter(balls)
common = c.most_common()
changed = 0

while len(common) > K :
	changed += common[-1][1]
	del common[-1]

print(changed)

Submission status: Accepted

Solution code:
# encoding: utf-8
N, K = map(int, input().split())
A = list(map(int, input().split()))

A.sort()

counts = []
view = 0
cnt = 0
for i, Ai in enumerate(A):
    if i == 0: view = Ai
    elif Ai > view:
        counts.append(cnt)
        view = Ai
        cnt = 0
    cnt += 1
else:
    counts.append(cnt)

counts.sort()

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

Submission status: Accepted

Solution code:
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a = sorted(a)
a.append(-1)
variation = []
target = a[0]
start = 0
for i in range(len(a)):
    if a[i] != target:
        variation.append(i-start)
        start = i
        target = a[i]
variation = sorted(variation)
cost = 0
for i in range(len(variation) - k):
    cost += variation[i]
print(cost)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import collections

N, K = map(int, input().split())
A = [int(i) for i in input().split()]
ans = 0
con = collections.Counter(A).most_common()
if len(con) <= K:
    print(ans)
else:
    for a, b in con[K:]:
        ans += b
    print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 11 15:18:17 2018
ABC081C
@author: maezawa
"""
import collections as col
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

kinds = col.Counter(a)
commonk = kinds.most_common(k)

ans = n - sum([elem[1] for elem in commonk])
print(ans)

Submission status: Accepted

Solution code:
import collections

n, k = map(int, input().split())
a = input().split()
a = [int(m) for m in a]
a.sort()
c = collections.Counter(a)

values, counts = zip(*c.most_common()[::-1])

count = 0
i = 0

if len(c) <= k:
    print(0)
    exit()
else:
    for j in counts:
        if i >= 0 and i <= len(counts) - 1 - k:
            count += j
        i += 1

print(count)

"""for i, name in enumerate(l, 1):
    print('{:03}_{}'.format(i, name))"""

Submission status: Accepted

Solution code:
from collections import Counter, OrderedDict

n, k = (int(_) for _ in input().split())
a = [int(_) for _ in input().split()]

ac = Counter(a)
# sort of Disctionary
# https://qiita.com/ysk24ok/items/b546471c37b2f443f4c7

ac.most_common()

tac = list(ac.values())
tac.sort()
v = len(tac)
if(k < v):
    print(sum(tac[:v-k]))
else:
    print(0)

Submission status: Accepted

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

n,k=map(int,input().split())
l=list(map(int,input().split()))
#print(l)
s=collections.Counter(l)
s=s.most_common()
#print(s)
a=0
for i in range(k,len(s)):
    a+=s[i][1]
print(a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def 解():
    iN,iK = [int(_) for _ in input().split()]
    dA = {}
    def addD(d,a):
        if a in d:
            d[a] += 1
        else:
            d[a] = 1
    for a in [int(_) for _ in input().split()]:
        addD(dA,a)
    aV = dA.values()
    print(sum(sorted(aV)[0:len(aV)-iK]))
解()

Submission status: Accepted

Solution code:
def 解():
    iN,iK = [int(_) for _ in input().split()]
    aA = [0]*(iN+1)
    for a in [int(_) for _ in input().split()]:
        aA[a] += 1
    print(sum(sorted(aA)[0:iN-iK+1]))
解()

Submission status: Accepted

Solution code:
def 解():
    iN,iK = [int(_) for _ in input().split()]
    #aA = [int(_) for _ in input().split()]
    dA = {}
    def addD(d,a):
        if a in d:
            d[a] += 1
        else:
            d[a] = 1
    for a in [int(_) for _ in input().split()]:
        addD(dA,a)
    aList = sorted(list(dA.items()),key=lambda x:x[1])
    print(sum(map(lambda x:x[1],aList[0:len(aList)-iK])))
解()

Submission status: Accepted

Solution code:
def 解():
    iN,iK = [int(_) for _ in input().split()]
    aA = [int(_) for _ in input().split()]
    dA = {}
    def addD(d,a):
        if a in d:
            d[a] += 1
        else:
            d[a] = 1
    for a in aA:
        addD(dA,a)
    aList = sorted(list(dA.items()),key=lambda x:x[1])
    print(sum(map(lambda x:x[1],aList[0:len(aList)-iK])))
解()

Submission status: Accepted

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

Submission status: Accepted

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

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

mapsa = sorted(mapa.items(), key=lambda x: x[1])
count = 0
if len(mapsa) > k:
    for i in range(len(mapsa) - k):
        count += mapsa[i][1]
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

dict = {}

for a in A:
	try:
		dict[a] += 1
	except KeyError:
		dict[a] = 1

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

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

Submission status: Accepted

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

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

over = max(0, len(D) - K)
Dkey = sorted(D.items(), key = lambda x:x[1])

change = 0
current = 0
while over > 0:
    change += Dkey[current][1]
    over -= 1
    current += 1
print(change)

Submission status: Accepted

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

count = 0
b = [0] * 200000

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

b.sort()

for i in range(len(b)):
    if b[i] == 0:
        count += 1

del b[:count]

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

Submission status: Accepted

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

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

count = 0
result = sorted(result.items(), key=lambda x: x[1])
if len(result) > K:
    for key, val in result[:len(result) - K]:
        count += val

print(count)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
num_map = {}
for a in A:
    if a in num_map:
        num_map[a] += 1
    else:
        num_map[a] = 1

keys = num_map.keys()
nums = list(num_map.values())
sum_kind = len(keys)
nums.sort()

cnt = sum_kind
ans = 0
for num in nums:
    if cnt <= K:
        break
    else:
        ans += num
        cnt -= 1
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().strip().split(" ")))
dict={}

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

X=[]
sorted_A = sorted(dict.items(), key=lambda x: x[1], reverse=True)
for i,v in sorted_A:
    X.append(v)

ans=0
if len(X)>k:
    for i in X[k:]:
        ans+=i
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
an = list(map(int, input().split()))

dict = {}
for a in an:
    if a in dict:
        dict[a] += 1
    else:
        dict[a] = 1

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

print(ans)

Submission status: Accepted

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

import collections
s_c=collections.Counter(a)


key, value=zip(*s_c.most_common())
value = list(value)
value.sort()
ans = 0
if len(key) - k >0:
	ans = sum(value[:(len(key)-k)])
print(ans)

Submission status: Accepted

Solution code:
from collections import *
n, k = map(int, raw_input().split())
c = Counter(raw_input().split())
s = sorted(c.values())[::-1]
print sum(s[k:])

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A_list = list(map(int,input().split()))
A_dict = dict()
for elem in A_list:
    if not elem in A_dict:
        A_dict[elem] = 1
    else:
        A_dict[elem] += 1
total = 0
A_dict = sorted(A_dict.items(), key=lambda x: x[1])
for i in range(len(A_dict) - K):
    total += A_dict[i][1]
print(total)

Submission status: Accepted

Solution code:
import collections

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

count = collections.Counter(A)
if len(count) <= K:
    print(0)
else:
    num_cnt = 0
    for _, c in count.most_common()[K:]:
        num_cnt += c

    print(num_cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

count = Counter(A)
same = sum([v[1] for v in count.most_common(K)])

print(N - same)

Submission status: Accepted

Solution code:
def getN():
    return int(input())


def getNM():
    return map(int, input().split())


def getList():
    return list(map(int, input().split()))

n, k = getNM()
nums = getList()

bucket = [0 for i in range(200001)]
for num in nums:
    bucket[num] += 1

bucket.sort()
print(sum(bucket[:-k]))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
D = {}
for i in A :
    if i in D :
        D[i] += 1
    else :
        D[i] = 1
L = list(D.values())
L = sorted(L)
ans = 0
counter = 0
for i in range(len(L)) :
    if len(L) - counter > K :
        ans += L[i]
        counter += 1
    else :
        print(ans)
        break

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import Counter

n,k = li()
a = list(li())

cnt = Counter(a)

al = cnt.most_common()

if len(al) <= k:
    print(0)
else:
    ans = 0
    for _, v in al[k:]:
        ans += v
        
    print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
D = {}
for i in A :
    if i in D :
        D[i] += 1
    else :
        D[i] = 1
D = sorted(D.items(),key=lambda x:x[1])
counter = 0
answer = 0
for j in range(len(D)) :
    if len(D) - counter > K :
        counter += 1
        answer += D[j][1]
    else :
        print(answer)
        break

Submission status: Accepted

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

c = Counter(a)
c_most = c.most_common()
count = 0
for i in range(min(len(c_most), k)):
    count += c_most[i][1]

print(n - count)

Submission status: Accepted

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

from collections import Counter
counter=Counter(A)

counter=counter.most_common()

ANS=0
for i in range(K,len(counter)):
    ANS+=counter[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()))
ctr = Counter(A)
stay = 0
for k,v in ctr.most_common(K):
    stay += v
print(N - stay)

Submission status: Accepted

Solution code:
s=0
R=[]
N,K=[int(i) for i in input().split()]
A=[int(i) for i in input().split()]
import collections
c=collections.Counter(A).most_common()
# for i in c.values():
#     R.append(i)
if len(c)>K:
    for i in range(K):
        s+=c[i][1]
    print(N-s)
else:
    print("0")

Submission status: Accepted

Solution code:
import collections
n,k=map(int,input().split())
a=[int(i) for i in input().split()]

c=collections.Counter(a)
l=[]

for i,j in c.most_common():
    l.append(j)

l.sort()
l.reverse()
ans=n


for i in range(k):
    ans-=l[i]
    if ans==0:
        break
print(ans)

Submission status: Accepted

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

seisuu.sort()
seisuu.reverse()
for i in range(K, N):
    ans += seisuu[i]
print(ans)

Submission status: Accepted

Solution code:
import collections

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


a = collections.Counter(a)

a = a.most_common()
if k<len(a):
	x = 0
	for i in range(k,len(a)):
		x += a[i][1]

	print(x)
else:
	print(0)

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

	print(x)
else:
	print(0)

Submission status: Accepted

Solution code:
#ABC081C
N, K=map(int, input().split())
A=list(map(int,input().split()))
#N=1の時の例外処理
if N==1:
    print(0)
    exit()
#昇順に並べ替える
A_sorted=sorted(A)
#Aの要素の個数を格納するリストの準備
BallCount=[N+1]*N
BallCount_order=0   #ボールの種類と一致する
BallCount_count=1   #注目しているボールの個数
#Aの各要素の個数を数える
for i in range(N-1):
    if A_sorted[i]==A_sorted[i+1]:
        BallCount_count+=1
    else:
        BallCount[BallCount_order]=BallCount_count
        BallCount_order+=1
        BallCount_count=1
    # 末尾処理
    if i == N - 2:
        BallCount[BallCount_order] = BallCount_count
#昇順に並び替え
BallCount_sorted=sorted(BallCount)
#減らすべきボールの種類数
remains=BallCount_order+1-K
#書き換え作業
act_count=0     #作業回数
i=0     #ボール種の序列
while remains>0:
    act_count+=BallCount_sorted[i]
    remains-=1
    i+=1
print(act_count)

Submission status: Accepted

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

d = dict()
for x in a:
    d[x] = d.get(x, 0) + 1
d2L = [(k, v) for k, v in d.items()]
d2L = sorted(d2L, key = lambda x: x[1])

ans = 0
i = 0
while len(d2L) - k - i > 0:
    ans += d2L[i][1]
    i += 1
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
listA = list(map(int, input().split()))
listA.sort()
countL =[]

index=0
beforeN=0
count=0
while index < len(listA):
    if index ==0:
        beforeN = listA[index]
        count+=1
    else:
        if listA[index] == beforeN:
            count+=1
        else:
            countL.append(count)
            beforeN = listA[index]
            count=1
    index+=1
countL.append(count)

if len(countL)== k:
    print(0)
else:     
    countL.sort()
    index2 =0
    sum =0
    while index2 < len(countL)-k:
        sum+=countL[index2]
        index2+=1
    print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
N,K=map(int,input().split())
A=list(map(int,input().split()))
x=Counter(A)
l=x.most_common(K)
s=0
for i in range(len(l)):
  s+=l[i][1]
  
print(N-s)

Submission status: Accepted

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

if K >= len(set(ball)):
  print("0")
  exit()

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

d_val = sorted(d.values(),reverse=True)
print(N - sum(d_val[:K]))

Submission status: Accepted

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

for x in input().split():
    dic[x] = dic.get(x,0) + 1

sort_d = sorted(dic.values())

ans = 0
el = len(dic) -K
for i,x in enumerate(sort_d):
    if i >= el:
        break
    else:
        ans += x
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = list(input().split()) # str
 
cnt = {} # dict
for a_i in a:
    if a_i not in cnt.keys(): 
        cnt[a_i] = 1
    else:
        cnt[a_i] += 1
        
cnts = sorted(cnt.values())
print(sum(cnts[:len(cnts) - k]))

Submission status: Accepted

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

num_balls = [0]*(n+1)
for i in range(n):
    num_balls[a[i]] += 1
num_balls = sorted(num_balls, reverse = True)
# print(num_balls)

print(sum(num_balls[k:]))

Submission status: Accepted

Solution code:
from collections import Counter

n, k = [int(v) for v in input().split()]
a = [int(v) for v in input().split()]

cnt_a = Counter(a).most_common()

ans = 0
while len(cnt_a) > k:
    last = cnt_a.pop()
    ans += last[1]

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
C = defaultdict(int)
for a in A:
  C[a] += 1
print(sum(sorted(C.values())[:-K]))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
from collections import Counter
c = Counter(A).most_common()[::-1]
rewrite = len(c) - K
count = 0
for k, v in c[:rewrite]:
    count += v
print(count)