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

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]

D = {}

for a in A:
    if not a in D:
        D[a] = 1
    else:
        D[a] = D[a] + 1

ret = 0
for k in D.keys():
    if D[k] > k:
        ret += D[k] - k
    elif D[k] < k:
        ret += D[k]

print(ret)

Solution (Python):
import collections
n = int(input())
a = list(map(int,input().split()))
d = collections.Counter(a)
c = 0
for x in d:
  c += max(d[x]-x,0) + d[x]*(d[x]<x)
print(c)

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

L = []

c = 1

for i in range(1, N):
	if a[i - 1] == a[i]:
		c += 1

	else:
		L.append([c, a[i - 1]])
		c = 1

L.append([c, a[-1]])

ans = 0
for i in range(len(L)):
	if L[i][0] >= L[i][1]:
		ans += L[i][0] - L[i][1]
	else:
		ans += L[i][0]

print(ans)

Solution (Python):
N = int(input())
l = list(map(int, input().split()))
unique_l = list(set(l))
unique_l.sort()
l.sort()
l.append(0)
ans = 0
j = 0
cnt =[]
for i in unique_l:
    temp = 0
    while l[j] == i and j < N:
        temp += 1
        j += 1
    cnt.append(temp)
for i, j in zip(unique_l, cnt):
    if i == j:
        pass
    elif j > i:
        ans += j - i
    elif j < i:
        ans += j
print(ans)

Solution (Python):
from collections import Counter
N = int(input())
ans = 0
for k, v in Counter(map(int, input().split())).items():
    if k < v:
        ans += v - k
    elif k > v:
        ans += v
print(ans)

Solution (Python):

n = int(input())

d = {}
ret = 0
for x in input().split():
    a = int(x)
    if a not in d:
        d[a] = 0

    if d[a] == a:
        ret += 1
    else:
        d[a] += 1

for k,v in d.items():
    if k!=v:
        ret += v
print(ret)

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

ret = 0
cnt = 0
n = 0

for i in range(N):
    if n == 0:
        cnt += 1
        n = A[i]
        continue

    if n != A[i]:
        ret += cnt if cnt < n else cnt - n
        n = A[i]
        cnt = 1
    else:
        cnt += 1

if cnt != n:
    ret += cnt if cnt < n else cnt - n

print(ret)

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int,input().split()))
c = Counter(a)
ans = 0
for i in c:
    if c[i]<i:
        ans+=c[i]
    else:
        ans+=c[i]-i
print(ans)

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
D=dict()
for i in range(N):
    if A[i] in D:
        D[A[i]]+=1
    else:
        D[A[i]]=1
ans=0
for key in D:
    if D[key]<key:
        ans+=D[key]
    else:
        ans+=D[key]-key
print(ans)

Solution (Python):
from collections import defaultdict
N=int(input())
A=[int(i) for i in input().split()]
dd=defaultdict(int)
for a in A:
    dd[a]+=1
ans=0
for d,s in dd.items():
    
    if s>=d:
        ans+=s-d
    else:
        ans+=s
print(ans)

Solution (Python):
n = int(input())

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

b = {}

for i in a:
    if i in b:
        b[i] += 1
    else:
        b[i] = 1
ans = 0
for i in b:
    if i < b[i]:
        ans += b[i]-i
    elif i > b[i]:
        ans += b[i]
print(ans)

Solution (Python):
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

N = read()
a = reads()
freq = defaultdict(lambda: 0)
for x in a:
  freq[x] += 1
ans = sum(v - k if v >= k else v for k, v in freq.items())
print(ans)

Solution (Python):
from collections import Counter

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

c = Counter(a)

ans = 0

for key, value in c.items():
	if key > value:
		ans += value
	else:
		ans += value - key

print(ans)

Solution (Python):



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

dic = {}

for e in A:
    if e not in dic:
        dic[e] = 1
    else:
        dic[e] += 1

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

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

a.sort()

count = 0

