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:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))

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

Submission status: Accepted

Solution code:
import collections

N = int(input())
a = list(map(int, input().split()))
a_s = collections.Counter(a) 
A = a_s.most_common()
count = 0

for i in range(len(A)):
    if int(A[i][1]) >= int(A[i][0]):
        count += int(A[i][1]) - int(A[i][0])
    else:
        count += int(A[i][1])

print(count)

Submission status: Accepted

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

from collections import defaultdict
d = defaultdict(int)

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

for i in d.keys():
    if d[i]-i>0:
        cnt += d[i]-i
    elif i == d[i]:
        pass
    else:
        cnt += d[i]
print(cnt)

Submission status: Accepted

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

n = 0
n_cnt = 0
ans = 0
for a in A + [0]:
	if a != n:
		ans += n_cnt if n_cnt < n else n_cnt - n
		n = a
		n_cnt = 1
	else:
		n_cnt += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
A = Counter(list(map(int,input().split())))
res = 0
#print(A)
for item in A.items():
    k = item[0]
    v = item[1]
    if k > v:
        res += v
    elif k < v:
        res += v-k
print(res)

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt_li = dict(Counter(a))
ans = 0
for x, y in cnt_li.items():
    diff = x-y
    if diff <= 0:
        ans += abs(diff)
    else:
        ans += y
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

a_count = Counter(a)

ans = 0
for i in a_count:
    if int(i) > a_count[i]:
        ans += a_count[i]
    else:
        ans += a_count[i] - int(i)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = [int(i) for i in input().split()]
la = {k:v for k,v in Counter(a).items()}
ans = 0
for k,v in la.items():
    if k != v:
        if k < v:
            ans += abs(k-v)
        else:
            ans += v
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=input()
a=list(map(int,input().split()))
a.sort()
#print(a)
i=0
ans=0
while i!=len(a):
    x=a[i]
    x1=a.count(x)
    if (x>len(a)-i):
        ans=ans+len(a)-i
        break
    else:

        if x<x1:
            ans=ans+x1-x
        elif x>x1:
            ans=ans+x1
        else:
            pass
        i=i+x1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
dic = {}
ans = 0
for a in A:
    if a in dic:
        dic[a] += 1
        if dic[a] == a:
            ans -= a-1
        else:
            ans += 1
    else:
        dic[a] = 1
        if a != 1:
            ans += 1
print(ans)

Submission status: Accepted

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

a.sort()
# print (a)

ans = 0
count = 0
keep = a[0]
for i in a+[0]:
    if i == keep:
        count +=1
    else:
        if count < keep:
            ans += count
            count = 1
            # print ('A', ans)
        else:
            ans += count-keep
            count = 1
            # print ('B', ans)
    keep = i

print (ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N=int(input())
A=list(map(int,input().split()))
C=defaultdict(int)
B=[]
for a in A:
    C[a]+=1
ans=0
for i,v in C.items():
    if i>v:
        ans+=v
    elif i<=v:
        ans+=v-i
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
# N <= 10^5
# a = 10^9
A = Counter([int(x) for x in input().split()])

r = 0
for x, c in A.items():
    if x == c:
        continue
    elif x < c:
        r += c - x
    elif x > c:
        r += c
print(r)

Submission status: Accepted

Solution code:
from collections import defaultdict

def main():
    num = int(input())
    data = list(map(int, input().split()))
    dic = defaultdict(int)

    for ele in data:
        dic[ele] += 1

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

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
countlist = []
tmp = 0
for i in range(0, N):
	if A[i] != tmp:
		countlist.append([A[i], 0])
		tmp = A[i]
	countlist[-1][1] += 1
sum = 0
for i in range(0, len(countlist)):
	if countlist[i][1] < countlist[i][0]: sum += countlist[i][1]
	else: sum += countlist[i][1] - countlist[i][0]
print(sum)

Submission status: Accepted

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

for i in range(N):
  count += 1
  if i == N-1 or a[i+1] != a[i]:
    if a[i] < count:
      ans += count - a[i]
    elif a[i] > count:
      ans += count
    count = 0
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


N = int(input())
lst_A = list(map(int, input().split()))
ctr_A = Counter(lst_A)


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


print(ans)

Submission status: Accepted

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


def main():
    from collections import Counter
    n = int(input())
    a = Counter(list(map(int, input().split())))
    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    from collections import Counter
    N = I()
    a = LI()
    c = Counter(a)
    ans=0
    for i, j in c.items():
        if j - i < 0:
            ans += j
        else:
            ans += j - i
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
S = {}
for a in A:
    if a in S:
        S[a] += 1
    else:
        S[a] = 1
ans = 0
for k, v in S.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()))
b=Counter(a)
c=b.most_common()
ans=0
for i in range(len(c)):
  if c[i][0]>c[i][1]:
    ans+=c[i][1]
  else:
    ans+=c[i][1]-c[i][0]
