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: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=map(int,input().split())
B={}
ans=0
for i in A:
    if i not in B:
        B[i]=1
    else:
        B[i]+=1
while K<len(B):
    ans+=min(B.values())
    del(B[min(B, key=B.get)])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K=[int(i) for i in input().split()]
A=[int(i) for i in input().split()]
s=[0 for i in range(200000)]
for i in range(200000):
    t=A.count(i)
    s[i]+=t
s.sort()
s.reverse()
p=0
for i in range(K):
    p+=s[i]
print(N-p)

Submission status: Time Limit Exceeded

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):
    for j in range(N+1):
        if A[i] == j:
            seisuu[j] += 1

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
data_list = [int(data) for data in input().split()]
data_set = set(data_list)
if len(data_set) <= k:
    print(0)
    exit()
data_dict = {}
count = 0
for data in data_set:
    data_dict[data] = data_list.count(data)
sorted_list = sorted(data_dict.items(), key=lambda x: x[1])
for i in range(len(sorted_list) - k):
    count += sorted_list[i][1]
print(count)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
data_list = [int(data) for data in input().split()]
data_set = set(data_list)
data_dict = {}
count = 0
for data in data_set:
    data_dict[data] = data_list.count(data)
sorted_list = sorted(data_dict.items(), key=lambda x: x[1])
for i in range(len(sorted_list) - k):
    count += sorted_list[i][1]
print(count)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
data_list = [int(data) for data in input().split()]
data_set = set(data_list)
data_dict = {}
count = 0
for data in data_set:
    data_dict[data] = data_list.count(data)
sorted_list = sorted(data_dict.items(), key=lambda x: x[1])
while len(sorted_list) > k:
    count += sorted_list[0][1]
    del sorted_list[0]
print(count)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
L=[]
B=set(A)
for i in B:
  x=A.count(i)
  L.append(x)
L.sort(reverse=True)
S=sum(L[0:K:1])
print(N-S)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int,input().split())
ball = input().split()
ball_list = sorted(list(set(ball)))
nums = len(ball_list)

if K >= nums:
  print("0")
  exit()

balls = []
for i in range(nums):
  balls.append(ball.count(ball_list[i]))

balls = sorted(balls, reverse=True)
print(N - sum(balls[:K]))

Submission status: Time Limit Exceeded

Solution code:
N , K = map(int, input().split())
A = list(map(int, input().split()))
syurui = set(A)
n_syurui =len(syurui)
ans = 0
if n_syurui > K:
    ans = sum(sorted([A.count(i) for i in syurui])[:n_syurui-K])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N , K = map(int, input().split())
A = list(map(int, input().split()))
syurui = set(A)
n_syurui =len(syurui)
ans = 0
goukei = sorted([A.count(i) for i in syurui])

if n_syurui > K:
    ans = sum(goukei[:n_syurui-K])
    print(ans)
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N , K = map(int, input().split())
A = list(map(int, input().split()))
syurui = set(A)
goukei = []
ans = 0
for i in syurui:
    goukei.append(A.count(i))

goukei.sort()

if len(syurui) <= K:
    print(ans)
else:
    ans = sum(goukei[0:len(syurui)-K])
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N , K = map(int, input().split())
A = sorted(map(int, input().split()))
syurui = set(A)
goukei = []
ans = 0
for i in syurui:
    goukei.append(A.count(i))

goukei.sort()

if len(syurui) <= K:
    print(ans)
else:
    for i in range(len(syurui)-K):
        ans += goukei[i]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=set(a)
sl=len(s)
b=[]
result=0
if sl<=k:
    print(0)
    exit()
else:
    for i in s:
        b.append(a.count(i))
b.sort()
for i in range(sl-k):
    result+=b[i]
print(result)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=set(a)
b=[]
result=0
if len(s)<=k:
    print(0)
    exit()
else:
    for i in s:
        b.append(a.count(i))
b.sort()
for i in range(len(s)-k):
    result+=b[i]
