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:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect


class Scanner():
    def int():
        return int(sys.stdin.readline().rstrip())

    def string():
        return sys.stdin.readline().rstrip()

    def map_int():
        return [int(x) for x in Scanner.string().split()]

    def string_list(n):
        return [input() for i in range(n)]

    def int_list_list(n):
        return [Scanner.mapInt() for i in range(n)]

    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    def roundUp(a, b):
        return -(-a // b)

    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    def toLowerMultiple(a, x):
        return (a // x) * x

    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


MOD = int(1e09) + 7


def main():
    #sys.stdin = open("sample.txt")
    N, K = Scanner.map_int()
    A = Scanner.map_int()
    M = sorted(list(Counter(A).values()))
    cnt = len(M)
    ans = 0
    for i in range(len(M)):
        n = min(M[i], max(cnt - K, 0))
        cnt -= n
        ans += n * (i+1)

    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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 set(cnt):
        if i == 0:
            continue
        else:
            if len(set(a)) <= cnt.count(i) + k:
                ans += (len(set(a)) - k) * i
                print(ans)
                exit()
            else:
                ans += cnt.count(i) * i
                k += cnt.count(i)

Submission status: Wrong Answer

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

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=set(a)
if len(s)<=k:
    print(0)
else:
    print(len(s)-k)

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split())
L = list(map(int,input().split()))
L.sort()
#print(L)
cnt = 1
ind = 0
if len(set(L)) <= b:
    print(0)
    exit()
else:
    for i in range(a-1):
        if L[i] != L[i+1]:
            cnt += 1
            if cnt == b+1:
                ind = i
                break
print(len(L[ind+1:]))

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split())
L = list(map(int,input().split()))
L.sort()
#print(L)
cnt = 1
ind = 0
if len(set(L)) <= b:
    print(0)
    exit()
for i in range(a-1):
    if L[i] != L[i+1]:
        cnt += 1
        if cnt == b+1:
            ind = i
            break
print(len(L[ind+1:]))

Submission status: Wrong Answer

Solution code:
import collections
N,K = map(int,input().split())
A = list(map(int,input().split()))
c = collections.Counter(A).most_common()[::-1]
print(c)
k = len(c)
if k<=K:print(0)
else: print(sum([e[1] for e in c[:k-K]]))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

d = defaultdict(int)

for key in a_li:
    d[key] += 1
d = sorted(d.items(), key=lambda x:x[1])

if k >= len(d):
    print(0)
    
s = len(d) - k
ans = 0

for i in range(s):
    ans += d[i][1]
    
print(ans)

Submission status: Wrong Answer

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(len(set(ball))):
    counter.append(ball.count(i+1)) 

N = 0
while K < len(counter):
    N += min(counter)
    counter.remove(min(counter))
    
print(N)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
a.sort()
cnt=1
ans=[]
for i in range(n):
    if a[i-1]==a[i]:
        cnt+=1
    else:
        ans.append(cnt)
        cnt=1
ans.sort()
sum=0
for i in range(len(ans)-k):
    sum+=ans[i]
print(sum)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
a = set(A)
t = len(set(A))
dic = {"{}".format(i):A.count(i) for i in a}
dic = dict(sorted(dic.items(), key=lambda x:x[1]))
cnt = 0
ans = 0
if t > k:
    for i, v in dic.items():
        if cnt == (t - k + 1):
            break
        cnt += 1
        ans += v
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

n, k = map(int, input().split())
A = sorted(Counter(list(map(int, input().split()))).most_common(), key=lambda x:x[1])
cnt = 0
print(A)
if len(A) > k:
    for i in range(len(A)-k):
        cnt += A[i][1]
print(cnt)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
a = set(A)
t = len(set(A))
dic = {"{}".format(i):A.count(i) for i in a}
dic = dict(sorted(dic.items(), key=lambda x:x[1]))
cnt = 0
ans = 0
if t > k:
    for i, v in dic.items():
        if cnt == (t - k + 1):
            break
        cnt += 1
        ans += v
