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())
in_list = list(map(int,input().split()))

# カウント数のMinMaxの値を取得
def countMinMaxVal(cnt_list):
  min_cnt = 200000
  max_cnt = 0
  min_cnt_val = -1
  max_cnt_val = -1
  for i in set(cnt_list):
    i_cnt = cnt_list.count(i)
    if min_cnt > i_cnt:
      min_cnt,min_cnt_val = i_cnt, cnt_list[cnt_list.index(i)]
    if max_cnt < i_cnt:
      max_cnt,max_cnt_val = i_cnt, cnt_list[cnt_list.index(i)]
    # print('------------------------------------------------')
    # print('i_cnt:{0},i:{1}'.format(i_cnt,i))
    # print('min_cnt:{0},min_cnt_val:{1}'.format(min_cnt,min_cnt_val))
    # print('max_cnt:{0},max_cnt_val:{1}'.format(max_cnt,max_cnt_val))
    # print('------------------------------------------------')

  return min_cnt_val,max_cnt_val

update_cnt = 0
if len(set(in_list)) <= K:
  print(update_cnt)
else:
  while(len(set(in_list)) > K):
    a,b = countMinMaxVal(in_list)
    for idx in [i for i, x in enumerate(in_list) if x == a]:
      in_list[idx] = b
      update_cnt += 1
  print(update_cnt)

Submission status: Time Limit Exceeded

Solution code:
url = "https://atcoder.jp/contests/abc081/tasks/arc086_a"
import copy
def main():
    n, m = list(map(int, input().split()))
    li = list(map(int, input().split()))
    dic = {}

    for v in li:
        dic.setdefault(v, 0)
        dic[v] += 1

    if len(dic) <= m:
        print(0)
        exit()

    count = 0
    for i in range(len(dic) - m):
        key, val = min(dic.items(), key=lambda x: x[1])
        count += dic.pop(key)



    print(count)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

a_cnt = {}
for i in a:
    if i in a_cnt:
        a_cnt[i] += 1
    else:
        a_cnt[i] = 1

cnt_list = [0] * len(a_cnt)
for i, value in enumerate(a_cnt.values()):
    cnt_list[i] = value

cnt_list.sort()

ans = 0
while len(cnt_list) > K:
    ans += cnt_list[0]
    cnt_list.pop(0)
print(ans)

Submission status: Time Limit Exceeded

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

a_cnt = {}
for i in a:
    if i in a_cnt:
        a_cnt[i] += 1
    else:
        a_cnt[i] = 1

cnt_list = [0] * len(a_cnt)
for i, value in enumerate(a_cnt.values()):
    cnt_list[i] = value

cnt_list.sort()

ans = 0
while len(cnt_list) > K:
    ans += cnt_list[0]
    cnt_list.pop(0)
print(ans)

Submission status: Time Limit Exceeded

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

s = set(array)
s = sorted(s)
num = len(s)
cnt = 0

ary = []

for item in s:
  ary.append(array.count(item))

ary.sort() 

for i in range(num-k):
  cnt+=ary[i]
  
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
x, y = map(int, input().split())
l = list(map(int, input().split()))
ans = 0
d = {}
l2 = []
for i in l:
  if i not in d:
    d[i] = 1
  else:
    d[i] += 1
#print(d)
if len(d) <= y:
  print(0)
else:
  for v in d.values():
    l2.append(v)
    l2.sort(reverse = True)
  print(sum(l2[y:]))

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
line = list(map(int,input().split()))
num = len(set(line))
nums = {}
for i in set(line):
  numnum = line.count(i)
  nums[i] = numnum
snums = sorted(nums.values())
if num <= k:
  print(0)
else:
  require = num - k
  answer = 0
  for j in range(require):
    answer += snums[j]
  print(answer)

Submission status: Time Limit Exceeded

Solution code:
import sys
from math import sqrt
from collections import Counter

input = sys.stdin.readline


