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:
N = int(input())
A = list(map(int, input().split()))
d = dict()
for i in range(N):
    if d.get(A[i]) is None:
        d[A[i]] = 1
    else:
        d[A[i]] += 1
ans = 0
for key, value in d.items():
    if key <= value:
        ans += value - key
    else:
        ans += value
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
#a, b = map(int,input().split())
al = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
ans = 0
dic = {}
for a in al:
    dic[a] = dic.get(a, 0)+1

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

Submission status: Accepted

Solution code:
from collections import *
n=int(input())
l=list(map(int,input().split()))
d=defaultdict(int)
for i in l:
    d[i]+=1
c=0
for i in d:
    if(i!=d[i]):
        if(d[i]>i):
            c+=d[i]-i
        else:
            c+=d[i]
print(c)

Submission status: Accepted

Solution code:
import collections

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

a_cnt = collections.Counter(a)
#print(a_cnt)
cnt = 0
for item in a_cnt.items():
    if item[0]<item[1]:
        cnt += item[1]-item[0]
    elif item[0]>item[1]:
        cnt += item[1]
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
from collections import Counter
counter = dict(Counter(A))
ans = 0
for num in set(A):
    if counter[num] >= num:
        ans += counter[num] - num
    else:
        ans += counter[num]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
A = list(map(int,input().split()))
hashmap = defaultdict(int)
for a in A:
  hashmap[a]+=1
ans = 0
for k, v in hashmap.items():
  if k < v:
    ans+=v-k
  elif k > v:
    ans+=v
print(ans)

Submission status: Accepted

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

cnt = 0
for i, j in A.items():
    if i > j:
        cnt += j
    elif i < j:
        cnt += j-i
print(cnt)

Submission status: Accepted

Solution code:
# import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial, gcd
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10 ** 7)
enu = enumerate
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep="\n")

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

d = defaultdict(int)
for a in A:
    d[a] += 1
cnt = 0
for key, val in d.items():
    if key <= val:
        cnt += val - key
    else:
        cnt += val
print(cnt)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    A = LIST()

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

A_c = Counter(A)

ans = 0
for k, v in zip(A_c.keys(), A_c.values()):
    if k < v:
        ans += v - k
    elif k > v:
        ans += v
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 0
dict = {}
for i in range(N):
    if A[i] in dict:
        dict[A[i]] += 1
    else:
        dict[A[i]] = 1
#print(dict)
for x in dict:
    if x > dict[x]:
        ans += dict[x]
    else:
        ans += dict[x]-x
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
*A,=map(int,input().split())
import collections

c = collections.Counter(A)

ans=0
for k,v in c.items():
    if k == v: continue
    if k < v: # kに対して要素数が多いので取り除く
        ans+=v-k
    if v < k: # kに対して要素数が少ないので、すべて取り除く
        ans+=v
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

S = set(A)
C = Counter(A)
cnt = 0
for s in S:
    if C[s] != s:
        if C[s] > s:
            cnt += C[s] - s
        else:
            cnt += C[s]
print(cnt)

Submission status: Accepted

Solution code:
def count(list):
    dict = {}
    for i in range(len(list)):
        dict[list[i]] = dict.get(list[i], 0) + 1
    return dict

n = int(input())
a = list(map(int,input().split()))
dic = count(a)
ans = 0

for i, x in dic.items():
    differ = x - i
    if differ>0:
        ans += differ
    elif differ<0:
        ans += x

print(ans)

Submission status: Accepted

Solution code:
import collections
N=int(input())
a=list(map(int,input().split()))
c=collections.Counter(a)
ans=0
for i,j in c.items():
  if i>j:
    ans+=j
  elif j>i:
    ans += j-i
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 k,v in c.items():
  cnt += min(v-k,v) if v-k >=0 else v
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

ca = Counter(a)
ans = 0
for k in ca.keys():
    if ca[k] > k:
        ans += ca[k] - k
    elif ca[k] < k:
        ans += ca[k]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = Counter(list(map(int, input().split())))
cnt = 0
for x,y in a.items():
    if x==y: continue
    if x>y: cnt += y
    else: cnt += y-x
