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: Wrong Answer

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


def test(n, k, a):
    count = 0
    dict = {}
    for i in range(n):
        a[i] = int(a[i])
        if a[i] in dict:
            dict[a[i]] += 1
        else:
            dict[a[i]] = 1


    if len(dict) < k:
        return k - len(dict)
    elif len(dict) > k:
        num = 0
        for ke, v in sorted(dict.items(), key=lambda x:x[1]):
            if num < (len(dict) - k):
                count += v
                num += 1
            else:
                return count
    else:
        return 0

if __name__ == "__main__":
    nk = input().split(" ")
    a = input().split(" ")
    print(test(int(nk[0]), int(nk[1]), a))

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K = list(map(int,input().split()))
listA = Counter(input())
del listA[" "]
# print(listA)

listA = list(listA.values())
listA.sort()

num = 0
# print(len(listA),K)
if len(listA) <= K:
    num = 0
else:
    while len(listA) > K:
        print(listA)
        tmp = listA.pop(0)
        listA.sort()
        listA[0] += tmp
        num += tmp
        listA.sort()


print(num)

Submission status: Wrong Answer

Solution code:
import bisect
n,k=map(int,input().split())
li=sorted(list(map(int,input().split())))
keys=set(li)
if len(keys) <= k:
    print(0)
dic={}
for key in keys:
    dic[key]=li.count(key)
print(sum(sorted(list(dic.values()),reverse=True)[k:]))

Submission status: Wrong Answer

Solution code:
from collections import Counter

N, K = map(int, input().split())
A = list(map(int, input().split()))
counter = dict(Counter(A))
counter_list = list(counter.values())
counter_list.sort()

ans = 0


z = len(counter_list)
for c in counter_list:
    if z > K:
        z -= c
        ans += c
    else:
        print(ans)
        break

Submission status: Wrong Answer

Solution code:
nm = input().split()
arr = list(map(int, input().split()))
n= nm[0]
k= nm[1]
c = []
b = 0
s = 0
for i in set(arr):
    c.append(arr.count(i))
c = sorted(c)
#print(c)

for i in c:
    if int(k) < len(c) :
        b += i
        del c[0]
    else:
        break
print(b)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

c=Counter(input().split())

l=len(c)

if(l<=k):
    print(0)
else:
    print(sum(v for key,v in c.most_common()[l-k+1:]))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
counter = []
x = 0
count = 1
while x < n-1:
	for i in range(n-x-1):
		if a[x] == a[x+1+i]:
			count += 1
		else:
			break
	counter.append(count)
	x = x+1+i
	count = 1
if x == n-1:
	counter.append(1)
counter.sort()

y = len(counter)
l = y
answer = 0
for i in range(y):
	if l > k:
		answer += counter[i]
		l = l - 1
	else:
		break
print(answer)

Submission status: Wrong Answer

Solution code:
Input = map(int,raw_input().split(' '))
N = Input[0]
K = Input[1]
A = map(int,raw_input().split(' '))
#A = sorted(A)

count = []
for n in range(N):
	count.append(A.count(n))

count = sorted(count)
zeroCount = count.count(0)
rslt = 0
if zeroCount > N - K :
	print rslt
else:
	while count.count(0) != N - K:
		count[zeroCount] -= 1
		if count[zeroCount] == 0:
			zeroCount += 1
		rslt += 1

	print rslt

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K = list(map(int,input().split()))
listA = Counter(input())
del listA[" "]
# print(listA)

listA = list(listA.values())
listA.sort()

num = 0
# print(len(listA),K)
if len(listA) <= K:
    num = 0
else:
    count = len(listA) - K
    for _ in range(count):
        num += listA.pop(0)
print(num)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

c=Counter(input().split())

l=len(c)

print(sum(v for key,v in c.most_common()[l-k+1:]))

Submission status: Wrong Answer

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

t=[float('inf')]*(N+1)
prev=A[0]
i=0
for a in A:
  if a==prev:
    t[i]+=1
  else:
    i+=1
    t[i]=1
  prev=a

d=len(set(A))
if d<=K:
  print 0
else:
  t.sort()
  s=0
  for i in range(d-K):
    s+=t[i]
  print s

Submission status: Wrong Answer

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

for i in range(N):
    if A[i]>K:
        count=count+1