print(ans)

Submission status: Accepted

Solution code:
N = input()
d = dict()
for x in map(int, input().split()):
  if d.get(x) is None:
    d[x] = 0
  d[x] += 1

print(sum((d[x]-x if d[x]>=x else d[x]) for x in d))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
c = Counter(int(x) for x in input().split())

answer = 0

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

Submission status: Accepted

Solution code:
from collections import Counter

n = input()
arr = Counter(map(int, input().split()))
count = 0
for k, v in arr.items():
  if k > v:
    count += v
  if k < v:
    count += v-k
print(count)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  a=LI()

  a.sort()

  b=a[0]
  c=1
  sm=0
  for x in a[1:]:
    if b==x:
      c+=1
    else:
      if c==b:
        pass
      elif c>b:
        sm+=c-b
      else:
        sm+=c

      c=1
      b=x

  if c==b:
    pass
  elif c>b:
    sm+=c-b
  else:
    sm+=c

  return sm

print(main())

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(a)

cnt = 0
for i, x in c.items():
    if x > i:
        cnt += (x - i)
    elif x < i:
        cnt += x

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
arr = sorted(list(map(int, input().split())))+[0]

ans = 0
num = arr[0]
cnt = 0

for i in arr:
        if i == num: cnt += 1
        else:
                if cnt<num : dif = 1000000001
                else: dif = cnt-num
                ans += min(cnt, dif)
                cnt = 1
                num = i
print(ans)

Submission status: Accepted

Solution code:
import collections
N=int(input())
a=list(map(int,input().split()))
b=collections.Counter(a).most_common()
ans=0
for key,value in b:
    if key>value:
        ans+=value
    else:
        ans+=value-key
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = Counter(list(map(int, input().split()))).most_common()
ans = 0
for a in A:
    if a[0] < a[1]:
        ans += a[1] - a[0]
    elif a[0] > a[1]:
        ans += a[1]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import Counter
INF = float("inf")


def solve(N: int, a: "List[int]"):
    counter = Counter(a)

    ans = 0
    for key in counter:
        if counter[key] < key:
            ans += counter[key]
        else:
            ans += counter[key]-key
    print(ans)
    return


a = []
for i in range(1, 10):
    a.extend([i]*3)

counter = Counter(a)


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:
n=int(input())
s=list(map(int,input().split()))
a={i:0 for i in set(s)}
for i in s:
	a[i]+=1
cnt=0
for k,v in a.items():
	cnt+=v-k if v>=k else v
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
a = sorted([int(x) for x in input().split()])
i,ans = 0,0
while i < N:
    if a[i] > N:
        ans += N-i
        break
    c = a.count(a[i])
    if c > a[i]:
        ans += c-a[i]
    elif c < a[i]:
        ans += c
    i += c

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N, *a = map(int, open(0).read().split())
c = Counter(a)
print(sum(c[v] if v > c[v] else c[v] - v for v in c))

Submission status: Accepted

Solution code:
from collections import Counter
N, *a = map(int, open(0).read().split())
c = Counter(a)
ans = 0
for v in c:
  if v > c[v]:
    ans += c[v]
  else:
    ans += c[v] - v
print(ans)

Submission status: Accepted

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

cnt = [0]*(10**5)
c = 0

for i in a:
    if i <= 10**5:
        cnt[i-1] += 1
    else:
        c += 1



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

print(c)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
ans = 0
idx = []
for i,x in enumerate(a):
    if x > 10**5:
        ans += 1
        idx += [i]
for i in reversed(idx):
    del a[i]

import sys
if len(a) == 0:
    print(ans)
    sys.exit()

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

for i in a:
    counts[i] += 1
    