print(cnt)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
a = set(A)
t = len(set(A))
dic = {"{}".format(i):A.count(i) for i in a}
dic = dict(sorted(dic.items(), key=lambda x:x[1]))
cnt = 0
for i, v in dic.items():
    if t <= k:
        print(cnt)
        exit()
    t -= 1
    cnt += v

Submission status: Wrong Answer

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

tmp = 0
if K > len(d)-K:
    for i in range(K):
        tmp += max(d)
        d.remove(max(d))
    print(len(A)-tmp)
else:
    for i in range(len(d)-K):
        tmp += min(d)
        d.remove(min(d))
    print(len(A)-tmp)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a_list=[int(i) for i in input().split()]
count={}
for a in a_list:
    count[a]=count.get(a,0)+1
val=list(count.values())
sorted(val,reverse=True)
print(sum(val[k:]))

Submission status: Wrong Answer

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

d = {}
for i in a:
    d[i] = d.get(i, 0) + 1

d = sorted(d.items(), key=lambda x: -x[0])

if k >= len(d):
    ans = 0
else:
    ans = sum([ v for k, v in d[:len(d)-k]])

print(ans)

Submission status: Wrong Answer

Solution code:
import collections

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

B = list(collections.Counter(A).values())

print(sum(B[K::]))

Submission status: Wrong Answer

Solution code:
#AGC086C
import collections
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
max_count = 0
kind =0

for i in range(N):
    count = 0
    for j in range(N-1):
        if A[j] == A[j+1]:
            count += 1  
        else:
            break
    max_count = max(max_count, count)
    kind += 1
if kind <= K:
    print(0)
else:
    print(K - max_count)

Submission status: Wrong Answer

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(len(set(A))):
  tot+=c.most_common()[i][1]
print(n-tot)

Submission status: Wrong Answer

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(m):
    c+=l2[i]
  print(c)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
d=dict()
for i in map(int,input().split()):
  if i not in d:
    d[i]=1
  else:
    d[i]+=1
m=list(d.keys())
m.sort()
a=len(d.values())-k
ans=0
for i in m:
  if a<=0:
    break
  a-=1
  ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
d=dict()
for i in map(int,input().split()):
  if i not in d:
    d[i]=1
  else:
    d[i]+=1
m=list(d.keys())
m.sort()
n-=k
ans=0
for i in m:
  if n<=0:
    break
  n-=d[i]
  ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

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

C = Counter(A)
v = list(C.values())
v = sorted(v)

div = len(v)
ans = 0
if div <= K:
    ans = 0
else:
    ans += sum(v[0:K-div-1])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N,K = map(int,input().split())
A = list(map(int,input().split()))
c = Counter(A)
values_list = list(c.values())
values_list.reverse()
ans = 0
for i in range(len(values_list)-K):
    ans += values_list[i]
print(ans)

Submission status: Wrong Answer

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

kinds = []

A.sort()

now = A[0]
count = 1
for i in range(1,N):
    if A[i] == now:
        count += 1
    else:
        kinds.append(count)
        now = A[i]
        count = 1
        if i == (N-1):
            kinds.append(count)

kinds.sort()
#print(kinds)

if len(kinds) <= K:
    answer = 0
else:
    answer = 0
    while len(kinds) > K:
        answer += kinds[0]
        kinds.remove(kinds[0])

print(answer)

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys

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

if k>=len(c):
    print(0)
    sys.exit()
d = c.values()
print(sorted(d))
print(sum(sorted(d)[:len(c)-k]))

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

if len(A2) > K:
    r = sorted(A2.items(),key=lambda x:x[1])
    print(sum([i[1] for i in r[:K]]))
else:
    print("0")

Submission status: Wrong Answer

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

for i in range(len(A)):
    if K < A[i]:
        kakikae += 1;
print(kakikae)

Submission status: Wrong Answer

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

