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 = (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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
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:] ))

Submission status: Accepted

Solution code:
#abc081c
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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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]
    ]))

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
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)

Submission status: Accepted

Solution code:
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:]))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
[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]))

Submission status: Accepted

Solution code:
_, 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

Submission status: Accepted

Solution code:
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]))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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:]))

Submission status: Accepted

Solution code:
# coding: utf-8

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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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:]))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

# 4近傍（右, 下, 左, 上）
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
def c_not_so_diverse(N, K, A):
    from collections import Counter
    count = Counter(A)  # 各数字が何回出たかを格納

    if len(count) <= K:
        # 数字の種類がK以下の場合、数字を書き換える必要はない
        return 0
    else:
        count = sorted(count.items(), key=lambda x: x[1], reverse=True)
        count = count[K:]  # 出現頻度が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))

Submission status: Accepted

Solution code:
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]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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]))

Submission status: Accepted

Solution code:
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]))

Submission status: Accepted

Solution code:
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]))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
from collections import Counter
N, K = list(map(int,input().split()))
listA = Counter(input().split())
# print(listA)
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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) ) )
#    print(n_num)
#    nums = [0] * n_num
    nums = [0]* max(A)
#    print(nums)
    for a in A:
        nums[a - 1] += 1
    nums.sort()
#    print(nums)
    
    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()

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
import collections

N,K=input().split(" ")
N = int(N)
K = int(K)
A = input().split(" ")
#temp_list = list(set(A))

test = collections.Counter(A)
test_key = list(test.keys())
#print(type(test_key))
temp = []
for i in test_key:
    temp.append(int(test[str(i)]))
tyouka = K - len(temp)
temp = sorted(temp)
#print(temp)
ans = 0 
if tyouka >=0:
    print(0)
else:
    tyouka = -tyouka
    for i in range(tyouka):
        ans += int(temp[i])
    print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# coding: utf-8


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

Submission status: Accepted

Solution code:
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]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""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())

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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


# d = defaultdict(lambda: 0)
# setrecursionlimit(10**7)
# inf = float("inf")


##### stdin ####
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)

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



##### main #####		
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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:]))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# coding:utf-8                                                                  

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#http://abc081.contest.atcoder.jp/
# -*- coding: utf-8 -*-

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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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:]))

Submission status: Accepted

Solution code:
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)]))

Submission status: Accepted

Solution code:
n,k=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
count=1
a.sort()
#print(a)
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)

#print(num)

num.sort()

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

print(count_kakikae)

Submission status: Accepted

Solution code:
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]))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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