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())
line = list(input().split())
setted_line = list(set(line))
origin_num = len(setted_line) 
pairs = {}
for i in range(len(setted_line)):
  count = line.count(setted_line[i])
  pairs[setted_line[i]] = count
sorted_pairs = sorted(pairs.items(), key = lambda x:x[1])
answer = 0
if origin_num <= k:
  answer = 0
else:
  gap = origin_num - k 
  for j in range(gap):
    answer += sorted_pairs[j][1]
print(answer)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if len(set(a)) <= k:
	print(0)
	exit()

ans = 0

while len(set(a)) > k :

	b = list(set(a))

	c = [0]*len(b)

	for i in range(len(b)):
		c[i] = a.count(b[i])

	for i in range(len(a)):
		#print(i)
		if a[i] == b[c.index(min(c))]:
			#print(a)
			del a[a.index(b[c.index(min(c))])]
			#print(a)
			a.append(b[c.index(max(c))])
			#print(a)
			ans = ans + 1

print(ans)

Submission status: Time Limit Exceeded

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

n, k = map(int, input().split())
a = list(map(int, input().split()))
d = collections.Counter(a)
v = list(d.values())
v = sorted(v)
ans = 0

while len(v) > k:
    ans += v.pop(0)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

n, k = map(int, input().split())
a = list(map(int, input().split()))
d = collections.Counter(a)
v = list(d.values())
ans = 0
while len(v) > k:
    ans += v.pop(v.index(min(v)))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

n, k = map(int, input().split())
a = list(map(int, input().split()))
d = collections.Counter(a)
ans = 0

while len(d.keys()) > k:
    min_kv = min(d.items(), key=lambda x: x[1])
    ans += min_kv[1]
    del d[min_kv[0]]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

n, k = map(int, input().split())
a = list(map(int, input().split()))
d = collections.Counter(a)
ans = 0

while len(d.keys()) > k:
    min_kv = min(d.items(), key=lambda x: x[1])
    d[min_kv[0]] -= 1
    ans += 1
    if d[min_kv[0]] == 0:
        del d[min_kv[0]]

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


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


    if len(A) > K:
        set_A = set(A)
        
        count = np.empty(len(set_A), dtype=int)
        i = 0
        for s_a in set_A:
            count[i] = A.count(s_a)
            i += 1

        sorted_count = np.sort(count)
        reduce_num = len(set_A) - K
        
        sum_count = 0
        counter = 0
        for c in sorted_count:
            if counter == reduce_num :
                break
            sum_count += c
            counter += 1

        print(sum_count)
    else:
        print(0)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


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


    if len(A) > K:
        set_A = set(A)
        #print(set_A)
        count = np.empty(len(set_A), dtype=int)
        for i, s_a in enumerate(set_A):
            count[i] = A.count(s_a)
        sorted_count = np.sort(count)
        #print(sorted_count)
        reduce_num = len(set_A) - K
        sum_count = 0
        counter = 0
        for c in sorted_count:
            if counter == reduce_num :
                break
            sum_count += c
            counter += 1

        print(sum_count)
    else:
        print(0)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:



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

    if len(A) > K:
        set_A = set(A)
        count = []
        for s_a in set_A:
            card = A.count(s_a)
            count.append(card)
        sorted_count = sorted(count)
        reduce_num = len(count) - K
        sum_count = 0
        counter = 0
        for c in sorted_count:
            if counter == reduce_num :
                break
            sum_count += c
            counter += 1

        print(sum_count)
    else:
        print(0)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

if len(a_val) > k:
    for i in range(len(a_val) - k):
        count += a_val.most_common()[-1-i][1]

print(count)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
dict_ = dict()
[exec("dict_[{0}] = dict_.get({0}, 0) + 1".format(num)) for num in map(int, input().split())]
print(sum(sorted(dict_.values(), reverse=True)[k:]))

Submission status: Time Limit Exceeded

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

B = []
for i in range(1,N+1):
    S = A.count(i)
    B.append((i,int(S)))
B = sorted(B ,key = lambda x : x[1] ,reverse = True)
#print(B)

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

Submission status: Time Limit Exceeded

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

from collections import defaultdict
dic = defaultdict(int)
for a in al:
    dic[a] += 1

l = [i for i in dic.values()]
l = sorted(l)

loop = 0
ans = 0
while True:
    if len(l) > k:
        ans += l[0]
    else:
        break
    l.pop(0)

print(ans)

Submission status: Time Limit Exceeded

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

from collections import defaultdict
dic = defaultdict(int)
for a in al:
    dic[a] += 1