kakikae = 0;
l_unique = list(set(A))

for i in range(len(l_unique)):
    if K < l_unique[i]:
        kakikae += 1;
print(kakikae)

Submission status: Wrong Answer

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


kakikae = 0;
l_unique = list(set(A))
print(l_unique)
for i in range(len(l_unique)):
    if K < l_unique[i]:
        kakikae += 1;
print(kakikae)

Submission status: Wrong Answer

Solution code:
import math
import sys
from collections import Counter
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()

    
    array_result.append(data.split(" "))

    flg = 1

    try:


        while flg:

            data = input()

            array_temp = []

            if(data != ""):
                
                array_result.append(data.split(" "))

                flg = 1

            else:

                flg = 0
    finally:


        return array_result





arr_data = getinputdata()

n = int(arr_data[0][0])
k = int(arr_data[0][1])

arr=arr_data[1]


list01=Counter(arr)

#print(list01,len(list01))

#print(list01)

cnt=0
if len(list01) > k:
    
    for key,val in sorted(list01.items(),reverse=True):

        print(key,val,len(list01))

        cnt+=list01.pop(key)
        
        
        if len(list01)==k:
            
            break
        

print(cnt)

Submission status: Wrong Answer

Solution code:
import math
import sys
from collections import Counter
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()

    
    array_result.append(data.split(" "))

    flg = 1

    try:


        while flg:

            data = input()

            array_temp = []

            if(data != ""):
                
                array_result.append(data.split(" "))

                flg = 1

            else:

                flg = 0
    finally:


        return array_result





arr_data = getinputdata()

n = int(arr_data[0][0])
k = int(arr_data[0][1])

arr=arr_data[1]


list01=Counter(arr)

#print(list01,len(list01))

cnt=0
if len(list01) > k:
    
    for key,val in sorted(list01.items(),reverse=True):

#        print(key,val,len(list01))

        cnt+=list01.pop(key)
        
        
        if len(list01)==k:
            
            break
        

print(cnt)

Submission status: Wrong Answer

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

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
import collections
count=0
data=collections.Counter(a)
data=sorted(data.items(),key=lambda x:x[1])
if k<len(data):
    for i in range(len(data)-k):
        count+=data[i][1]
else:
    print(0)
print(count)

Submission status: Wrong Answer

Solution code:
from collections import OrderedDict

temp=input()
n,k=list(map(int,temp.split(" ")))
co=OrderedDict()
ans=[]
temp=input()
nums=list(map(int,temp.split(" ")))
for i in nums:
  if i in co:
	  co[i]+=1
  else:
	  co[i]=1
for i in co:
  ans.append(co[i])
kinds=len(ans)
dif=kinds-k
if dif<=0:
    print(0)
else:
    ans.sort()
    print(sum(ans[:dif-1]))

Submission status: Wrong Answer

Solution code:
from collections import OrderedDict

temp=input()
n,k=list(map(int,temp.split(" ")))
co=OrderedDict()
ans=[]
temp=input()
nums=list(map(int,temp.split(" ")))
for i in nums:
  if i in co:
	  co[i]+=1
  else:
	  co[i]=1
for i in co:
  ans.append(co[i])
kinds=len(ans)
dif=kinds-k
if dif<=0:
    print(0)
else:
    ans.sort()
    print(sum(ans[:k-1]))

Submission status: Wrong Answer

Solution code:
def ncnt(i,j):
 n=i.index(j)
 nn=list(reversed(i)).index(j)
 return len(i)-n-nn
n,k=list(map(int,input().split()))
inl=list(map(int,input().split()))
inl.sort()
ins=set(inl)
d=len(ins)-k
if d<=0:
 print(0)
else:
 ddat=[n for i in range(d)]
 for i in ins:
  l=ncnt(inl,i)
  if ddat[0]>l:
   ddat.pop(0)
   ddat.append(l)
 print(sum(ddat))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

