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 = list(map(int,input().split()))
A = sorted(A)
c = -1
num = []
kazu = []
for i in range(N):
    if not(A[i] in num):
        num.append(A[i])
        kazu.append(0)
        c += 1
        for j in range(i,N):
            if num[c] == A[j]:
                kazu[c] += 1
            else:
                i = j
                break
num = len(kazu) - K
kazu = sorted(kazu)
c = 0
if num > 0:
    for i in range(num):
        c += kazu[i]
print(c)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
A = sorted(A)
c = -1
num = []
kazu = []
for i in range(N):
    if not(A[i] in num):
        num.append(A[i])
        kazu.append(0)
        c += 1
        for j in range(i,N):
            if num[c] == A[j]:
                kazu[c] += 1
num = len(kazu) - K
kazu = sorted(kazu)
c = 0
if num > 0:
    for i in range(num):
        c += kazu[i]
print(c)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
AS = list(set(A))
F = list()
for i in AS:
    F.append(A.count(i))
print(sum(sorted(F)[:len(AS) - k]))

Submission status: Time Limit Exceeded

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

num = []
count = []
for a in range(len(A)):
    tmp = A[a]
    if not tmp in num:
        num.append(tmp)
        count.append(1)
    else:
        index = num.index(tmp)
        count[index] += 1

count = sorted(count)
ans = 0
if len(count)<=K:
    pass
else:
    tmp = len(count)-K
    count = count[:tmp]
    ans = sum(count)

print(ans)

Submission status: Time Limit Exceeded

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

num = []
count = []
for a in range(len(A)):
    tmp = A[a]
    if not tmp in num:
        num.append(tmp)
        count.append(1)
    else:
        index = num.index(tmp)
        count[index] += 1

count = sorted(count)
ans = 0
if len(count)<=K:
    pass
else:
    tmp = len(count)-K
    count = count[:tmp]
    ans = sum(count)

print(ans)

Submission status: Time Limit Exceeded

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

num = []
count = []
for a in range(len(A)):
    tmp = A[a]
    if not tmp in num:
        num.append(tmp)
        count.append(1)
    else:
        index = num.index(tmp)
        count[index] += 1

count = sorted(count)
ans = 0
if len(count)<=K:
    pass
else:
    for i in range(len(count)-K):
        ans += count[i]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
AS = set(A)
B = []
for i in AS:
    B.append(A.count(i))
B.sort()
print(sum(B[:len(AS) - k]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, K = list(map(int, input().split()))
list_A = list(map(int, input().split()))
list_A.sort()
list_A_set = list(set(list_A))
length = len(list_A_set)
cnt = 0
list_cnt = []
for i in list_A_set:
  list_cnt.append(list_A.count(i))
list_cnt.sort()
while length > K:
  Min = list_cnt[0]
  cnt += Min
  list_cnt.remove(Min)
  length -= 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N, K = list(map(int, input().split()))
list_A = list(map(int, input().split()))
list_A.sort()
list_A_set = list(set(list_A))
length = len(list_A_set)
cnt = 0
list_cnt = []
for i in list_A_set:
  list_cnt.append(list_A.count(i))
while length > K:
  Min = min(list_cnt)
  cnt += Min
  list_cnt.remove(Min)
  length -= 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N, K = list(map(int, input().split()))
list_A = list(map(int, input().split()))
list_A.sort()
list_A_set = list(set(list_A))
cnt = 0
cnt_list = []
while len(list_A_set) > K:
  for i in list_A_set:
    cnt_list.append(list_A.count(i))
  Min = list_A_set[cnt_list.index(min(cnt_list))]
  Max = list_A_set[cnt_list.index(max(cnt_list))]
  list_A[list_A.index(Min)] = Max
  list_A.sort()
  list_A_set = list(set(list_A))
  cnt_list = []
  cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

d = dict()
for a in A:
    d.setdefault(a, 0)
    d[a] += 1

if len(d) <= k:
    print(0)
else:
    c = 0
    while len(d) > k:
        v = d.pop(min(d, key=d.get))
        c += v
    print(c)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
length = list(set(A))
dict = {}
count = 0
if len(length) > K:
  for i in length:
    if i not in dict:
      dict[i] = A.count(i)
  dict=sorted(dict.items(),key=lambda x:-x[1])
  for i in reversed(range(K,len(length))):
    count += dict[i][1]
print(count)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
from collections import defaultdict
d = defaultdict(int)
num_list = list(map(int,input().split()))
for tmp in num_list:
	d[tmp] += 1
    
v_list = sorted(list(d.values()))
while len(v_list) > k:
    v_list.pop(0)
print(n - sum(v_list))

Submission status: Time Limit Exceeded

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

s = set(a)
x = len(s)

c = []
for j in s:
    c.append(a.count(j))
c.sort(reverse=True)

if x <= k:
    print(0)
else:
    print(n-sum(c[:k]))

Submission status: Time Limit Exceeded

Solution code:
import collections
from collections import Counter

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

ac = collections.Counter(a)
s = set(a)
x = len(s)

if x <= k:
    print(0)
else:
    ans = 0
    for i in range(x - k):
        p = ac.most_common()[-1-i][1]
        ans += p
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
from collections import Counter

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

ac = collections.Counter(a)
s = set(a)

if len(s) <= k:
    print(0)
else:
    ans = 0
    for i in range(len(s) - k):
        p = ac.most_common()[-1-i][1]
        ans += p
    print(ans)

Submission status: Time Limit Exceeded

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

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()))
li = []
ans = 0

