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()))

from collections import Counter
X = Counter(A)
b = set(A)
leng = len(b)

ans = 0
if k < leng:
    for i in range(leng - k):
        p,q = X.most_common()[i*-1-1]
        ans += q
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()))
c = Counter(A).most_common()[::-1]

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

Submission status: Time Limit Exceeded

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

aset = set(a)
count = []
n = len(aset)
for i in aset:
    count.append([i,a.count(i)])
if n <= k:
    print(0)
else:
    c = n - k
    count.sort(key=lambda x: x[1])
    ans = 0
    for i in range(c):
        ans += count[i][1]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import collections
n,k = map(int,input().split())
ls = list(map(int,input().split()))
c = collections.Counter(ls)
s = len(c)-k
t = 0
if s <=0:
    print(0)
else:
    for i in range(s):
        t += c.most_common()[::-1][i][1]
    print(t)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
counts = dict(Counter(a))
count = 0
while len(counts) > k:
        mins = min(counts.values())
        count += mins
        del counts[min(counts,key=counts.get)]
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
array = []
ans = 0

for i in set(A):
    array.append(A.count(i))

array.sort()

if len(set(A)) > k:
    num = len(array) - k
    for i in range(num):
        ans += array[i]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
array = {}
ans = 0

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

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


if len(sort_array) > k:
    num = len(sort_array) - k
    for i in range(num):
        ans += sort_array[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(set(a))
res = 0
if len(b) <= k:
  print(0)
else:
  if k <= n / 2:
    tmp = [0] * k
    for i in b:
      a_count = a.count(i)
      if a_count > tmp[0]:
        tmp.pop(0)
        tmp.append(a_count)
        tmp.sort()
    for i in range(k):
      res = res + tmp[i]
    print(n - res)
  else:
    tmp = [200000] * (n - k)
    for i in b:
      a_count = a.count(a)
      if a_count < tmp[0]:
        tmp.pop(0)
        tmp.append(a_count)
        tmp.sort(reverse=True)
    for i in range(n - k):
      res = res + tmp[i]
    print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s, t  = map(int, input().split())
u = list(map(int,input().split()))
from collections import Counter
v = Counter(u)
w = 0
for i in range(s):
    if len(v) <= t:
        print(w)
        break
    else:
        w += min(v.values())
        del v[min(v, key = v.get)]

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

if len(c) <= k:
    print("0")
    exit(0)

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

print(max(len(a)-sum, 0))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int,input().split())
a = list(map(int,input().split()))
cnt = [0] * n
ans = 0
for i in a:
    cnt[i-1] += 1

if len(set(a)) <= k:
    print(0)
    exit()
else:
    for i in sorted(cnt):
        if i == 0:
            continue
        else:
            ans += i
            k += 1
            if len(set(a)) <= k:
                print(ans)
                exit()

Submission status: Time Limit Exceeded

Solution code:
import collections
import sys
N,K = map(int,input().split())
A = list(map(int,input().split()))
c = collections.Counter(A)
cnt = 0
if len(c)<= K:
    print(0)
    sys.exit()
for i in range(K):
    cnt += c.most_common()[i][1]    
