Problem:


Score : 300 points

Problem Statement

You are given a sequence of positive integers of length N, a = (a_1, a_2, ..., a_N).
Your objective is to remove some of the elements in a so that a will be a good sequence.

Here, an sequence b is a good sequence when the following condition holds true:

For each element x in b, the value x occurs exactly x times in b.

For example, (3, 3, 3), (4, 2, 4, 1, 4, 2, 4) and () (an empty sequence) are good sequences, while (3, 3, 3, 3) and (2, 4, 1, 4, 2) are not.

Find the minimum number of elements that needs to be removed so that a will be a good sequence.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of elements that needs to be removed so that a will be a good sequence.

Sample Input 1

4
3 3 3 3

Sample Output 1

1

We can, for example, remove one occurrence of 3. Then, (3, 3, 3) is a good sequence.

Sample Input 2

5
2 4 1 4 2

Sample Output 2

2

We can, for example, remove two occurrences of 4. Then, (2, 1, 2) is a good sequence.

Sample Input 3

6
1 2 2 3 3 3

Sample Output 3

0

Sample Input 4

1
1000000000

Sample Output 4

1

Remove one occurrence of 10^9. Then, () is a good sequence.

Sample Input 5

8
2 7 1 8 2 8 1 8

Sample Output 5

5



Sample input:
4
3 3 3 3


Sample output:
1

Submission status: Accepted

Solution code:
import collections

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

B = collections.Counter(A)
ans = 0

for key, val in B.items():
    if val > key:
        ans += val - key
    elif val < key:
        ans += val 

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter 

n = int(input())
aa = list(map(int,input().split()))

c = Counter(aa)

ans = 0
for k,count in c.items():
  if k > count:        
    ans += count
  elif k == count:    
    pass
  else:        
    ans += count - k    
    
print(ans)

Submission status: Accepted

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

a_dic = {}
for i in range(N):
    if a[i] in a_dic:
        a_dic[a[i]] += 1
    else:
        a_dic[a[i]] = 1

count = 0
for key, value in a_dic.items():
    if key < value:
        count += value - key
    elif key > value:
        count += value
    else:
        pass

print(count)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
A = Counter(A)
#print(A)
ans = 0
for key , val in A.items():
    if key > val:
        ans += val
    elif key < val:
        ans += val - key
print(ans)

Submission status: Accepted

Solution code:
import collections
N=int(input())
A=[int(x) for x in input().split()]
c=collections.Counter(A)
P=list(c.keys())
Q=list(c.values())
count=0
for i in range(len(P)):
  if P[i]<=Q[i]:
    count += Q[i]-P[i]
  else:
    count +=Q[i]
print(count)

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = list(map(int, input().split()))
c = collections.Counter(s)
ans = 0
for x in c.items():
  if x[0]<x[1]:
    ans+=x[1]-x[0]
  elif x[0]>x[1]:
    ans+=x[1]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort()
i = 0
ans = 0
cnt = 1
if N == 1 and A[0] != 1:
    print(1)
    exit()

while i+1 < N:
    if A[i] == A[i+1]:
        cnt += 1
        if i+1 == N-1:
            ans += cnt if A[i] > cnt else cnt - A[i]
    else:
        if i+1 == N-1:
            ans += 1
        ans += cnt if A[i] > cnt else cnt - A[i]
        cnt = 1
    i += 1
print(ans)

Submission status: Accepted

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

a.sort()
ans = 0
hoge = a[0]
count = 1
for i in range(1,N):
     if a[i] == hoge:
         count += 1
     else:
         if count >= hoge:
             ans += count-hoge
         else:
             ans += count
         hoge = a[i]
         count = 1
if count >= hoge:
    ans += count - hoge
else:
    ans += count

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = sorted(list(map(int, input().split())))
ans = 0
from itertools import groupby
for k, v in groupby(A):
    c = 0
    for i in v:
        c += 1
    if k > c:
        ans += c
    elif k < c:
        ans += c-k
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
n = {}
pop = 0
for ai in a:
    n[ai] = n.get(ai, 0) + 1