for i in range(len(counts)):
    if counts[i] == 0:continue
    if counts[i] < i:
        ans += counts[i]
    elif counts[i] > i:
        ans += counts[i]-i
        
print(ans)

Submission status: Accepted

Solution code:

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

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

for a in an:
    if a < len(bucket):
        bucket[a] += 1
    else:
        bucket[0] += 1

#print(bucket[:5])
ans = 0
for i in range(len(bucket)):
    if i == bucket[i]:
        continue

    ans += bucket[i] if i > bucket[i] else bucket[i] - i


print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    N = int(input())
    c = Counter()
    for x in map(int, input().split()): c[x] += 1

    print(sum([v if v < k else v - k for k, v in c.items()]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  N=int(input())
  l=LI()

  l.sort()
  l.append(inf)

  c=0
  a=l[0]
  cnt=1
  for x in l[1:]:
    if a==x:
      cnt+=1
    else:
      if a<cnt:
        c+=cnt-a
      elif a==cnt:
        pass
      else:
        c+=cnt
      a=x
      cnt=1

  print(c)

main()
# print(main())

Submission status: Accepted

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

r = 0
d = {}
pass_list = []
for a in A:
    if a in pass_list:
        r += 1
        continue
    r += 1
    if a in d:
        d[a] += 1
        if d[a] == a:
            r -= a
            pass_list.append(a)
    else:
        d[a] = 1
        if d[a] == a:
          r -= a
          pass_list.append(a)

print(r)

Submission status: Accepted

Solution code:
N = int(input())
numstr = input().split()
arr = [int(num) for num in numstr]

def main():
    dic = {}
    m = max(arr)
    for num in arr:
        if num in dic:
            dic[num] += 1
        else:
            dic[num] = 1
    n = 0
    for key in dic.keys():
        if dic[key] > key:
            n += dic[key] - key
        if dic[key] < key:
            n += dic[key]
    return n

if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

now = a[0]
nowc = 1
ans = 0
for i in range(1, N):
    if now == a[i]:
        nowc += 1
    else:
        if nowc > now:
            ans += nowc - now
        elif nowc < now:
            ans += nowc
        now = a[i]
        nowc = 1

if nowc > now:
    ans += nowc - now
elif nowc < now:
    ans += nowc

print(ans)

Submission status: Accepted

Solution code:
from functools import reduce
import math
import collections
def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)

    # N の逆元
    # N_inv = pow(N,MOD-2,MOD)

    # nCr
    # Nの階乗 * rの階乗の逆元 * n-rの階乗の逆元
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")

    # 大きな素数
    MOD = 10**9+7
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N = int(input())
    a = list(int(_) for _ in input().split())
    a.sort()
    d = collections.defaultdict(int)
    for a_i in a:
        d[a_i] +=1
    ans = 0
    for k,v in d.items():
        if v>k:
            ans += v - k
        elif v<k:
            ans += v
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt_dict = Counter(A)
keys_lst = list(cnt_dict.keys())
values_lst = list(cnt_dict.values())

ans = 0
for i in range(len(keys_lst)):
  if keys_lst[i] > values_lst[i]:
    ans += values_lst[i]
  elif keys_lst[i] < values_lst[i]:
    ans += values_lst[i] - keys_lst[i]
    
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)
for key,val in zip(C.keys(),C.values()):
	ans += val if key > val else val-key
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = Counter(list(map(int, input().split())))
ans = 0
for key, value in a.items():
    if key <= value:
        ans += value - key
    else:
        ans += value
print(ans)

Submission status: Accepted

Solution code:
import bisect

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

a.sort()
pullout = 0
#print(a)

for i in list(set(a)):
    numi = bisect.bisect_right(a,i) - bisect.bisect_left(a,i)
    #print(i,numi)
    if numi == 0 or numi == i:
        continue
    elif numi > i:
        pullout += numi - i
    elif numi < i:
        pullout += numi

print(pullout)

Submission status: Accepted

Solution code:
from collections import defaultdict

d=defaultdict(int)

N=input()
a=list(map(int,input().split(" ")))
ans=0
for z in a:
  d[z]+=1

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(a)

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

print(s)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
dic = {}
for i in range(N):
  if dic.get(A[i]) is None: 
    dic[A[i]] = 1
  else:
    dic[A[i]] += 1

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

Submission status: Accepted