l = [i for i in dic.values()]
l = sorted(l)

loop = 0
ans = 0
while True:
    loop += 1
    if loop > 200000:
        print("yabe")
        import sys
        sys.exit()
    if len(l) > k:
        ans += l[0]
    else:
        break
    l.pop(0)

print(ans)

Submission status: Time Limit Exceeded

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

from collections import defaultdict
dic = defaultdict(int)
for a in al:
    dic[a] += 1

l = [i for i in dic.values()]
l = sorted(l)

ans = 0
while True:
    if len(l) > k:
        ans += l[0]
    else:
        break
    l.pop(0)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
from collections import Counter
a = list(map(int,input().split()))
c = Counter(a)
ks = 0
if len(c.keys()) <= K:print(0)
else:
    for i in range(K):

        ks += c.most_common()[i][1]
    print(N-ks)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int,input().split())
a = list(map(int, input().split()))
countlist = sorted([a.count(i) for i in range(1, n + 1)],reverse= True)
ans = sum(countlist) - sum(countlist[:k])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int,input().split())
a = list(map(int, input().split()))
countlist = [a.count(i) for i in range(1, n + 1)]
countlist = sorted([i for i in countlist if i != 0], reverse = True)
if len(countlist) >= k :
    ans = sum(countlist) - sum(countlist[:k])
else :
    ans = 0
print(ans)

Submission status: Time Limit Exceeded

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

arr=[]
for i in b:
  arr+=[[a.count(i),i]]

arr=sorted(arr)

d=len(b)-k
cnt=0
for i in range(d):
  cnt+=arr[i][0]
print(cnt)

Submission status: Time Limit Exceeded

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

arr=[]
for i in b:
  arr+=[[i,a.count(i)]]

arr.sort(key=lambda x: x[1])

d=len(b)-k
cnt=0
for i in range(d):
  cnt+=arr[i][1]
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=set(a)
h=[]
ans=0
for i in s:
  l=a.count(i)
  h.append(l)
if len(s)-k>0:
  for i in range(len(s)-k):
    ans+=min(h)
    h.remove(min(h))
  print(ans)
else:
  print(0)

Submission status: Time Limit Exceeded

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

ans = 0
a_values = collections.Counter(a)

while len(a_values) > k:
    a_most = a_values.most_common()[0]
    a_least = a_values.most_common()[-1]

    a.remove(a_least[0])
    a.append(a_most[0])
    a_values = collections.Counter(a)
    ans += 1
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = sorted([a_list.count(i) for i in range(n+1)],reverse = True)[:k]
print(n-sum(b_list))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = sorted([a_list.count(i) for i in range(n+1)],reverse = True)
print(n-sum(b_list[:k:]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

count = [0 for i in range(200010)]
for A in AAA:
    count[A]+=1
ans=0
while sum([1 if i!=0 else 0 for i in count])>K:
    #print(count[:10])
    buff = min([i for i in count if i!=0])
    ans += buff
    count.remove(buff)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
species = list(set(A))
dic = {}
for i in species:
    dic[i] = 0
for i in A:
    dic[i] += 1
if K >= len(species):
    print(0)
else:
    ans = 0
    while len(dic) > K:
        ans += min(dic.values())
        dic.pop(min(dic, key=dic.get))
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
A = sorted(list(map(int,input().split())))
species = list(set(A))
dic = {}
for i in species:
    dic[i] = 0
for i in A:
    dic[i] += 1
if K >= len(species):
    print(0)
else:
    ans = 0
    while len(dic) > K:
        ans += min(dic.values())
        dic.pop(min(dic, key=dic.get))
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')

N, K = [int(x) for x in input().split()]
nums = [int(x) for x in input().split()]

count =	0
while len(set(nums)) > K:
	c = collections.Counter(nums)
	ma = c.most_common()[0]
	mi = c.most_common()[-1]

	nums = [ma[0] if i == mi[0] else i for i in nums]
	count += mi[1]
print(count)

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()))
b = set(a)
c = [a.count(i) for i in b]
d = len(b)
c.sort()
ans = 0
for i in c:
    if K >= d:
        break
    ans += i
    d -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
b = set(a)
c = [a.count(i) for i in b]
d = len(b)
c.sort()
ans = 0
for i in c:
    if K >= d:
        break
    ans += i
    d -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N,K = map(int,input().split())
a = list(map(int,input().split()))
b = set(a)
c = [a.count(i) for i in b]
d = len(b)
c.sort()
e = deque(c)
ans = 0
for i in e:
    if K >= d:
        break
    ans += i
    d -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