print(cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

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

d = {}
cnt = 0 
for i in range(n):
    if a[i] not in d:
        d[a[i]] = 1 
    else: 
        d[a[i]] += 1

for key in d.keys():
    if d[key] != key:
        if d[key] < key:
            cnt += d[key]
        else:
            cnt += d[key]-key

print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

Submission status: Accepted

Solution code:
N=int(input())
lst=list(map(int,input().split()))
lst.sort()
freq={}
for i in lst:
    if i in freq:
        freq[i]+=1
    else:
        freq[i]=1
ans=0
for key,value in freq.items():
    if key>value:
        ans+=value
    elif key<value:
        ans+=abs(key-value)
print(ans)

Submission status: Accepted

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

cnt=[0]*(10**5+1)

for i in a:
  if i>10**5:
    ans+=1
   
  else:
    cnt[i]+=1
    
for i in range(10**5+1):
  if i>cnt[i]:
    ans+=cnt[i]
  elif i<cnt[i]:
    ans+=cnt[i]-i

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt_A = Counter(A)

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

Submission status: Accepted

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

d = {}
for i in s:
    d[i] = 0
for i in a:
    d[i] += 1

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

Submission status: Accepted

Solution code:
import collections as c
input();print(sum(j if i>j else j-i for i,j in c.Counter(map(int,input().split())).items()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
input();a=Counter(map(int,input().split()))
print(sum([j if i>j else j-i for i,j in a.items()]))

Submission status: Accepted

Solution code:
from collections import Counter
input();a=Counter(map(int,input().split()))
print(sum(j if i>j else j-i for i,j in a.items()))

Submission status: Accepted

Solution code:
input();a=list(map(int,input().split()));d={}
for i in a:
  if i not in d: d[i]=1
  else: d[i]+=1
print(sum([j if i>j else j-i for i,j in d.items()]))

Submission status: Accepted

Solution code:
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
print(sum([j if i>j else j-i for i,j in d.items()]))

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
a.append(10**10)
a.sort()
n=0
c=0
ans=0
for i in range(N+1):
    if n!=a[i]:
        if n<c:ans+=c-n
        elif c<n:ans+=c
        n=a[i]
        c=1
    else:c+=1
print(ans)

Submission status: Accepted

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

ans = 0
for k in key:
    if k > d[k]:
        ans += d[k]
    elif k == d[k]:
        pass
    else:
        ans += d[k] - k
print(ans)

Submission status: Accepted

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

ans = 0
for k in key:
    if k > d[k]:
        ans += d[k]
    elif k == d[k]:
        pass
    else:
        ans += d[k] - k
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

input()
a = Counter(map(int, input().split()))
print(sum(v - k if k <= v else v for k, v in a.items()))

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(a)
ans = 0

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
hindo=dict()
for i in a:
    if i in hindo:
        hindo[i]+=1
    else:
        hindo[i]=1
ans=0
for num,cnt in hindo.items():
    if num==cnt:
        continue
    else:
        if num>cnt:
            ans+=cnt
        else:
            ans+=cnt-num
print(ans)

Submission status: Accepted

Solution code:
import collections as c
N,*A = map(int,open(0).read().split())
A = c.Counter(A)
ans=0
for a,n in A.items():
  if a<n:
    ans+=n-a
  elif a>n:
    ans+=n
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=dict()
for x in a:
  if x in b:
    b[x] += 1
  else:
    b[x] = 1
ans = 0
for k, v in b.items():
  if k < v:
    ans += v - k
  elif k > v:
    ans += v
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
d = dict()
for i in a:
    if i not in d:
        d[i] = 1

    else:
        d[i] += 1
count = 0
for key, value in d.items():
    if int(key) > value:
        count += int(value)
    elif int(key) < value:
        count += value - int(key)

print(count)

Submission status: Accepted

Solution code:
from collections import Counter

n,*a=map(int,open(0).read().split())

cnt=Counter(a)

ans=0

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

print(ans)

Submission status: Accepted

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

values = {}
cnt = n

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

    if values[a[i]] == a[i]:
            cnt -= a[i]

print(cnt)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    inlis = list(map(int, input().split()))
    adic = dict()
    ans = 0

    for i in range(n):
        a = inlis[i]
        if a not in adic:
            adic[a] = 1
        else:
            adic[a] += 1
        if adic[a] > a:
            ans += 1
    
    for num in adic:
        if adic[num] < num:
            ans += adic[num]

    print(ans)
            

        


if __name__ == "__main__":
    main()

Submission status: Accepted

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

for ai in a:
    if ai not in cnt.keys():
        cnt[ai] = 1
    else:
        cnt[ai] += 1

ans = 0
for k in cnt.keys():
    if cnt[k] > k:
        ans += cnt[k] - k
    elif cnt[k] < k:
        ans += cnt[k]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

CD = Counter(D)
CD=list(CD.items())
count=0
for i in range(len(CD)):
    if CD[i][0]<CD[i][1]:
        count=count+CD[i][1]-CD[i][0]
    elif CD[i][0]>CD[i][1]:
        count=count+CD[i][1]
print(count)

Submission status: Accepted

Solution code:
from collections import Counter

N=input()
list1=list(map(int,input().split()))
dict1=Counter(list1)

key1=list(dict1.keys())
value1=list(dict1.values())

ans=0
for i in range(len(key1)):
    x=value1[i]
    y=key1[i]
    if x>=y:
        ans+=x-y
    else:
        ans+=x

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
c = Counter(a)
#print(c)
ans = 0
for k, v in c.items():
    if k > v:
        ans += v
    else:
        ans += v-k
print(ans)

Submission status: Accepted

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


def solve(N: int, a: "List[int]"):
    from collections import Counter
    ans = 0
    for k, v in Counter(a).items():
        ans += v - k if v >= k else v
    return ans


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
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]"
    print(solve(N, a))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
a=map(int,input().split())
ans,d=0,{}
for x in a:
    d[x]=d.get(x,0)+1
    ans+=1
    if d[x]==x:
        ans-=x
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
L=list(map(int,input().split()))
ke=list(Counter(L).values())
val=list(Counter(L).keys())
ans=0
for i in range(len(val)):
  if val[i]>ke[i]:
    ans+=ke[i]
  else:
    ans+=ke[i]-val[i]
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 k in c.keys():
  val = c[k]
  if val>k:ans+=val-k
  elif val<k:ans+=val
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort()
a.append(-1)
ans = 0
p = 1
for i in range(1,n+1):
    if a[i-1] != a[i]:
        if a[i-1] != p:
            if p < a[i-1]:
                ans += p
            else:
                ans += p - a[i-1]
        p = 1
    else:
        p += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import Counter
# from numba import njit

# input = stdin.readline

# @njit
def solve(n,a):
  d = Counter(a)
  res = 0
  for k,v in d.items():
    if k <= v:
      res += v-k
    else:
      res += v
  return res



def main():
  N = int(input())
  # N,M = map(int,input().split())
  a = list(map(int,input().split()))
  print(solve(N,a))
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = Counter(map(int, input().split()))
ans = 0
for x, y in A.items():
  if y >= x:
    ans += y - x
  else:
    ans += y
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

read=sys.stdin.read

n,*a=map(int,read().split())
ca=Counter(a)
ans=0
for x in iter(ca):
  if ca[x]<x:
    ans+=ca[x]
  elif ca[x]>x:
    ans+=ca[x]-x
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    from collections import Counter

    n = int(input())
    a = list(map(int, input().split()))
    b = Counter(a)
    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
def main():
    N = int( input())
    A = list( map( int, input().split()))
    d = defaultdict( int)
    for a in A:
        d[a] += 1
    ans = 0
    for key, value in d.items():
        if key > value:
            ans += value
        elif key < value:
            ans += value - key
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
import copy
import heapq
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        self.init_cost()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])

    def search(self, s, g):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]