B = set(A)

cnt = len(B)

for x in B:
    a = A.count(x)
    li.append(a)

li = sorted(li)

if cnt - K > 0:
    for x in range(cnt - K):
        ans += li[x]
    print(ans)

else :
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
li = []
ans = 0

B = set(A)

cnt = len(B)

for x in B:
    a = A.count(x)
    li.append(a)

li = sorted(li)

if cnt - K > 0:
    for x in range(cnt - K):
        ans += li[x]
    print(ans)

else :
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
li = []
ans = 0

B = set(A)

cnt = len(B)

for x in B:
    a = A.count(x)
    li.append(a)

li = sorted(li)

if cnt - K > 0:
    for x in range(cnt - K):
        ans += li[x]
    print(ans)

else :
    print(0)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# hello worldと表示する
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n,k=MI()
lis=LI()
cou=Counter(lis)
u=cou.most_common()
#print(u)
ans=0
for i in range(len(u)-k):
    j=len(u)-i-1
    #print(j)
    ans+=cou.most_common()[j][1]
    #print(cou.most_common()[j][1])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
set_A = set(A)
len_types = len(set_A)
memo = [0] * N
ans = 0

for num in set_A:
    count_num = A.count(num)
    memo[num-1] = count_num

memo.sort()

for i in memo[:N-K]:
    ans += i

# while len_types > K:
#     ans += heapq.heappop(memo)
#     len_types -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq

N, K = map(int, input().split())
A = list(map(int, input().split()))
set_A = set(A)
len_types = len(set_A)
memo = []
ans = 0

for num in set_A:
    count_num = A.count(num)
    heapq.heappush(memo, count_num)

while len_types > K:
    ans += heapq.heappop(memo)
    len_types -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
n, k = map(int, input().split())
list = input().split()
c_list = collections.Counter(list)
sorted_c_list = sorted(c_list.values())
for i in range(len(sorted_c_list)):
  if len(sorted_c_list[i:])==k:
    print(sum(sorted_c_list[:i]))
    break
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
import collections
n, k = map(int, input().split())
list = [int(i) for i in input().split()]
c_list = collections.Counter(list)
sorted_c_list = sorted(c_list.values())
for i in range(len(sorted_c_list)):
  if len(sorted_c_list[i:])==k:
    print(sum(sorted_c_list[:i]))
    break
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
list = [int(i) for i in input().split()]
dict = {}
for l in list:
  if l not in dict:
    dict[l]=1
  else:
    dict[l] += 1
if len(dict)<=k:
  print(0)
else:
  sorted_list = sorted(dict.items(), key=lambda x:x[1])
  res = 0
  for i,d in enumerate(sorted_list):
    if len(sorted_list[i:])<=k:
      print(res)
      break
    res += d[1]