def I():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    n, k = MI()
    a_list = MI()

    c = Counter(a_list)
    n_key = len(c)
    n_rewrite = 0
    while n_key > k:
        n_rewrite += c.pop(c.most_common()[-1][0])
        n_key -= 1

    print(n_rewrite)
    pass


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

num_flag = [0 for i in range(n)]

for item in a:
    num_flag[item - 1] += 1

while 0 in num_flag:
    num_flag.remove(0)

num_flag.sort()

cnt = 0
while len(num_flag) > k:
    cnt += num_flag[0]
    num_flag.pop(0)

print("{}".format(cnt))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N,K=map(int,input().split())
A = list(map(int,input().split()))
L = Counter(A)
count = len(L.keys()) - K if len(L.keys()) - K > 0 else 0
ans = 0
for c in range(count):
  mi_k = min(L, key=L.get)
  ans+=L[mi_k]
  L.pop(mi_k)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=input().split()
balls=[a for a in A]
aballs=[]
for a in balls:
    if a in aballs:
        pass
    else:
        aballs.append(a)
number=len(aballs)
n=number-K
counts=[]
for a in aballs:
    kazu=balls.count(a)
    counts.append(kazu)
newcounts=sorted(counts)
answer=0
for a in range(n):
    answer+=newcounts[a]
print(answer)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(set(a))
l=len(b)
c=[0]*l
for i in range(l):
  c[i]=a.count(b[i])
c=sorted(c)
print(sum(c[:l-k]))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
array = list(map(int, input().split()))
from collections import Counter
lis = sorted((Counter(array)).items(), key = lambda x: x[1])
count = 0
while len(lis) > k:
    count += lis[0][1]
    lis.remove(lis[0])
print(count)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
num_list = list(map(int, input().split()))
cnt = 0
cnt_list = []
ans = 0

for i in set(num_list):
    for j in num_list:
        if i == j:
            cnt += 1
    cnt_list.append(cnt)
    cnt = 0

dic = dict(zip(set(num_list), cnt_list))
dic_sorted = sorted(dic.items(), key=lambda x: x[1])
del_num = dic_sorted[:len(dic_sorted)-k]

for i in del_num:
    ans += i[1]

print(ans)

Submission status: Time Limit Exceeded

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

dic= {}
cnt = 0 
swap = 0

for i in range(1,N+1):
	dic["{}".format(i)] = 0

for i in range(0,N):
	dic["{}".format(lst[i])] += 1

sorted_dic = sorted(dic.items(), key = lambda x : x[1])
    
for i in range(0,N):
  swap += sorted_dic[i][1]
  if sorted_dic[i][1] != 0:
    cnt +=1
  if cnt >= len(set(lst))-K:
    break
      
print(swap)

Submission status: Time Limit Exceeded

Solution code:
kazu = 0
count = 0
N , K = map(int, input().split())
numbers = list(input().split())
from collections import Counter

while kazu < K:
  if not len(numbers) == 0:
    c = Counter(numbers)
    mode = c.most_common(1)
    count = 0
    numbers = [i for i in numbers if not i == mode[0][0]] 
    kazu +=1
  else:
    break
print(len(numbers))

Submission status: Time Limit Exceeded

Solution code:
kazu = 0
count = 0
N , K = map(int, input().split())
numbers = list(input().split())
from collections import Counter

while kazu < K:
  if not len(numbers) == 0:
    c = Counter(numbers)
    mode = c.most_common(1)
    count = 0
    while count < int(mode[0][1]):
      numbers.remove(mode[0][0])
      count +=1
    kazu +=1
  else:
    break
print(len(numbers))

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=set(A)
C=[]
ans=0
for i in B:
  C.append(A.count(i))
C.sort()
for i in range(len(C)-K):
  ans+=C[i]
print(ans)

Submission status: Time Limit Exceeded

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

set_A = set(list_A)
if len(set_A) <= K :
    print(0)
    exit()

dict_A = dict.fromkeys(set_A, 0)

for i in range(N) :
    dict_A[list_A[i]] += 1