print(count)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
list_min = [200001] * (N+1)
set_A = set(A)
for a in set_A:
    list_min[a] = A.count(a)

target = len(set_A)
while True:
    if target <= K:
        break
    minv = list_min.index(min(list_min))
    tmp = list_min[minv]
    ans += tmp
    target -= tmp
    list_min[minv] = 200001
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
nums = list(map(int, input().split()))
def judge(n, k, nums):
  original = nums
  only = set(nums)
  diff = len(only) - k
  count = []
  ans = 0

  if len(only) <= k:
    return 0

  for i in only:
    count.append([original.count(i), i])
  count.sort()
  print(count)
  for i in range(diff):
    ans += count[i][0]
  
  return ans

print(judge(n, k, nums))

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, K = list(map(int,input().split()))
listA = Counter(input())
# sorted(listA.items(), key=lambda x: x[1])
del listA[" "]
# print(listA)
listA = list(listA.values())
# print(listA)
listA.sort()
# print(listA)

num = 0
# print(len(listA),K)
if len(listA) <= K:
    num = 0
else:
    while len(listA) > K:
        tmp = listA.pop(0)
        listA[len(listA)-1] += tmp
        num += tmp
print(num)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import bisect
n,k=map(int,input().split())
li=sorted(list(map(int,input().split())))
dic={}
#print(li)
while len(li)!=0:
    ind=bisect.bisect_right(li,li[0])
    dic[li[0]]=ind#かずをきろく
    li=li[ind:]
    #print(li)
print(sum(sorted(list(dic.values())[k:])))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python

import sys

def main(argv):
    line = sys.stdin.readline()
    while line:
        N, K = line.rstrip("\n").split(" ", 2)
        N = int(N)
        K = int(K)
        A = map(
          lambda a: int(a), sys.stdin.readline().rstrip("\n").split(" "))
        
        histogram = {}
        for a in A:
            if a not in histogram:
                histogram[a] = 0
            histogram[a] += 1

        if K > len(histogram):
            diff = K - len(histogram)
            print diff
        elif K < len(histogram):
            diff = len(histogram) - K
            print sum(sorted(histogram.values())[0:diff])
        else:
            print 0

        line = sys.stdin.readline()

if __name__ == "__main__":
    main(sys.argv)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import bisect
n,k=map(int,input().split())
li=sorted(list(map(int,input().split())))
dic={}
#print(li)
while len(li)!=0:
    ind=bisect.bisect_right(li,li[0])
    dic[li[0]]=ind#かずをきろく
    li=li[ind:]
    #print(li)
print(sum(list(dic.values())[k:]))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
list_min = [200001] * (N+1)
for a in set(A):
    list_min[a] = A.count(a)

target = len(set(A))
while True:
    if target <= K:
        break
    minv = list_min.index(min(list_min))
    ans += A.count(minv)
    list_min[minv] = 200001
    target -= A.count(minv)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import bisect
n,k=map(int,input().split())
li=sorted(list(map(int,input().split())))
dic={}
#print(li)
while len(li)!=0:
    ind=bisect.bisect_right(li,li[0])
    dic[li[0]]=ind#かずをきろく
    li=li[ind:]
    #print(li)
if(len(list(dic.values())) <= k):
    print(0)
    
else:
    print(sum(list(dic.values())[k:]))

Submission status: Wrong Answer

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

N, K = sys.stdin.readline().split()
K = int(K)
line = sys.stdin.readline()
A = map(int, line.strip().split())
D = Counter(A)
diff = len(D) - K
print(sum( i[1] for i in D.most_common()[-1*diff:]) )

Submission status: Wrong Answer

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

t=[float('inf')]*(N+1)
prev=A[0]
i=0
for a in A:
  if a==prev:
    t[i]+=1
  else:
    i+=1
    t[i]=1
  prev=a

d=len(set(A))
t.sort()
s=0
for i in range(d-K):
  s+=t[i]
print s

Submission status: Time Limit Exceeded

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

ans = []
for i in set(a):
  ans.append(a.count(i))