Solution code:
N=int(input())
list=list(map(int,input().split()))
while len(list) != 0and max(list) > len(list):
    list = [i for i in list if i < len(list)]
#list2 = [i for i in list if i < N]
set = set(list)
cnt=0
for i in set:
    c=list.count(i)
    if c > i:
        cnt +=c-i
    elif c < i:
        cnt +=c
print(N-len(list)+cnt)

##できなかった．

Submission status: Accepted

Solution code:
N=int(input())
list=list(map(int,input().split()))
while len(list) != 0and max(list) > len(list):
    list = [i for i in list if i < len(list)]
#list2 = [i for i in list if i < N]
set = set(list)
cnt=0
for i in set:
    c=list.count(i)
    if c > i:
        cnt +=c-i
    elif c < i:
        cnt +=c
print(N-len(list)+cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

ans = 0
for num, freq in Counter(a).items():
    ans += freq- num if freq >= num else freq

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

ca = Counter(a)

ans = 0
for num, freq in ca.items():
    ans += freq- num if freq >= num else freq

print(ans)

Submission status: Accepted

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

m = {}
for a in A:
	if not a in m:
		m[a] = 1
	else:
		m[a] += 1

c = 0
for k in m:
	if k < m[k]:
		c += m[k] - k
	elif m[k] < k:
		c += m[k]

print(c)

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
cnt=[0]*(10**5+1)
ans=0
for i in range(n):
  if arr[i]<=10**5:
    cnt[arr[i]]+=1
  else:
    ans+=1
for i in range(1,10**5+1):
  if cnt[i]==0:
    continue
  elif cnt[i]==i:
    continue
  elif cnt[i]>i:
    ans+=cnt[i]-i
  else:
    ans+=cnt[i]
print(ans)

Submission status: Accepted

Solution code:
import collections


def main():
    _ = int(input())
    a = map(int, input().split())
    c = collections.Counter(a)
    ans = 0
    for i, j in c.items():
        if i > j:
            ans += j
        else:
            ans += j - i
    print(ans)


main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)

ans = 0
for c in C:
    if c > C[c]:
        ans += C[c]
    elif c < C[c]:
        ans += C[c] - c
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 c in C:
    if c > C[c]:
        ans += C[c]
    elif c < C[c]:
        ans += C[c] - c
print(ans)

Submission status: Accepted

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

from collections import Counter

b = Counter(a)

counter = 0
for key in b.keys():
    if b[key] < key:
        counter += b[key]
    elif b[key] > key:
        counter += b[key] - key
print(counter)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

Submission status: Accepted

Solution code:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy
import collections
from operator import itemgetter
#以下てんぷら
def j(q):
    if q==1: print("Yes")
    elif q == 0:print("No")
    exit(0)


"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def printrow(a):
    for i in range(len(a)):
        print(a[i])
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
n = ip()                                     #入力整数1つ
#s,c= (int(i) for i in input().split())       #入力整数横2つ以上
a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#a=[input() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
#こっから本文

a.sort()
cnum = a[0]
ct = 1
s = 0
if n==1:
    if a[0] != 1:
        print(1)
        j(-1)
    else :
        print(0)
        j(-1)
for i in range(1,n):
    if a[i] != cnum:
        if ct != cnum:
            if ct > cnum:
                s+=ct-cnum
            else : s+=ct
        ct = 1
        cnum = a[i]
    else:
        ct+=1
if ct != cnum:
    if ct > cnum:
        s += ct - cnum
    else:
        s += ct
print(s)

Submission status: Accepted

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

ans = 0
for key, value in Counter(a).items():
  if key <= value:
    ans += value - key
  else:
    ans += value
print(ans)

Submission status: Accepted

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

ans = 0
for i in set(a):
  if i <= n:
    c = a.count(i)
    if c >= i:
      ans += i
print(n - ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())

if N == 1:
  print(1 if int(input()) != 1 else 0)
  exit()

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

b = list(collections.Counter(a).items())

c = 0
for i in b:
  if i[1] - i[0] < 0:
    c += i[1]
  else:
    c += i[1] - i[0]

print(c)

Submission status: Accepted

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n = f()[0]
    a = f()

    cnt = {}
    for i in a:
        if i in cnt:
            cnt[i] += 1
        else:
            cnt[i] = 1
    
    ans = 0
    for k,v in cnt.items():
        if k!= v:
            if k>v:
                ans += v
            else:
                ans += v - k 
    
    print(ans)