ans = 0

for i in range(len(set_A)-K) :
    ans += min(dict_A.values())
    del dict_A[min(dict_A, key=dict_A.get)]

print(ans)

Submission status: Time Limit Exceeded

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

uniq_items = []
for i in set(A):
    uniq_items.append(A.count(i))

uniq_items = sorted(uniq_items,reverse=True)

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

Submission status: Time Limit Exceeded

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

uniq_items = []
for i in set(A):
    uniq_items.append(A.count(i))
    uniq_items = sorted(uniq_items,reverse=True)

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

Submission status: Time Limit Exceeded

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

uniq_items = []
for i in set(A):
    uniq_items.append(A.count(i))

if len(uniq_items) <= K:
    print("0")
else:
#     print(uniq_items)
#     print(sorted(uniq_items,reverse=True))
    print(sum(sorted(uniq_items,reverse=True)[K:]))

Submission status: Time Limit Exceeded

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

uniq_items = []
for i in set(A):
    uniq_items.append(A.count(i))

print(sum(sorted(uniq_items,reverse=True)[K:]))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
aList = list(map(int, input().split()))
num = [0 for _ in range(n + 1)]
for i in range(n + 1):
    if i in aList:
        num[i] += aList.count(i)
num.sort()
num = num[:-k]
print(sum(num))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
aList = list(map(int, input().split()))
aSet = set(aList)
aCount = [aList.count(i) for i in aSet]
aCount.sort()
aCount = aCount[:-k]
print(sum(aCount))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
aList = list(map(int, input().split()))
aSet = set(aList)
aCount = []
for i in aSet:
    aCount.append(aList.count(i))
aCount.sort()
aCount = aCount[:-k]
print(sum(aCount))

Submission status: Time Limit Exceeded

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

A_dict = {}
for i in A_set:
    A_dict[i] = A.count(i)
A_dict = sorted(A_dict.items(), key=lambda x:x[1])

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

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A_set = set(A)
A_set_len = len(A_set)
rewrite_set = A_set_len - K

A_dict = {}
for i in A_set:
    A_dict[i] = A.count(i)

A_dict_values = sorted(A_dict.values())

ans = 0
if rewrite_set > 0:
    ans = sum(A_dict_values[:rewrite_set])

print(ans)

Submission status: Time Limit Exceeded

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

#a=A
#a=list(range(10))+list(range(5))
unq=[0]*len(a)
unqCt=[0]*len(a)
ans=0
ct2=1
ct3=0
while ct2 > 0:
	ct1=0
	ct2=0
	nokori=[0]*len(a)
	for i in range(len(a)):
		if a[0]==a[i]:
			#ctlist[ct1]= i
			ct1 +=1
			#print(ct1)
		else:
			nokori[ct2]=a[i]
			ct2 +=1
			#print(ct2)
	unq[ct3]=a[0]
	unqCt[ct3]=ct1
	ct3+=1
	a=nokori[:-ct1]

kakikae=ct3-K
if kakikae>0:
	ctmin=1
	while kakikae >0:
		for n in range(len(unqCt)):	
			if unqCt[n]==ctmin:
				ans+=ctmin
				kakikae-=1
				if kakikae==0:
					break
		ctmin+=1		
else:
	ans=0	
		
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = input().split()
n, k = int(n), int(k)
balls = [int(x) for x in input().split()]
dic = {}
for x in balls:
	if x not in dic:
		dic[x] = 1
	else:
		dic[x] += 1
change = 0
qnt = list(dic.values())
qnt.sort()
while len(qnt) > k:
	change += qnt[0]
	qnt.pop(0)
print(change)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

unq=np.array(list(set(A)))
A=np.array(A)

ct=np.zeros(unq.size,dtype=int)

for i in range(unq.size):
	ct[i]=sum(unq[i]==A)

if unq.size > K:
	x=unq.size-K
	sort_ct=np.sort(ct)
	ans=sum(sort_ct[list(range(x))])