print(N-cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N,K = map(int,input().split())
A = list(map(int,input().split()))
count = Counter(A)
rew = 0
if K >= len(count):
    print(0)
else:
    values = sorted(list(count.values()))
    while len(values) != K:
        rew += values.pop(0)
    print(rew)

Submission status: Time Limit Exceeded

Solution code:
import collections
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = collections.Counter(a)
cnt = 0
c = b.most_common()[::-1]
while len(c) > k:
	cnt += c[0][1]
	c.pop(0)
print (cnt)

Submission status: Time Limit Exceeded

Solution code:

# liset


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


index = []
count = []


for i in A:
    if i not in index:
        index.append(i)
        count.append(1)
    else:
        count[index.index(i)] += 1
        

count.sort()        
        
        
if len(count) <= K:
     ans = 0
else:
    num = len(count) - K

    ans = sum(count[:num])
  
    
    
    

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

c = collections.Counter(A_list)

n = len(c)

if n <= K:
    ans = 0
else:
    change_no = 0
    for i in range(1,n-K+1):
        change_no += c.most_common()[-i][1]
    
    ans = change_no
    

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K, *A = map(int, open(0).read().split())
B = sorted(map(lambda x:A.count(x), set(A)))
print(sum(B[:-K]))

Submission status: Time Limit Exceeded

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

d = dict()
for i in range(n):
    d[a[i]] = d.get(a[i], 0) + 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main(n: int, k: int, a: list):
    calc = [0 for i in range(n)]
    ans = 0 
    for _a in a:
        calc[_a - 1] += 1

    calc = [i for i in calc if i != 0]
    calc.sort()

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

if __name__ == '__main__':
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    main(n, k, a)

Submission status: Time Limit Exceeded

Solution code:
num = list(map(int, input().split()))
N = num[0]
K = num[1]

ball = list(map(int, input().split()))
counter = []

for i in range(max(ball)):
    counter.append(ball.count(i+1)) 
    
N = 0
while K < len(counter):
    N += min(counter)
    counter.remove(min(counter))
    
print(N)

Submission status: Time Limit Exceeded

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

N,K = map(int, input().split())
A = list(map(int, input().split()))
List = {}
for i in A:
    if List.get(i,None)==None:
        List[i]=1
    else:
        List[i]+=1

List_sorted = sorted(List.items(), key=lambda x:x[1])
tmp=0
Num=len(List_sorted)-K

for i in range(Num):
    tmp+=list(List_sorted[0])[1]
    del List_sorted[0]

print(tmp)

Submission status: Time Limit Exceeded

Solution code:
import collections
N,K = map(int, input().split())
A = list(map(int, input().split()))
List = {}
for i in A:
    if List.get(i,None)==None:
        List[i]=1
    else:
        List[i]+=1

List_sorted = sorted(List.items(), key=lambda x:x[1])
tmp=0
Num=len(List_sorted)-K

for i in range(Num):
    tmp+=list(List_sorted[0])[1]
    del List_sorted[0]

print(tmp)

Submission status: Time Limit Exceeded

Solution code:
import collections
N,K = map(int, input().split())
A = list(map(int, input().split()))
List = {}
for i in A:
    if List.get(i,None)==None:
        List[i]=1
    else:
        List[i]+=1
#c = collections.Counter(List)
#print(min((v, k) for k, v in List.items()))
List_sorted = sorted(List.items(), key=lambda x:x[1])
tmp=0
while(True):
    if(len(List_sorted)<=K):break
    tmp+=list(List_sorted[0])[1]
    del List_sorted[0]

print(tmp)

Submission status: Time Limit Exceeded

Solution code:
import collections
N,K = map(int, input().split())
A = list(map(int, input().split()))
List = {}
for i in A:
    if List.get(i,None)==None:
        List[i]=1
    else:
        List[i]+=1
#c = collections.Counter(List)
#print(min((v, k) for k, v in List.items()))
tmp=0
while(True):
    c = collections.Counter(List)
    if(len(c)<=K):break
    Str=min((v, k) for k, v in List.items())
    tmp+=int(Str[0])
    del List[int(Str[1])]

print(tmp)

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)
vs = []
for v in c.values():
  vs.append(v)
vs.sort()

i = 0
if len(vs) <= k:
  print(i)
  exit()
else:
  while len(vs) > k:
    i += vs[0]
    vs = vs[1:]
  else:
    print(i)
    exit()

Submission status: Time Limit Exceeded

Solution code:
import collections

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

d = len(set(a))
if d <= k:
  print('0')
