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)

x = set(a)


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


print(res)
#
#
#for i in a:
#    if i > 100000:
#        res+=1
#
#for i in x:
#    if (a.count(i) >= i):res += (a.count(i) - i)
#    else:res += a.count(i)
#
#print(res)

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    a=list(map(int,readline().split()))

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

    res=0
    for key in d.keys():
        x=d[key]
        if key<=x:
            res+=x-key
        else:
            res+=x

    print(res)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import collections
N = int(input())
a = sorted(list(map(int,input().split())))
c = collections.Counter(a)
cnt = 0
for i in c.most_common():
   # print(i)
    if i[0]<i[1]:
      cnt=cnt+i[1]-i[0]
    if i[0]>i[1]:
      cnt=cnt+i[1]
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

di = {}
for i in a:
    if i in di:
        di[i] += 1
    else:
        di[i] = 1

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

Submission status: Accepted

Solution code:
from collections import defaultdict
def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    pair = defaultdict(int)

    for i in range(N):
        pair[A[i]] += 1

    cnt = 0
    for num, count in pair.items():
        if count >= num:
            cnt += count - num
        else:
            cnt += count

    print(cnt)
if __name__ == "__main__":
    main()

Submission status: Accepted

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

dic={}
for i in li:
    if i not in dic:
        dic[i] = 1
    elif dic[i] >= 1:
        dic[i] += 1
        
#print(dic)
cnt = 0
for i in dic:
    #print(i)
    #print(dic[i])
    if i>dic[i]:
        cnt += dic[i]
    else:
        cnt += dic[i]-i
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

import collections
c=collections.Counter(a)


#print(c.most_common())
cnt=0
for i in c.most_common():
   # print(i)
    if i[0]<i[1]:
      cnt=cnt+i[1]-i[0]
    if i[0]>i[1]:
      cnt=cnt+i[1]
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import os
import math
import bisect
import collections
import itertools

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    X = ii()
    A = collections.Counter(il())

    ret = 0
    for k, v in A.items():
        if k > v:
            ret += v
        if k < v:
            ret += abs(k-v)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
count=0
lcount=[0]*(10**5+1)
for i in l:
    if i>10**5:
        count+=1
    else:
        lcount[i]+=1
        if lcount[i]>i:
            count+=1
for j in l:
    if j<=10**5 and lcount[j]<j:
        count+=1
print(count)

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections

n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)
item = []
for key,value in c.items():
    item.append([key,value])
cnt = 0
for i in range(len(item)):
    if item[i][0] == item[i][1]:
        continue
    elif item[i][0] < item[i][1]:
        cnt += item[i][1] - item[i][0]
    else:
        cnt += item[i][1]
print(cnt)

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:
import collections
n = int(input())
li = list(map(int,input().split()))

c = collections.Counter(li)

#print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})
#print(c.items())
# dict_items([('a', 4), ('b', 1), ('c', 2)])

if len(li) == 0:
    print(0)
    exit()

count = 0
for ci in c.items():
    a = ci[0]
    b = ci[1]
    if a<b:
        count += b-a
    elif a>b:
        count += b
print(count)

Submission status: Accepted

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

answer = 0
C = Counter (A)
C_taple = C.most_common()
for c in C_taple:
    if c[0] > c[1]:
        answer += c[1]
    elif c[1] > c[0]:
        answer += (c[1]-c[0])
print(answer)

Submission status: Accepted

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

dic = dict()

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

res = 0
for i, cnt in dic.items():
    ii = int(i)
    if ii < cnt:
        res += cnt - ii
    elif ii > cnt:
        res += cnt

print(res)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
import collections
a=collections.Counter(a)
a=a.most_common()
ans=0
l=len(set(a))
for i in range(l):
  if a[i][0]>a[i][1]:
    ans+=a[i][1]
  if a[i][0]<a[i][1]:
    ans+=a[i][1]-a[i][0]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter


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

ans = 0