for ni in n:
    if ni < n[ni]:
        pop += n[ni] - ni
    elif ni > n[ni]:
        pop += n[ni]
print(pop)

Submission status: Accepted

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

from collections import defaultdict
dic = defaultdict(int)  

for a in al:
    dic[a] += 1

ans = 0
for n,m in dic.items():
    if n > m:
        ans += m
    else:
        ans += m - n

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
line = list(map(int, input().split()))
ans = 0

line.sort()
line.append(10**9 + 1) # 番兵
prev_num = 0
total = 0
for i in range(n+1):
    if line[i] == prev_num:
        total += 1
    else:
        if prev_num > total:
            ans += total
        else:
            ans += total - prev_num
        total = 1
        prev_num = line[i]


print(ans)

Submission status: Accepted

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

arr=Counter(a).most_common()

cnt=0
for i in arr:
  if i[0]>i[1]:
    cnt+=i[1]
  elif i[0]<i[1]:
    cnt+=i[1]-i[0]
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
import collections
counter=collections.Counter(A)
ans=0
for c in counter.items():
  if c[0]<=c[1]:
    ans+=c[1]-c[0]
  else:
    ans+=c[1]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
d = defaultdict(int)
N = int(input())
A = sorted(int(i) for i in input().split())
ans = 0
for i in A:
  d[i] += 1
for i,j in d.items():
  if i < j:
    ans += j-i
  elif i > j:
    ans += j
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = sorted(int(i) for i in input().split())+[0]
cnt, ans = 0, 0
a = A[0]
for i in range(len(A)):
  if a == A[i]:
    cnt += 1
  elif a != A[i]:
    if cnt < a:
      ans += cnt
    elif cnt > a:
      ans += cnt-a
    a = A[i]
    cnt = 1
print(ans)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(A) #要素と出現回数の辞書
#print(c)

ans = 0
for k,v in c.items():
    ans += v if v<k else v-k
print(ans)

Submission status: Accepted

Solution code:
import collections

N=int(input())
List=sorted(list(map(int, input().split())))

c = collections.Counter(List) #要素と出現回数の辞書
value, count = zip(*c.most_common())


newList=[]

for i in range(len(value)):
    if N-len(newList)>=value[i] and count[i]>=value[i]:
        for j in range(value[i]):
            newList.append(value[i])
    else:
        continue

#print(newList)