Submission status: Time Limit Exceeded

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

    res = 0
    balls = {}

    for ball in A:
        if ball in balls:
            balls[ball] += 1
        else:
            balls[ball] = 1
    
    delList = []
    bucket = [0 for _ in range(n+1)]

    for a in A:
        bucket[a] += 1
    
    for i in range(len(bucket)):
        if bucket[i] == 0:
            bucket[i] = 10**9
    
    for _ in range(len(set(A))):
        minBucket = min(bucket)
        minBucketIdx = bucket.index(minBucket)
        delList.append(minBucketIdx)
        bucket[minBucketIdx] = 10**9

    while len(delList) > k:
        res += balls[delList[0]]
        delList.pop(0)
    
    print(res)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

    res = 0
    balls = {}

    for ball in A:
        if ball in balls:
            balls[ball] += 1
        else:
            balls[ball] = 1

    while len(balls.keys()) > k:
        
        minValue = 200000
        maxValue = 0

        for key, value in balls.items():
            if value < minValue and value > 0:
                minValue = value
                minKey = key
        
            if value >= maxValue:
                maxValue = value
                maxKey = key
        
        balls[maxKey] += minValue
        balls.pop(minKey)

        res += minValue
    
    print(res)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

    res = 0
    balls = {}

    for ball in A:
        if ball in balls:
            balls[ball] += 1
        else:
            balls[ball] = 1

    while len(balls.keys()) > k:
        
        minValue = 200000
        maxValue = 0

        for key, value in balls.items():
            if value < minValue and value > 0:
                minValue = value
                minKey = key
        
            if value > maxValue:
                maxValue = value
                maxKey = key
        
        balls[maxKey] += minValue
        balls.pop(minKey)

        res += minValue
    
    print(res)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

    res = 0
    balls = {}

    for ball in A:
        if ball in balls:
            balls[ball] += 1
        else:
            balls[ball] = 1

    while len(set(A)) > k:
        
        minValue = 200000
        maxValue = 0

        for key, value in balls.items():
            if value < minValue and value > 0:
                minValue = value
                minKey = key
        
            if value > maxValue:
                maxValue = value
                maxKey = key
        
        A[A.index(minKey)] = maxKey
        balls[minKey] -= 1
        balls[maxKey] += 1

        res += 1
    
    print(res)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

    res = 0
    balls = {}

    for ball in A:
        if ball in balls:
            balls[ball] += 1
        else:
            balls[ball] = 1

    while len(set(A)) > k:
        
        minValue = 200000
        maxValue = 0

        for key, value in balls.items():
            if value < minValue and value > 0:
                minValue = value
                minKey = key
        
        for key, value in balls.items():
            if value > maxValue:
                maxValue = value
                maxKey = key
        
        A[A.index(minKey)] = maxKey
        balls[minKey] -= 1
        balls[maxKey] += 1

        res += 1
    
    print(res)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

count = [0] * n

for x in range(n):
    count[a[x]-1] += 1

count.sort()
length = len(count)

for x in range(length):
    if len(count[x:]) == k:
        break

print(sum(count[:x]))

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=[int(a) for a in input().split()]
C=set(A)
if len(C)<=K:
    print(0)
else:
    B=[b for b in C]
    X=[[A.count(B[i]),B[i]] for i in range(len(B))]
    X=sorted(X)
    q=len(X)-K
    ans=0
    for i in range(q):
        ans+=X[i][0]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=[int(a) for a in input().split()]
C=set(A)
B=[b for b in C]
if len(C)<=K:
    print(0)
else:
    X=[[A.count(B[i]),B[i]] for i in range(len(B))]
    X=sorted(X)
    q=len(X)-K
    ans=0
    for i in range(q):
        ans+=X[i][0]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=[int(a) for a in input().split()]
C=set(A)
B=[b for b in C]
X=[[A.count(B[i]),B[i]] for i in range(len(B))]
if len(X)<=K:
    print(0)
else:
    X=sorted(X)
    q=len(X)-K
    ans=0
    for i in range(q):
        ans+=X[i][0]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=[int(a) for a in input().split()]
B=[b for b in set(A)]
X=[[A.count(B[i]),B[i]] for i in range(len(B))]
X=sorted(X)
if len(X)<=K:
    print(0)
else:
    q=len(X)-K
    ans=0
    for i in range(q):
        ans+=X[i][0]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=[int(a) for a in input().split()]
B=[b for b in set(A)]
X=[]
for i in range(len(B)):
    x=A.count(B[i])
    X.append([x,B[i]])
X=sorted(X)
if len(X)<=K:
    print(0)
else:
    q=len(X)-K
    ans=0
    for i in range(q):
        ans+=X[i][0]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
index_list = []
for i in range(1, n+1):
    index_list.append(i)

int_list = []
for i in index_list:
    a_count = a.count(i)
    int_list.append(a_count)

int_dict = dict(zip(index_list, int_list))
sort_dict = sorted(int_dict.items(), key=lambda x: x[1], reverse=True)

for i in range(k):
    pop_l = sort_dict.pop(0)