while a:
    k = a.pop(-1)
    k_count = 1
    while (len(a) > 0) and (a[-1] == k):
        a.pop(-1)
        k_count += 1
    if k_count < k:
        count += k_count
    else:
        count += k_count - k
print(count)

Solution (Python):
from pprint import pprint
n=int(input())
a=[int(i) for i in input().split()]
d={}
cnt=0
for i in a:
    if str(i) not in d:
        d[str(i)]=1
    else:
        d[str(i)]+=1
for i in d:
    if int(i)>d[i]:
        cnt+=d[i]
    else:
        if int(i)<d[i]:
            cnt+=d[i]-int(i)
print(cnt)

Solution (Python):
from collections import defaultdict

N = int(input())

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

C = defaultdict(int)

for a in A: C[a] += 1
    
ans = 0
for k in C:
    if C[k] < k:
        ans += C[k]
    elif C[k] > k:
        ans += C[k]-k
        
print(ans)

Solution (Python):
from collections import defaultdict as dd

n = int(raw_input().strip())
ais = map(int, raw_input().strip().split())

coll = dd(int)
for ai in ais:
    coll[ai] += 1

total = 0
for key, value in coll.iteritems():
    if value < key:
        total += value
    else:
        total += value - key

print total

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

s=[0 for i in range(n+1)]
count=0

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

for i in range(n+1):
  if s[i]>=i:
    count+=s[i]-i
  else:
    count+=s[i]
   
print(count)

Solution (Python):


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

num = {}
for x in a:
    if x in num:
        num[x] += 1
    else:
        num[x] = 1

res = 0
for x in num:
    if num[x] >= x:
        res += num[x] - x
    else:
        res += num[x]

print(res)

Solution (Python):
import collections
a = int(input())
ppp = [0]
ppp = list(map(int,input().split()))
count = 0             
data = collections.Counter(ppp)
aaa = 0
for k, v in data.items():
    if int(k) > int(v):
        aaa = v
        count += aaa
    elif int(k) < int(v):
        aaa = v-k
        count += aaa
    
print(count)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
import collections
data=collections.Counter(a)
count=0
for i,j in data.items():
    if i<j:
        count+=j-i
    elif j<i:
        count+=j
print(count)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a.sort()

res = 0
cnt = 1
for i in range(1, n) :
    if a[i-1] == a[i] :
        cnt += 1
    elif cnt >= a[i-1] :
        res += cnt - a[i-1]
        cnt = 1
    else :
        res += cnt
        cnt = 1
else :
    if cnt >= a[-1] :
        res += cnt - a[-1]
    else :
        res += cnt
    print(res)

Solution (Python):
N=int(input())
nums=list(map(int,input().split(" ")))
ap={}
co=0 
for i in nums:
	if i in ap:
		ap[i]+=1
	else:
		ap[i]=1

for i in ap:
    if ap[i]<i:
        co+=ap[i]
    else:
        co+=(ap[i]-i)
print(co)

Solution (Python):
from collections import Counter
n = input()
xs = [int(c) for c in input().split()]
c = Counter(xs)
ans = 0
for x, cnt in c.items():
  ans += cnt if cnt < x else (cnt - x)
print(ans)

Solution (Python):
n = int(input())
a = map(int, input().split())
ans = 0
cnt = [0] * n + [0]
for i in a:
  if i > n:
    ans += 1
  else:
    cnt[i] += 1
for i in range(n + 1):
  ans += cnt[i] - i if cnt[i] >= i else cnt[i]
print(ans)

Solution (Python):
n = int(input())
a = [int(e) for e in input().split(' ')]

ans = 0
freq = {}
for e in a:
    if e not in freq:
        freq[e] = 0
    freq[e] += 1
    if (freq[e] > e):
        ans += 1
        freq[e] -= 1

for e in freq:
    if e != freq[e]:
        ans += freq[e]

print(ans)

Solution (Python):
length = input()
nums = input().split(" ")

