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: Runtime Error

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

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: Runtime Error

Solution code:
from collections import Counter
from pandas import Series

N,K=map(int,input().split())
A=list(map(int,input().split()))
count=0
S=set(A)
c=Counter(A)
L=c.most_common(len(S))

x=len(S)-1
for _ in range(len(S)-K):
    count=count+int(L[x][1])
    x=x-1

print(count)

Submission status: Runtime Error

Solution code:
a = input();
a.split();
n=int(a[0]);
k=int(a[2])
b=input();
list=b.split(" ", n-1)
for i in range(0,n):
  list[i] = int(list[i])
counts=[0]*n
for x in range(0,n):
  counts[x]=list.count(x)
counts.sort()
num=0
for x in range(1,k+1):
  num += counts[n-x]
print(n-num)

Submission status: Runtime Error

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(C[K:len(C)]))

Submission status: Runtime Error

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

"""arc086_a"""

import sys
import pprint
import re

from collections import Counter

from functools import reduce

import itertools
pp = pprint.PrettyPrinter(indent=2)


def main():
    """Main function."""
    n, k = list(map(int, input().split(' ')))
    as_ = list(map(int, input().split(' ')))

    counter = Counter(as_)

    occ_list = sorted(counter.most_common(), key=lambda t: t[1], reverse=True)
    if len(occ_list) <= k:
        print(0)
    elif len(occ_list) == k + 1:
        print(occ_list[-1][1])
    else:
        print(reduce(lambda t1, t2: t1[1] + t2[1], occ_list[k:]))



if __name__ == '__main__':
    sys.exit(main())

Submission status: Runtime Error

Solution code:
from collections import Counter
N,K = [int(i) for i in input().split()] 
A = [int(i) for i in input().split()]
counter = Counter(A)
ans = 0
counter_list = counter.most_common()
if K > len(counter_list):
    while (K!=len(counter_list)):
        ans += counter_list[-1][1]
        counter_list = counter_list[:-1]

print(ans)

Submission status: Runtime Error

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

from collections import Counter
import numpy as np
c = Counter(A)

no = len(c.most_common())
numlist = []
for label, num in c.most_common():
    numlist.append(num)

diff = no - K
if(diff > 0):
    ans = np.sum(numlist[-diff:])
    
print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter
N,K = [int(i) for i in input().split()] 
A = [int(i) for i in input().split()]
counter = Counter(A)
ans = 0
counter_list = counter.most_common()
while (K!=len(counter_list)):
    ans += counter_list[-1][1]
    counter_list = counter_list[:-1]
    
print(ans)