else:
  i = 0
  while d > k:
    c = collections.Counter(a)
    c = sorted(c.items(), key=lambda x:x[1])
    a.remove(c[0][0])
    a.append(c[-1][0])
    d = len(set(a))
    i += 1
  else:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
N, k = map(int, input().split())
S = list(map(int, input().split()))
S_unique = list(set(S))
S_unique_count = []

for i in range(len(S_unique)):
    tmp = S.count(S_unique[i])
    S_unique_count.append(tmp)

S_unique_count.sort()

ans = sum(S_unique_count[0:len(S_unique_count)-k])
print(ans)

Submission status: Time Limit Exceeded

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

kinds = set(balls)
amo = sorted([balls.count(i) for i in kinds])
print(sum(amo[:max(0, len(kinds)-k)]))

Submission status: Time Limit Exceeded

Solution code:
n,k= (int(i) for i in input().split())       #入力整数横2つ以上
a = [int(i) for i in input().split()]  
a.sort()
cnum = a[0]
ct =1
st = []
for i in range(1,n):
    if a[i] != cnum:
        cnum = a[i]
        st.append(ct)
        ct = 1
    else:ct+=1
st.append(ct)
st.sort()
total = 0
while len(st)>k:
    total+=st.pop(0)
print(total)

Submission status: Time Limit Exceeded

Solution code:
import math
import fractions
import copy
#以下てんぷら
def j(q):
    if q==1: print("YES")
    elif q == 0:print("NO")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def printrow(a):
    for i in range(len(a)):
        print(a[i])
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


#n = ip()                                     #入力整数1つ
n,k= (int(i) for i in input().split())       #入力整数横2つ以上
a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#a=[input() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#全足しに4 3sum変数使用はsum関数使用できないので注意
a.sort()
cnum = a[0]
ct =1
st = []
for i in range(1,n):
    if a[i] != cnum:
        cnum = a[i]
        st.append(ct)
        ct = 1
    else:ct+=1
st.append(ct)
st.sort()
total = 0
while len(st)>k:
    total+=st.pop(0)
print(total)

Submission status: Time Limit Exceeded

Solution code:
import math
import fractions
import copy
#以下てんぷら
def j(q):
    if q==1: print("YES")
    elif q == 0:print("NO")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def printrow(a):
    for i in range(len(a)):
        print(a[i])
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


#n = ip()                                     #入力整数1つ
n,k= (int(i) for i in input().split())       #入力整数横2つ以上
a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#a=[input() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#全足しに4 3sum変数使用はsum関数使用できないので注意
s = [0 for i in range(200001)]
for i in range(n):
    s[a[i]]+=1
#print(s[:max(a)+1])
st = []
for i in range(max(a)+1):
    if s[i]:st.append(s[i])
st.sort()
#print(st)
total = 0
while len(st)>k:
    total+=st.pop(0)
print(total)

Submission status: Time Limit Exceeded

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

A_list = list(set(A))
l = len(A_list)

for a in A_list:
    c = A.count(a)
    c_list.append(c)

c_list.sort()

if K >= l:
    print(0)
else:
    for i in range(l - K):
        count += c_list[i]
    print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N,K = (int(i) for i in input().split())
A = [int(i) for i in input().split()]
if len(set(A)) <= K:
    print(0)
    exit()

d = [a for a in Counter(A).values()]
tmp = 0
for i in range(K):
    tmp += max(d)
    d.remove(max(d))
print(len(A)-tmp)

Submission status: Time Limit Exceeded

Solution code:
def main():
    [n,k] = [int(i) for i in input().split()]
    a = [int(i) for i in input().split()]
    dic = {}
    for i in a:
        if i not in dic.keys():
            dic[i] = 1
        else:
            dic[i] += 1
    ls = list(dic.values())
    ls.sort()
    s = 0
    if k <= len(dic):
        for i in range(len(dic) - k):
            s += ls[i]
    print(s)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
[n,k] = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
dic = {}
for i in a:
    if i not in dic.keys():
        dic[i] = 1
    else:
        dic[i] += 1
