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 = map(int, input().split(' '))
sort_a = sorted(a)

bucket = {}

for i in sort_a:
    if i not in bucket:
        bucket[i] = 1
    else:
        bucket[i] += 1

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

print(delete)

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


from collections import defaultdict
d = defaultdict(int)


for i in a:
	d[i] += 1



ans = 0
for i in d.keys():
	x = d[i]

	if x == i: continue
	if x < i: ans += x
	if x > i: ans += (x - i)


print(ans)

Solution (Python):
from itertools import groupby
n, s = input(), sorted(map(int, raw_input().split()))
v, ans = [(i, len(list(t))) for i, t in groupby(s)], 0
for i, t in v:
    if i != t:
        ans += ((t - i) if i < t else t)
print ans

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

z = {}
for i in a:
    try:
        z[i] += 1
    except KeyError:
        z[i] = 1

ans = 0
for q in z.keys():
    if z[q] > q:
        ans += z[q] - q
    elif z[q] < q:
        ans += z[q]

print(ans)

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

Solution (Python):
from itertools import groupby
n, s = input(), sorted(map(int, raw_input().split()))
v, ans = [(i, len(list(t))) for i, t in groupby(s)], 0
for i, t in v:
    if i == t:
        continue
    ans += ((t - i) if i < t else t)
print ans

Solution (Python):
import collections
N = int(input())
P = list(map(int,input().split()))
ans = 0
P.sort()
cnt = collections.Counter(P)
for i in cnt:
    s = cnt[i]
    if s > i:
        t =  s - i
        ans += t
    elif s < i:
        ans += s
print(ans)

Solution (Python):
a=input()
b=map(int,raw_input().split())
b.sort()
c=[]
d=[]
max=0
loc=-1
for i in b:
    if max!=i:
        c.append(i)
        d.append(1)
        max=i
        loc+=1
    else:
        d[loc]+=1
ans=0
for i in range(len(c)):
    if c[i]>d[i]:
        ans+=d[i]
    elif c[i]<d[i]:
        ans+=d[i]-c[i]

print ans

Solution (Python):

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

x = {}

for i in a:
    if i not in x:
        x[i] = 0
    x[i] = x[i]+1

out = 0
for k in x:
    v = x[k]
    if v < k:
        out = out + v
    if v > k:
        out = out + v - k

print(out)

Solution (Python):
n = int(raw_input())
a = map(int,raw_input().split(" "))

dic = {}

for x in a:
  if x not in dic:
    dic[x] = 1
  else:
    dic[x] += 1

ans = 0
    
for x in dic:
  
  if x > dic[x]:
    ans += dic[x]
  elif x < dic[x]:
    ans += (dic[x]-x)

print ans

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

res = 0

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

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

print(res)

Solution (Python):
from collections import Counter

N = int(input())
lis = list(map(int,input().split()))
c = dict(Counter(lis))
count = 0
for key,value in c.items():
  if key > value:
    count += value
  else:
    count += (value-key)
print (count)

Solution (Python):
N = int(input())
a = []
ainfo = []
astrs = input().split()
for astr in astrs:
    a.append(int(astr))
sorteda = sorted(a)



for pos in range(N):
    if pos == 0:
        ainfo.append([sorteda[pos], 1])
    else:
        if sorteda[pos]==sorteda[pos-1]:
            ainfo[-1][1] += 1
        else:
            ainfo.append([sorteda[pos], 1])

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

Solution (Python):
def c_good_sequence(N, A):
    from collections import Counter
    count = Counter(A)  
    ans = 0
    for k, v in count.items():
        if k == 1:
            if v == 1:
                continue
            else:
                ans += v - k
        elif k != v:
            ans += v % k
    return ans
  
N = int(input())
A = [int(_) for _ in input().split()]
print(c_good_sequence(N, A))

Solution (Python):
debug = False
if debug:
    a = [2, 7, 1, 8, 2, 8, 1, 8]
    a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5]
    N = len(a)
else:
    N = int(input())
    a = list(map(int, input().split()))

assert len(a) == N
d = {}
for ai in a:
    if ai in d:
        d[ai] += 1
    else:
        d[ai] = 1
s = 0
for k, v in d.items():
    t = min(abs(k - v), v)
    t = 0
    if k > v:
        t = v
    elif k == v:
        pass
    else:
        t = v - k
    s += t

print(s)

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

counter = [0]*(10**5+10)
ans = 0
for i in range(N):


    if a[i] < 10**5 + 3:
        counter[a[i]] += 1

    else:
        ans += 1

for i in range(10**5+3):
    if counter[i] < i:
        ans += counter[i]
 
    else:
        ans += counter[i] - i
 

 