Ans=len(List)-len(newList)
print(Ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = {}
for a in A:
    if a not in B: B[a] = 0
    B[a]+=1

ans = 0
for k,v in B.items():
    ans += v if v<k else v-k
print(ans)

Submission status: Accepted

Solution code:
import bisect
n=int(input())
a=list(map(int,input().split()))
a.sort()
res = 0
for i in range(n):
    if i != 0 and a[i] == a[i-1]:
        continue
    ko = bisect.bisect_right(a,a[i]) - i
    if ko > a[i]:
        res += ko - a[i]
    elif ko < a[i]:
        res += ko

print(res)

Submission status: Accepted

Solution code:
import collections

N = int(input())

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

C = dict(collections.Counter(A))

ans = 0
for k, v in C.items():
    if (k>v):
        ans += v
    elif(k<v):
        ans += v-k

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
dic_A = Counter(A)
ans = 0
for key, val in dic_A.items():
    if key!=val:
        if key <= val:
            ans += val-key
        else:
            ans+=val
print(ans)

Submission status: Accepted

Solution code:
import collections

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

keys = set(A)
c = collections.Counter(A)

ans = 0
for k in keys:
    v = c[k]
    if k < v:
        ans += v-k
    elif v < k:
        ans += v

print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = [int(i) for i in input().split()]

a = collections.Counter(a)
a_keys = list(a.keys())
a_values = list(a.values())

ans = 0
for i in range(len(a_keys)):
    if a_values[i] - a_keys[i] < 0:
        ans += a_values[i]
    else:
        ans += a_values[i] - a_keys[i]

print(ans)

Submission status: Accepted

Solution code:
#82c
import collections

N = int(input())
A_list = [int(e) for e in input().split()]
counted = collections.Counter(A_list)

ans =0

for number,count in counted.items():
    if number != count:
        if number < count:
            ans += count - number
        elif number > count:
            ans += count
            
print(ans)

Submission status: Accepted

Solution code:
import collections
import sys

readline = sys.stdin.buffer.readline


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    A = collections.Counter(A)

    cnt = 0
    for a in A:
        if A[a] < a:
            cnt += A[a]
        elif A[a] > a:
            cnt += A[a] - a

    print(cnt)


main()

Submission status: Accepted

Solution code:
import bisect
N=int(input())
A=list(map(int,input().split()))
A.sort()
removal=0
B=set(A)
for i in B:
  m=(bisect.bisect_right(A,i)-bisect.bisect_left(A,i))
  if m-i<0:
    removal+=m
  else:
    removal+=m-i
print(removal)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = list(Counter(A).items())
ans = 0
for x, y in c:
    if x <= y:
        ans += y - x
    else:
        ans += y

print(ans)

Submission status: Accepted

Solution code:
import collections
n = input()
a = list(map(int,input().split()))
c = collections.Counter(a)
c = dict(c)
ans = 0
for i in c.keys():
    if i < c[i]:
        ans +=c[i] - i
    elif i > c[i]:
        ans += c[i]
print(ans)

Submission status: Accepted

Solution code:
def examC():
    N = I()
    a = LI()
    d = defaultdict(int)
    for i in a:
        d[i] +=1
    cur = 0
    for i,j in d.items():
        if i==j:
            continue
        elif i<j:
            cur += (j-i)
        else:
            cur +=j
    print(cur)


import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

Submission status: Accepted

Solution code:
from collections import Counter

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

c=Counter(a)
cnt=0
for k,v in c.items():
    if k!=v:
        if k>v:
            cnt+=v
        else:
            cnt+=(v-k)

print(cnt)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(int)
for a in A:
    d[a] += 1

ans = 0
for i, c in d.items():
    if c >= i:
        ans += c - i
    else:
        ans += c
print(ans)

Submission status: Accepted

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

lst = list()
i = 0
while i < N:
    num = A[i]
    cnt = 1
    while i < (N - 1) and A[i] == A[i+1]:
        cnt += 1
        i += 1
    i += 1
    lst.append((num, cnt))
ans = 0
for x in lst:
    if x[0] > x[1]:
        ans += x[1]
    elif x[0] < x[1]:
        ans += x[1] - x[0]
print(ans)

Submission status: Accepted

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

N = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)
ans = 0
for w in c.keys():
  if int(w) > c[w]:
    ans += c[w]
  elif int(w) < c[w]:
    ans += c[w] - int(w)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import collections

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

c = collections.Counter(li)
count = c.items()
point = 0
for i in count:
    if i[0] < i[1]:
        point += i[1]-i[0]
    elif i[0] > i[1]:
        point += i[1]
print(point)

Submission status: Accepted

Solution code:
import collections

N = int(input())
A = list(map(int, input().split()))
ans = 0
for num, count in collections.Counter(A).items():
    if num > count:
        ans += count
    elif num < count:
        ans += count - num

print(ans)

Submission status: Accepted

Solution code:
import collections

N=int(input())
a=list(map(int,input().split()))
c=collections.Counter(a)

cnt=0

for item in c:
  t=c[item]
  if t>item:
    cnt+=t-item
  elif t==item:
    pass
  else:
    cnt+=t
  
print(cnt)

Submission status: Accepted

Solution code:
import collections

N=int(input())
a=list(map(int,input().split()))
a.sort()

cnt=0

c=collections.Counter(a)
#print(c)

for item in c:
  t=c[item]
  if t>item:
    cnt+=t-item
  elif t==item:
    pass
  else:
    cnt+=t