ls = list(dic.values())
ls.sort()
s = 0
if k <= len(dic):
    for i in range(len(dic) - k):
        s += ls[i]
print(s)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
b = list(set(A))
if len(b) <= K:
  print(0)
else:
  q = []
  for i in range(len(b)):
    q.append(A.count(b[i]))
  q.sort(reverse=True)
  s = 0
  for i in range(K):
    s += q[i]
  print(N-s)

Submission status: Time Limit Exceeded

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

C = []
for i in B:
  C.append(A.count(i))

print(sum(sorted(C)[:-K:]))

Submission status: Time Limit Exceeded

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

C = []
for i in B:
  C.append([i,A.count(i)])

D = sorted(C,key=lambda x: x[1])

print(sum(x[1] for x in D[:-K:]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
l=list(map(int,input().split()))
l1=[]
l2=[]
c=0
for i in l:
  if i in l1:
    l2[l1.index(i)]+=1
  else:
    l1.append(i)
    l2.append(1)
if m>=len(l2):
  print(0)
else:
  l2=sorted(l2)
  for i in range(len(l2)-m):
    c+=l2[i]
  print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

#辞書dの0でない要素の数を返す関数
def count_dict(d):
    list = d.values()
    cnt_dict = 0
    for i in list:
        if i == 0:
            cnt_dict += 1
    return len(d) - cnt_dict

dict = {}
for i in a_n:
    if i in dict:
        dict[i] += 1
    else:
        dict[i] = 1

cnt = 0
while count_dict(dict) > K:
    minimum = 100000000
    flag = False
    for i in dict.values():
        if i <= minimum and i != 0:
            minimum = i
    for j in dict:
        if dict[j] == minimum:
            dict[j] = 0
            cnt += minimum
            flag = True
        if flag == True:
            break
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
a,b = map(int,input().split(" "))
ar = list(map(int,input().split(" ")))
br = set(ar)
cr = []
for r in br:
    cr.append(ar.count(r))
cr = sorted(cr,reverse=True)
dr = cr[0:b]
print(sum(cr)-sum(dr))

Submission status: Time Limit Exceeded

Solution code:
a,b = map(int,input().split(" "))
ar = list(map(int,input().split(" ")))
br = set(ar)
cr = []
for r in br:
    cr.append(ar.count(r))
cr.sort()
count = 0
for i in range(len(br)-b):
    count += cr[i]
print(count)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = []
c = set(a)
for i in c:
    b.append(a.count(i))
b.sort(reverse = True)
print(sum(b[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(reverse = True)
print(sum(b[k:]))

Submission status: Time Limit Exceeded

Solution code:
lis = list(map(int, input().split()))
lis2 = list(map(int, input().split()))
n = lis[0]
k = lis[1]
a = []
b = []
for i in range(n):
    if lis2[i] not in a:
        a.append(lis2[i])
        count = 1
        for j in range(i+1,n):
            if lis2[j] == lis2[i]:
                count += 1
        b.append(count)

if len(b) <= k:
    print(0)
else:
    b.sort()
    b.reverse()
    swap = 0
    for i in range(k,len(b)):
        swap += b[i]
    print(swap)

Submission status: Time Limit Exceeded

Solution code:
import heapq

n,k=map(int,raw_input().split())
a=map(int,raw_input().split())
res = [(a.count(e),e) for e in set(a)]
div=max(0,len(res)-k)
heapq.heapify(res)
ans=0
while div>0:
    ans+=heapq.heappop(res)[0]
    div-=1
print ans

Submission status: Time Limit Exceeded

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

res = [(a.count(e),e) for e in set(a)]
div=max(0,len(res)-k)

resl=sorted(res,key=lambda x:x[0],reverse=True)
ans=0
while div>0:
    ans+=resl[-1][0]
    resl.pop()
    div-=1
print ans

Submission status: Time Limit Exceeded

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

div=max(0,int(aa)-k)
res = [(a.count(e),e) for e in set(a) if a.count(e) > 0]
resl=sorted(res,key=lambda x:x[0],reverse=True)
ans=0
while div>0:
    ans+=resl[-1][0]
    resl.pop()
    div=div-1

print ans

Submission status: Time Limit Exceeded

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

m=map(int,input().split())
l=list(m)

uql=list(set(l))
if k < len(uql):
    modcnt=len(uql) - k
    items=[]
    for x in l:
        tmp=[]
        for item in items:            
            tmp.append(item[0])
        if x not in tmp:
            items.append([x,1])
        else:            
            items[tmp.index(x)][1]+=1

    counts=[]
    for i in range(len(items)):
        counts.append(items[i][1])
    counts.sort()
    cnt=0
    for i in range(modcnt):
        cnt+=counts[i]
    print(cnt)
else:
    print(0)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, raw_input().split())
a = map(int, raw_input().split())
s = set(a)
if len(s) < k:
    print 0
    exit()
d = sorted([(i, a.count(i)) for i in s], key = lambda x: x[1])
cnt = 0
for i in d[:len(s) - k]:
    cnt += i[1]
print cnt

Submission status: Time Limit Exceeded

Solution code:
#!usr/bim/python
#! -*- coding: utf-8 -*-

nk = raw_input()
nk = nk.split()
n,k = map(int,nk)

a = raw_input()
a = a.split()
a = map(int,a)

d = {}

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

l = d.values()

ans = 0

if len(l) <= k:
    pass
else:
    for i in range(len(l)-k):
        ans += min(l)
        l.remove(min(l))

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 set(a):
    b.append(a.count(i))
b.sort()
ans = 0
for i in range(len(set(a)) - k):
    ans += b[i]
print(ans)

Submission status: Time Limit Exceeded

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

cnt = {}

for a in A:
    if a not in cnt:
        cnt[a] = 0
    cnt[a] += 1

cnt = sorted(cnt.items(), key=lambda x: x[1])
ans = 0

while len(cnt) > K:
    ans += cnt[0][1]
    cnt = cnt[1:]

print(ans)

Submission status: Accepted

Solution code:
# author:  Taichicchi
# created: 28.09.2020 19:33:45

import sys

from collections import Counter


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

c = dict(Counter(map(int, input().split())))

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

l = len(s_c)
if l <= k:
    print(0)
else:
    ans = 0
    for i in range(l - k):
        ans += s_c[i][1]
    print(ans)

Submission status: Accepted

Solution code:
import collections as c
N,K,*A= map(int,open(0).read().split())
ans=0
C=c.Counter(A)
C=sorted(C.values())
#Aprint(C)
for i in range(len(C)-K):
  ans+=C[i]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n,k = map(int, input().split())
a = list(map(int,input().split()))
kinds = dict(Counter(a))
kinds = sorted(kinds.values(), reverse = True)
if len(kinds) > k:
  print(sum(kinds[k:]))
else:
  print("0")

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

from collections import deque

n, k = map(int, input().split())
a = list(map(int, input().split()))
count = [0] * (n + 1)
for v in a:
    count[v] += 1
count = deque(sorted(v for v in count if v != 0))
kind = len(count)
ans = 0
while kind > k:
    v = count.popleft()
    kind -= 1
    ans += v
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
A=[int(i) for i in input().split()]
tab=[0 for i in range(n+1)]
for num in A:
    tab[num]+=1
tab.sort(reverse=True)
print(n-sum(tab[:k]))

Submission status: Accepted

Solution code:
from collections import Counter 

N, K = map(int, input().split())
A = list(map(int, input().split()))
s = len(set(A))
t = s-K

c = Counter(A)
num = list(c.values())
num.sort()

print(sum(num[:t])) if t>0 else print(0)

Submission status: Accepted

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

to_del = max(0, len(c) - k)
cm = c.most_common()[::-1]
ans = sum(cm[i][1] for i in range(to_del))
print(ans)

Submission status: Accepted

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

to_del = max(0, len(c) - k)
cm = c.most_common()[::-1][:to_del]
ans = sum(cm[i][1] for i in range(to_del))
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt = Counter(A)

s = len(cnt.keys())

ans = 0
kosu = sorted(cnt.values())
ind = 0
while s > k:
    s -= 1
    ans += kosu[ind]
    ind += 1

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
int_dic = {}
for ai in a:
    if ai in int_dic:
        int_dic[ai] += 1
    else:
        int_dic [ai] = 1
num_int = len(int_dic.keys())
if num_int <= k:
    print(0)
else:
    rpl = num_int - k
    value_ls = list(int_dic.values())
    value_ls.sort()
    print(sum(value_ls[:rpl]))

Submission status: Accepted

Solution code:
from collections import defaultdict
N, K = map(int,input().split())
A = list(map(int,input().split()))
D = defaultdict(int)
for a in A:
  D[a]+=1
D = sorted(D.values())
ans = 0
if len(D)<=K:
  print(ans)
else:
    print(sum(D[:len(D)-K]))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
import sys
mod=10**9+7 ; inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict #すべてのkeyが用意されてる defaultdict(int)で初期化
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal as D  #変換後の末尾桁を0や0.01で指定
  #Decimal((str(0.5)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
from functools import lru_cache
from bisect import bisect_left as bileft, bisect_right as biright
from fractions import Fraction as F  #F(a,b)で正確なa/b
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########

C=Counter(A)
Q=list(C.values())
Q=sorted(Q,reverse=1)
if len(Q)<k:
    print(0);exit()
print(n-sum(Q[:k]))

Submission status: Accepted

Solution code:
import collections
N, K = map(int, input().split())
arr = list(map(int,input().split()))
M = len(list(set(arr))) - K #減らさなきゃいけない種類
ans = 0

#操作不要の場合は、そのままゼロ出して終わる
if K >= len(list(set(arr))):
    print(0)
    exit()

#与えられた数列のKeyとValueをvalueの昇順でリスト化する
c = collections.Counter(arr)
cnt_arr = list(sorted(c.items(), key=lambda x:x[1]))
for i in range(M):
    ans += cnt_arr[i][1]
print(ans)

Submission status: Accepted

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

ca = collections.Counter(aa) # counter aa
arca = [a for a in ca.items()] #array ca
sa = sorted(arca, key = lambda x:x[1]) # sorted araca
lca = len(ca) #length ca
cnt = 0
if lca > k:
  for i in range(lca - k):
    cnt += sa[i][1]
else:
  cnt = 0
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

l=Counter(l)

if len(l) <= k:
  ans=0
else:
  sl=sorted(list(l.values()))
  num=len(l)-k
  ans=sum(sl[:num])
  
print(ans)

Submission status: Accepted

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

c = collections.Counter(A)
score_sorted = sorted(c.items(), key=lambda x:-x[1])

memo = 0
count = 0
for key,value in score_sorted:
    if count < K:
        count += 1
        memo += value
    else:
        break
print(N-memo)

Submission status: Accepted

Solution code:
from collections import Counter


N, K = map(int, input().split())
A = list(map(int, input().split()))
A_cntr = Counter(A)
print(sum(sorted(v for v in A_cntr.values())[:-K]))

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
  return int(readline())
n,K = readInts()
A = readInts()
kind = set()
for i in range(n):
    kind.add(A[i])
if len(kind) <= K:
    print(0)
else:
    C = Counter(A)
    lis = []
    for k,v in C.items():
        lis.append((k,v))
    lis = sorted(lis, key = lambda x:x[1])
    ans = 0
    for i in range(len(lis)-K):
        ans += lis[i][1]
    print(ans)