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
inf = 10**10


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())

from collections import defaultdict
cnt=defaultdict(int)
M=0
for a in map(int,input().split()):
    cnt[a]+=1
    M=max(a,M)
ans=0
for i in cnt.keys():
    if cnt[i]>i:
        ans+=cnt[i]-i
    elif cnt[i]<i:
        ans+=cnt[i]
print(ans)

Submission status: Accepted

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

appeared = dict()

for a in A:
    if a in appeared.keys():
        appeared[a] += 1
    else:
        appeared[a] = 1

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
ans=0
aa=list(set(a))
dict={}
for s in aa:
    dict[s]=0
for l in a:
    dict[l]+=1
for i in dict.items():
    num=i[0]
    amount=i[1]
    """                                                           
    cnt=a.count(i)                                                
    if cnt>i:                                                     
        ans+=cnt-i                                                
    elif cnt<i:                                                   
        ans+=cnt                                                  
    """
    if amount>num:
        ans+=amount-num
    elif amount<num:
        ans+=amount
print(str(ans))

Submission status: Accepted

Solution code:
n=int(input())
d=dict()
for i in map(int,input().split()):
  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]-i
  else:
    ans+=d[i]
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
L=list(map(int,input().split()))
S=set(L)
ans=0
dict={}

for s in S:
    dict[s]=0

for l in L:
    dict[l]+=1

for d in dict.items():
    num=d[0]
    amount=d[1]

    if amount>num:
        ans+=amount-num
    elif amount<num:
        ans+=amount

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter as ct

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

ans = 0
for i, j in c.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=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)

Submission status: Accepted

Solution code:
import collections
N=int(input())
a=[int(i) for i in input().split()]
kosulist=collections.Counter(a)
ans=0
for num,kosu in kosulist.items():
    if kosu>=num:
        ans+=kosu-num
    else:
        ans+=kosu
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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


checked = []
ans = 0
cnt_a = {}
for i in a:
  if i in cnt_a:
    cnt_a[i] += 1
  else:
    cnt_a[i] = 1

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

Submission status: Accepted

Solution code:
# 2019/04/20
# AtCoder Beginner Contest 082 - C

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

dict = {}
for i in range(n):
    if dict.get(a[i]) == None:
        dict[a[i]] = 1
    else:
        dict[a[i]] += 1

cnt = 0

for keys, vals in dict.items():
    if keys > vals:
        cnt += vals
    elif keys < vals:
        cnt += vals - keys

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
def MI(): return map(int, input().split())
def II(): return int(input())
def IS(): return input()
def LI(): return list(map(int, input().split()))


n = II()
l = list(Counter(LI()).items())
ans = 0
for num in l:
    if num[0] < num[1]:
        ans += num[1]-num[0]
    elif num[0] > num[1]:
        ans += num[1]
    else:  # num[0] == num[1]
        pass
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
cnt = [ 0 for i in range(10**5+5) ]
a = [ int(i) for i in input().split() ]
ans = 0
for i in a:
    if i <= 10**5:
        cnt[i] += 1
    else:
        ans += 1    
for i in range(10**5+5):
    if cnt[i] < i:
        ans += cnt[i]
    else:
        ans += (cnt[i] - i)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

a.sort()
a.append(a[-1] + 1)

c = 0
m = 0

for i in range(0, n):
    c += 1
    if a[i] != a[i + 1]:
        if c < a[i]:
            m += c
        else:
            m += c - a[i]
        c = 0

print(m)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
an = list(map(int,input().split()))
at = tuple(Counter(an).items())
ans = 0
for a in at:
    if a[0] > a[1]:
        ans += a[1]
    elif a[0] < a[1]:
        ans += (a[1] - a[0])
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
N=ni()
A=na()
A.sort()
dic={}
ans=0
for i in A:
    if(i in dic):
        dic[i]+=1
    else:
        dic[i]=1