print(result)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
import collections
c = collections.Counter(A)
ans = 0
while len(c) > K:
    n = c.most_common()[-1][0]
    ans += c[n]
    del c[n]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

cnt_list = []

for i in range(N):
    if A[i] not in cnt_list:
        cnt_list.append(A[i])

c = collections.Counter(A)
values, counts = zip(*c.most_common())
counts = list(counts)
counts.reverse()

answer=0

if len(cnt_list) > K:
    for i in range(len(cnt_list)-K):
        answer += counts[i]
        
        
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import collections
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
dic = collections.Counter(a)
#for i in a:
#    if i in dic:
#        dic[i] += 1
#    else:
#        dic[i] = 1
dic = sorted(dic.items(), key=lambda x: x[1])
while len(dic) > k:
    ans += dic[0][1]    
    del dic[0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
dic = {}
for i in a:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1
dic = sorted(dic.items(), key=lambda x: x[1])
while len(dic) > k:
    ans += dic[0][1]    
    del dic[0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
ans = 0
dic = {}
for i in a:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1
dic = sorted(dic.items(), key=lambda x: x[1])
while len(dic) > k:
    ans += dic[0][1]    
    del dic[0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# vim: set fileencoding=utf-8 :
n, k = map(int, raw_input().split())
a = map(int, raw_input().split())
freq = {}
b = [0] * n
for ele in a:
    if {ele} <= set(freq):
        freq[ele] += 1
    else:
        freq[ele] = 1

i = 0
ct = 0
freq_cp = freq
for ke, v in sorted(freq.items(), key=lambda x: x[1]):
    if k < len(set(freq_cp)):
        ct += v
        del freq_cp[ke]
    else:
        break

print(ct)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
s = set(a)
r = []
for i in s:
	r.append(a.count(i))
r = sorted(r)
dif = len(s) - k
print(sum(r[0:max(0,dif)]))

Submission status: Time Limit Exceeded

Solution code:
#-*-coding:utf-8-*-
from collections import Counter

def main():
    n, k = map(int, input().split())
    a_list = list(input().split())
    a_set = set(a_list)

    c = Counter(a_list)

    cnt = 0
    for i in range(len(a_set) - k):
        cnt += c.most_common()[::-1][i][1]

    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

b = [0]*200000
c = []
ans = 0

for x in a:
    b[x-1] += 1

for x in b:
    if x > 0:
        c.append(x)

c.sort()

while len(c) > k:
    ans += c[0]
    c.pop(0)

print(ans)

Submission status: Time Limit Exceeded

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

b = [0]*200000
c = []
ans = 0

for x in a:
    b[x-1] += 1

for x in b:
    if x > 0:
        c.append(x)

while len(c) > k:
    ans += min(c)
    c.remove(min(c))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N,K = map(int,input().split())
a = list(map(int,input().split()))
matome = set(a)
counter =0
ctr = Counter(a)
while len(ctr)>K:
    min_ctr = min(ctr, key=ctr.get)
    counter +=min(ctr.values())
    del ctr[min_ctr]
print(counter)

Submission status: Time Limit Exceeded

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

a = sorted(a)
p = list(set(a))

if len(p) <= k:
    print('0')
    exit()

j = 0
ans = [0 for _ in range(len(p))]
for i in range(len(p)):
    ans[i] += a.count(p[i])

ans = sorted(ans)

print(sum(ans[:len(p) - k]))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
t=[]
s=list(map(int,input().split()))
def f(s):
    for i in list(set(s)):
        t.append(s.count(i))
    return sum(sorted(t)[:len(t)-m])   
print(f(s))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
t=[]
s=list(map(int,input().split()))
for i in list(set(s)):
    t.append(s.count(i))
print(sum(sorted(t)[:len(t)-m]))

Submission status: Time Limit Exceeded

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

A_set = list(set(A))
len_A_set = len(A_set)

# i = 0
# ct = 0
# while True:
#     if ct >= len_A_set - K:
#         break
#     min = 10000000
#     for i in A_set:
#         if A.count(i) < min:
#             min = A.count(i)
#     ct += min
# 
# print(ct)



A_count =[]
for i in range(len_A_set):
    A_count.append(A.count(A_set[i]))

ans=0
for i in range(len_A_set-K):
    ans += min(A_count)
    A_count.pop(A_count.index(min(A_count)))

print(ans)

Submission status: Time Limit Exceeded

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

A_set = list(set(A))
len_A_set = len(A_set)

A_count =[]
for i in range(len_A_set):
    A_count.append(A.count(A_set[i]))

ans=0
for i in range(len_A_set-K):
    ans += min(A_count)
    A_count.pop(A_count.index(min(A_count)))

print(ans)

Submission status: Time Limit Exceeded

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

A_set = list(set(A))

A_count =[]
for i in range(len(A_set)):
    A_count.append(A.count(A_set[i]))

A_count.sort()

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
set_a = set(a)

count_list = []
if len(set_a) <= k:
    print(0)
else:
    for num in set_a:
        count_list.append(a.count(num))
    count_list = sorted(count_list, reverse = True)

    print(sum(count_list[k:]))

Submission status: Time Limit Exceeded

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

from collections import Counter 

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

# 各数値ごとに数を集計
num_dict = Counter(num_list)

ans = 0
while True:
    # 種類数がK以下なら終了
    if len(num_dict) <= K:
        print(ans)
        exit()

    # 一番数が少ない要素の個数分、答えに追加
    ans += num_dict.most_common()[-1][1]
    # 一番数が少ない要素を削除
    del num_dict[num_dict.most_common()[-1][0]]

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
a = [int(i) for i in input().split()]
cnt = []
n = 1
while n <= N:
  c = a.count(n)
  if c > 0:
    cnt.append(c)
  n += 1
cnt.sort()
l = len(cnt)
print(sum(cnt[0:(l-K)]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N, K = map(int, input().split())
cnt = Counter(list(map(int, input().split())))
res = 0
l = len(cnt)
for i in range(l, K, -1):
    res += cnt.most_common(i)[-1][1]
print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
L = list(set(A))
res = 0
num = []

if len(L) <= K:
    res = 0

else:
    for i in L:
        num.append(A.count(i))
    
    num.sort()

    for i in range(len(L) - K):
        res += num[i]

print(res)

Submission status: Time Limit Exceeded

Solution code:

N,K = list(map(int,input().split()))
balls = []
bucket = [0]*(N+1)

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

for ball_number in balls:
    bucket[ball_number] += 1

def judge(number_of_types, types):
    if number_of_types <= types:
        return True

number_of_types = 0
for i in bucket:
    if i >= 1:
        number_of_types += 1

if number_of_types <= K:
    print("0")
    quit()
else:
    count = 0
    for i in range(1,N+1):
        for j in bucket:
            if j == i:
                count += i
                number_of_types -= 1
                if judge(number_of_types, K):
                    print(count)
                    quit()

Submission status: Time Limit Exceeded

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

counter = 0
d = {}
types = list(set(A))

for i in range(len(types)):
    d[str(types[i])] = A.count(types[i])


for i in range(len(d)-K):
    minKey = min(d, key=d.get)
    counter += d[minKey]
    del d[minKey]

'''
while len(d) > K:
    minKey = min(d, key=d.get)
    counter += d[minKey]
    del d[minKey]
'''
print(counter)

Submission status: Time Limit Exceeded

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

counter = 0
d = {}
types = list(set(A))

for i in range(len(types)):
    d[str(types[i])] = A.count(types[i])

while len(d) > K:
    minKey = min(d, key=d.get)
    counter += d[minKey]
    del d[minKey]
print(counter)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N, K = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
A.sort()
Balls = {k: len(list(g)) for k, g in itertools.groupby(A)}
cnt = 0
while len(Balls.keys()) > K:
    change = -1
    for k, v in Balls.items():
        if v == min(Balls.values()):
            cnt += v
            change = k
            break
    del Balls[change]
print(cnt)

Submission status: Time Limit Exceeded

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

sort_count = sorted(a.items(), key=lambda x: x[1])

total = 0
while len(sort_count) > k:
    _, n = sort_count.pop(0)
    total += n

print(total)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N,K = map(int,input().split())
A=list(map(int,input().split()))
count = Counter(A)
diff = len(count)-K

ans=0

if diff<=0:
    print(0)
else:
    for i in range(diff):
        ans+=count.most_common()[len(count)-i-1][1]
    print(ans)

Submission status: Time Limit Exceeded

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

sort_count = sorted(a.items(), key=lambda x: x[1])

total = 0
while len(sort_count) > k:
    _, n = sort_count.pop(0)
    total += n

print(total)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans=0
num=[]
if len(set(a))-k <0:
    print(0)
else:
    for x in set(a):
        num.append(a.count(x))
    num.sort()
    for i in range(len(set(a))-k):
        ans+=num[i]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int,input().split())
al = list(map(int,input().split()))
dics = {}
 
for i in range(n):
    if al[i] in dics:
        dics[al[i]] = dics[al[i]] + 1
    else:
        dics.update({al[i]:1})
        
counts = len(dics.values())
kesu = 0
sortv = list(sorted(list((dics.values()))))
 
while len(sortv) > k:
    kesu = kesu + sortv[0]
    del sortv[0]
print(kesu)

Submission status: Time Limit Exceeded

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

for i in range(n):
    if al[i] in dics:
        dics[al[i]] = dics[al[i]] + 1
    else:
        dics.update({al[i]:1})
        
counts = len(dics.values())
kesu = 0
sortv = list(sorted(list((dics.values()))))

while len(sortv) > k:
    kesu = kesu + sortv[0]
    sortv.remove(sortv[0])
print(kesu)

Submission status: Time Limit Exceeded

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

for i in range(n):
    if al[i] in dics:
        dics[al[i]] = dics[al[i]] + 1
    else:
        dics.update({al[i]:1})
        
counts = len(dics.values())
kesu = 0
sortv = list(dics.values())
while len(sortv) > k:
    kesu = kesu + min(sortv)
    sortv.remove(min(sortv))
print(kesu)

Submission status: Time Limit Exceeded

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

for i in range(n):
    if al[i] in dics:
        dics[al[i]] = dics[al[i]] + 1
    else:
        dics.update({al[i]:1})
        
counts = len(dics.values())
kesu = 0
sortv = list(sorted(dics.values()))
while len(sortv) > k:
    kesu = kesu + sortv[0]
    sortv.remove(sortv[0])
print(kesu)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n, k = map(int, input().split())
a = list(input().split())
counter = Counter(a)
count = 0
while len(counter) > k:
    max_c = min(counter.values())
    for name, cnt in counter.most_common():
        if cnt == max_c:
            count += cnt
            counter.pop(name)
            break
print(count)

Submission status: Time Limit Exceeded

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

    AA = []
    num = []
    for A in A:
        if (A in AA)==False:
            AA.append(A)
            num.append(1)
        else:
            num[AA.index(A)] += 1

    count = 0
    num.sort()
    num = num[::-1]
    if len(num)<=K:
        return 0
    else:
        for i in range(K,len(num)):
            count += num[i]
        return count


if __name__ == '__main__':
    print(main())

Submission status: Time Limit Exceeded

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

    AA = []
    num = []
    for A in A:
        if (A in AA)==False:
            AA.append(A)
            num.append(1)
        else:
            num[AA.index(A)] += 1

    B = list(zip(num, AA))
    B.sort()
    B = B[::-1]
    count = 0
    if len(B)<=K:
        return 0
    else:
        for i in range(K,len(B)):
            count += B[i][0]
        return count


if __name__ == '__main__':
    print(main())

Submission status: Time Limit Exceeded

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

    AA = []
    num = []
    for A in A:
        if (A in AA)==False:
            AA.append(A)
            num.append(1)
        else:
            num[AA.index(A)] += 1

    B = list(zip(num, AA))
    B.sort()
    B = B[::-1]
    count = 0
    while len(B) > K:
        count += B.pop()[0]

    return count

if __name__ == '__main__':
    print(main())

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
s=list(map(int,input().split()))
t=list(set(s))
ss,ans=[],0
for i in t:
    ss.append(s.count(i))
    ss=sorted(ss)
if len(ss)>K:
    for i in range(len(ss)-K):
        ans+=ss[i]
    print(ans)      
else:
    print("0")

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
knd = len(set(a))##種類数
d = dict(list((_, a.count(_))) for _ in set(a))
print(sum(sorted(d.values())[:knd - k]))

Submission status: Time Limit Exceeded

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

d = dict(list((_, a.count(_))) for _ in set(a))
a_cnt = sorted(d.values())

cnt = 0##書き換えた数
for i in range(len(set(a)) - k):##書き換え必要な種類の数
    cnt += a_cnt[i]
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
a = {}
s = 0
for i in input().split():
    a[int(i)] = a.get(int(i),0)+1
b = list(a.values())
while(k<len(b)):
    c = min(b)
    s+=c
    b.remove(c)
print(s)

Submission status: Time Limit Exceeded

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

numlist = {}
for i in A:
    if i in numlist:
        numlist[i] += 1
    else:
        numlist[i] = 1
numbers = numlist.values()
numbers = sorted(numbers)

change = 0
while len(numbers) > K:
    change += numbers[0]
    del numbers[0]
print(change)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(10000)

# Debug output
def chkprint(*args):
    names = {id(v):k for k,v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

# Binary converter
def to_bin(x):
    return bin(x)[2:]

# Set 2 dimension list
def dim2input(N):
    li = []
    for _ in range(N):
        li.append(list(map(int, input())))
    return li

""" input template
S = input()
N = int(input())
L = list(map(int, input().split()))
a, b = list(map(int, input().split()))
SL = list(input())
"""

# --------------------------------------------

dp = None

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

    D = collections.defaultdict(lambda: 0)

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

    selected = 0
    while selected != K and len(D) > 0:
        k, v = max(D.items(), key=lambda x:x[1])
        del D[k]

        selected += 1

    op = 0
    for v in D.values():
        op += v


    print(op)


main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(10000)

# Debug output
def chkprint(*args):
    names = {id(v):k for k,v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

# Binary converter
def to_bin(x):
    return bin(x)[2:]

# Set 2 dimension list
def dim2input(N):
    li = []
    for _ in range(N):
        li.append(list(map(int, input())))
    return li

""" input template
S = input()
N = int(input())
L = list(map(int, input().split()))
a, b = list(map(int, input().split()))
SL = list(input())
"""

# --------------------------------------------

dp = None

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

    D = collections.defaultdict(lambda: 0)

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

    op = 0
    while len(D) > K:
        min_key, min_value = min(D.items(), key=lambda x:x[1])

        op += min_value
        del D[min_key]

    print(op)


main()

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
kind=[]
count=1
for i in range(len(a)-1):
  if(a[i]==a[i+1]):count+=1
  else:
    kind.append(count)
    count=1
kind.append(count)
kind.sort()
change=0
while(len(kind)>k):
  change+=kind.pop(0)
print(change)

Submission status: Time Limit Exceeded

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

import sys
import collections

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
count_rmv = 0
count_int = sorted(collections.Counter(A).values())
num_int_type = len(count_int)

while num_int_type > K:
    count_rmv += count_int[0]
    del count_int[0]
    num_int_type -= 1

print(count_rmv)

Submission status: Time Limit Exceeded

Solution code:

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

    count = 0
    for i in range(len(dic.keys())-K):
        count += (sorted(dic.items(), key= lambda x:x[1])[i][1])

    print (count)


if __name__ == "__main__":
    # global stime
    # stime = time.clock()
    main()

Submission status: Time Limit Exceeded

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

n = []
for i in range(1, N+1):
    if i in l:
        n.append(l.count(i))

ans = 0
for i in range(len(n) - K):
    ans += min(n)
    n.remove(min(n))

print(ans)

Submission status: Time Limit Exceeded

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

n = []
for i in range(1, N+1):
    if i in l:
        n.append(l.count(i))

ans = 0
while len(n) > K:
    ans += min(n)
    n.remove(min(n))

print(ans)

Submission status: Time Limit Exceeded

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

n = []
for i in range(1, N+1):
    if i in l:
        n.append(l.count(i))

ans = 0
while len(n) > K:
    n[n.index(min(n))] += -1
    ans += 1
    if 0 in n:
        n.remove(0)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
balls, target = [int(i) for i in input().split()]
num_dict = {}
count = 0

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

while target < len(num_dict):
    # for key, value in sorted(num_dict.items(), key = lambda x: x[1]):
    #     count += value
    #     del(num_dict[key])
    #     break
    min_key = min(num_dict, key=(lambda x: num_dict[x]))
    count += num_dict[min_key]
    del(num_dict[min_key])

print(count)

Submission status: Time Limit Exceeded

Solution code:
balls, target = [int(i) for i in input().split()]
num_dict = {}
count = 0

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

while target < len(num_dict):
    for key, value in sorted(num_dict.items(), key = lambda x: x[1]):
        count += value
        del(num_dict[key])
        break

print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import pdb

def main():
    [N,K] = map(int, raw_input().split(' '))
    As  = np.array(raw_input().split(' ')).astype(int)
    answer = process(As, K)
    print answer

def process(As, K):
    setsize = len(set(As))
    setAs = list(set(As))
    if setsize <= K:
        return 0
    else:
        popn = population(setAs, As)
        heathenrace = range(setsize -K)
        num_heathens = np.sum(popn[heathenrace])
        return int(num_heathens)

def population(species, As):
    popn = np.zeros(len(species))
    for k in range(len(species)):
        popn[k] = len(np.where(As == species[k])[0])
    return np.sort(popn[::-1])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
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:
        ns.append(t)
ns.sort()
ans = 0
while len(ns) > K:
    ans += ns[0]
    ns = ns[1:]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
_, K = map(int, input().split())
nl = list(map(int, input().split()))

from collections import Counter

cnt = Counter(nl)
c = list(cnt.values())
res = 0

for _ in range(len(c)-K):
    res += min(c)
    c.remove(min(c))
print(res)

Submission status: Time Limit Exceeded

Solution code:
_, K = map(int, input().split())
nl = list(map(int, input().split()))

from collections import Counter

cnt = Counter(nl)
c = list(cnt.values())
res = 0

while len(c) > K:
    res += min(c)
    c.remove(min(c))
print(res)

Submission status: Time Limit Exceeded

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

rewrite = 0

while len(soretedList) > K:
    head = soretedList.pop(0)
    rewrite += head
    
print(rewrite)

Submission status: Time Limit Exceeded

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

rewrite = 0

while len(soretedList) > K:
    head = soretedList.pop(0)
    rewrite += head[1]

print(rewrite)

Submission status: Time Limit Exceeded

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

rewrite = 0

while len(soretedList) > K:
    head = soretedList.pop(0)
    tail = soretedList.pop()
    
    newTail = (tail[0], head[1]+tail[1])
    soretedList.append(newTail)
    rewrite += head[1]
    
print(rewrite)

Submission status: Time Limit Exceeded

Solution code:
N, K = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
dict = {}
count = 0

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

for i in range(len(dict)):
    if i == K:
        break
    maxKey = max([(v, k) for k, v in dict.items()])[1]
    maxValue = dict.pop(maxKey)
    count += maxValue

print(N - count)