for a, b in list(S.items()):
    if a < b:
        ans += b - a
    elif a > b:
        ans += b

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
INIT_VAL = 0
def DD2(d1,d2): return [[INIT_VAL]*d2 for _ in range(d1)]
def DD3(d1,d2,d3): return [DD2(d2,d3) for _ in range(d1)]
## math ##
MOD=10**9+7
def divc(x,y): return -(-x//y)
def divf(x,y): return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers"""
    is_prime = [True] * MAX_NUM
    is_prime[0] = False
    is_prime[1] = False
    primes = []
    for i in range(MAX_NUM):
        if is_prime[i]:
            primes.append(i)
            for j in range(2*i, MAX_NUM, i):
                is_prime[j] = False
    return primes
## libs ##
from itertools import accumulate as acc
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left

#======================================================#

def main():
    n = II()
    aa = MII()
    c = Counter(aa)
    keys = sorted(c.keys())
    cnt = 0
    for k in keys:
        if c[k] > k:
            cnt += c[k] - k
        elif c[k] < k:
            cnt += c[k]
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

num_dict = dict()
for a_i in a:
    if a_i in num_dict:
        num_dict[a_i] += 1
        continue
    num_dict[a_i] = 1

ans = 0
for num in num_dict:
    if num_dict[num] == num:
        continue
    if num_dict[num] < num:
        ans += num_dict[num]
        continue
    ans += num_dict[num]-num

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for x,y in a.items():
    if x == y: continue
    if x < y: ans += y-x
    else: ans += y
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
d = {}
for c in a:
    if c in d.keys():
        d[c] += 1
    else:
        d.setdefault(c, 1)
ans = 0
for indx, cnt in d.items():
    if indx < cnt:
        ans += cnt - indx
    elif indx > cnt:
        ans += cnt
print(ans)

Submission status: Accepted

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

ans = 0
for k,v in D.items():
    if k==v:
        continue
    elif k>v:
        ans += v
    else:
        ans += v-k

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,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_ls = list(map(int, input().split()))
c = Counter(a_ls)

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

Submission status: Accepted

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

d=dict()

for i in range(n):
    if a[i] in d:
        d[a[i]]=d[a[i]]+1
    else:
        d[a[i]]=1
        
xsum=0
for x in d.keys():
    if d[x]<x:
        xsum=xsum+d[x]
    elif d[x]>x:
        xsum=xsum+d[x]-x
        
print(xsum)

Submission status: Accepted

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

ans = 0
for k,v in D.items():
    if k==v:
        continue
    elif k>v:
        ans += v
    else:
        ans += v-k

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
if not N == 1:
    counter = defaultdict(lambda: 0)
    for e in list(map(int, input().split())):
        counter[e] += 1
    answer = 0
    for i in counter:
        ball_count = counter[i]
        if not i == ball_count:
            if i > ball_count:
                answer += ball_count
            elif i < ball_count:
                answer += ball_count - i
    print(answer)
else:
    if not 1 == int(input()):
        print(1)
    else:
        print(0)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
if not N == 1:
    counter = defaultdict(lambda: 0)
    for e in list(map(int, input().split())):
        counter[e] += 1
    answer = 0
    for i in counter:
        ball_count = counter[i]
        if i > ball_count:
            answer += ball_count
        elif i < ball_count:
            answer += ball_count - i
    print(answer)
else:
    if not 1 == int(input()):
        print(1)
    else:
        print(0)

Submission status: Accepted

Solution code:
import collections
import sys
sys.setrecursionlimit(2000000)
# import math
# import itertools
# import statistics
# import numpy as np
n = int(input())
#n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
a_count = collections.Counter(a)
a_list = list(a_count.items())
a_sorted = sorted(a_list, key=lambda x: x[1])
# print(a_sorted)
count = 0
for i in range(len(a_sorted)):
    if a_sorted[i][0] != a_sorted[i][1]:
        if a_sorted[i][0] < a_sorted[i][1]:
            count += a_sorted[i][1]-a_sorted[i][0]
        else:
            count += a_sorted[i][1]

print(count)

Submission status: Accepted

Solution code:
def mycounter(d,i):
    if i in d:
        d[i]+=1
    else:
        d[i]=1
    return d
    
n=int(input())
a=list(map(int,input().split()))
d=dict()
for i in a:
    d=mycounter(d,i)
p=0
for i,j in d.items():
    if i<j:
        p+=j-i
    if i>j:
        p+=j
print(p)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

adic=defaultdict(int)
for a in alist:
  adic[a]+=1
#print(adic)

answer=0
for key in adic:
  if adic[key]<key:
    answer+=adic[key]
  else:
    answer+=adic[key]-key
    
print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    N = int(readline())
    A = list(map(int, readline().rstrip().split()))
    c = Counter(A)
    ans = 0
    for k, v in zip(c.keys(), c.values()):
        if v - k >= 0:
            ans += v - k
        else:
            ans += v
    
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a_dic = {}
for i in range(n):
    if a[i] not in a_dic:
        a_dic[a[i]] = 0
    a_dic[a[i]] += 1
a_key = list(a_dic.keys())
a_val = list(a_dic.values())
#print(a_key,a_val)
ans = 0

for i in range(len(a_key)):
    if a_key[i] > a_val[i]:
        ans += a_val[i]
    else:
        ans += a_val[i] - a_key[i]

print(ans)

Submission status: Accepted

Solution code:

import sys
import collections

readline = sys.stdin.readline

def main():
    N = int(input())
    A = list(map(int, readline().split()))
    countA = collections.Counter(A)
    ans = 0
    for c in countA.items():
        if c[1] >= int(c[0]):
            ans += c[1] - c[0]
        else:
            ans += c[1]

    print(ans)    


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = [int(i) for i in input().split()]
ans = 0
count = dict(Counter(a))
for i in count:
    j = count[i]
    if i > j:
        ans += j
    elif i==j:
        continue
    else:
        ans += j-i
print(ans)

Submission status: Accepted

Solution code:
import io
from collections import Counter

data=int(input())
num=list(map(int,input().split()))

dic=Counter(num)
counter=0
for k,v in dic.items():
  if k<v:
    counter+=v-k
  elif v<k:
    counter+=v
print(counter)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(input())
    a = list(map(int, readline().split()))

    from collections import Counter

    counter = Counter(a)
    ans = 0

    for num, cnt in counter.items():
        if cnt > num:
            ans += (cnt-num)
        if cnt < num:
            ans += cnt

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

n=int(input())
a=list(map(int,input().split()))
a=collections.Counter(a)
ans=0
#print(a)

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

print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a).most_common()
cnt = 0
for i, cc in enumerate(c):
    if cc[0] < cc[1]:
        cnt += cc[1] - cc[0]
    elif cc[0] > cc[1]:
        cnt += cc[1]
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#Let's go
import sys
import math
import random

sys.setrecursionlimit(100000)

br=dict()

n=int(input())
ar=list(map(int, input().split()))
cr=set(ar)

for i in range(n):
    br[ar[i]]=0
for i in range(n):
    br[ar[i]]=br[ar[i]]+1

ans=0
for xd in cr:
    times = br[xd]
    if(times < xd):
        ans = ans + times
    else:
        ans = ans + times-xd

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n=ii()
arr=limii()

mat=Counter(arr)
ans=0
for i,j in mat.items():
    if i<=j:
        ans+=j-i
    else:
        ans+=j
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    a=list(map(int,input().split()))
    a.sort()
    a.append(-1)
    cnt=1
    ans=0
    for i in range(n):
        if a[i]==a[i+1]:
            cnt+=1
        else:
            if cnt>=a[i]:
                ans+=cnt-a[i]
            else:
                ans+=cnt
            cnt=1
    print(ans)
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split(" ")))
    d = {0: 0}
    for a in A:
        if a in d:
            d[a] += 1
        else:
            d[a] = 1
    cnt = 0
    for i in d.keys():
        if d[i] >= i:
            cnt += d[i] - i
        else:
            cnt += d[i]
    print(cnt)

if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
seta=set(a)
d=dict([[x,0] for x in seta])
for i in range(len(a)):
  d[a[i]]+=1
seta=set(a)
ans=0
for ai in seta:
  if d[ai]==ai:
    pass
  elif d[ai]<ai:
    ans+=d[ai]
  else:
    ans+=d[ai]-ai
print(ans)

Submission status: Accepted

Solution code:
#4
#入力
N=int(input())
A=list(map(int,input().split()))

#デバッグ関連
debug=0#0:本番,1:デバッグ

def pront(*n):
    if debug==1:
        print(n)

#処理
X={}
for i in A:
    if i in X:
        X[i]+=1
    else:
        X[i]=1

        x=0
for i in X.items():
    pront(i)
    if i[0]>i[1]:
        x+=i[1]
    else:
        x+=i[1]-i[0]
        
print(x)

Submission status: Accepted

Solution code:
from collections import defaultdict
d = defaultdict(int)
N = int(input())
a = list(map(int, input().split()))
for x in a:
    d[x] += 1
ans = 0
for k in d:
    if d[k] < k:
        ans += d[k]
    else:
        ans += d[k]-k
print(ans)

Submission status: Accepted

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

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

cnt = [[k, v] for k, v in cnt.items()]
cnt.sort(reverse=True)
ans = 0
for k, v in cnt:
    # e.g.) 4という数が4以上存在する
    if k <= v:
        # 4が4つになるまで取り除く
        ans += v - k
    else:
        # 4という要素は全部取り除くしかない
        ans += v

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 - i
    else:
        ans += j
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = sorted([int(x) for x in input().split()])
d = {}
ans = 0

for i in range(len(a)):
    tmp = a[i]
    if tmp not in d:
        d[tmp] = 0
    d[tmp] += 1

for kk in d.keys():
    if kk > d[kk]:
        buf = d[kk]
    elif kk < d[kk]:
        buf = d[kk] - kk
    else:
        buf = 0
    ans += buf

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

#https://atcoder.jp/contests/abc082/tasks/abc082_c
length = int(input())
a = [int(i) for i in input().split()]
set_a = set(a)

count = 0
for i in set(a):
	if (i > length):
		continue
	if(i <= a.count(i)):
		count += i
print(length - count)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

n, *a = map(int, sys.stdin.read().split())

def main():
    res = 0
    for v, c in Counter(a).items():
        res += c if c < v else c - v
    print(res)

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 a in set(A):
  ans += [C[a], C[a] - a][C[a] >= a]
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 in c:
  if i>c[i]:
    ans+=c[i]
  else:
    ans+=c[i]-i
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
a = list(map(int, input().split()))
dic = defaultdict(int)
for i in range(n):
    dic[a[i]] += 1
ans = 0
for k, v in dic.items():
    if k < v:
        ans += v - k
    elif v < k:
        ans += v

print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())
a = [int(i) for i in input().split()]
count = 0
b = collections.Counter(a)
for i in b:
    if int(i) < b[i]:
        count += (b[i] - int(i))
    elif(int(i) > b[i]):
        count += b[i]
print(count)

Submission status: Accepted

Solution code:
from sys import stdin 
n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
a = sorted(a)
t = 1
c = 0
answer = 0
for i in range(n):
    if a[i] == t:
        c += 1
    else:
        if c > t:
            answer += c-t
        elif c < t:
            answer += c
        c = 1
        t = a[i]
if c > t:
    answer += c-t
elif c < t:
    answer += c
print(answer)

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
A = list(map(int,input().split()))
backet = [0]*n

for a in A:
  if a > n:ans += 1
  else:backet[a-1] += 1

for i,b in enumerate(backet):
  if b >= i+1:
    ans += b-i-1
  else:
    ans += b
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
from collections import Counter as co
var=co(list(map(int,input().split()))).items()
ans=0
for k,v in var:
    if k<=v:ans+=(v-k)
    else:ans+=v
print(ans)

Submission status: Accepted

Solution code:
#import collections
#aa = collections.Counter(a) # list to list
def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
    n = int(input())
    dic = {}
    A = readInts()
    #print(*A)
    cnt = 0
    for i in range(n):
        #print('now')
        if A[i] in dic:
            if dic[A[i]] == A[i]:
                #print('collision')
                cnt += 1
            else:
                #print('add')
                dic[A[i]] += 1
        else:
            #print('Insert')
            dic[A[i]] = 1
    # まだ省かれいないもの
    for k,v in dic.items():
        if k != v:
            cnt += v
    print(cnt)



if __name__ == '__main__':
  main()

Submission status: Accepted

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


d = defaultdict(list)
for i in range(1, N+1):
    d[a[i-1]].append(a[i-1])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n, *A = map(int, open(0).read().split())
counter = Counter(A)

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

Submission status: Accepted

Solution code:
import math

line1 = int(input())
line2 = str(input()).split(" ")
for index in range(len(line2)):
    line2[index] = int(line2[index])

newList = []
counter = dict()
newLength = 0
for element in line2:
    try:
        counter[element] += 1
    except:
        counter[element] = 1
    if (counter[element] == element):
        newLength += element

print(line1 - newLength)