else :
	ans=0

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt_list = []
cnt_dict = {}
for i in a:
    if i in cnt_dict:
        cnt_dict[i] += 1
    else:
        cnt_dict[i] = 1
cnt_dict_sorted = sorted(cnt_dict.items(), key=lambda x: x[1])
for i in set(a):
    cnt_list.append(a.count(i))
ans = 0
if len(cnt_dict_sorted) <= k:
    ans = 0
else:
    tmp = len(cnt_dict_sorted) - k
    for j in range(tmp):
        ans += cnt_dict_sorted[j][1]
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
num = [int(i) for i in input().split()]
n=num[0]
k = num[1]
a = [int(i) for i in input().split()]
c = list(set(a))
o = [10**3]*len(c)
j = 0

count = 0
if len(c) > k:
    for i in c:
        o[j]=a.count(i)
        j += 1
    o.sort()
    dec =len(c)-k
    count=sum(o[:dec])
print(count)

Submission status: Time Limit Exceeded

Solution code:
num = [int(i) for i in input().split()]
n=num[0]
k = num[1]
a = [int(i) for i in input().split()]
o = []
c = list(set(a))

count = 0
if len(c) > k:
    for i in c:
        o.append(a.count(i))
    o.sort()
    dec =len(c)-k
    count=sum(o[:dec])
print(count)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
o = []
c = list(set(a))

count = 0
if len(c) <= k:
    1+1
else:
    for i in c:
        o.append(a.count(i))
    o.sort()
    dec =len(c)-k
    #print(o[:dec])
    count=sum(o[:dec])
print(count)

Submission status: Time Limit Exceeded

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

count = 0
if len(c) <= k:
    1+1
else:
    for i in c:
        o.append(a.count(i))
    o.sort()
    dec =len(c)-k
    #print(o[:dec])
    count=sum(o[:dec])
print(count)

Submission status: Time Limit Exceeded

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

count = 0
if len(c) <= k:
    print(0)
else:
    for i in c:
        o.append(a.count(i))
    o.sort()
    dec =len(c)-k
    #print(o[:dec])
    count=sum(o[:dec])
    print(count)

Submission status: Time Limit Exceeded

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

c = list(set(a))

count = 0
if len(c) <= k:
    print(0)
else:
    while len(c) > k:
        minv = 10**3
        for i in c:
            o = a.count(i)
            if minv > o:
                minv = o
                atai = i
        c.remove(atai)
        count += minv
 
    print(count)

Submission status: Time Limit Exceeded

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

for i in range(len(a)):
    flag = False
    for j in range(len(c)):
        if a[i]==c[j]:
            flag = True
            break
    if flag==False:
        c.append(a[i])
count = 0
if len(c) <= k:
    print(0)
else:
    while len(c) > k:
        minv = 10**3
        for i in c:
            o = a.count(i)
            if minv > o:
                minv = o
                atai = i
        c.remove(atai)
        count += minv
 
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n,k = map(int,readline().split())
lst1 = list(map(int,readline().split()))

dic1 = dict()

for i in lst1:
    if i in dic1:
        dic1[i] += 1
    else:
        dic1[i] = 1

values = list(dic1.values())

values.sort()

ans = 0
while k < len(values):
    ans += values.pop(0)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
li = []
for i in range(N+1):
    cnt = A.count(i+1)
    if cnt>0:
        li.append(cnt)
li.sort(reverse=True)
print(sum(li[K:]))

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter

input = sys.stdin.readline


def main():
    N, K = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

    C = Counter(A)

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

print(
    map(
        lambda first: sum(
            list(
                sorted(
                    reduce(
                        lambda acc, e: map(
                            lambda x: x[1] + 1 if x[0] == (e - 1) else x[1], 
                            enumerate(acc)
                        ),
                        map(int, input().split()),
                        [0 for _ in range(first[0])]
                    ),
                    reverse=True
                )
            )[first[1]:]
        ),
        [tuple(map(int, input().split()))]
    ).__next__()
)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