for key, value in dic.items():
    if(key==value):
        continue
    elif(key>value):
        ans+=value
    else:
        ans+=value-key


print(ans)

Submission status: Accepted

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

from collections import Counter
counter = Counter(an)

c = 0
for a, n in counter.most_common():
    if n > a:
        c += (n-a)
    elif n < a:
        c += n
print(c)

Submission status: Accepted

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

def io_generator():
	return input()

#+++++++++++++++++++

def main(io):
	n=int(io())
	l=list(map(int, io().split()))
	di={}
	for i in l:
		try:
			di[i] = di[i]+1
		except:
			di[i] = 1
	
	c=0
	for k in di.keys():
		c += di[k] if di[k] < k else di[k] - k
	
	return c

#++++++++++++++++++++

if __name__ == "__main__":
	io= lambda : io_generator()
	print (main(io))

Submission status: Accepted

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

dict = {}
for a in A:
  if(a in dict): dict[a] += 1
  else: dict[a] = 1

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

Submission status: Accepted

Solution code:
from collections import Counter

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

count = Counter(a)
ans = 0
for i in count.keys():
    if count[i] < i:
        ans += count[i]
    else:
        ans += count[i] - i

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
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 pf(s): return print(s, flush=True)

N = I()
A = LI()

counts = collections.Counter(A)

result = 0
for c in counts:
    if c < counts[c]:
        # 必要な数より多い場合
        result += counts[c] - c
    elif c > counts[c]:
        # 必要な数より少ない場合
        result += counts[c]
print(result)

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 d.get(i) else 1

remove_count = 0
for k, v in d.items():
    if k != v:
        remove_count += v if k > v else v - k
        
print(remove_count)

Submission status: Accepted

Solution code:
import collections
input()
c = collections.Counter(list(map(int,input().split())))
ans = 0
for i,j in c.items():
        if j >= i:
            ans += j - i
        else:
            ans += j
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
c = Counter(a)
key = list(c.keys())
val = list(c.values())
ans = 0
for i, j in zip(key, val):
    if i > j:
        ans += j
    else:
        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)
ans=0
for k,v in c.items():
    if v>=k:
        ans+=v-k
    else:
        ans+=v
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
if N == 1:
    if a[0] != 1:
        print(1)
        exit()
    else:
        print(0)
        exit()
a.sort()
A = 0
c = 0
while True:
    A = a[0]
    if len(a) > 1:
        for i in range(1, len(a)):
            if a[i] != A:
                del a[:i]
                if i >= A:
                    c += i - A
                else:
                    c += i
                break
            elif i == len(a) - 1:
                if i + 1 >= A:
                    c += i + 1 - A
                else:
                    c += i + 1
                print(c)
                exit()
    else:
        print(c + 1)
        exit()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

c = defaultdict(int)
for ai in a:
    c[ai] += 1

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
if N == 1:
    if a[0] != 1:
        print(1)
        exit()
    else:
        print(0)
        exit()
a.sort()
A = 0
c = 0
while True:
    try:
        #print(a)
        A = a[0]
        if len(a)>1:
            for i in range(1, len(a)):
                if a[i] != A:
                    # print(i + 1)
                    # c += i
                    del a[:i]
                    if i >= A:
                        c += i - A
                    else:
                        c += i
                    # print(a[i], c)
                    break
                elif i == len(a) - 1:
                    if i + 1 >= A:
                        c += i + 1 - A
                    else:
                        c += i + 1
                    print(c)
                    exit()
        else:
            print(c + 1)
            exit()
    except IndexError:
        break

Submission status: Accepted

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

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

set_a = set(a)
a.sort()
k = 0
p = 0
ans = 0
for a_ in set_a:
    s = a[k]
    while True:
        if k<len(a) and a[k]==s:
            k+=1
            p+=1
        else:
            if p>=s:
                ans+=p-s
            else:
                ans+=p
            p=0
            break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