print(ans)

Solution (Python):
import collections

N = int(input())
a = input().split(" ")

test = collections.Counter(a)
test_key = list(test.keys())
test_value = list(test.values())

ans = 0
for i in range(len(test_key)):
    if int(test_key[i]) > test_value[i]:
        ans += test_value[i]
    elif int(test_key[i]) < test_value[i]:
        ans += test_value[i] - int(test_key[i])

print(ans)

Solution (Python):
from collections import Counter

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




num = 0
for k, v in a.most_common():
    if k - v > 0:
        num += v
    else:
        num +=  v - k

print(num)

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

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

A = sorted(A)
set_A = sorted(list(set(A)))

min_elim = 0
count = 0
sub_count = 0
index = 0

for element in set_A:
    for i in range(index, len(A)):
        if A[i] == element:
            count += 1
        else:
            index = i
            break
    if count > element:
        sub_count = count - element
    elif count < element:
        sub_count = count
    min_elim += sub_count
    count = 0
    sub_count = 0

print(min_elim)

Solution (Python):
import collections

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

c = collections.Counter(a)
k = list(c.keys())
v = list(c.values())

count = 0
for i in range(len(k)):
    if k[i] < v[i]:
        count += v[i] - k[i]
    elif k[i] > v[i]:
        count += v[i]

print(count)

Solution (Python):
N=int(input())
a=[0 for i in range(N)]
st=input().split()
for i in range(N):
    a[i]=int(st[i])
aa=sorted(a)
count=0
nowp=aa[0]
aa.append(10**9+1)
nowcount=1
for i in range(1,N+1):
    if aa[i-1]==aa[i]:
        nowcount+=1
    else:
        nowp=aa[i]
        if nowcount<aa[i-1]:
            count+=nowcount
        else:
            count+=nowcount-aa[i-1]
        nowcount=1
print(count)

Solution (Python):

from collections import Counter
input()
A = [int(x) for x in input().split()]
c = Counter(A)

remove = 0
for num, count in c.most_common():
    if count < num:
        remove += count
    else:
        remove += (count - num)
print(remove)

Solution (Python):
N = raw_input()
array = map(int, raw_input().split())



dic = {}

while len(array):
    key = array.pop(0)
    if key not in dic:
        dic[key] = 1
    else:
        dic[key] = dic[key] + 1




reduce_num = 0


for key in dic:
    if dic[key] == key:
        pass
    elif dic[key] > key:
        reduce_num = reduce_num + dic[key] - key
    else:
        reduce_num = reduce_num + dic[key]
        
print reduce_num

Solution (Python):
n = input()
ns = list(map(int, input().split()))
nums = {}
for i in ns:
    if i not in nums:
        nums[i] = 1
    else:
        nums[i] += 1

count = 0

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


print(count)

Solution (Python):



import math

def slove(n, a):
    dict = {}
    for i in range(n):
        if a[i] in dict:
            dict[a[i]] += 1
        else:
            dict[a[i]] = 1

    count = 0
    for k,v in dict.items():
        if int(k) < v:
            count += (v - int(k))
        elif int(k) != v:
            count += v
    return count

if __name__ == "__main__":
    n = int(input())
    a = input().split()

    print(slove(n, a))

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

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

print(ans)

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

count = 0

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

print(count)

Solution (Python):

import math

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

ans = {}
for b in a:
    if not b in ans:
        ans[b] = 1
    else:
        ans[b] += 1

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

Solution (Python):
N=int(input())
A=[]
A=list(map(int,input().split()))
B={}
for i in range(N):
    if(str(A[i]) in B):
        B[str(A[i])]+=1
    elif(str(A[i]) not in B):
        B[str(A[i])]=1

ans=0

for key,value in B.items():
    key=int(key)
    if(key<value):
        ans+=(value-key)
    elif(key==value):
        pass
    elif(key>value):
        ans+=value
        
print(ans)

Solution (Python):
N = int(input())
ali = list(map(int,input().split()))
sali = sorted(ali)
now = None
num = []
suu = []
kari = 0 
for i in range(N):
    if sali[i] == now:
        kari += 1
    else:
        if kari > 0:
            suu.append(kari)
        kari = 1 
        num.append(sali[i])
        now = sali[i]
suu.append(kari)
kari = 0 
for i in range(len(suu)):
    if num[i] > suu[i]:
        kari += suu[i]
    elif num[i] < suu[i]:
        kari += suu[i] - num[i]
print(kari)

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

num=[]
num2=[]
count=0
m=a[0]
k=0
ans=0