b = set(a)
c = []
d = len(b)
ans = 0
for i in b:
  c.append(a.count(i))
c.sort()
for i in c:
  if K >= d:
    break
  ans += i
  d -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
C = list(set(B))
D ={}
bag = 0

for i in C:
    D[i] = 0
    for j in B:
        if i == j:
            D[i] += 1

LLL = sorted(list(D.values()))
AA = len(D) - K
if len(D) <= K:
    print(0)
else:
    for i in range(AA):
        bag += LLL[i]
    print(bag)

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()))
sum = 0
import collections

c = collections.Counter(A)
c = c.most_common()
while len(c) > k:
    sum += c[-1][1]
    c = c[:-1]
print(sum)

Submission status: Time Limit Exceeded

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

A = list(map(int,input().split()))
sum = 0
import collections

c = collections.Counter(A)
c = c.most_common()
while len(c) > k:
    sum += c[-1][1]
    c = c[:-1]
print(sum)

Submission status: Time Limit Exceeded

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

A = list(map(int,input().split()))
sum = 0
import collections

c = collections.Counter(A)
c = c.most_common()
for i in range(n):
  if len(c) > k:
    sum += c[-1][1]
    c = c[:-1]
  else:
    break
print(sum)

Submission status: Time Limit Exceeded

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

dic = {}
for num in a:
    if num not in dic.keys():
        dic[num] = 0
    dic[num] += 1

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

ans = 0