argv = input().split()

dic = {}
for a in argv:
    i = int(a)
    v = dic.get(i, 0)
    dic[i] = v + 1

count = 0
for k in dic.keys():
    size = dic.get(k)
    if k < size:
        count += size - k
    elif k > size:
        count += size

print(count)

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = [int(i) for i in input().split()]
l = collections.Counter(a)
c = 0
for k, v in l.items():
    if k < v:
        c += v - k
    elif k > v:
        c += v
print(c)

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

Solution code:
import collections

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split())) #カウンター使うときは文字列で行ったほうがいい？

A = Counter(a)
# print(a)
num = len(set(a))
count = 0
# print(A.most_common())
D = A.most_common()
for i in range(num):
    if D[i][0] > D[i][1]:
        count += D[i][1]
    else:
        count += D[i][1] - D[i][0]

print(count)

Submission status: Accepted

Solution code:
n = int(input())

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

d = dict()

for i in range(n):
    if d.__contains__(a[i]):
        d[a[i]]+=1
    else:
        d[a[i]] =1
res = 0
for num,cnt in list(d.items()):
    if cnt>=num:
        res += cnt-num
    else:
        res += cnt 

print(res)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
set_a = set(a)
a.sort()
k = 0
p = 0
ans = 0
for a_ in set_a:
    s = a[k]
    while True:
        if k<len(a) and a[k]==s:
            k+=1
            p+=1
        else:
            if p>=s:
                ans+=p-s
            else:
                ans+=p
            p=0
            break
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

print(answer)

Submission status: Accepted

Solution code:
from collections import Counter


def solve(string):
    n, *a = map(int, string.split())
    count = Counter(a)
    return str(sum([v if int(k) > v else v - int(k) for k, v in count.items()]))


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
dic = {}
for a in A:
    dic.setdefault(a, 0)
    dic[a] += 1
ans = 0
for k, v in dic.items():
    if k == v:
        continue
    if k > v:
        ans += v
    else:
        ans += (v - k)
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 k, v in c.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 = [int(i) for i in input().split()]

D = Counter(A)

res = 0
for i in D.items():
    if i[0] < i[1]:
        res += i[1]-i[0]
    elif i[0] > i[1]:
        res += i[1]

print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2019/3/21
Solved on 2019/3/
@author: shinjisu
"""


# ABC 082 C Good Sequence
#import math
#import numpy as np

def getInt(): return int(input())

def getIntList(): return [int(x) for x in input().split()]
# def getIntList(): return np.array(input().split(), dtype=np.longlong)

def zeros(n): return [0]*n
# def zeros(n): return np.zeros(n, dtype=np.longlong)

def getIntLines(n): return [int(input()) for i in range(n)]
"""
def getIntLines(n):
    data = zeros(n)
    for i in range(n):
        data[i] = getInt()
    return data