solve()

Submission status: Accepted

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

for i,j in counter.items():
    if i <= j:
        ans += j-i
    else:
        ans += j
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

print(sum([v-k if v>=k else v for k,v in a.items()]))

Submission status: Accepted

Solution code:
N = int(input())
a = [int(x) for x in input().split()]
nums = {}
ans = 0
for i in a:
    if(i in nums):
        nums[i] += 1
    else:
        nums[i] = 1
for x in nums:
    if(x > nums[x]):
        ans += nums[x]
    elif(x == nums[x]):
        pass
    else:
        ans += nums[x]-x
print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect
import math
input = sys.stdin.readline

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

m = {}
for v in l:
    if m.get(v) != None:
        m[v] += 1
    else:
        m[v] = 1
        
c = 0
for num, count in m.items():
    if num > count:
        c += count
    elif num < count:
        c += count - num
        
print(c)

Submission status: Accepted

Solution code:

from collections import defaultdict

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))

    return n, alist


def submit():
    n, alist = read_input()

    occurs = defaultdict(int)
    for a in alist:
        occurs[a] += 1

    el = 0
    for k in occurs.keys():
        if occurs[k] < k:
            el += occurs[k]
        elif k < occurs[k]:
            el += occurs[k] - k

    print(el)

if __name__ == '__main__':
    submit()

Submission status: Accepted

Solution code:
from collections import Counter
from sys import exit, stdin
N = int(stdin.readline().rstrip())
A = Counter(int(_) for _ in stdin.readline().rstrip().split())
ans = []
for k, v in A.items():
    if k-v > 0:
        ans.append(v)
    elif k-v < 0:
        ans.append(abs(k-v))
print(sum(ans))

Submission status: Accepted

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


N = int(input())
a_list = list(map(int, input().split()))
a_list_unique = list(set(a_list))
a_cnt = Counter(a_list)
cnt = 0
for i in a_list_unique:
    a_dict = a_cnt[i]
    if i <= a_dict:
        cnt +=  a_dict - i
    else:
        cnt += a_dict
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

num = Counter(a).most_common()

for i, j in num:
    if j >= i:
        sum += j - i
    else:
        sum += j

print(sum)

Submission status: Accepted

Solution code:
N = input()

cnt = 0
strdit = {}
for i in input().split():
    i = int(i)
    if i not in strdit.keys():
        strdit[i] = 0
    strdit[i] += 1

for i in strdit.keys():
    if i > strdit[i]:
        cnt += strdit[i]
    else:
        cnt += strdit[i] - i

print(cnt)

Submission status: Accepted

Solution code:
import collections
n=int(input())
X=list(map(int,input().split()))
c=dict(collections.Counter(X))
ans=0
for x in set(X):
  count=c[x]
  if count>=x:
    ans+=count-x
  else:
    ans+=count
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
C = Counter(list(map(int, input().split())))
res = 0
for v, cnt in C.items():
    if v < cnt:
        res += cnt - v
    if v > cnt:
        res += cnt
print(res)

Submission status: Accepted

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

ans = 0
num = [0] * int(2e5)
for i in a:
    if i < 2e5:
        num[i] += 1
    else:
        ans += 1


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

print(ans)

Submission status: Accepted

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

cnt = {}
for b in a:
    cnt[b] = cnt.get(b,0)+1
ans = 0
for key,val in cnt.items():
    if key <= val:
        ans += val - key
    else:
        ans += val
print(ans)

Submission status: Accepted

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math



N = int(input())
An = list(map(int, sys.stdin.readline().rstrip().split()))

Bn = {}
for a in An:
  if a not in Bn:
    Bn[a] = 1
  else:
    Bn[a] += 1

ans = 0
for b in Bn:
  if Bn[b] < b:
    ans += Bn[b]
  else:
    ans += Bn[b] - b

print(ans)

Submission status: Accepted

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

a.sort()

n = 0
c = 0
ans = 0
for i in range(N):
    if n != a[i]:
        if c >= n:
            ans += c - n
        else:
            ans += c
        n = a[i]
        c = 1
    else:
        c += 1
if c >= n:
    ans += c - n
else:
    ans += c
print(ans)