for i in range(len(dic)- k):
    ans += dic[i][1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
N,K=map(int,input().split())
li=list(map(int,input().split()))
li_count=collections.Counter(li)
change=0
S=max(len(li_count)-K,0)
for s in range(S):
    change+=li_count.most_common()[-1-s][1]
print(change)

Submission status: Time Limit Exceeded

Solution code:
import collections
N,K=map(int,input().split())
li=list(map(int,input().split()))
li_count=collections.Counter(li)
not_change=0
S=min(K,len(li_count))
for k in range(S):
    not_change+=li_count.most_common()[k][1]
print(N-not_change)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

cnt=[]
for a in set(A):
  cnt.append(A.count(a))
cnt.sort()
ans=0
while len(cnt)>k:
  ans+=cnt[0]
  cnt=cnt[1:]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
b=list(set(A))
c=[]
if len(b)<=k:
  print(0)
else:
  for i in range(len(b)):
    c.append(A.count(b[i]))
  c=sorted(c)[::-1]
  print(sum(c[k:]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
c = Counter(a)
ans = 0
for i in range(len(c)-k):
  ans+=c.most_common()[-i-1][1]
print(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)
count = 0

for i in range(k, len(c)):
        count += c.most_common()[i][1]

print(count)

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)
count = 0

for i in range(len(c)):
    if i >= k:
        count += c.most_common()[i][1]

print(count)

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)

cnt = 0
if K < len(c.keys()):
    for i in range(1,abs(len(c.keys()) - K)+1):
            cnt += c.most_common()[-i][1]
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
kind = len(set(a))
kind_num = []

if kind < k:
    print(0)
    exit()

for num in set(a):
    one = a.count(num)
    kind_num.append(one)

kind_num.sort()
ans = sum(kind_num[:-k])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def unique_list(seq):
    seen = []
    return [x for x in seq if x not in seen and not seen.append(x)]

N, K = map(int,input().split())
A = list(map(int, input().split()))
B = []
for i in range(N):
  count = 0
  for j in range(N):
    if A[i] == A[j]:
      count += 1
  B.append([A[i],count])
  count = 0
C = unique_list(B)
C.sort(key=lambda x: x[1])
E = len(C) - K
if E <= 0:
  print(0)
else:
  ans = 0
  for i in range(E):
    ans += C[i][1]
  print(ans)

Submission status: Time Limit Exceeded

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

seta = set(a)
cntK = len(seta)
cnta = [0] * cntK
if cntK <= K:
    print(0)
    sys.exit()

for i in range(len(seta)):
    cnta[i] = a.count(seta.pop())

cnta.sort()
cnt = 0
for i in range(cntK - K):
    cnt += cnta[i]

print(cnt)

Submission status: Time Limit Exceeded

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

b=set(a)
c=[]
for i in b:
    c+=[a.count(i)]
    
c.sort()
if len(b)>k:
    print(sum(c[:len(b)-k]))
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
l = list(map(int, input().split()))
set_l = list(set(l))
set_l_len = len(set_l)
count = {}
for i in range(set_l_len):
    count[str(set_l[i])] = l.count(set_l[i])
count_sorted = sorted(count.items(), key=lambda x: x[1])
sum = 0
for i in range(max(0, set_l_len - k)):
    sum += count_sorted[i][1]
print(sum)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N, K = map(int, input().split())
    targets = list(map(str, input().split()))
    targets_dict = {}

    for i in range(N):
        if targets[i] not in targets_dict:
            targets_dict[targets[i]] = 1
        else:
            targets_dict[targets[i]] += 1

    need_reduce = len(targets_dict) - K

    if need_reduce <= 0:
        print(0)
    else:
        ans = 0
        for _ in range(need_reduce):
            min_kv = min(targets_dict.items(), key=lambda x: x[1])
            ans += min_kv[1]
            del targets_dict[min_kv[0]]

        print(ans)
        
resolve()

Submission status: Time Limit Exceeded

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

sarr = list(set(arr))

dic = dict()

for i in range(len(sarr)):
    dic[sarr[i]] = arr.count(sarr[i])

ans = 0

while len(dic) > k:
    # 辞書の要素数をカウントし、キーを返却
    dick = min(dic, key=dic.get)
    ans += dic.pop(dick)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections

def calc(k, balls):
    balls_count = set(copy.copy(balls))
    change_count = 0
    flg = True

    while flg == True:
        if len(balls_count) <= int(k):
            flg = False
            return change_count
        else:
            c = collections.Counter(balls)
            most_common = c.most_common()[-1]
            balls = [i for i in balls if i != most_common[0]]
            balls_count = sorted(set(copy.copy(balls)))
            change_count += int(most_common[1])


_ = input().split()
k = _[1]
balls = sorted(input().split())

print(calc(k, balls))

Submission status: Time Limit Exceeded

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

l = []
for i in a_uniq:
  l.append(len([j for j in a if j == i]))
l = sorted(l)
  
print(sum(sorted(l[0:len(a_uniq)-k])))

Submission status: Time Limit Exceeded

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

l = []
for i in a_uniq:
  l.append(len([j for j in a if j == i]))
l = sorted(l)
  
print(sum(sorted(l[0:len(a_uniq)-k])))

Submission status: Time Limit Exceeded

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

l = []
for i in a_uniq:
  l.append(len([j for j in a if j == i]))
l = sorted(l)
  
print(sum(sorted(l[0:len(a_uniq)-k])))

Submission status: Time Limit Exceeded

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

d = {}
for i in a_uniq:
  d[i] = len([j for j in a if j == i])

print(sum(sorted(d.values())[0:len(a_uniq)-k]))

Submission status: Time Limit Exceeded

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


d = {}
for i in a_uniq:
  d[i] = len([j for j in a if j == i])

print(sum(sorted(d.values())[0:len(a_uniq)-k]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

ans = 0
num = len(A.keys())
while num > K:
  min_key = [key for key in A if A[key] == min(A.values())][0]
  ans += A[min_key]
  num -= 1
  del A[min_key]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

ans = 0
while len(A.keys()) > K:
  min_key = [key for key in A if A[key] == min(A.values())][0]
  ans += A[min_key]
  del A[min_key]
print(ans)

Submission status: Time Limit Exceeded

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

from collections import Counter

c = Counter(A)
cnt = 0

if len(c) > K:
    for i in range(K, len(c)):
        cnt += c.most_common()[i][1]
        
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
hoge=list(map(int,input().split()))
huga=[]
for i in range(n):
  huga.append(hoge.count(i+1))
huga.sort()
ans=n
for i in range(m):
  ans-=huga[-(i+1)]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,K = map(int, input().split())
As = input().split()
for i in range(N):
  As[i] = int(As[i])
As = np.array(As)

As_uni = np.unique(As)
K2 = len(As_uni)

if K2 <= K:
  ans = 0
else:
  for i,a in enumerate(As_uni):
    As_uni[i] = (As==a).sum()
  As_uni = np.sort(As_uni)
  ans = As_uni[:K2-K].sum()
  
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,K = map(int, input().split())
As = input().split()
for i in range(N):
  As[i] = int(As[i])
As = np.array(As)

As_uni = np.unique(As)
for i,a in enumerate(As_uni):
  As_uni[i] = (As==a).sum()
As_uni = np.sort(As_uni)

K2 = len(As_uni)

if K2 > K:
  ans = As_uni[:K2-K].sum()
else:
  ans = 0
  
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,K = map(int, input().split())
As = input().split()
for i in range(N):
  As[i] = int(As[i])
  
As = np.array(As)

As_uni = np.unique(As)

As_uni_count = np.zeros_like(As_uni)
for i,a in enumerate(As_uni):
  As_uni_count[i] = (As==a).sum()
  
As_uni_count = np.sort(As_uni_count)

K2 = len(As_uni)

if K2 > K:
  ans = As_uni_count[:K2-K].sum()
else:
  ans = 0
  
print(int(ans))

Submission status: Time Limit Exceeded

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

ans_date = []
for i in new_arr:
  date = []
  num_count = arr.count(i)
  num = i
  date.append(num_count)
  date.append(num)
  ans_date.append(date)
ans_date.sort()

t = len(ans_date)
x = t - k
if x <= 0:
  print(0)
  exit()
ans = 0
for l in range(x):
  ans += ans_date[l][0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split(" "))
arr = list(map(int, input().split(" ")))
new_arr = set(arr)
# print(arr)
# print(new_arr)

ans_date = []
for i in new_arr:
  date = []
  num_count = arr.count(i)
  num = i
  date.append(num_count)
  date.append(num)
  ans_date.append(date)
# print(ans_date)
ans_date.sort()
# print(ans_date)

t = len(ans_date)
x = t - k

ans = 0
for l in range(x):
  ans += ans_date[l][0]
print(ans)

Submission status: Time Limit Exceeded

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

from collections import Counter, OrderedDict

c = Counter(a)
d = OrderedDict(sorted(c.items(), key=lambda x: x[1]))

if len(d) <= k:
    print(0)

else:
    ans = 0
    while len(d) > k:
        item = list(d.items())[0]
        ans += item[1]
        del d[item[0]]
    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()))
s=len(set(A))
C=Counter(A)
if s>k:
    for i in range(k):
        n-=C.most_common()[i][1]
    print(n)
else:
    print(0)

Submission status: Time Limit Exceeded

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

sa = list(set(a))

if len(sa) > nk[1]:
	c = []
	for i in range(len(sa)):
		c.append(a.count(sa[i]))
		
	sortc = sorted(c, reverse = True)
	m = 0
	for i in range(nk[1]):
		m += sortc[i]
	ans = nk[0] - m
else:
	ans = 0

print(ans)

Submission status: Time Limit Exceeded

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

sa = list(set(a))

if len(sa) > nk[1]:
	c = []
	for i in range(len(sa)):
		c.append(a.count(sa[i]))
		
	sc = sorted(c)
	ans = 0
	for i in range(len(sa)-nk[1]):
		ans += sc[i]
else:
	ans = 0

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

int_variations = list(set(balls))

count = {}
for i in int_variations:
    count[i] = balls.count(i)

counted = sorted(count.items(), key = lambda x: x[1])

need_to_rewrite = 0
for i in range(len(counted) - k):
    need_to_rewrite += counted[i][1]

print(need_to_rewrite)

Submission status: Time Limit Exceeded

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

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

int_variations = list(set(balls))

count = {}
for i in int_variations:
    count[i] = balls.count(i)

counted = sorted(count.items(), key = lambda x: x[1])

need_to_rewrite = 0
for i in counted[:len(counted) - k]:
    need_to_rewrite += i[1]

print(need_to_rewrite)

Submission status: Time Limit Exceeded

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

A_dict = {str(i):0 for i in set(A)}
for a in A:
  A_dict[str(a)] += 1
del A
  
def get_A_key_from_value(dic, value):
  for i, j in dic.items():
    if j == value:
      break
  return i
  
ans = 0
while True:
  if len(A_dict) <= K:
    break
  ans += min(A_dict.values())
  del A_dict[get_A_key_from_value(A_dict,min(A_dict.values()))]
print(ans)

Submission status: Time Limit Exceeded

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

count = {}

for a in A:
    count[a] = count.get(a, 0) + 1

sorted_ = sorted(count.items(), key=lambda x: x[1])

total = 0
while K < len(sorted_):
    k, v = sorted_[0]
    sorted_ = sorted_[1:]
    total += v

print(total)

Submission status: Time Limit Exceeded

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

count = {}

for a in A:
    count[a] = count.get(a, 0) + 1

sorted_ = sorted(count.items(), key=lambda x: x[1])

total = 0
while K < len(sorted_):
    k, v = sorted_[0]
    sorted_ = sorted_[1:]
    total += v

if len(sorted_) == 0:
    total = 0

print(total)

Submission status: Time Limit Exceeded

Solution code:
from collections import*;n,k,*a=map(int,open(0).read().split());print(n-sum(Counter(a).most_common()[i][1] for i in range(min(len(set(a)),k))))