for i in range(n):
    if a[i]==m:
        count=count+1
    else:
        num.append(m)
        num2.append(count)
        count=1
        m=a[i]
    if i==n-1:
        num.append(m)
        num2.append(count)

for i in range(len(num)):
    if num2[i]-num[i]>0:
        ans=ans+num2[i]-num[i]
    elif num2[i]-num[i]<0:
        ans=ans+num2[i]

print(ans)

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

a.sort()
cnt = 1
ans = 0
key = a[0]

for i in range(1,len(a)):
    if a[i] == key:
        cnt += 1
    else:
        if cnt >= a[i-1]:
            ans += ( cnt - a[i-1] )
        else:
            ans += cnt

        cnt = 1
        key = a[i]

if cnt >= a[len(a)-1]:
    ans += ( cnt - a[len(a)-1] )
else:
    ans += cnt

print(ans)

Solution (Python):


from collections import Counter


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


counter = Counter(a)


count = 0
for k, v in counter.most_common():
    if k > v:
        count += v
    elif k < v:
        count += v - k


print(count)

Solution (Python):

N = int(input())
a = list(map(int, input().split()))
num = [0] * (N + 1)
ans = 0
for i in range(N):
    if a[i] > N:
        ans += 1
    else:
        num[a[i]] += 1

ans += sum([(0 if (num[i] == 0) else num[i] - i if (num[i] >= i) else num[i]) for i in range(N + 1)])
print(ans)

Solution (Python):
import collections
N=input()
a=map(int,raw_input().split())
b = collections.Counter(a)


cnt=0
for i,j in b.items():
	if i<j:
		cnt+=j-i
	elif i>j:
		cnt+=j
	else:
		pass

print cnt

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
counter = Counter(a)
res = 0
for key, value in counter.items():
    if key < value:
        res += value - key
    elif key > value:
        res += value
print(res)

Solution (Python):



from collections import Counter

def main():
    n = int(input())
    A = list(map(int, input().split()))
    counts = Counter()
    for a in A:
        counts[a] += 1
    remove_count = 0
    for i, c in counts.items():
        if c < i:
            remove_count += c
        else:
            remove_count += c - i
    print(remove_count)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

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

ans = 0
cnt = Counter(a_list)
for num in cnt:
	if (cnt[num] > num):
		ans += cnt[num]-num
	elif (cnt[num] < num):
		ans += cnt[num]
print(ans)

Solution (Python):
import collections

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

ans = 0
count_dict = collections.Counter(a)
for k, v in count_dict.items():
    if k < v:
        ans = ans + abs(int(k)-int(v))
    elif k > v:
        ans = ans + int(v)

print(ans)

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

count_a = {}
for n in range(N):
    if a[n] in count_a.keys():
        count_a[a[n]] += 1
    else:
        count_a[a[n]] = 1

result = 0
for n in list(count_a.keys()):
    if count_a[n] != n:
        result = result + count_a[n] if count_a[n] < n else result + count_a[n] - n

print(result)

Solution (Python):
from collections import Counter
n = input()
a = list(map(int, input().split()))
c = list(set(a))
b = Counter(a)
ans = 0
for i, l in b.most_common():
    if i > l:
        ans += l
    elif i < l:
        ans += l - i
print(ans)

Solution (Python):
nb = int(input())
l = list(map(int,input().split()))
d = dict()
m = 0
for i in l:
   if i in d:
      d[i] += 1
   else:
      d[i] = 1
for k in d:
   if d[k] < k:
      m += d[k]
   else:
      m += d[k] - k
print(m)

Solution (Python):
N=int(input())
a=[int(i) for i in input().split()]
a=sorted(a)
a.append(0)
ans=0
num_count=0
prev=0
for ai in a:
    
    if ai!=prev:
        if num_count>=prev:
            ans+=num_count-prev
        else:
            ans+=num_count
        num_count=1
    else:
        num_count+=1
    prev=ai

print(ans)

Solution (Python):
from collections import Counter


def main():
    N = int(input())
    A = map(int, input().split())
    c = Counter(A)

    x = [
        (num, count, count - num)
        for num, count in c.items()
        if count != num
    ]

    ans = 0
    for num, count, diff in x:
        if diff > 0:
            ans += diff
        else:
            ans += count
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
n=input()
data = [int(i) for i in input().split(" ")]
counter = Counter(data)
n=0
for i, c in counter.most_common():
  if i>c:
    n+=c
  elif c>i:
    n+=c-i
print(n)

Solution (Python):
from collections import Counter
N = int(input())
C = Counter([int(i) for i in input().split()])
ans = 0
for i in C:
    if(C[i] > i):
        ans += C[i]-i
    elif(C[i] < i):
        ans += C[i]