def main():
    [n,k]= list(map(int,input().split()))
    As = list(map(int,input().split()))
    t = {}
    for A in As:
        if A  in t.keys():
            t[A]+=1
        else:
            t[A]=1
    if k-len(t.values()) < 0:
        print(sum(list(reversed(list(t.values())))[:len(t.values())-k]))
    else:
        print(0)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, K= map(int, input().split())
A = map(int, input().split())
d = {}
for a in A:
    if a in d:
        d[a] = d[a] + 1
    else:
        d[a] = 1
o = []
for dd in d:
    o.append(dd)
o = sorted(o, reverse=True)

if K >= len(o):
    print(0)
else:
    s = 0
    print(sum(o[K:]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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
i = 0

for i in range(len(cnt)):
    if len(cnt) + i <= K:
        break
    ans += cnt[i][1]

print(ans)

Submission status: Wrong Answer

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

print min(len(set(a)) - k, 0)

Submission status: Time Limit Exceeded

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

colors = len(C)

for i in range(colors):
  if len(C)<=K:
    break
  _c = C.pop(0)
  ans+=_c
print(ans)

Submission status: Time Limit Exceeded

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

A = collections.Counter(A)
cnt = 0
n = len(A.keys())
for _ in range(n):
    if len(A.keys()) <= K:
        break
    k, v = min(A.items(), key=lambda x: x[1])
    cnt += v
    del A[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 = collections.Counter(A)
cnt = 0
for _ in range(N):
    if len(A.keys()) <= K:
        break
    k, v = min(A.items(), key=lambda x: x[1])
    cnt += v
    del A[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 = collections.Counter(A)
cnt = 0
while len(A.keys()) > K:
    k, v = min(A.items(), key=lambda x: x[1])
    cnt += v
    del A[k]
    
print(cnt)

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:
  s = []
  for i in range(len(b)):
    s.append(a.count(b[i]))
  s.sort()
  ans = sum(s[:len(b)-k])
  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))
if(len(b)<=k):
  print(0)
else:
  s = []
  for i in range(len(b)):
    s.append(a.count(b[i]))
  s.sort()
  ans = 0
  for i in range(len(b)-k):
    ans += s[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)
ans = 0
len_A=len(set(A))
if(len_A<=K):
    print(ans)
else:
    for i in range(len_A-K):
        ans +=c.most_common()[-1*(i+1)][1]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
readline = sys.stdin.readline


def main():
    n, k = map(int, readline().rstrip().split())
    a = list(map(int, readline().rstrip().split()))
    setA = list(set(a))
    arr = [0] * len(setA)
    for l in range(len(setA)):
        arr[l] = a.count(setA[l])
    print(sum(sorted(arr, reverse=True)[k: len(arr)]))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
readline = sys.stdin.readline


def main():
    n, k = map(int, readline().rstrip().split())
    a = list(map(int, readline().rstrip().split()))
    setA = list(set(a))
    arr = []
    for l in setA:
        arr.append(a.count(l))
    print(sum(sorted(arr, reverse=True)[k: len(arr)]))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
x = list(set(a))
b = []
for i in range(len(x)):
    b.append(a.count(x[i]))

b.sort(reverse = True)
c = len(b)
if c > k:
    d = c - k
    print(sum(b[c-d:c]))
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
N,K= map(int, input().split())
A =list(map(int, input().split()))
 
Aset =list(set(A))
B=[0]*len(Aset)
cnt =0
 
for i in range(len(Aset)):
  for j in range(len(A)):
    if Aset[i] == A[j]:
      cnt += 1
  B[i] = cnt
  cnt =0
 
B=sorted(B)[::-1]  
 
ans =0
 
for i in range(K,len(B)):
  ans +=B[i]
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int,input().split()))
ans = []
for num in list(set(a)):
    ans.append(a.count(num))
ans.sort(reverse=True)
del ans[0:k]
print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n,k  = list(map(int, input().split())) 
argList = np.array(list(map(int, input().split())))
argSet = set(argList)