#while len(a)>0:
#  item=a[0]
#  t=(item)
#  #print(item,t,a)
#  if t>item:
#    cnt+=t-item
#  elif t==item:
#    pass
#  else:
#    cnt+=t
#  del a[0:t]
#  #print("after",a)
  
print(cnt)

Submission status: Accepted

Solution code:
import collections
N = int(input())
a = list(map(int,input().split()))
cnt = collections.Counter(a)
ans = 0
aset = sorted(set(a))
for i in aset:
    if cnt[i] < i:
        ans += cnt[i]
    elif cnt[i] > i:
        ans += cnt[i] - i
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
input = sys.stdin.readline
N = int(input().strip())
A = [int(a) for a in input().strip().split()]
d = defaultdict(int)
ans = 0
for a in A:
    if d[a] < a:
        d[a] += 1
    else:
        ans += 1

for key, val in d.items():
    if val < key:
        ans += val

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
from collections import defaultdict
D = defaultdict(int)
for e in A:
    D[e] += 1
ans = 0
for key,count in D.items():
    if key > count:
        ans += count
    else:
        ans += count - key
print(ans)

Submission status: Accepted

Solution code:
from collections import *
input()
print(sum(v if k>v else v-k for k,v in Counter(map(int,input().split())).items()))

Submission status: Accepted

Solution code:
import bisect

n = int(input())
A = sorted(list(map(int, input().split())))
c = 0
temp = 0
for i in sorted(list(set(A))):
  d = bisect.bisect_right(A, i)
  if d-temp >= i:
    c += d-temp - i
  else:
    c += d-temp
  temp = d
print(c)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    from collections import Counter
    c = Counter(A)
    ans = 0
    for k, v in c.items():
        if k <= v:
            ans += v - k
        else:
            ans += v
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Dic={}
for i in A:
    if Dic.get(i,None)==None:
        Dic[i]=1
    else:
        Dic[i]+=1
total=0
for i,j in Dic.items():
    if i <= j:
        total+=j-i
    else :
        total+=j
print(total)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = Counter(map(int,input().split()))
ans = 0
for i in a:
  if a[i] >= i:
    ans += a[i]-i
  else:
    ans += a[i]
print(ans)

Submission status: Accepted

Solution code:
import collections

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

Cnt = collections.Counter(A)
answer = 0
for k, v in Cnt.items():
    if v >= k:
      answer += v-k
    else:
      answer += v
print(answer)

Submission status: Accepted

Solution code:
#import pysnooper
#import numpy
#import os,re,sys,operator
from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit
#from bisect import bisect_left
#from copy import deepcopy
#import heapq
#import math
#import string
#from time import time

setrecursionlimit(10**6)
input=stdin.readline

n=int(input().rstrip())
a=[int(i) for i in input().split()]

b=Counter(a).most_common()
ans=0
for i in b:
    if i[0]==i[1]:
        continue
    if i[0]>i[1]:
        ans+=i[1]
    else:
        ans+=i[1]-i[0]
print(ans)

Submission status: Accepted

Solution code:
import sys
import collections
# import math
# import string
# import bisect
# import re
# import itertools
# import statistics

def main():

    n, *a = (int(x) for x in sys.stdin.read().split())
    c = collections.Counter(a)
    count = 0
    for i, j in c.items():
        if j < i: count += j
        else: count += j - i
    
    print(count)
    
if __name__ == "__main__":
    # execute only if run as a script
    main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
l = [int(i) for i in input().split()]
c = Counter(l)

ans = 0
for k,v in c.items():
    if k == v:
        pass
    elif k < v:
        ans += v - k
    else:
        ans += v
        
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
b = Counter(tuple(map(int, input().split())))
counter = 0
for item in b.items():
    if item[1] >= item[0]:
        counter += item[1] - item[0]
    else:
        counter += item[1]
print(counter)

Submission status: Accepted

Solution code:
#ABC082 C - Good Sequence
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
from collections import defaultdict
N = ni()
A = nl()
D = defaultdict(int)
for a in A:
    D[a] += 1

ans = 0

