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 = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
dict = {}
count = 0

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

diff = len(dict) - K

for i in range(diff):
    maxKey = max([(v, k) for k, v in dict.items()])[1]
    minKey = min([(v, k) for k, v in dict.items()])[1]
    m = dict.pop(minKey)
    dict[maxKey] += m
    count += m
print(count)

Submission status: Time Limit Exceeded

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

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

diff = len(dict) - K

if diff <= 0:
    print(count)
else:
    for i in range(diff):
        maxKey = max([(v, k) for k, v in dict.items()])[1]
        minKey = min([(v, k) for k, v in dict.items()])[1]
        m = dict.pop(minKey)
        dict[maxKey] += m
        count += m
    print(count)

Submission status: Time Limit Exceeded

Solution code:

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

    A = sorted(A)
    kindNum = len(list(set(A)))
    delKindNum = kindNum - K
    if delKindNum < 1:
        print("0")
        return

    countNumList = [0 for i in range(200001)]
    for i in A:
        countNumList[i] = countNumList[i] +1
    while 0 in countNumList:
        countNumList.remove(0)


    cnt = 0
    for i in range(delKindNum):
        cnt = cnt + min(countNumList)
        countNumList.pop(countNumList.index(min(countNumList)))
    print(str(cnt))


###############
main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, k = list(map(int, input().split()))
b = list(map(int, input().split()))
a = [0] * n
m = set(b)
ans = 0
for l in range(n):
    a[b[l]-1] += 1
c = np.array([item for item in a if item != 0])
for j in range(len(m)-k):
    try:
        ans += np.min(c)
    except:
        pass
    d = np.where(c == np.min(c))
    c = np.delete(c,d[0][0], 0)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = list(map(int, input().split()))
b = list(map(int, input().split()))
a = [0 for i in range(n)]
m = set(b)
ans = 0
for l in range(n):
    a[b[l]-1] += 1
c = [item for item in a if item != 0]
for j in range(len(m)-k):
    ans += min(c)
    c.pop(c.index(min(c)))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
a = list(map(int,input().split()))
N = a[0]
K = a[1]
b = list(map(int,input().split()))
li = list(set(b))
c = np.array(b)
d = np.array([])
h = len(li) -K
Num = 0
if K >= len(li):
  print(0)
  exit()
for i in li :
  ind = np.take(c,np.where( c== i),axis = 0)
  d = np.append(d,ind.shape[1])
d = np.sort(d)
for i in range(h):
  Num += d[i]
print(int(Num))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
a = list(map(int,input().split()))
N = a[0]
K = a[1]
b = list(map(int,input().split()))
li = list(set(b))
c = np.array(b)
d = np.array([])
h = len(li) -K
Num = 0
if K >= len(li):
  print(0)
  exit()
for i in li :
  ind = c[np.where(c== i)]
  d = np.append(d,ind.shape)
d = np.sort(d)
print(int(np.sum(d[:h])))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
a = list(map(int,input().split()))
N = a[0]
K = a[1]
b = list(map(int,input().split()))
li = list(set(b))
c = np.array(b)
d = np.array([])
h = len(li) -K
Num = 0
if K >= len(li):
  print(0)
  exit()
for i in li :
  ind = c[np.where(c== i)]
  d = np.append(d,ind.shape)
d = np.sort(d)
for i in range(h):
  Num += d[i]
print(int(Num))

Submission status: Time Limit Exceeded

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

A_count = dict()
for i in set(A):
	A_count.update({i:len([j for j in A if j == i])})