print(
    map(
        lambda first: sum(
            list(
                sorted(
                    reduce(
                        lambda acc, e: [prev + 1 if i == (e - 1) else prev for i, prev in enumerate(acc)],
                        map(int, input().split()),
                        [0 for _ in range(first[0])]
                    ),
                    reverse=True
                )
            )[first[1]:]
        ),
        [tuple(map(int, input().split()))]
    ).__next__()
)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
al = list(map(int, input().split()))
a_s = list(set(al))

if k == len(a_s):
    print(0)
else:
    count = []
    for a in a_s:
        count.append(al.count(a))
    change_a = len(a_s) - k
    count_s = sorted(count)
    print(sum(count_s[:change_a]))

Submission status: Time Limit Exceeded

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

## 種類数の求め方
# 1. A内の要素を取り出して、それがA内に存在するとき：種類＋１
# 2. その要素を重複数だけAから削除してループ

kinds = 0
A.sort()
duplicatedList = []
while len(A) > 0:
    if A[0] in A:
        kinds += 1
        duplicatedList.append(A.count(A[0]))
        for j in range(A.count(A[0])):
            A.remove(A[0])

if kinds <= K:
    print('0')
else:
    duplicatedList.sort()
    tmp = 0
    for i in range(kinds-K):
        tmp += duplicatedList[i]
    print(tmp)

Submission status: Time Limit Exceeded

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

## 種類数の求め方
# 1. A内の要素を取り出して、それがA内に存在するとき：種類＋１
# 2. その要素を重複数だけAから削除してループ

kinds = 0
A.sort()
duplicatedList = []
while len(A) > 0:
    if A[0] in A:
        kinds += 1
        duplicatedList.append(A.count(A[0]))
        for j in range(A.count(A[0])):
            A.remove(A[0])

if kinds <= K:
    print('0')
elif kinds > K:
    duplicatedList.sort()
    tmp = 0
    for i in range(kinds-K):
        tmp += duplicatedList[i]
    print(tmp)

Submission status: Time Limit Exceeded

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

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

## 種類数の求め方
# 1. A内の要素を取り出して、それがA内に存在するとき：種類＋１
# 2. その要素を重複数だけAから削除してループ

kinds = 0
A.sort()
duplicatedList = []
while len(A) > 0:
    if A[0] in A:
        kinds += 1
        duplicatedList.append(A.count(A[0]))
        for j in range(A.count(A[0])):
            A.remove(A[0])

if kinds <= K:
    print('0')
elif kinds > K:
    duplicatedList.sort()
    tmp = 0
    for i in range(kinds-K):
        tmp += duplicatedList[i]
    print(tmp)

Submission status: Time Limit Exceeded

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

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

## 種類数の求め方
# 1. A内の要素を取り出して、それがA内に存在するとき：種類＋１
# 2. その要素を重複数だけAから削除してループ

kinds = 0
A = sorted(A)
duplicatedList = []
while len(A) > 0:
    if A[0] in A:
        kinds += 1
        duplicatedList.append(A.count(A[0]))
        for j in range(A.count(A[0])):
            A.remove(A[0])

if kinds <= K:
    print('0')
elif kinds > K:
    duplicatedList = sorted(duplicatedList)
    tmp = 0
    for i in range(kinds-K):
        tmp += duplicatedList[i]
    print(tmp)

Submission status: Time Limit Exceeded

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

## 種類数の求め方
# 1. A内の要素を取り出して、それがA内に存在するとき：種類＋１
# 2. その要素を重複数だけAから削除してループ

kinds = 0
A = sorted(A)
duplicatedList = []
while len(A) > 0:
    if A[0] in A:
        kinds += 1
        duplicatedList.append(A.count(A[0]))
        for j in range(A.count(A[0])):
            A.remove(A[0])

if kinds <= K:
    print('0')
elif kinds > K:
    duplicatedList = sorted(duplicatedList)
    tmp = 0
    for i in range(kinds-K):
        tmp += duplicatedList[i]
    print(tmp)