for d in D:
    if D[d]>=d:
        ans += min(abs(D[d]-d),D[d])
    else:
        ans += D[d]
print(ans)

Submission status: Accepted

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

c = Counter(a)
ans = 0
for i in c.keys():
	if c[i] >= i:
		ans += c[i] - i
	else:
		ans += c[i]
print (ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
list_a = list(map(int, input().split()))
count = collections.Counter(list_a)
result = 0
for k, v in count.items():
  if k < v:
    result += v - k
  elif k > v:
    result += v
print(result)

Submission status: Accepted

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

A.sort()
A.append(0)
temp = A[0]
num = 0
ans = 0
for i in range(N+1):
    if A[i] == temp:
        num += 1
    elif num < temp:
        ans += num
        num = 1
        temp = A[i]
    else:
        ans += num-temp
        num = 1
        temp = A[i]
        
print(ans)

Submission status: Accepted

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

dic = dict()
ans = 0

for i in range(N):
	if A[i] in dic:
		dic[A[i]] += 1
	else:
		dic[A[i]] = 1
		
for key, val in dic.items():
	if key == val:
		pass
	elif key < val:
		ans += val - key
	else:
		ans += val
		
		
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
C=[0]*(N+1)
D=[i for i in range(N+1)]
ans=0

for i in range(N):
    if A[i]>N:
        ans+=1
    else:
        C[A[i]]+=1

for i in range(N+1):
    if C[i]==0:
        continue
    elif C[i]>=D[i]:
        ans+=C[i]-D[i]
    else:
        ans+=C[i]

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
num=a[0]
count=0
cnt=1
for i in range(1,n):
    if a[i]==num:
        cnt+=1
    else:
        if num>cnt:
            count+=cnt
        else:
            count+=cnt-num
        num=a[i]
        cnt=1
if num>cnt:
    count+=cnt
else:
    count+=cnt-num
print(count)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
ans = 0
x = 10**5
count_list = [0]*(x+1)

for i in a:
    if i > x:
        ans += 1
    else:
        if count_list[i] >= i:
            ans += 1
        else:
            count_list[i] += 1

for j in range(x+1):
    if count_list[j] < j:
        ans += count_list[j]
print(ans)

Submission status: Accepted

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

    d = {}
    for ai in A:
        if d.get(ai):
            d[ai] += 1
        else:
            d[ai] = 1

    # print(d)
    ans = 0
    for k, v in d.items():
        if k <= v:
            ans += v - k
        else:
            ans += v

    print(ans)
    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
ans=0 
l=[int(i) for i in input().split()]
from collections import Counter 
c=Counter(l)
for i in sorted(c):
    if c[i]>=i :
        ans+=(c[i]-i)
    else:
        ans+=c[i]
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s={}
for a in list(map(int,input().split())):
    if a not in s:
        s[a]=1
    else:
        s[a]+=1

ans=0
for a in s:
    if s[a]>=a:
        ans+=s[a]-a
    else:
        ans+=s[a]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
ans = 0
cnt = 1
prev = a[0]
#print(a)

for i in range(1, n):
    # print('\n# i: ' + str(i))
    # print('# a[i]: ' + str(a[i]))
    if a[i] == prev:
#        print('same!')
        cnt += 1
    else:
        if cnt > prev:
#            print('cnt is larger!')
            ans += cnt - prev
        elif cnt < prev:
#            print('cnt is smaller!')
            ans += cnt
#        print('ans: ' + str(ans))
        prev = a[i]
        cnt = 1

if cnt > prev:
    ans += cnt - prev
elif cnt < prev:
    ans += cnt

print(ans)

Submission status: Accepted

Solution code:
import collections
N=int(input())
li=list(map(int,input().split()))
li_count=collections.Counter(li)
li_count_most=li_count.most_common()
ans=0
for k in range(len(li_count_most)):
    if li_count_most[k][0]<=li_count_most[k][1]:
        ans+=(li_count_most[k][1]-li_count_most[k][0])
    else:
        ans+=li_count_most[k][1]
print(ans)

Submission status: Accepted

Solution code:

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

S = {}

for a in A:
    S[a] = S.get(a, 0) + 1

cnt = 0
for k, v in S.items():
    if k > v:
        cnt += v
    else:
        cnt += min(v, v - k)

print(cnt)

Submission status: Accepted

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

ans = 0
#辞書型でデータを管理
dict = {}
for i in range(n):
    if a[i] not in dict:
        dict[a[i]] = 1
    else:
        dict[a[i]] += 1
for i in dict.keys():
    if i < dict[i]:
        #多い場合は減らすのが確定
        ans += dict[i] - i
    elif i > dict[i]:
        ans += dict[i]
print(ans)

Submission status: Accepted

Solution code:
import collections
n=int(input())
a=[int(i) for i in input().split()]
x=collections.Counter(a)
ans=0
for i in x:
    if x[i]!=i:
        ans+=[x[i],x[i]-i][x[i]>i]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n,*a=map(int,open(0).read().split())
print(sum(j*(i>j)+(j-i)*(i<j) for i,j in Counter(a).items()))

Submission status: Accepted

Solution code:
n=int(input())
a = list(map(int,input().split()))
cnt={}
for i in a:
    if i not in cnt:
        cnt[i]=1
    else:
        cnt[i]+=1
ans=0
for i in cnt:
    if cnt[i]>i:
        ans+=cnt[i]-i
    elif cnt[i]<i:
        ans+=cnt[i]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import collections

def solve(N: int, a: "List[int]"):
    counter = collections.Counter(a)
    aset = set(a)
    answer = 0
    for aa in aset:
        answer += (counter[aa]-aa) if counter[aa]>=aa else counter[aa]
    print(answer)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections as coll

def test():
    n = int(input())
    a = list(map(int,input().split()))
    b = coll.Counter(a)
    ans = 0
    for i,j in b.items():
        if i < j:
            ans += j - i
        elif i > j:
            ans += j
    print(ans)


if __name__ == "__main__":
    test()

Submission status: Accepted

Solution code:
from collections import defaultdict
N=int(input())
a=[int(item) for item in input().split()]

dd=defaultdict(int)

for item in a:
  dd[item]+=1


ans=0
for item in set(a):
  if dd[item]>=item:
    ans+=dd[item]-item
  else:
    ans+=dd[item]
print(ans)

Submission status: Accepted

Solution code:
def input():
    import sys
    return sys.stdin.readline().rstrip()


def main():
    import math
    import collections
    import itertools

    n = int(input())
    as_ = list(map(int, input().split()))
    counter_a = collections.Counter(as_)
    ans = 0
    for a, count in counter_a.items():
        if count == a:
            pass
        elif count < a:
            ans += count
        elif count > a:
            ans += count - a
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
a.sort()
i = 0
ans = 0
while i <= n-1:
    m = a[i]
    c = 0
    while i <= n-1 and a[i] == m:
        c += 1
        i += 1
    if c < m:
        ans += c
    elif c > m:
        ans += c - m
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
number = [ i for i in range(n+1)]
for i in range(n):
    if a[i]>n:
        ans +=1
    else:
        number[a[i]]-=1

for i in range(n):
    if number[i+1]==i+1:
        number[i+1]=0
    elif number[i+1]>0:
        number[i+1]= i+1-number[i+1]
    else:
        number[i+1]=abs(number[i+1])
    ans += number[i+1]
print(ans)

Submission status: Accepted

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


table = collections.defaultdict(int)
for x in a:
    table[x] += 1
ans = 0
for x in table:
    if x < table[x]:
        ans += table[x] - x

    elif x > table[x]:
        ans += table[x]

print(ans)

Submission status: Accepted

Solution code:
import collections as col

n = int(input())
li = list(map(int,input().split()))

dic = col.Counter(li)
k = len(list(dic.keys()))

likey = list(dic.keys())
ans = 0
for i in range(k):
  tmp = dic[likey[i]]
  if likey[i] <= tmp:
    ans += tmp - likey[i]
  else:
    ans += tmp
print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
A_C = collections.Counter(A)
#{[3:4, 5:4]}

ans = 0
for i in A_C.keys():#3,5
  if i > A_C[i]:
    ans += A_C[i]
  else:
    ans += A_C[i] - i
    
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
b = Counter(a)
ans = 0
for x,i in b.items():
  if x<i:
    ans+=i-x
  elif x>i:
    ans+=i
print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())
a = list(map(int, input().split()))