aList = np.array([0]*len(argSet))
j = 0
for i in argSet:
    aList[j] = sum(argList == i)
    j+=1

#list.sort(aList, reverse=True)
aList = np.sort(aList)[::-1]
print(sum(aList[k:]))

Submission status: Time Limit Exceeded

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

aList = [0]*len(argSet)
j = 0
for i in argSet:
    aList[j] = sum([x==i for x in argList])
    j+=1

list.sort(aList, reverse=True)
print(sum(aList[k:]))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a_l = list(map(int, input().split()))
base = set(a_l)
num = len(base)-k
ans = []

if num <= 0:
    print(0)
else:
    for idx, v in enumerate(base):
        ans.append(a_l.count(v))
    print(sum(sorted(ans)[:num]))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a_l = list(map(int, input().split()))
base = list(set(a_l))
num = len(base)-k
ans = [n] * len(base)

if num <= 0:
    print(0)
else:
    for idx, v in enumerate(base):
        ans[idx] = a_l.count(v)
    print(sum(sorted(ans)[:num]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n,k  = list(map(int, input().split())) 
argList = np.array(list(map(int, input().split())) )
argSet = set(argList)

dictNum = {}
for i in argSet:
    dictNum[i] = sum(argList == i)

cnt = 0
ret = 0
for sk, sv in sorted(dictNum.items(), key=lambda x: -x[1]):
    cnt+=1
    if( cnt <= k):
        continue
    #print(str(sk) + ": " + str(sv))
    ret += sv
print (ret)

Submission status: Time Limit Exceeded

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

A = sorted(A,key=A.count,reverse=True)
for i in range(K):
  A = ([j for j in A if j != A[0]])
print(len(A))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

cA = collections.Counter(A)

ans = 0
if len(cA) > K:
  for x in range(len(cA)-K):
    ans += list(reversed(cA.most_common()))[x][1]
else:
  ans = 0
print(ans)

Submission status: Time Limit Exceeded

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

a = list(set((A)))
check = len(a)
dff = check - K
if dff <= 0:
  print(0)
  exit(0)
ans_list = []

for i in range(check):
  ans = 0
  checker = a[i]
  for j in range(len(A)):
    if checker == A[j]:
      ans += 1
  ans_list.append(ans) 

ans_list.sort()

result = sum(ans_list[:dff])
print(result)

Submission status: Time Limit Exceeded

Solution code:
import sys

n, k = map(int, input().split())
A = list(map(int, input().split()))
A_set = set(A)
if len(A_set) <= k:
    print(0)
    sys.exit()
A_count = [0]*len(A_set)
for i, a in enumerate(A_set):
    A_count[i] = A.count(a)
A_count.sort()

num = len(A_set) - k
s = 0
for i in range(num):
    s += A_count[i]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

dic = Counter(l)
ans = 0
kv = dic.items()
kv = sorted(kv, key=lambda x: x[1])
while len(kv) > k:
    ans += kv[0][1]
    del kv[0]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K=map(int, input().split())
A=list(map(int, input().split()))
sa=set(A)
if len(sa)<=K:
  print(0)
else:
  # 書き換える要素の種類数
  sub=len(sa)-K
  # 少ないやつを書き換えたい
  a=list(sa)
  
  numl=[]
  for i in a:
    numl.append(A.count(i))
  
  numl.sort()
  print(sum(numl[:sub]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N, K = map(int, input().split())
counter = Counter(list(map(int, input().split())))
ans = 0
if len(counter.keys()) > K:
    for i in range(len(counter.keys()) - K):
        ans += counter.most_common()[0 - i - 1][1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
*a, = map(int, input().split())
d = {}
for i in a:
    d[i] = d.get(i, 0)+1
a.sort(key=lambda x: d[x])
b = sorted(set(a), key=a.index)
cnt = len(b)
ans = 0
for i in b:
    if cnt <= k:
        break
    ans += d[i]
    cnt -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
*a, = map(int, input().split())
d = {}
for i in a:
    d[i] = d.get(i, 0)+1
a.sort(key=lambda x: d[x])
b = sorted(set(a), key=a.index)
cnt = len(b)
ans = 0
for i in b:
    if cnt <= k:
        break
    ans += a.count(i)
    cnt -= 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()))

now_cnt = len(list(set(A)))

if (now_cnt <= k):
    print(0)
    exit()

c = Counter(A)
c_sorted = sorted(c.items(), key=lambda x: x[1])
dec = now_cnt - k
cnt = 0
L = []

for (i, v) in c_sorted:
    L.append([i, v])

while True:
    if (dec != 0):
        L[0][1] -= 1
        L[-1][1] += 1
        cnt += 1
    else:
        print(cnt)
        exit()

    if (L[0][1] == 0):
        L.pop(0)
        dec -= 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
L = list(map(int, input().split()))
d = {}
for a in L:
    if a not in d:
        d[a] = 1
    else:
        d[a] += 1
l = []
for v in d.values():
    l.append(v)

if len(l) <= k:
    print(0)
else:
    ans = float('inf')
    for i in range(2 ** len(l)):
        b = [0] * len(l)
        for j in range(len(l)):
            if ((i >> j) & 1):
                b[j] = 1
        if b.count(1) <= k:
            cnt = 0
            for m in range(len(l)):
                if b[m] == 0:
                    cnt += l[m]
            ans = min(ans, cnt)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
L = list(map(int, input().split()))
d = {}
for a in L:
    if a not in d:
        d[a] = 1
    else:
        d[a] += 1
l = []
for v in d.values():
    l.append(v)

if len(l) <= k:
    print(0)
else:
    ans = float('inf')
    for i in range(2 ** len(l)):
        b = [0] * len(l)
        for j in range(len(l)):
            if ((i >> j) & 1):
                b[j] = 1
        cnt = 0
        val = len(l)
        for m in range(len(l)):
            if l[m] * b[m] == 0:
                cnt += l[m]
                val -= 1
        if val <= k:
            ans = min(ans, cnt)
    print(ans)

Submission status: Time Limit Exceeded

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

for i in a:
    if i in ball.keys():
        ball[i] += 1
    else:
        ball[i] = 1

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

ans = 0
if len(ball) > k:
    for i in range(k):
        ans += ball[i][1]
    ans = n - ans
print(ans)

Submission status: Time Limit Exceeded

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

for i in a:
    if i in cnt.keys():
        cnt[i] += 1
    else:
        cnt[i] = 1

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

ans, k = 0, len(cnt) - k
for i in range(len(cnt) + 1):
    if 0 >= k:
        print(ans)
        break
    ans += cnt[i][1]
    k -= 1

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

# 減らす種類の個数
SS = len(AS) - K


def search_max(Array, AS):
    min = len(Array)
    for i in AS:
        if min > Array.count(i):
            min = Array.count(i)
            rt = i
    return rt


count = 0
for i in range(SS):
    r = search_max(A, AS)
    count += A.count(r)
    A.remove(r)
    AS.remove(r)

print(count)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
d = [0] * n
for i in a:
  d[i-1] += 1
d.sort()
ans = 0
for i in range(n):
  if len(d[i:n]) == k:
    print(ans)
    break
  if d[i] == 0: continue
  ans += d[i]

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
d = [0] * n
for i in a:
  d[i-1] += 1
d.sort()
ans = 0
for i in range(n):
  if len(d[i:n]) == k:
    print(ans)
    break
  if d[i] == 0: continue
  ans += d[i]

Submission status: Time Limit Exceeded

Solution code:
def main():
  from collections import Counter
  n,k,*a = map(int,open(0).read().split())
  a = Counter(a)
  ans = 0
  for i in range(max(0,len(a)-k)):
    ans += a.most_common()[-1-i][1]
  print(ans)
main()

Submission status: Time Limit Exceeded

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