Problem: 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.

Submission attempts (in order):
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 lis[j] == lis[i]
                count += 1
        b.append(count)

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

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 lis[j] == lis[i]:
                count += 1
        b.append(count)

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

lis = list(map(int, input().split()))
n = lis[0]
k = lis[1]
a = []
b = []
for i in range(n):
	if lis[i] not in a
		a.append(lis[i])
		count = 0
		for j in range(i+1,n)
			if lis[j] == lis[i]
				count += 1
		b.append(count)
if len(b) <= k
	print(0)
else:
	b.sort()
	b.reserve()
	swap = 0
	for i in range(k,len(b))
		swap += b[i]
	print(swap)


lis2 = list(map(int, input().split()))
n = lis[0]
k = lis[1]
d = {}

for i in range(n):
    if lis2[i] not in d.keys():
       d[lis2[i]] = 1
    else:
        d[lis2[i]] += 1 
        
if len(b) <= k:
    print(0)
else:
    a = d.values()
    a.sort()
    a.reverse()
    swap = 0
    for i in range(k,len(a)):
        swap += a[i]
    print(swap)

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.reserve()
    swap = 0
    for i in range(k,len(b)):
        swap += b[i]
    print(swap)

lis = list(map(int, input().split()))
lis2 = list(map(int, input().split()))
n = lis[0]
k = lis[1]
d = {}

for i in range(n):
    if lis2[i] not in d.keys():
       d[lis2[i]] = 1
    else:
        d[lis2[i]] += 1 
        
if len(b) <= k:
    print(0)
else:
    a = list(d.values())
    a.sort()
    a.reverse()
    swap = 0
    for i in range(k,len(a)):
        swap += a[i]
    print(swap)

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)

lis = list(map(int, input().split()))
lis2 = input().split()
n = lis[0]
k = lis[1]
d = {}

for i in range(n):
    if lis2[i] not in d.keys():
       d[lis2[i]] = 1
    else:
        d[lis2[i]] += 1 
        
if len(list(d.values())) <= k:
    print(0)
else:
    a = list(d.values())
    a.sort()
    a.reverse()
    swap = 0
    for i in range(k,len(a)):
        swap += a[i]
    print(swap)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Accepted