Submission status: Time Limit Exceeded

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

## 種類数の求め方
# 1. A内の要素を取り出して、それがA内に存在するとき：種類＋１
# 2. その要素を重複数だけAから削除してループ

kinds = 0
A = sorted(A)
duplicatedList = []
while len(A) > 0:
    if A[0] in A:
        kinds += 1
        duplicatedList.append(A.count(A[0]))
        for j in range(A.count(A[0])):
            A.remove(A[0])

if kinds <= K:
    print('0')
else:
    duplicatedList = sorted(duplicatedList)
    i = 0
    tmp = 0
    a = kinds
    while a > K:
        a -= 1
        tmp += duplicatedList[i]
        i += 1
    print(tmp)

Submission status: Time Limit Exceeded

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

## 種類数の求め方
# 1. A内の要素を取り出して、それがA内に存在するとき：種類＋１
# 2. その要素を重複数だけAから削除してループ

kinds = 0
A = sorted(A)
duplicatedList = []
while len(A) > 0:
    if A[0] in A:
        kinds += 1
        duplicatedList.append(A.count(A[0]))
        for j in range(A.count(A[0])):
            A.remove(A[0])

if kinds <= K:
    print('0')
elif kinds > K:
    duplicatedList = sorted(duplicatedList)
    i = 0
    tmp = 0
    while kinds > K:
        kinds -= 1
        tmp += duplicatedList[i]
        i += 1
    print(tmp)

Submission status: Time Limit Exceeded

Solution code:

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

## 種類数の求め方
# 1. A内の要素を取り出して、それがA内に存在するとき：種類＋１
# 2. その要素を重複数だけAから削除してループ

kinds = 0
A = sorted(A)
A_Left = A
duplicatedList = []
while len(A_Left) > 0:
    if A_Left[0] in A_Left:
        kinds += 1
        duplicatedList.append(A.count(A_Left[0]))
        for j in range(A_Left.count(A_Left[0])):
            A_Left.remove(A_Left[0])

if kinds <= K:
    print('0')
elif kinds > K:
    duplicatedList = sorted(duplicatedList)
    i = 0
    tmp = 0
    while kinds > K:
        kinds -= 1
        tmp += duplicatedList[i]
        i += 1
    print(tmp)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

## 種類数の求め方
# 1. A内の要素を取り出して、それがA内に存在するとき：種類＋１
# 2. その要素を重複数だけAから削除してループ

kinds = 0
A = sorted(A)
A_Left = copy.copy(A)
duplicatedList = []
while len(A_Left) > 0:
    if A_Left[0] in A_Left:
        kinds += 1
        duplicatedList.append(A.count(A_Left[0]))
        for j in range(A_Left.count(A_Left[0])):
            A_Left.remove(A_Left[0])

if kinds <= K:
    print('0')
elif kinds > K:
    duplicatedList = sorted(duplicatedList)
    i = 0
    tmp = 0
    while kinds > K:
        kinds -= 1
        tmp += duplicatedList[i]
        i += 1
    print(tmp)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

## 種類数の求め方
# 1. A内の要素を取り出して、それがA内に存在するとき：種類＋１
# 2. その要素を重複数だけAから削除してループ

kinds = 0
A = sorted(A)
A_Left = copy.copy(A)
duplicatedList = []
while len(A_Left) > 0:
    if A_Left[0] in A_Left:
        kinds += 1
        duplicatedList.append(A.count(A_Left[0]))
        for j in range(A_Left.count(A_Left[0])):
            A_Left.remove(A_Left[0])

if kinds <= K:
    print('0')
elif kinds > K:
    duplicatedList = sorted(duplicatedList)
    i = 0
    tmp = 0
    while kinds > K:
        kinds -= 1
        while duplicatedList[i] > 0:
            tmp += 1
            duplicatedList[i] -= 1
        i += 1
    print(tmp)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