A_sort = sorted(A_count.values())[::-1]
print(sum(A_sort[K:]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N, K = map(int, raw_input().split())
A = map(int, raw_input().split())

cnt = Counter()
for num in A:
    cnt[str(num)] += 1

values = sorted(cnt.values())
ans = 0
for x in xrange(len(values) - K):
    ans += values.pop(0)

print ans

Submission status: Time Limit Exceeded

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

if k == 20000:
    print 0
else:
    dic = {}
    for i in a:
        if i not in dic:
            dic[i] = 1
        else:
            dic[i] += 1
    
    ans = 0
    keys = dic.keys()
    
    while len(keys) > k:
        minKey = min(dic.items(), key=(lambda x:x[1]))[0]
        ans += dic[minKey]
        del dic[minKey]
        keys = dic.keys()
    
    print ans

Submission status: Time Limit Exceeded

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

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

ans = 0
keys = dic.keys()

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

print ans

Submission status: Time Limit Exceeded

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

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

ans = 0
keys = dic.keys()

while len(keys) > k:

    minKey = min(dic.items(), key=(lambda x:x[1]))[0]
    dic[minKey] -= 1
    ans += 1
    if dic[minKey] == 0:
        del dic[minKey]
    keys = dic.keys()


print ans

Submission status: Time Limit Exceeded

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

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

ans = 0
keys = dic.keys()

while len(keys) > k:
    minKey = keys[0]
    minValue = dic[keys[0]]
    for forKey, forValue in dic.items():
        if forValue < minValue:
            minValue, minKey = forValue, forKey

    dic[minKey] -= 1
    ans += 1
    if dic[minKey] == 0:
        del dic[minKey]
    keys = dic.keys()


print ans

Submission status: Time Limit Exceeded

Solution code:
#AtcoderABC081 #3
'''
10 3
5 1 3 2 4 1 1 2 3 4
3
'''

N,K = input().split(' ')
N,K = [int(N),int(K)]
#A = list(map(int, input().split(' ')))
A=list(int(_) for _ in input().split()) 

list=[]
cnt = 0
ans = 0

for n in range(N+1):
	if (A.count(n)) > 0:
		cnt += 1
		list.append(A.count(n))

list.sort()

if cnt > K:
	for n in range(cnt-K): ans += list[n] 

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()]
d=sorted([a.count(i) for i in range(n)])[::-1]
print(n-sum(d[: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()]
d=sorted([a.count(i) for i in set(a)])[::-1]
print(n-sum(d[: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()]
d=sorted([a.count(i) for i in set(a)])[::-1]
if len(d)-k>0: print(len(a)-sum(d[:k]))
else: print("0")

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
C = list(set(A))
B = [0 for i in range(len(C))]
for a in A:
    B[C.index(a)] += 1
print(sum(sorted(B)[:-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()]
d={i:a.count(i) for i in set(a)}
p=len(d)-k
l=[]
for i in range(p):
  t=list(d.items())[0]
  for j in d.items():
    if t[1]>j[1]:
      t=j
  l.append(t[1])
  d.pop(t[0], None)
if p>0: print(sum(l))
else: print("0")

Submission status: Time Limit Exceeded

Solution code:
n,k = [int(x) for x in input().split()]
s = {}

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


t = list(s.values())
t.sort()
a = 0

while len(t) > k:
    a += t.pop(0)

print(a)

Submission status: Time Limit Exceeded

Solution code:
#AtcoderABC081 #3
'''
10 3
5 1 3 2 4 1 1 2 3 4
3
'''

N,K = input().split(' ')
N,K = [int(N),int(K)]
#A = list(map(int, input().split(' ')))
A=list(int(_) for _ in input().split()) 

list=[]
cnt = 0
ans = 0

for n in range(N+1):
	if (A.count(n)) > 0:
		cnt += 1
		list.append(A.count(n))

list.sort()

if cnt > K:
	for n in range(cnt-K): ans += list[n] 

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()]
d={i:a.count(i) for i in set(a)}
p=len(d)-k
if p>0: print(sum((t[1] for t in sorted(d.items(), key=lambda x:x[1])[:p])))
else: print("0")

Submission status: Time Limit Exceeded

Solution code:
#AtcoderABC081 #3
'''
10 3
5 1 3 2 4 1 1 2 3 4
3
'''

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

l=[]
c = 0
a = 0

for n in range(N+1):
	if (A.count(n)) > 0:
		c += 1
		l.append(A.count(n))

l.sort()

if c > K:
	for n in range(c-K): a += l[n] 

print(a)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N, K = list(map(int,input().split()))
listA = Counter(input().split())
listA = list(listA.values())
listA.sort()
times = len(listA) - K
# print(times,len(listA),K)
num = 0

if times <= 0:
    num = 0
else:
    for _ in range(times):
        num += listA.pop(0)

print(num)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a_l = list(map(int, input().split()))
           
import sys
def g():
    m_l = [0] * (n+1)

    r = 0
    for a in a_l:
        if m_l[a] == 0: r += 1
        m_l[a] += 1

    if r <= k:
        print(0)
        sys.exit(0)

    mn = 0
    
    while True:
        mm = min(i for i in m_l if i > 0)

        for i in range(n+1):
            if mm == m_l[i]:
                mn += mm
                r -= 1
                m_l[i] = 0

                if r <= k: return mn
        
print(g())

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a_l = list(map(int, input().split())
)
import sys
def g():
    m_l = [0] * (n+1)

    r = 0
    for a in a_l:
        if m_l[a] == 0: r += 1
        m_l[a] += 1

    if r <= k:
        print(0)
        sys.exit(0)

    mn = 0
    
    while True:
        mm = min(i for i in m_l if i > 0)

        for i in range(n+1):
            if mm == m_l[i]:
                mn += mm
                r -= 1
                m_l[i] = 0

                if r <= k: return mn
        
print(g())

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(sorted([A.count(b) for b in range(1,N+1)], reverse=True))
"""
C = list(set(A))
B = [0 for i in range(len(C))]
for a in A:
    B[C.index(a)] += 1
#B = list(sorted([A.count(b) for b in set(A)], reverse=True))
B = list(sorted(B, reverse=True))
"""
print(sum(B[K:len(B)]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N, K = list(map(int,input().split()))
listA = Counter(input().split())
# print(listA)
listA = list(listA.values())
listA.sort()
# print(listA)

num = 0
# print(listA)
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: Time Limit Exceeded

Solution code:
from collections import Counter
N, K = list(map(int,input().split()))
listA = Counter(input().split())
# print(listA)
listA = list(listA.values())
listA.sort()
# print(listA)

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

print(num)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n, k = list(map(int, input().split()))
nums = list(map(int, input().split()))
count = 0

numbers = Counter(nums)
sorted_list = sorted(numbers.items(), key=lambda x: x[1], reverse=False)


while len(sorted_list) > k:
    count += sorted_list[0][1]
    sorted_list.pop(0)

print(count)

Submission status: Time Limit Exceeded

Solution code:
A,B=map(int,input().split())
L=list(map(int,input().split()))
X=[]
C=[]
S=0
for I in L:
	if I not in C:
		C.append(I)
		X.append(L.count(I))
while(len(X)>B):
	K=min(X)
	S+=K
	X.remove(K)
print(S)

Submission status: Time Limit Exceeded

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

import sys
def g():
    m_l = [0] * (n+1)

    for a in a_l:
        m_l[a] += 1

    r = 0
    for m in m_l:
        if m != 0: r+= 1


    if r <= k:
        print(0)
        sys.exit(0)

    mn = 0
    
    while True:
        mm = min(i for i in m_l if i > 0)

        for i in range(n+1):
            if mm == m_l[i]:
                mn += mm
                r -= 1
                m_l[i] = 0

                if r <= k: return mn
        
print(g())

Submission status: Time Limit Exceeded

Solution code:
import sys

DBG_FLAG = 0

def pdbg(str):
    if(DBG_FLAG == 1):
        sys.stdout.flush()
        print str

# get num
[N, K] = map(int, raw_input().split())
array = map(int, raw_input().split())

array.sort()
array.append('')
#pdbg(array)

num = 1

pre = array.pop(0)

group_sum = []

while len(array) > 0:
#    pdbg(array)
    item = array.pop(0)
#    pdbg(str(item) + ':'+ str(pre))
    if item == pre :
        num = num + 1
    else:
        group_sum.append(num)
        pre = item
        num = 1
#    pdbg(group_sum)

#pdbg('aa')
#pdbg(group_sum)

group_sum.sort()

sum_num = 0

if K > len(group_sum):
    print 0
else:
    while K > sum_num:
        sum_num = sum_num + 1
        tmp = group_sum.pop(-1)
#        pdbg('i:'+str(tmp))
#        pdbg('g:')
#        pdbg(group_sum)

    mod_num = 0

    while len(group_sum) > 0:
        mod_num = mod_num + group_sum.pop(0)
         
    print mod_num

Submission status: Time Limit Exceeded

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

    A_sc = [0] * len(A_set)
    for i in range(len(A_set)):
        A_sc[i] = A.count(A_set[i])

    A_sc.sort(reverse = True)
    print(sum(A_sc[K:]))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
C = list(set(A))
B = [0 for i in range(len(C))]
for a in A:
    B[C.index(a)] += 1
#B = list(sorted([A.count(b) for b in set(A)], reverse=True))
B = list(sorted(B, reverse=True))
print(sum(B[K:len(B)]))

Submission status: Time Limit Exceeded

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

kind = []
for i in range(n):
    a_s[i] = int(a_s[i])
    if a_s[i] in kind:
        pass
    else:
        kind.append(a_s[i])

kind_suuzi = len(kind)
a_s_suuzi = len(a_s)
now_shurui = len(kind)
if k>=kind_suuzi:
    print(0)
else:
    kind_nums = [0 for x in range(kind_suuzi)]
    for i in range(kind_suuzi):
        for ii in range(a_s_suuzi):
            if a_s[ii] == kind[i]:
                kind_nums[i] += 1
    cnt = 0
    kind_nums_sorted = sorted(range(len(kind_nums)), key=kind_nums.__getitem__)
    for i in range(1000000):
        cnt += kind_nums[kind_nums_sorted[i]]
        now_shurui -= 1
        if k == now_shurui:
            print(cnt)
            break

Submission status: Time Limit Exceeded

Solution code:
import sys

DBG_FLAG = 0

def pdbg(str):
    if(DBG_FLAG == 1):
        sys.stdout.flush()
        print str

def max_num(num):
    if(num % 2):
        return 0
    else:
        return 1 + max_num(num / 2)

# get num
[N, K] = map(int, raw_input().split())
array = map(int, raw_input().split())

array.sort()
array.append('')
pdbg(array)

num = 1

pre = array.pop(0)

group_sum = []

while len(array) > 0:
    pdbg(array)
    item = array.pop(0)
    pdbg(str(item) + ':'+ str(pre))
    if item == pre :
        num = num + 1
    else:
        group_sum.append(num)
        pre = item
        num = 1
    pdbg(group_sum)

pdbg('aa')
pdbg(group_sum)

group_sum.sort()

sum_num = 0

if K > len(group_sum):
    print 0
else:
    while K > sum_num:
        sum_num = sum_num + 1
        tmp = group_sum.pop(-1)
        pdbg('i:'+str(tmp))
        pdbg('g:')
        pdbg(group_sum)

    mod_num = 0

    while len(group_sum) > 0:
        mod_num = mod_num + group_sum.pop(0)
         
    print mod_num

Submission status: Time Limit Exceeded

Solution code:
a = input();
w = [int(x) for x in a.split()]
n = w[0]
k = w[1]
a = input();
w = [int(x) for x in a.split()]
d = dict()
j = 0;
out = 0;
key = 0
for i in w:
	if i in d:
		d[i] = d[i] + 1
	else:
		 d[i] = 1
		 j = j + 1
while j > k:
	key = min(d, key=d.get)
	out = out + d[key]
	del d[key]
	j = j - 1
print(out)

Submission status: Time Limit Exceeded

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

    A_sc = []
    for a in A_set:
        A_sc.append(A.count(a))

    A_sc.sort(reverse = True)
    print(sum(A_sc[K:]))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(sorted([A.count(b) for b in set(A)], reverse=True))
print(sum(B[K:len(B)]))

Submission status: Time Limit Exceeded

Solution code:
Input = map(int,raw_input().split(' '))
N = Input[0]
K = Input[1]
A = map(int,raw_input().split(' '))
A = sorted(A)
count = []
i = 0
while i < len(A):
	count.append(A.count(A[i]))
	i += A.count(A[i])
count = sorted(count)
dim = len(count)
rslt = 0
if dim <= K:
	print rslt
else:
	rslt += sum(count[:dim-K])
	print rslt

Submission status: Time Limit Exceeded

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

    A_sc = []
    for a in A_set:
        A_sc.append(A.count(a))

    A_sc.sort(reverse = True)
    c = 0
    for i in range(K, len(A_sc)):
        c += A_sc[i] 

    print(c)   


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
li=sorted(list(map(int,input().split())))
keys=set(li)
knum=len(keys)
if knum <= k:
    print(0)
    quit()
knum=knum-k
knuma=knum
di=[]
for key in keys:
    a=li.count(key)
    di.append(a)
    if a==1:
        knum-=1
        if knum ==0:
            print(knuma)
            quit()
print(sum(sorted(di,reverse=True)[k:]))

Submission status: Time Limit Exceeded

Solution code:
nm = input().split()
arr = [int(x) for x in input().split()]

k= int(nm[1])
c = []

d = set(arr)
for i in d:
    c.append(arr.count(i))
#c = sorted(c)

print(sum(sorted(c)[:-k]))

Submission status: Time Limit Exceeded

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

    A_sc = []
    for a in A_set:
        A_sc.append(A.count(a))

    A_sc.sort(reverse = True)
    c = 0
    for i in range(K, len(A_sc)):
        c += A_sc[i] 

    print(c)   


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

list=[]
cnt = 0
ans = 0

for n in range(N+1):
	if (A.count(n)) > 0:
		cnt += 1
		list.append(A.count(n))

list.sort()

if cnt > K:
	for n in range(cnt-K): ans += list[n] 

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

Input = map(int,raw_input().split(' '))
N = Input[0]
K = Input[1]
A = map(int,raw_input().split(' '))
A = sorted(A)
count = []
i = 0
while i < len(A):
	count.append(A.count(A[i]))
	i += A.count(A[i])
count = sorted(count)
count = np.array(count)
dim = count.shape[0]
rslt = 0
if dim <= K:
	print rslt
else:
	rslt += count[:dim-K].sum()
	print rslt

Submission status: Time Limit Exceeded

Solution code:
n, k=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
s=set(a)
d=[a.count(i) for i in s]
p=len(s)-k
d=sorted(d)
if p>0: print(sum(d[:p]))
else: print("0")

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
li=sorted(list(map(int,input().split())))
keys=set(li)
knum=len(keys)
if knum <= k:
    print(0)
    quit()

di=[]
for key in keys:
    di.append(li.count(key))
print(sum(sorted(di,reverse=True)[k:]))

Submission status: Time Limit Exceeded

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

k= int(nm[1])
c = []

d = set(arr)
for i in d:
    c.append(arr.count(i))
c = sorted(c)



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

Submission status: Time Limit Exceeded

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

kind = []
for i in range(n):
    a_s[i] = int(a_s[i])
    if a_s[i] in kind:
        pass
    else:
        kind.append(a_s[i])

kind_suuzi = len(kind)
a_s_suuzi = len(a_s)
now_shurui = len(kind)
if k>=kind_suuzi:
    print(0)
else:
    kind_nums = [0 for x in range(kind_suuzi)]
    for i in range(kind_suuzi):
        for ii in range(a_s_suuzi):
            if a_s[ii] == kind[i]:
                kind_nums[i] += 1
    flag = True
    cnt = 0
    while flag:
        kind_min = min(kind_nums)
        for i in range(kind_suuzi):
            if kind_nums[i] == kind_min:
                break
        cnt += kind_nums[i]
        kind_nums[i] = 200001
        now_shurui -= 1 
        if k == now_shurui:
            print(cnt)
            flag = False

Submission status: Time Limit Exceeded

Solution code:
n,k = [int(x) for x in input().split()]
s = {}

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

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

a = 0

while len(t) > k:
    a += t.pop(0)[1]

print(a)

Submission status: Time Limit Exceeded

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

list=[]
cnt = 0
ans = 0

for n in range(N+1):
	if (A.count(n)) > 0:
		cnt += 1
		list.append(A.count(n))

list.sort()

if cnt > K:
	change = cnt-K
	for n in range(change): ans += list[n] 

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(reversed(sorted([A.count(b) for b in set(A)])))
print(sum(B[K:len(B)]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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(1,N+1):
	count.append(A.count(n))
count = sorted(count)
zeroCount = count.count(0)
count = np.array(count)
rslt = 0
if zeroCount > N - K :
	print rslt
else:
	rslt += count[zeroCount:N-K].sum()
	print rslt

Submission status: Time Limit Exceeded

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

    A_sc = []
    for a in A_set:
        A_sc.append(A.count(a))

    A_sc.sort(reverse = True)
    c = 0
    for i in range(K, len(A_sc)):
        c += A_sc[i] 

    print(c)   


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n, k=[int(x) for x in input().split()]
a=input().split()
s=set(a)
d=[]
for i in s:
  d.append(a.count(i))
p=len(s)-k
if p<=0:
  print("0")
  exit()
d=sorted(d)
print(sum(d[:p]))

Submission status: Time Limit Exceeded

Solution code:
#AtcoderABC081 #3

N,K = input().split(' ')
N,K = [int(N),int(K)]
A = list(map(int, input().split(' ')))
#A.sort()

list=[]
cnt = 0
ans = 0
i = 0

for n in range(N+1):
	if (A.count(n)) > 0:
		cnt += 1
		list.append(A.count(n))
	else: list.append(0)

list.sort()

if cnt > K:
	change = cnt-K
	while change > 0:
		if(list[i]!=0):
			ans += list[i]
			change -= 1
		i += 1

print(ans)

Submission status: Time Limit Exceeded

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

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

    A_sc.sort(reverse = True)
    c = 0
    for i in range(K, len(A_sc)):
        c += A_sc[i] 

    print(c)   


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#AtcoderABC081 #3

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

list=[]
cnt = 0
ans = 0
i = 0

for n in range(N+1):
	if (A.count(n)) > 0:
		cnt += 1
		list.append(A.count(n))
	else: list.append(0)

list.sort()
#print('list:{}'.format(list))
#print('cnt:{}'.format(cnt))

if cnt > K:
	change = cnt-K
	#print('change:{}'.format(change))
	while change > 0:
		if(list[i]!=0):
			#print('add:{}'.format(list[i]))
			ans += list[i]
			change -= 1
		i += 1

print(ans)

Submission status: Time Limit Exceeded

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)
    quit()
dic={}
for key in keys:
    dic[key]=li.count(key)
print(sum(sorted(list(dic.values()),reverse=True)[k:]))

Submission status: Time Limit Exceeded

Solution code:
N , K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
cnt = []
while len(A) > 0:
    temp = A[0]
    i_cnt = 0
    while A[0] == temp:
        i_cnt += 1
        del A[0]
        if len(A) == 0:
            break
    cnt.append(i_cnt)
cnt.sort()
result = 0
for i in range(len(cnt) - K):
    result += cnt[i]
print(result)

Submission status: Time Limit Exceeded

Solution code:
n, k=[int(x) for x in input().split()]
a=input().split()
s=set(a)
d=[]
for i in s:
  d.append(a.count(i))
p=len(s)-k
if p<=0:
  print("0")
  exit()
d=sorted(d, key=lambda x:x)
r=0
for i in range(p):
  r+=d[i]
print(r)

Submission status: Time Limit Exceeded

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

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

    A_sc.sort(reverse = True)
    c = 0
    for i in range(len(A_sc)):
        if i < K:
            pass
        else:
            c += A_sc[i] 

    print(c)   


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

d = dict()

for i in ls:
    if i in d:
        d[i] += 1
    else:
        d[i] = 1
       
adj = 0
key_num = len(d.keys()) 

while(1):
    if key_num <= nk[1]:
        print(adj)
        break
    min_idx = min(d.items(), key=lambda x:x[1])[0]
    min_num = min(d.items(), key=lambda x:x[1])[1]
    del d[min_idx]
    adj += min_num
    key_num -= 1

Submission status: Time Limit Exceeded

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(1,N+1):
	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:
		rslt += count[zeroCount]
		count[zeroCount] = 0
		zeroCount += 1

	print rslt

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 b in B:
    C.append(A.count(b))
C = list(reversed(sorted(C)))
print(sum(C[K:len(C)]))

Submission status: Time Limit Exceeded

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

  if len(types) <= k:
    return 0

  for i in types:
    count.append(original.count(i))
  count.sort()

  for i in range(diff):
    ans += count[i]
  
  return ans

print(judge(n, k, nums))

Submission status: Time Limit Exceeded

Solution code:
N , K = list(map(int,input().split()))
li = list(map(int,input().split()))
typ = []
for i in range(N):
    if li[i] == -1: 
        pass
    else:
        count = 0 
        num = li[i]
        for i2 in range(N - i): 
            if li[i + i2] == num:
                count += 1
                li[i + i2] = -1
        typ.append(count)
typ.sort()
count = 0 
if len(typ) > K:
    for i in range(len(typ) - K): 
        count += typ[i]
print(count)

Submission status: Time Limit Exceeded

Solution code:
n, k=[int(x) for x in input().split()]
a=input().split()
s=set(a)
d={}
for i in s:
  d[i]=a.count(i)
p=len(s)-k
if p<=0:
  print("0")
  exit()
d=sorted(d.items(), key=lambda x:x[1])
r=0
for i in range(p):
  r+=d[i][1]
print(r)

Submission status: Time Limit Exceeded

Solution code:
N , K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
cnt = []
while len(A) > 0:
    temp = A[0]
    cnt.append(A.count(temp))
    for j in range(cnt[-1]):
        A.remove(temp)
cnt.sort()
result = 0
for i in range(len(cnt) - K):
    result += cnt[i]
print(result)

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools
import heapq
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect, bisect_left, bisect_right
from collections import defaultdict, deque


# d = defaultdict(lambda: 0)
# setrecursionlimit(10**7)
# inf = float("inf")


##### stdin ####
def LM(t, r): return list(map(t, r))
def R(): return stdin.readline()
def RS(): return R().split()
def I(): return int(R())
def F(): return float(R())
def LI(): return LM(int,RS())
def LF(): return LM(float,RS())
def ONE_SL(): return list(input())
def ONE_IL(): return LM(int, ONE_SL())
def ALL_I(): return map(int, stdin)
def ALL_IL(): return LM(int,stdin)

##### tools #####
def ap(f): return f.append
def pll(li): print('\n'.join(LM(str,li)))
def pljoin(li, s): print(s.join(li))



##### main #####		
def main():
	N,K = LI()
	A = LI()
	set_A = set(A)
	d = {}
	max_k = 0
	min_l = []
	heapq.heapify(min_l)
	for a in set_A:
		cnt_a = A.count(a)
		max_k = max(max_k, cnt_a)
		d[a] = cnt_a
		heapq.heappush(min_l, (cnt_a,a))

	ans=0
	while len(d)>K:
		min_v, min_k = heapq.heappop(min_l)
		ans += min_v
		del d[min_k]

	print(ans)


if __name__ == '__main__':
	main()