a_count = collections.Counter(a)

ans = 0
for key in list(a_count.keys()):
    if a_count[key] < int(key):
        ans += a_count[key]
    else:
        ans += a_count[key] - int(key)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
al = list(map(int, input().split()))
counter = Counter(al)
total = 0
for key, val in counter.items():
    if key > val:
        total += val
    elif key < val:
        total += val-key
print(total)

Submission status: Accepted

Solution code:
N = int(input())
array = list(map(int, input().split()))
dic = {}
ans = 0
for item in array:
    if item in dic:
        dic[item] += 1
    else:
        dic[item] = 1
# print(dic)
for key in dic.keys():
    item = dic[key]
    if item == 0:
        continue
    else:
        if item > key:
            ans += abs(item-key)
        elif item < key:
            ans += item
print(ans)

Submission status: Accepted

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

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

c=collections.Counter(l)

count=0
for k,v in c.items():
    if k<=v:
        count+=v-k
    else:
        count+=v
    
print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
res = 0
d = defaultdict(int)
a = sorted(a)
a += [1e9+1]
tmp = 0
for i in range(n+1):
    if tmp < a[i] and tmp != d[tmp]:
        if tmp < d[tmp]:
            res += d[tmp] - tmp
        else:
            res += d[tmp]
        tmp = a[i]
        d[tmp] += 1
    else:
        tmp = a[i]
        d[tmp] += 1