ans = sum(dict(sort_dict).values())
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, k, *a = map(int, open(0).read().split())
    s = set(a)
    v = len(s) - k
    l = sorted([a.count(i) for i in s])
    ans = sum(l[:v])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

d = sorted(A.count(a) for a in set(A))

if len(d) > K:
    print(sum(d[:len(d) - K]))
else:
    print(0)

Submission status: Time Limit Exceeded

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

d = sorted([A.count(a) for a in set(A)])

if len(d) > K:
    print(sum(d[:len(d) - K]))
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

Submission status: Time Limit Exceeded

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

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)
if len(c) <= k:
    print(0)
else:
    sum = 0
    for i in range(k, len(c)):
        sum += c.most_common()[i][1]
    print(sum)

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)
    

if len(c) <= k:
    print(0)
else:
    sum = 0
    for i in range(k, len(c)):
        sum += c.most_common()[i][1]
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    # 整数 1 つ
    # n = int(input())
    # 整数複数個
    n, k = list(map(int, input().split()))
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    A = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    # A = [list(map(int, input().split())) for i in range(H)]

    Unique = list(set(A))
    Dic = {x:A.count(x) for x in set(A)}

    if len(set(A)) <= k:
        print(0)
    else:
        Dicsort = sorted(Dic.items(), key=lambda x: x[1])
        cnt = 0
        for i in range(len(set(A)) - k):
            cnt += Dicsort[i][1]
        print(cnt)
resolve()

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
d=[0]*N
ans=0
for i in range(N):
    d[a[i]-1]+=1
d.sort(reverse=True)
while len(d)>K:
    if i!=0:
        ans += d.pop(K)
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
d=[0]*N
ans=0
for i in range(N):
    d[a[i]-1]+=1
d.sort(reverse=True)
while len(d)>K:
    if i!=0:
        ans += d[K]
        d.pop(K)
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    # 整数 1 つ
    # n = int(input())
    # 整数複数個
    n, k = list(map(int, input().split()))
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    A = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    # A = [list(map(int, input().split())) for i in range(H)]

    Unique = list(set(A))
    Dic = {x:A.count(x) for x in set(A)}

    if len(set(A)) <= k:
        print(0)
    else:
        cnt = 0
        for i in range(len(set(A)) - k):
            cnt += min(Dic.values())
            del Dic[min(Dic, key=Dic.get)]
        print(cnt)
resolve()

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
d=[0]*N
ans=0
for i in range(N):
    d[a[i]-1]+=1
d.sort()
while len(d)>K:
    ans += d[0]
    d.pop(0)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    # 整数 1 つ
    # n = int(input())
    # 整数複数個
    n, k = list(map(int, input().split()))
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    A = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    # A = [list(map(int, input().split())) for i in range(H)]

    Unique = list(set(A))
    Dic = {}
    for i in range(len(Unique)):
        Dic[Unique[i]] = A.count(Unique[i])

    numU = len(Unique)
    cnt = 0
    while numU > k:
        cnt += min(Dic.values())
        del Dic[min(Dic, key=Dic.get)]
        numU -= 1

    print(cnt)
resolve()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
if len(list(set(a)))<=k:
  print(0)
else:
  l=[0]*n
  for i in range(n):
    l[a[i]-1]+=1
  l.sort()
  ans=0
  cnt=0
  i=0
  while cnt<(len(list(set(a)))-k):
    if l[i]!=0:
      ans+=l[i]
      cnt+=1
      i+=1
    else:
      i+=1
  print(ans)

Submission status: Time Limit Exceeded

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

cnt = [0]*n
for i in range(1, n+1):
    cnt[i-1] = al.count(i)
cnt.sort()
cnt = cnt[cnt.count(0):]

print(sum(cnt[:-k]))

Submission status: Time Limit Exceeded

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

if len(al_s) <= k:
    print(0)
else:
    cnt_ball = [0]*len(al_s)
    for i in range(len(al_s)):
        cnt_ball[i] = al.count(al_s[i])
    cnt_ball.sort()
    print(sum(cnt_ball[:len(al_s)-k]))

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.append(a.count(i))
c.sort(reverse=True)
#if len(c) == k:
  #print(0)
#else:
print(sum(c[k:]))

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(set(a))
c = []
for i in b:
  c.append(a.count(i))
c.sort(reverse=True)
#if len(c) == k:
  #print(0)
#else:
print(sum(c[k:]))

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(set(a))
c = []
for i in b:
  c.append(a.count(i))
c.sort(reverse=True)
if len(c) == k:
  print(0)