"""

def zeros2(n, m): return [zeros(m) for i in range(n)] # obsoleted zeros((n, m))で代替

def getIntMat(n, m):  # n行に渡って、1行にm個の整数
    #mat = zeros((n, m))
    mat = zeros2(n, m)
    for i in range(n):
        mat[i] = getIntList()
    return mat

ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7
INF = 10**18

class Debug():
    def __init__(self):
        self.debug = True
 
    def off(self):
        self.debug = False
 
    def dmp(self, x, cmt=''):
        if self.debug:
            if cmt != '':
                print(cmt, ':  ', end='')
            print(x)
        return x


def prob():
    d = Debug()
    d.off()
    N = getInt()
    d.dmp((N), 'N')
    A = getIntList()
    d.dmp((A), 'A')
    A.sort()
    d.dmp((A), 'A')
    i = 0
    dlt = 0
    while i < N:
        k = 1
        while i+k < N and A[i] == A[i+k]:
            k += 1
            if i+k == N:
                break
        if A[i] < k:
            dlt += k - A[i]
        elif A[i] > k:
            dlt += k
        i += k   
        d.dmp((dlt), 'dlt')
    return dlt


ans = prob()
if ans is None:
    pass
elif type(ans) == tuple and ans[0] == 1:  # 1,ans
    for elm in ans[1]:
        print(elm)
else:
    print(ans)

Submission status: Accepted

Solution code:
from sys import exit
import math
N = int(input())
dic = {}
a = [int(n) for n in input().split()]
for e in a:
    if e in dic:
        dic[e]+=1
    else:
        dic[e] = 1
ans = 0
for k in dic.keys():
    if k < dic[k]:
        ans+=dic[k]-k
    elif k > dic[k]:
        ans+=dic[k]
    else:
        continue
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
lst=[int(i) for i in input().split()]
ans=0
for i,j in Counter(lst).items():
  if i>j:
    ans+=j
  elif i<j:
    ans+=j-i
print(ans)

Submission status: Accepted

Solution code:
n=input()
dc=dict()
for j in input().split():
    i=int(j)
    if i in dc:
        dc[i]+=1
    else:
        dc[i]=1
ans=0
for i in dc:
    if dc[i]>i:
        ans+=dc[i]-i
    elif dc[i]<i:
        ans+=dc[i]
print(ans)

Submission status: Accepted

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

ans = 0
d = {}

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

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())

an = Counter([int(i) for i in input().split()])


def main():
    # 要素(k) よりも 要素数(v) が 以上ならば その差(v - k)
    # 要素(k) よりも 要素数(v) が 小さければ 要素数(v)
    print(sum([v - k if k <= v else v for k, v in an.items()]))

    return


main()

Submission status: Accepted

Solution code:
from collections import Counter

from sys import stdin

input = stdin.readline

N = int(input())

an = Counter([int(i) for i in input().split()])


def main():
    # 要素(k) よりも 要素数(v) が 以上ならば その差(v - k)
    # 要素(k) よりも 要素数(v) が 小さければ 要素数(v)
    print(sum([v - k if k <= v else v for k, v in an.items()]))

    return


main()

Submission status: Accepted

Solution code:
def good_sequence(N: int, A: list) -> int:
    freq = {}
    for a in A:
        freq.setdefault(a, 0)
        freq[a] += 1

    return sum(f if f < n else f - n for n, f in freq.items())


if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    ans = good_sequence(N, A)
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

def main():
    _ = int(input())
    a = [int(s) for s in input().split()]
    print(solve(a))

def solve(a):
    counter = Counter(a)
    s = 0
    for x, c in counter.most_common():
        if c > x:
            s += c - x
        elif c < x:
            s += c
    return s

main()

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc082/tasks/arc087_a

import itertools
import collections
import bisect
import sys
input = sys.stdin.readline

def main():
    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
        else:
            ans += v - k
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0

T = [0]*(N+1)
for x in a:
  if x<=N:
    T[x] += 1
  else:
    ans += 1
for i in range(N+1):
  if T[i]>0:
    if T[i]>=i:
      ans += T[i]-i
    else:
      ans += T[i]
print ans

Submission status: Accepted

Solution code:
#082-C
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return list(input())
n=I()
A=IL()
Ad={}
for a in A:
    if a in Ad:
        Ad[a]+=1
    else:
        Ad[a]=1
ans=0
for key in Ad.keys():
    if Ad[key]<key:
        ans+=Ad[key]
    else:
        ans+=Ad[key]-key
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
ans=0
d={}
for ai in a:
    if ai in d:
        d[ai]+=1
    else:
        d[ai]=1
for k, v in d.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()))
cnt = Counter(a)
ans = 0
for k, v in cnt.items():
    if k > v:
        ans += v
    elif k < v:
        ans += (v-k)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return list(sys.stdin.readline())
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
mod = 1000000007

#A
"""
a, b = LI()
print((a+b+1)//2)
"""

#B
"""
a = S()
a = a[:-1]
b = S()
b = b[:-1]
a.sort()
b.sort(reverse=True)
if a < b:
    print("Yes")
else:
    print("No")
"""

#C
n = I()
a = LI()
a.sort()
seta = set(a)
ans = 0
for i in seta:
    x = bisect.bisect_right(a,i) - bisect.bisect_left(a,i)
    if x != 0 and x >= i:
        ans += x - i
    else:
        ans += x
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 in c:
    if c[key] < key:
        ans += c[key]
    elif c[key] > key:
        ans += c[key] - key
print(ans)

Submission status: Accepted

Solution code:
import math

def A():
    a, b = [int(x) for x in input().split()]
    print(math.ceil((a + b)/2))

def B():
    a = ''.join(sorted(list(input()), key=lambda x:ord(x)))
    b = ''.join(sorted(list(input()), key=lambda x:-ord(x)))
    if a < b:
        print('Yes')
    else:
        print('No')

def C():
    N = input()
    A = [x for x in input().split()]
    counter = {}

    for a in A:
        if(a in counter):
            counter[a] += 1
        else:
            counter[a] = 1
    remove_count = 0
    for key in counter:
        if counter[key] - int(key) >= 0:
            remove_count += counter[key] - int(key)
        else:
            remove_count += counter[key]
    print(remove_count)
C()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A
def A():
    a,b = LI()
    print(math.ceil((a+b)/2))

#B
def B():
    s = S()
    t = S()
    s.sort()
    t.sort()
    t = t[::-1]
    l = [s,t]
    l.sort()
    if s == t:
        print("No")
    elif l[0] == s:
        print("Yes")
    else:
        print("No")
#C
def C():
    n = I()
    d = defaultdict(int)
    a = LI()
    for i in a:
        d[i] += 1
    ans = 0
    for i in d.keys():
        if i <= d[i]:
            ans += d[i]-i
        else:
            ans += d[i]
    print(ans)
#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Accepted

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int, input().split()))

    B = [0] * (N+1)
    c = 0
    for a in A:
        if a > N:
            c += 1
        elif B[a] < a:
            B[a] += 1
        else:
            c += 1

    for i in range(N+1):
        if B[i] < i:
            c += B[i]

    print(c)


if __name__ == '__main__':
    main()

Submission status: Accepted

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


count = len([i for i in a if i > 10**5])
a = [e for e in a if e <= 10**5]
dic = list(set(a))
for x in dic:
    if a.count(x) > x:
        count+= a.count(x) - x
    elif a.count(x) < x:
        count += a.count(x)
print(count)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=tuple([int(i) for i in input().split()])
cnt=dict(Counter(a))
ans=0
for i,j in zip(cnt.keys(),cnt.values()):
    if i>j:
        ans+=j
    elif i!=j:
        ans+=j-i
print(ans)

Submission status: Accepted

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

count = 0
for e in d:
    if e < d[e]:
        count += d[e] - e
    elif e > d[e]:
        count +=  d[e]

print(count)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(a)

#print(c)
ans=0
for i in set(a):
  tmp=c[i]
  if tmp-i<0:
    ans+=tmp
  else:
    ans+=tmp-i
  
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = Counter(list(map(int, input().split())))
num = 0
for a, n in A.items():
    if a < n:
        num += n - a
    if a > n:
        num += n
print(num)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
A = map(int,input().split())
dic = defaultdict(int)
for a in A: dic[a] += 1

ans = 0
for k in dic.keys():
    n = dic[k]
    if n < k: ans += n
    elif k < n: ans += n-k

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
li_a = input().split()

ndict = defaultdict(int)

for i in li_a:
    ndict[i] += 1

result = 0
for k in ndict.keys():
    if ndict[k] > int(k):
        result += ndict[k] - int(k)
    elif ndict[k] < int(k):
        result += ndict[k]

print(result)