print(res)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
a.sort()
dif = []
c = Counter(a)
a = list(set(a))
for i in range(len(a)):
        if c[a[i]] - a[i] > 0:
                dif.append(c[a[i]] - a[i])
        elif c[a[i]] - a[i] < 0:
                dif.append(c[a[i]])
print(sum(dif))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
nums = {}

for a in A:
    if a in nums:
        nums[a]+=1
    else:
        nums[a] = 1
ans = 0

for a,amount in nums.items():
    if a==amount:
        continue
    else:
        if a>amount:
            ans += amount
        else:
            ans += amount - a
print(ans)

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = list(map(int, input().split()))
counter = collections.Counter(a)
ans = 0
for k, v in counter.items():
    if k > v:
        ans += v
    elif k < v:
        ans += v - k
print(ans)

Submission status: Accepted

Solution code:
def slove():
    import sys
    import collections
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    a = collections.Counter(a)
    cnt = 0
    for k, v in a.items():
        if k < v:
            cnt += (abs(k - v))
        elif k > v:
            cnt += v
    print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A = sorted(A)
cnt = 0
cur = 0
for i in range(1,N):
    if A[i]!=A[i-1]:
        a = A[i-1]
        if i-cur>=a:
            cnt += i-cur-a
        else:
            cnt += i-cur
        cur = i
a = A[-1]
if N-cur>=a:
    cnt += N-cur-a
else:
    cnt += N-cur
print(cnt)

Submission status: Accepted

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

D = dict()
for i in range(N):
    if A[i] not in D:
        D[A[i]] = 1
    else:
        D[A[i]] += 1
res = 0
for k,v in D.items():
    tmp = v - k 
    if tmp < 0:
        res += v
    else:
        res += tmp
print(res)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
C=[0]*100001
cnt=0
for i in range(N):
  if A[i]>10**5:
    cnt +=1
  else:
    C[A[i]] += 1
for j in range(100001):
  if C[j]<j:
    cnt=cnt+C[j]
  elif C[j]>j:
    cnt=cnt+C[j]-j
print(cnt)