numdict = {}
for i, num in enumerate(nums):
	if num not in numdict:
		numdict[str(num)] = 1
	else:
		numdict[str(num)] += 1

ret = 0
for k,v in numdict.items():
	if int(k) > int(v):
		ret += int(v) 
	else:
		ret += int(v) - int(k)

print(ret)

Solution (Python):
input()
nums = input().split(" ")

sett = set(nums)
dicto = {}
for a in range(len(sett)):
    dicto[str(sett.pop())] = 0
for a in nums:
    dicto[a] += 1

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

Solution (Python):
from itertools import groupby
from operator import itemgetter

line = input()
line = input().split(" ")

a = []
for x in line:
        a.append(int(x))

a.sort()

ret = 0
for k,g in groupby(a):
        g = list(g)
        if len(g)>k:
                ret += len(g)-k
        elif len(g)<k:
                ret += len(g)

print(ret)

Solution (Python):
from collections import Counter

N=int(input())
a_lst=[int(_) for _ in input().split()]
counter=Counter(a_lst)
count=0
for k, v in counter.items():
    if v>=k:
        count+=v-k
    else:
        count+=v
print(count)

Solution (Python):
N = int(raw_input())
a = [int(x) for x in raw_input().split(" ")]

lookup = {}
for val in a:
    lookup[val] = 1 + (lookup[val] if lookup.has_key(val) else 0)

remove_num = 0
for val, count in lookup.items():
    remove_num += count if count < val else (count - val)

print remove_num

Solution (Python):


N = int(input())

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

cnt = {}

for a in As:
    cnt.setdefault(a, 0)
    cnt[a] += 1

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

print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    a = LI()
    r = 0
    d = collections.Counter(a)
    for i,c in d.items():
        if i < c:
            r += c-i
        else:
            r += c%i

    return r



print(main())

Solution (Python):
N = int(input())
A = [int(n) for n in input().split()]

from collections import Counter

cnt = Counter(A)

res = 0
for k, v in cnt.items():
    if k <= v:
        res += v-k
    else:
        res += v

print(res)

Solution (Python):
from collections import Counter

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

    counter = Counter(a)
    ans = 0
    for ele, count in counter.items():
        if ele < count:
            ans += count - ele
        elif count < ele:
            ans += count

    print(ans)

Solution (Python):
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce
from operator import xor
from heapq import heappush, heappop
INF = float("inf")
sys.setrecursionlimit(10**7)