ans = sorted(ans, reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
a_list = list(map(int, input().split()))
counter_list = []
for i in range(1,N+1):
    if a_list.count(i) > 0:
        counter_list.append(a_list.count(i))

counter_list.sort()

count = 0
for i in range(len(counter_list) - K):
    count += counter_list[i]

print(count)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort()
counter_list = []
for i in range(1,N+1):
    if a_list.count(i) > 0:
        counter_list.append(a_list.count(i))

counter_list.sort()

count = 0
for i in range(len(counter_list) - K):
    count += counter_list[i]

print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(N):
  nums[A[i]-1] += 1

nums.sort()

while True:
  if nums[0] == 0:
    del nums[0]
  else:
    break

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if L <= K:
  print(0)
else:
  for i in range(1,N+1):
    count.append(A.count(i))
  count.sort(reverse=True)
  print(N-sum(count[:K]))

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A_set = set(A)
L = len(A_set)
count = []

if L <= K:
  print(0)
else:
  for i in range(L):
    count.append(A.count(list(A_set)[i]))
  count.sort()
  
  print(sum(count[:len(count)-K]))

Submission status: Time Limit Exceeded

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

Asset = set(As)
sclist = []
for x in Asset:
	cnt = As.count(x)
	sclist.append(cnt)
sclist.sort(reverse=True)
print(sum(sclist[K:]))

Submission status: Time Limit Exceeded

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

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

count = [0] * len(set(A))
labels = set(A)
for val in A:
    for i, label in enumerate(labels):
        if label==val:
            count[i] += 1

ans = 0
diff = len(set(A)) - K
if diff > 0:
    count_sorted = sorted(count)
    for i in range(diff):
        ans += count_sorted[i]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
A_unq=set(A)
b=[]
for i in A_unq:
       b.append(A.count(i))
b.sort()
print('0' if k>=len(A_unq) else sum(b[:len(A_unq)-k]))

Submission status: Time Limit Exceeded

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

a_set = list(set(a))
if len(a_set) < K:
    print(0)
    exit()

num_dict = {}
for num in a_set:
    num_dict[num] = a.count(num)
num_dict = sorted(num_dict.items(), key = lambda x: x[1])

diff = len(a_set) - K
ans = 0
for i in range(diff):
    ans += num_dict[i][1]
print(ans)

Submission status: Time Limit Exceeded

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

a.sort()

times=[0]*(n+1)
for i in a:
    times[i]+=1
times.sort()

while True:
    if times[0]==0:
        del times[0]
    else:
        break
ans=0
if len(times)>k:
    for i in range(len(times)-k):
        ans+=times[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K = (int(x) for x in input().split())
A = [int(x) for x in input().split()]
cnt = [A.count(a) for a in set(A)]
cnt.sort()
vrt = len(cnt)
print(sum(cnt[:vrt-K]) if vrt > K else 0)

Submission status: Time Limit Exceeded

Solution code:
import collections
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=0
c=collections.Counter(a)
if len(c)<k:
    print(ans)
    exit()

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = {}
for i in range(1,n+1):
    s[i] = a.count(i)
for i in range(1,n+1):
    if s[i] == 0:
        s.pop(i)
    else:
        continue
cnt = 0
t = sorted(s.items(), key=lambda x:x[1])

if len(s) > k:
    y = len(s) - k
    for j in range(y):
        cnt = cnt + t[j][1]
    print(cnt)
else:
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = {}
for i in range(n):
    if a[i] in s.keys():
        s[a[i]] = s[a[i]] + 1
    else:
        s[a[i]] = 1
cnt = 0
while len(s) > k:
    cnt = cnt + min(s.values())
    s.pop(min(s, key=s.get))
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
s=list(map(int,input().split()))
t=list(set(s))
c=[s.count(k) for k in t]
c.sort()

print(sum(c[:-K]))

Submission status: Time Limit Exceeded

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

X = set(A)
L = len(X)

S= []
for x in X:
    n = A.count(x)
    S.append(n)

S = sorted(S)

ans = sum(S[:max(0,L - K)])

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
 
a_spec = set(a) #aの種類
 
b = []
for i in a_spec:
  b.append(a.count(i))
 
b.sort()
 
d = len(a_spec) - k #減らす数字の種類
print(sum(b[0:d]))

Submission status: Time Limit Exceeded

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

a_spec = set(a) #aの種類

b = []
for i in a_spec:
  b.append(a.count(i))

b.sort()

d = len(a_spec) - k #減らす数字の種類
print(sum(b[0:d]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

balls, typ = map(int, input().split())
nums = list(map(int,input().split()))

dic = sorted(list({i:nums.count(i) for i in nums}.items()),key = lambda x : x[1], reverse = True)
ar_dic = np.array(dic)
print(np.sum(ar_dic[typ:],0)[1])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter as C

_, k = map(int, input().split())
a = [int(i) for i in input().split()]

ans = 0
c = sorted(C(a).values())
while k < len(c):
    ans += c.pop(0)
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = [int(an) for an in input().split()]
ans = 0
set_a = set(a)
num_of_types = len(set_a)
if num_of_types > k:
  ans = sum(sorted([a.count(num) for num in set_a])[:num_of_types - k])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = [int(an) for an in input().split()]
ans = 0
set_a = set(a)
num_of_types = len(set_a)
if num_of_types > k:
  ans = sum(sorted([a.count(num) for num in set(a)])[:num_of_types - k])
print(ans)

Submission status: Time Limit Exceeded

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

B = {}
for i in range(N):
    if A[i] not in list(B.keys()):
        B[A[i]] = A.count(A[i])

num = len(B)
C = list(B.values())
C.sort()
ans = 0
if num>K:
    ans = sum(C[0:num-K])
print(ans)

Submission status: Time Limit Exceeded

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

B = {A[0]:A.count(A[0])}
for i in range(1,len(A)):
    B[A[i]] = A.count(A[i])

num = len(B)
C = list(B.values())
C.sort()
ans = 0
if num>K:
    ans = sum(C[0:num-K])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]

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

d = [0] * (n+1)

for i in range(n):
    d[a[i]] += 1

d.sort()
cnt = 0
for i in range(len(d)):
    if d[i] == 0:
        cnt+=1
for i in range(cnt):
    del d[0]

if (len(d) -k > 0):
    num = len(d) - k
    print(sum(d[0:num]))
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
l = list(map(int, input().split()))
def counter(l):
    count = {}
    for i in l:
        count[i] = l.count(i)
    return sorted(count.values())

freq = counter(l)
vf = len(freq) - k if len(freq)> k else 0
print(sum(freq[:vf]))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right

N,K=list(map(int, input().split()))
A=list(map(int, input().split()))
A.sort()
S=set(A)
S=list(S)
B=[]

def bisectsearch_right(L, a):
    i = bisect_right(L, a)
    return(i)

for _ in S:
  a=A[0]
  j = bisect_right(A,a)
  B+=[j]
  del A[:j]
B.sort()
# print(B)
C=B[:len(S)-K]
print(sum(C))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right
import sys
input = sys.stdin.readline

N,K=list(map(int, input().split()))
A=list(map(int, input().split()))
A.sort()
S=set(A)
S=list(S)
B=[]

def bisectsearch_right(L, a):
    i = bisect_right(L, a)
    return(i)

for i in S:
  a=A[0]
  j = bisect_right(A,a)
  B+=[j]
  del A[:j]
B.sort()
# print(B)
C=B[:len(S)-K]
print(sum(C))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right

N,K=list(map(int, input().split()))
A=list(map(int, input().split()))
A.sort()
S=set(A)
S=list(S)
B=[]

def bisectsearch_right(L, a):
    i = bisect_right(L, a)
    return(i)

for i in S:
  a=A[0]
  j = bisect_right(A,a)
  B+=[j]
  del A[:j]
B.sort()
# print(B)
C=B[:len(S)-K]
print(sum(C))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right

N,K=list(map(int, input().split()))
A=list(map(int, input().split()))
A.sort()
S=set(A)
S=list(S)
B=[]

def bisectsearch_right(L, a):
    i = bisect_right(L, a)
    return(i)

for i in S:
  a=A[0]
  j = bisect_right(A,a)
  B.append(j)
  del A[:j]
B.sort()
# print(B)
C=B[:len(S)-K]
print(sum(C))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right

N,K=list(map(int, input().split()))
A=list(map(int, input().split()))
A.sort()
S=set(A)
S=list(S)
B=[]

def bisectsearch_right(L, a):
    i = bisect_right(L, a)
    return(i)

for i in S:
  a=A[0]
  j = bisect_right(A,a)
  B.append(j)
  A=A[j:]
B.sort()
# print(B)
C=B[:len(S)-K]
print(sum(C))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
l=list((map(int,input().split())))
s=list(set(l))
m=len(s)

x=[]
if m<=k:
    print(0)
else:
    for i in range(m):
        t=l.count(s[i])
        x.append(int(t))
        
    x.sort()
    print(sum(x[:m-k]))

Submission status: Time Limit Exceeded

Solution code:
N,K=list(map(int, input().split()))
A=list(map(int, input().split()))
A.sort()
S=set(A)
B=[]
for i in S:
  a=A[0]
  b=len(A)
  for k in range(b):
    if A[k]!=a:
      j=k
      break
  else:
    j=b
  B.append(j)
  A=A[j:]
B.sort()
# print(B)
C=B[:len(S)-K]
print(sum(C))

Submission status: Time Limit Exceeded

Solution code:
N,K=list(map(int, input().split()))
A=list(map(int, input().split()))
A.sort()
S=set(A)
B=[]
for i in S:
  j=0
  while A[j]==A[0]:
    j+=1
    if j==len(A):
      break
  B.append(j)
  A=A[j:]
B.sort()
# print(B)
C=B[:len(S)-K]
print(sum(C))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=[int(i)for i in input().split()]
d={i:a.count(i)for i in a}
t=sorted(d.values())
ans=0
for i in range(len(t)-k):
    ans+=t[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=[int(i)for i in input().split()]
d={i:a.count(i)for i in a}
t=sorted(d.values(),reverse=True)
ans=0
while len(t)>k:
    ans+=t.pop(-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=[int(i)for i in input().split()]
d={i:a.count(i)for i in a}
t=list(reversed(sorted(list(d.values()))))
ans=0
while len(t)>k:
    ans+=t.pop(-1)
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()))
syurui = len(set(a))
#print(syurui)
c = Counter(a)
ans = 0
for i in range(1, syurui-k + 1):
    ans += c.most_common()[-i][1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
r,k = map(int,input().split())
b = [i for i in map(int,input().split())]
s = set(b)

if len(s) <= k:
    print(0)
else:
    n = [b.count(i) for i in s]
    n.sort()
    ans = 0
    for i in range(len(s)-k):
        ans += n[i]
    print(ans)

Submission status: Time Limit Exceeded

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

num_counts = dict()
if len(set(num_lists)) <= k:
  ans = 0
else:
  for i in set(num_lists):
    num_counts[i] = num_lists.count(i)
    numbers = list(num_counts.values())
    numbers.sort()
    ans = 0
  for i in range(len(set(num_lists)) - k):
    ans += numbers[i]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

cnt = 0

for i in range(N):
    if len(A) > K:
        min_k = min(A, key=A.get)
        cnt += A[min_k]
        A.pop(min_k)
    else:
        break

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

cnt = 0

while len(A) > K:
    min_k = min(A, key=A.get)
    cnt += A[min_k]
    A.pop(min_k)

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

cnt = 0

while len(A) > K:
    min_k = min(A, key=A.get)
    cnt += A[min_k]
    A.pop(min_k)

print(cnt)

Submission status: Time Limit Exceeded

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

if len(set(num_lists)) <= k:
  ans = 0
else:
  num_counts = {}
  for i in set(num_lists):
    num_counts[i] = num_lists.count(i)
  
  numbers = list(num_counts.values())
  numbers.sort()
  ans = 0
  for i in range(len(set(num_lists))-k):
    ans += numbers[i]
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))
x=len(b)-k
b=list(map(lambda x:a.count(x),b))
b.sort()
if x<=0:
  print(0)
else:
  print(sum(b[:x]))

Submission status: Time Limit Exceeded

Solution code:
N_K = input().split()
A = list(input().split())

list_map = {}
chg_num = 0

for index in A:
    if index in list_map:
        list_map[index] += 1
    else:
        list_map[index] = 1

while (len(list_map) > int(N_K[1])):
    chg_num += min(list_map.values())
    list_map.pop(min(list_map.items(), key = lambda x:x[1])[0])
print(chg_num)

Submission status: Time Limit Exceeded

Solution code:
N_K = input().split()
A = list(input().split())


list_map = {}
chg_num = 0

for index in A:
    if index in list_map:
        list_map[index] += 1
    else:
        list_map[index] = 1

if (len(list_map) > int(N_K[1])):
    for i in range(len(list_map) - int(N_K[1])):
        chg_num += min(list_map.values())
        list_map.pop(min(list_map.items(), key = lambda x:x[1])[0])
print(chg_num)

Submission status: Time Limit Exceeded

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

num_counts = {}
if len(set(num_lists)) <= k:
  ans = 0
else:
  for i in set(num_lists):
    num_counts[i] = num_lists.count(i)
  numbers = list(num_counts.values())
  numbers.sort()
  ans = 0
  for i in range(len(set(num_lists)) - k):
    ans += numbers[i]

print(ans)

Submission status: Time Limit Exceeded

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

num_counts = {}
if len(set(num_lists)) <= k:
  ans = 0
else:
  for i in set(num_lists):
    num_counts[i] = num_lists.count(i)
    numbers = list(num_counts.values())
    numbers.sort()
    ans = 0
  for i in range(len(set(num_lists)) - k):
    ans += numbers[i]

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
dictA = {}
for i in a:
  if not(i in dictA.keys()):
    dictA[i] = a.count(i)

sdict = sorted(dictA.values())
ans = 0
for i in range(len(sdict) - k):
  ans += sdict[i]
print(ans)

Submission status: Time Limit Exceeded

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

num_list = list(map(int,input().split()))
num_list.sort(reverse=True)
num_type = []
num_lis = []
for i in num_list:
  if num_type.count(i) == 0:
    num_type.append(i)

for i in num_type:
  num_lis.append(num_list.count(i))
num_lis.sort(reverse=True)

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

Submission status: Time Limit Exceeded

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

num_list = list(map(int,input().split()))
num_list.sort(reverse=True)
num_lis = []
for i in range(len(num_list)):
  if num_list.count(i+1) == 0:
    continue
  num_lis.append(num_list.count(i+1))

num_lis.sort(reverse=True)


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

Submission status: Time Limit Exceeded

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

num_list = list(map(int,input().split()))
num_list.sort(reverse=True)
num_lis = []
for i in range(max(num_list)):
  if num_list.count(i+1) == 0:
    continue
  num_lis.append(num_list.count(i+1))

num_lis.sort(reverse=True)


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

Submission status: Time Limit Exceeded

Solution code:
nums=list(map(int,input().split()))
n=nums[0]
k=nums[1]
A=list(map(int,input().split()))
cnt=0
counter={}
for i in range(n):
    counter[A[i]]=0
    
for i in range(n):
    counter[A[i]]+=1
    
def get_keys_from_value(d, val):
    return [k for k, v in d.items() if v == val]

l=len(counter)

while l>k:
    key=get_keys_from_value(counter,min(counter.values()))
    key=key[0]
    value=min(counter.values())
    cnt+=value
    l-=1
    del counter[key]
    
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N,K = map(int, input().split())
A = Counter(list(map(int, input().split())))
B = A.items()
k = len(B) - K
if k <= 0:
    print(0)
else:
    print(sum([sorted(B,key=lambda x:x[1])[i][1] for i in range(k)]))

Submission status: Time Limit Exceeded

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

import io
import sys
import math

def solve(n, k, a_lst):
    # implement process
    a_set = set(a_lst)
    ak = len(a_set)
    diff = ak - k
    if diff < 1:
        return 0
    
    min_change = float("inf")
    cnt_lst = []
    for a in a_set:
        cnt_lst.append(a_lst.count(a))
    cnt_lst.sort()
    return sum(cnt_lst[0:diff])

def main():
    # input
    n, k = map(int,input().split())
    a_lst = list(map(int,input().split()))

    # process
    ans = str( solve(n, k, a_lst) )
    
    # output
    print(ans)
    return ans
        
### DEBUG I/O ###
_DEB = 0   # 1:ON / 0:OFF

_INPUT = """\
10 3
5 1 3 2 4 1 1 2 3 4
"""
_EXPECTED = """\
0
"""

def logd(str):
    """usage:
    if _DEB: logd(f"{str}")
    """
    if _DEB: print(f"[deb] {str}")

### MAIN ###
if __name__ == "__main__":
    if _DEB:
        sys.stdin = io.StringIO(_INPUT)
        print("!! Debug Mode !!")

    ans = main()

    if _DEB:
        print()
        if _EXPECTED.strip() == ans.strip(): print("!! Success !!")
        else: print(f"!! Failed... !!\nANSWER:   {ans}\nExpected: {_EXPECTED}")

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
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)