def solve():
    n = getN()
    nums = getList()
    c = Counter(nums)
    ans = 0
    for k, v in c.items():
        if v < k:
            ans += v
        elif v > k:
            ans += v - k
    print(ans)
    return
def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve()

Submission status: Accepted

Solution code:
import collections


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

c = collections.Counter(A)

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
from collections import Counter
ctr = Counter(A)

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

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
n,cc = raw_input(), collections.Counter(map(int, raw_input().split()))
print sum([(cc[k] - k if k <= cc[k] else cc[k]) for k in cc] or [0])

Submission status: Accepted

Solution code:
import collections

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

B = dict(collections.Counter(A))
M = len(B)

ans = 0

for i in B.keys():
    if B[i] >= int(i):
        ans += B[i] - int(i)
    else:
        ans += B[i]

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

    counter = Counter(A)

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

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np

# input
N = int(input().split()[0])
sequence = np.array(list( map(int, input().split()) ))

# a_N>N must be removed
remove = np.sum(sequence>N)
sequence = sequence[sequence<=N]

index = np.unique(sequence)

for idx in index:

    n_idx = np.sum(sequence==idx)
    if n_idx < idx:
        remove += n_idx
    else:
        remove += (n_idx-idx)

print(remove)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
sc=Counter(list(map(int,input().split())))
sm=0
for k,v in sc.items():
  sm+=min(v,(v-k if v-k>=0 else 10**9+1)) 
print(sm)

Submission status: Accepted

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

for x in range(n):
  if a[x] not in d:
    d[a[x]]=0
  d[a[x]]+=1
  
ans=0
for y in d:
  if d[y]>y:
    ans+=d[y]-y
  elif d[y]<y:
    ans+=d[y]
    
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
a.sort()
r = [[a[0], 1]]
c = 1
for i in range(n-1):
    if a[i] == a[i+1]:
        r[-1][1] += 1
    else:
        r.append([a[i+1], 1])
for i in range(len(r)):
    if r[i][0] <= r[i][1]:
        ans += r[i][1] - r[i][0]
    else:
        ans += r[i][1]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
alst = list(map(int, input().split()))
cnt = [0 for _ in range(n)]
for a in alst:
    if a > n:
        continue
    cnt[a - 1] += 1

rem = 0
for i, num in enumerate(cnt, start = 1):
    if num >= i:
        rem += i
        
print(n - rem)

Submission status: Accepted

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n=k()
c = Counter(list(l())).most_common()
for i in c:
    if i[0]>i[1]:
        cnt+=i[1]
    else:
        cnt+=i[1]-i[0]

print(cnt)

Submission status: Accepted

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

dd=defaultdict(lambda:0)
for aa in a: dd[aa]+=1

ans=0
for aa in dd.keys():
  ca=dd[aa]
  if ca>=aa:
    ans+=ca-aa
  else:
    ans+=ca
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
m={}
for i in range(N):
  if A[i] in m:
    m[A[i]] += 1
  else:
    m[A[i]] = 1
ans = 0
for k,n in m.items():
  if k <= n:
    ans += n-k
  else:
    ans += n
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(lis)

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())

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

a = Counter(a)

ans = 0
for i in a:
    if i <= a[i]:
        ans+=a[i]-i
    else:
        ans+=a[i]

print(ans)