f=[l.count(i) for i in range(1,n+1)]
f.sort()
print(sum(f[0:n-k]))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
f=[l.count(i) for i in range(1,n+1)]
f.sort()
print(sum(f[0:n-k]))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
f=[]
for i in range(1,n+1):
   a=l.count(i)
   f.append(a)
f.sort()
print(sum(f[0:n-k]))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
f=[]
for i in range(1,n+1):
   a=l.count(i)
   f.append(a)
f.sort()
total=0
for j in range (0,n-k):
   total=total+f[j]

print(total)

Submission status: Time Limit Exceeded

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

count=[balls[0]]
for i in range(1,n):
    x=0
    for j in range(len(count)):
        if balls[i]!=count[j]:
            x+=1
    if x==len(count):
        count.append(balls[i])        
#print(count)
if len(count)<=k:
    print(0)
else:
    frequency=[]
    for i in range(len(count)):
        frequency.append(balls.count(count[i]))    
    #print(frequency)
    y=0
    z=len(count)
    while k<z:
        y+=min(frequency)
        z=z-1
        del frequency[frequency.index(min(frequency))]
        #print(frequency)
    print(y)

Submission status: Time Limit Exceeded

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

count=[balls[0]]
for i in range(1,n):
    x=0
    for j in range(len(count)):
        if balls[i]!=count[j]:
            x+=1
    if x==len(count):
        count.append(balls[i])        
#print(count)
if len(count)<=k:
    print(0)
else:
    frequency=[]
    for i in range(len(count)):
        frequency.append(balls.count(count[i]))    
    #print(frequency)
    y=0
    z=len(count)
    while k<z:
        y+=min(frequency)
        z=z-1
        del frequency[frequency.index(min(frequency))]
        #print(frequency)
    print(y)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

c = Counter(a)
l = len(c)
cnt = 0
if k < l:
    i = -1
    for _ in range(l-k):
        cnt += c.most_common()[i][1]
        i -= 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

c = Counter(a)
l = len(c)
cnt = 0
if k < l:
    i = -1
    for _ in range(l-k):
        cnt += c.most_common()[i][1]
        i -= 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

c = list(collections.Counter(a).values())
c.sort()
num = len(c)
res = 0
while num > k:
  res += c[0]
  c.pop(0)
  num = len(c)
print(res)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n,k=map(int,input().split())
    A=tuple(map(int,input().split()))
    L=set(A)
    m=len(L)-k
    if m<=0:
        print(0)
    else:
        D=[A.count(l) for l in L]
        D.sort()
        print(sum(D[:m]))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n,k=map(int,input().split())
    A=list(map(int,input().split()))
    L=set(A)
    m=len(L)-k
    if m<=0:
        print(0)
    else:
        D=[0]*len(L)
        for i, l in enumerate(L):
            D[i]=A.count(l)
        print(sum(sorted(D)[:m]))
main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import collections
N,K=map(int,input().split())
A=list(map(int,input().split()))
m=len(set(A))
#print(m,K)
ans=0
if K>=m:
    print(0)
else:
    c = collections.Counter(A)
    for i in range(m-K):
        ans+=c.most_common()[m-1-i][1]
    print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

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

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

m = list(set(n))



c = 0
if len(m) <= b:
    print(0)
else:
    dic = {}
    for i in range(len(m)):
        dic[str(m[i])] = n.count(m[i])

    dic = sorted(dic.items(), key = lambda x:x[1])
    for i in range(len(m) - b):
        c += dic[i][1]
    print(c)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
line = list(input().split())
setted_line = list(set(line))
origin_num = len(setted_line) 
pairs = {}
for i in range(origin_num):
  count = line.count(setted_line[i])
  pairs[setted_line[i]] = count
sorted_pairs = sorted(pairs.items(), key = lambda x:x[1])
answer = 0
gap = origin_num - k
if gap <= 0:
  answer = 0
else:
   for j in range(gap):
    answer += sorted_pairs[j][1]
print(answer)