else:
  print(sum(c[k:]))

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
Are = list(set(A))
if len(Are) <= K:
  print(0)
  exit()
else:
  B = []
  for i in range(len(Are)):
    B.append(A.count(Are[i]))
  B.sort()
  for i in range(len(B)):
    if i+1 >= len(Are)-K:
      print(sum(B[:i+1]))
      break

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)
if len(c) <= K:
    print(0)
else:
    print(sum([c.most_common()[-i][1] for i in range(1,len(c)-K+1)]))

Submission status: Time Limit Exceeded

Solution code:

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

set_l = set(l)
cnt = k - len(set_l)
lst = []
for j in set_l:
    lst += [l.count(j)]
lst = sorted(lst)
print(sum(sorted(lst)[:-k]))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
dic = {}
for i in range(n):
    if a[i] not in dic.keys():
        dic[a[i]] = 1
    else:
        dic[a[i]] += 1
cnt = 0
d = sorted(dic.values())
for i, j in zip(range(len(dic)-k), d):
    cnt += j
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections as col

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

L= len(set(A))
A = col.Counter(A)
ans = 0

if L <= K:
    print(0)
    
else:
    for i in range(1,L-K+1):
        ans += A.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()))

C = list(set(A))

B = len(C) 

x = B - K

ans = 0

figure_list = [0] * B

if x<=0:
    print('0')
else:
    for i in range(B):
        stock = int(C[i])
        counter = 0
        for ii in range(N):
            if stock == A[ii]:
                counter += 1
        figure_list[i] = counter

    figure_list = sorted(figure_list)

    for i in range(B-K):
        ans += figure_list[i]

    print(ans)

Submission status: Time Limit Exceeded

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

C = list(set(A))

B = len(C)

x = B - K #種類の差

ans = 0

figure_list = [0] * B

if x<=0:
    print('0')
else:
    for i in range(B):
        stock = int(C[i])
        counter = 0
        for ii in range(N):
            if stock == A[ii]:
                counter += 1
        figure_list[i] = counter

    figure_list = sorted(figure_list)

    for i in range(B-K):
        ans += figure_list[i]

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k, *a = map(int, open(0).read().split())
print(n - sum(sorted([a.count(i) for i in range(n)])[:-k-1:-1]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
 
result,K, *a = map(int ,sys.stdin.read().split())
c=Counter(a)
if(len(c)>K):
  for i in range(K):
      result -= c.most_common(K)[i][1]
  print(result)
else :
  print(0)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

result,K = map(int , input().split())
c=Counter(list(input().split()))
if(len(c)>K):
  for i in range(K):
      result -= c.most_common(K)[i][1]
  print(result)
else :
  print(0)

Submission status: Time Limit Exceeded

Solution code:
import collections

result,K = map(int , input().split())
c=collections.Counter(list(input().split()))
if(len(c)>K):
  for i in range(K):
      result -= c.most_common(K)[i][1]
  print(result)
else :
  print(0)

Submission status: Time Limit Exceeded

Solution code:
import collections

N,K = map(int , input().split())
c=collections.Counter(list(input().split()))
result = N
if(len(c)>K):
  for i in range(K):
      result -= c.most_common(K)[i][1]
else :
  result = 0
print(result)

Submission status: Time Limit Exceeded

Solution code:
import collections

N,K = map(int , input().split())
c=collections.Counter(list(input().split()))
result = N
if(len(c)>K):
  for i in range(K):
      result = result - c.most_common(K)[i][1]
else :
  result = 0
print(result)

Submission status: Time Limit Exceeded

Solution code:
def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
    n,k = readInts()
    A = readInts()

    setA = list(set(A))
    if len(setA) <= k:
        print(0)
        exit()

    sLen = len(setA)
    # ここを何とかしよう
    kosu = [0] * sLen
    i = 0
    while i < sLen:
        kosu[i] = A.count(setA[i])
        i += 1
    #print(*kosu)
    # -----------------
    kosu = sorted(kosu)
    ans = 0
    #print(*ALL)
    for i in range(sLen-k):
        ans += kosu[i]
    print(ans)
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
    n,k = readInts()
    A = readInts()

    setA = list(set(A))
    if len(setA) <= k:
        print(0)
        exit()

    ALL = []
    for i in setA:
        ALL.append(A.count(i))
    ALL = sorted(ALL)
    ans = 0
    sLen = len(setA)
    #print(*ALL)
    for i in range(sLen-k):
        ans += ALL[i]
    print(ans)
if __name__ == '__main__':
  main()