dy4, dx4 = [0, -1, 0, 1], [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W
def ceil(a, b): return (a + b - 1) // b


def main():
    N = int(input())
    a = Counter(list(map(int, input().split())))

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

if __name__ == '__main__':
    main()

Solution (Python):
length = raw_input()
arr = map(int, raw_input().split())

from collections import defaultdict
d = defaultdict(int)
count = 0
for a in arr:
	if d[a] == a:
		count += 1
		continue
	d[a] += 1
for k,x in d.items():
	if k != x:
		count += x

print count

Solution (Python):
from collections import Counter

N = int(input())
text = input().split()

count = Counter()
for i in range(0, N):
    count[int(text[i])] += 1
remove = 0
for key, item in count.items():
    if key > item:
        remove = remove + item
    elif key < item:
        remove = remove + (item - key)
print(remove)

Solution (Python):
from collections import defaultdict

n = int(raw_input())
arr = map(int, raw_input().split())

cnt = defaultdict(int)
for x in arr:
    cnt[x] += 1

ans = 0
for k in cnt:
    if cnt[k] >= k:
        ans += cnt[k] - k
    else:
        ans += cnt[k]

print ans

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

Solution (Python):
N = input()
x = map(int,raw_input().split(" "))


v = {}
for q in x:
    if not q in v:
        v[q] = 0
    v[q] += 1


ans = 0
for key in v.keys():
    val = v[key]
    
    if val < key:
        ans += val
    elif val > key:
        ans += val - key
print ans

Solution (Python):


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

hash = {}
for elem in a:
    if not elem in hash:
        hash[elem] = 1
    else:
        hash[elem] += 1

ans = 0
for key, value in hash.items():
    if value > key:
        ans += value - key
    elif value < key:
        ans += value

print(ans)

Solution (Python):
from collections import defaultdict

d = defaultdict(int)

ans = 0

n = int(input())

for x in [int(y) for y in input().split()]:
    d[x] += 1

for k,v in d.items():
    if k > v:
        ans += v
    else:
        ans += v - k

print(ans)

Solution (Python):

import collections

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

a_col = collections.Counter(a)
remove_count = 0

for k in a_col:
	if k > a_col[k]:
		remove_count += a_col[k]
	elif k < a_col[k]:
		remove_count += a_col[k] - k

print remove_count

Solution (Python):
from collections import *
input()
r = 0
for k, v in Counter(map(int, input().split())).items():
    if v < k:
        r += v
    else:
        r += v - k
print(r)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
tmp = 1
cnt = 0
S = 0
a.sort()
a.append(0)
for i in range(N):
    if a[i] == a[i + 1]:
        tmp += 1
    else:
        if tmp > a[i]:
            cnt += tmp - a[i]
        elif tmp < a[i]:
            cnt += tmp
        tmp = 1
print(cnt)

Solution (Python):
import collections
N = int(input())
a = list(map(int, input().split()))
nums = collections.defaultdict(int)
ans=0

for i in range(N):
    nums[a[i]] += 1

num_flag = list(nums.keys())
    
for i in range(len(num_flag)):
    if nums[num_flag[i]] >= num_flag[i]:
        ans += nums[num_flag[i]] - num_flag[i]
    else:
        ans += nums[num_flag[i]]

print(ans)

Solution (Python):
import sys,collections
sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

n = I()
As = Is()
d = collections.defaultdict(int)
for a in As:
    d[a] += 1 
lis = list(d.items())
ans = 0
for e in lis:
    if e[0] - e[1] > 0:
        ans += e[1]
    else:
        ans += e[1] - e[0]
print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
a.sort()
a.append(0)
answer = 0
i = 0
counter = 1
while i <= n-1:
	if a[i] == a[i+1]:
		counter += 1
		i += 1
	else:
		if counter != a[i]:
			if counter > a[i]:
				answer += (counter - a[i])
				i += 1
				counter = 1
			else:
				answer += counter
				i += 1
				counter = 1
		else:
			i += 1
			counter = 1
print(answer)

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

d = defaultdict(int)
for i in range(n):
    d[a[i]] += 1

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

Solution (Python):
import sys
N=int(input())
A=list(map(int,input().split()))
A.sort()
ans=0
current=A.pop()
num=1
if len(A)==0:
  if num<current: ans+=num
  else: ans+=num-current
while A:
  if A[-1]==current:
    num+=1
    current=A.pop()
    if len(A)==0:
      if num<current: ans+=num
      else: ans+=num-current
  else:
    if num<current: ans+=num
    else: ans+=num-current
    current=A.pop()
    num=1
    if len(A)==0:
      if num<current: ans+=num
      else: ans+=num-current
print(ans)

Solution (Python):
from collections import Counter

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

cnt = Counter(As)
ans = 0
for A, num in cnt.items():
    if A <= num:
        ans += num - A
    else:
        ans += num

print(ans)

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string

N = int(input())
c = Counter(map(int, input().split()))
ans = 0
for k, v in c.items():
    if k < v:
        ans += v - k
    elif k > v:
        ans += v
print(ans)

Solution (Python):

"""
Created on Sun Nov 19 13:32:42 2017

@author: yamakoshi
"""

n = int(input())

seq = {}
a1n = map(int, input().split())
for a in a1n:
    if a not in seq:
        seq[a] = 1
    else:
        seq[a] += 1

ret = 0
for a in seq:
    if not seq[a] == a:
        if seq[a]-a > 0:
            ret += seq[a]-a
        else:
            ret += seq[a]

print(ret)

Solution (Python):
N = int(input())
A = map(int, (input().split()))
d = dict()
for a in A:
    if a not in d:
        d[a] = 1
    else:
        d[a] = d[a] + 1
ans = 0
for k, v in d.items():
    if k > v:
        ans = ans + v
    else:
        ans = ans + v - k
print(ans)

Solution (Python):

def main():
    N = int(input())
    a = list(map(int, input().split()))
    dic = {}
    for i in a:
        dic[i] = 1 + dic.get(i, 0)

    res = 0
    for k, v in dic.items():
        if k > 10**5:
            res += v
        elif k == v:
            continue
        elif k > v:
            res += v
        else:
            res += v- k

    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
N = int(input())
src = list(map(int,input().split()))
ctr = Counter(src)
ans = 0
for k,v in ctr.items():
    if v >= k:
        ans += (v-k)
    else:
        ans += v
print(ans)

Solution (Python):
n = int(input())
a = input().split()

from collections import Counter

b = Counter(a)
remove = 0

for x in b:
    if int(x) > b[x]:
        remove += b[x]
    else:
        remove += b[x]-int(x)

print(remove)

Solution (Python):
N=int(input())
a=[int(i) for i in input().split()]
data={}
for num in a:
        if num in data:
                data[num]+=1
        else:
                data[num]=1

ans=0
for key in data.keys():
        if data[key]<key:
                ans+=data[key]
        else:
                ans+=data[key]-key
print(ans)

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

b = [0] * (N + 1)
ans = 0
for ai in a:
    if ai <= N:
        b[ai] += 1
    else:
        ans += 1
for i in range(N + 1):
    if b[i] > i:
        ans += b[i] - i
    elif b[i] < i:
        ans += b[i]

print(ans)

Solution (Python):



from collections import defaultdict

def solve():
    n = int(input())
    ad = defaultdict(int)
    for i in input().split():
       ad[int(i)] += 1
    
    ans = 0
    for k, v in ad.items():
        if k > v:
            ans += v
        if v > k:
            ans += v - k
    print(ans)

if __name__=="__main__":
    solve()

Solution (Python):

from collections import Counter
def inpl(): return tuple(map(int, input().split()))

N = int(input())
A = inpl()
C = Counter(A)
res = 0

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

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

cnt = {}
for x in a:
    if x not in cnt:
        cnt[x] = 0
    cnt[x] += 1


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

print(ans)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
d = dict()
ans = 0
for i in a:
    if i not in d.keys():
        d[i] = 0
    d[i] += 1
for x, c in d.items():
    if c < x:
        ans += c
    else:
        ans += c - x
print(ans)

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

cnt = {}
for ai in a:
    if ai in cnt:
        cnt[ai] += 1
    else:
        cnt[ai] = 1

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

Solution (Python):
import collections

n = int(input())
a = collections.Counter(list(map(int, input().split())))
cnt = 0
for key in a.keys():
    if a[key] > key:
        cnt += a[key] - key
    elif a[key] < key:
        cnt += a[key]
print(cnt)

Solution (Python):
from collections import Counter
N = int(input())
c = Counter(list(map(int, input().split())))

ans = 0
for i in c:
  if c[i] < i:
    ans += c[i]
  elif c[i] > i:
    ans += c[i] - i

print(ans)

Solution (Python):
from collections import Counter
N = int(input())
A = Counter(map(int, input().split()))
ans = 0
for k, v in A.items():
    if k <= v:
        ans += v - k
    else:
        ans += v
print(ans)

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

Solution (Python):
from collections import Counter

input()
ac = Counter(map(int, input().split()))
ans = 0

for a, c in ac.items():
    if a > c:
        ans += c
    else:
        ans += c - a

print(ans)

Solution (Python):
from collections import Counter

N = input()
A = map(int, raw_input().split())
C = Counter(A)
ans = 0

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

print ans