print(ans)

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

from collections import Counter
c = Counter(a)

ans = 0
for label, num in c.most_common():
    if(label > num):
        ans += num
    elif(label < num):
        ans += num - label
        
print(ans)

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

Adict = {}

for a in A:
    if str(a) in Adict:
        Adict[str(a)] += 1
    else:
        Adict[str(a)] = 1

ct = 0

for i in Adict:
    if int(i) < Adict[i]:
        ct += (Adict[i]-int(i))
    elif int(i) > Adict[i]:
        ct += Adict[i]
print(ct)

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
d = {}
for i in a:
    if i in d:
        d[i] += 1
    else:
        d[i] = 1
count = 0
for key, value in d.items():
    if key < value:
        count += value - key
    elif key > value:
        count += value
print (count)

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

dic = {}
for n in a:
    dic.setdefault(n, 0)
    dic[n] += 1

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

print(s)

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

b = {}

for i in a:
    if i in b:
        b[i] += 1
    else:
        b[i] = 1

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

print(ans)

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

dic = defaultdict(int)
for i in range(N):
    dic[A[i]] += 1

ans = 0
for k in dic.keys():
    if dic[k] >= k:
        ans += dic[k] - k
    else:
        ans += dic[k]

print(ans)

Solution (Python):
from collections import Counter

def main():
    N = int(input())
    A = tuple(map(int, input().split()))

    counter = Counter(A).most_common()
    ans = 0
    for i, j in counter:
        if i > j:
            ans += j
        else:
            ans += j - i
    print(ans)

main()

Solution (Python):



from collections import Counter

N = int(input())
a = [int(i) for i in input().split()]
c = Counter(a)
cnt = 0
for i, j in c.items():
    if i < j:
        cnt += (j-i)
    elif i == j:
        continue
    elif i > j:
        cnt += j
    else:
        continue
print(cnt)

Solution (Python):
n = int(input())
a = list(map(str, input().split()))
dic = {}
for num in a:
    if num in dic:
        dic[num] += 1
    else:
        dic[num] = 1
ans = 0
for num in dic:
    if int(num) > dic[num]:
        ans += dic[num]
    else:
        ans += min(dic[num], abs(int(num)-dic[num]))
print(ans)

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

count = 0
for key in count_a.keys():
    if key < count_a[key]:
        count += count_a[key] - key
    elif key == count_a[key]:
        count += 0
    else:
        count += count_a[key]

print(count)

Solution (Python):
N = int(input())
an = map(int,input().split())
from collections import Counter
c = Counter(an)
ans = 0
for a,b in c.items():
    if b-a>=0:
        ans += b-a
    else:
        ans += b
print(ans)

Solution (Python):
from collections import OrderedDict, deque, Counter


def solve():
    N = int(input())
    A = list(map(int, input().split(" ")))
    count_dict = Counter(A)

    cnt = 0
    for key, value in count_dict.items():
        if key > value:
            cnt += value
        else:
            cnt += value - key

    print(cnt)


solve()

Solution (Python):
m = {}

N = int(input())
a = [int(x) for x in input().split()]

c = 0
for b in a:
    if b in m.keys():
        if m[b] >= b:
            c += 1
        else:
            m[b] += 1
    else:
        m[b] = 1

for k, v in m.items():
    if k > v:
        c += v

print(c)

Solution (Python):
import collections

num = int(input())
val = [int(x) for x in input().split()]

cnt = collections.Counter(val)
res = 0
for x in cnt:
    if cnt[x] > x:
        res += cnt[x] - x
    elif cnt[x] < x:
        res += cnt[x]

print(res)

Solution (Python):
import sys

line = sys.stdin.readline().strip()
N = int(line)

line = sys.stdin.readline().strip()
a = [int(x) for x in line.split()]

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

r = 0
for k in xs:
    if xs[k] < k:
        r += xs[k]
    elif xs[k] > k:
        r += xs[k] - k

print(r)

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
cnt={}
for e in a:
  if e in cnt:
    cnt[e]+=1
  else:
    cnt[e]=1
ans=0
for e in a:
  if cnt[e]==0:
    continue
  else:
    if cnt[e] > e:
      ans+=cnt[e]-e
    elif cnt[e] < e:
      ans+=cnt[e]
    cnt[e]=0
print(ans)

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

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

dic = {}

for num in a:
	if num in dic:
		dic[num] += 1
	else:
		dic[num] = 1

ans = 0
for key,value in dic.items():
	if key > value:
		ans += value

	else:
		ans += (value - key)


print(ans)