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.
Examples
Input
4
3 3 3 3
Output
1
Input
5
2 4 1 4 2
Output
2
Input
6
1 2 2 3 3 3
Output
0
Input
1
1000000000
Output
1
Input
8
2 7 1 8 2 8 1 8
Output
5
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
n = int(input())
a = list(map(int, input().split()))
from collections import Counter
count = Counter(a)
sum_kept = 0
for x, c in count.items():
if c >= x:
sum_kept += x
else:
sum_kept += c # Wait, this is a mistake. Because if c < x, we can't include any elements. So sum_kept should add 0, not c.
# Wait, no. Wait, the code I wrote earlier in the thinking was: if c >=x, sum +=x, else sum +=0. But in the code above, for the else, it's sum_kept +=c. That's incorrect.
# Oh no, that's a mistake! Let me re-express the code correctly.
# The correct code is:
n = int(input())
a = list(map(int, input().split()))
from collections import Counter
count = Counter(a)
sum_kept = 0
for x, c in count.items():
if c >= x:
sum_kept += x
else:
# If the count is less than x, we cannot include any elements of x, because including them would require exactly x copies.
# So we add 0 to sum_kept.
sum_kept += 0
print(n - sum_kept)
# But in the initial code, the else clause adds c. That's wrong. For example, in the first sample, if the code adds c=4 for x=3, then sum_kept would be 4, and answer 0. Which is wrong.
# So the code written in the initial response is incorrect. So need to correct that.
# Let me re-examine the thinking process. In the thought process, the correct approach is to add x if c >=x, else 0.
# So the code in the thinking process was correct, but when writing the code, the else clause was written as sum_kept +=c, which is a mistake.
# Therefore, the correct code should be as follows:
n = int(input())
a = list(map(int, input().split()))
from collections import Counter
count = Counter(a)
sum_kept = 0
for x, c in count.items():
if c >= x:
sum_kept += x
print(n - sum_kept)
# Because in the else case, we do nothing (add 0). So the code in the thinking was correct, but the initial code written here had a mistake. So the correct code is as above.
# Let's test the sample input where the original code had the mistake. For example, the first sample:
Input 4: 3 3 3 3 → count[3] =4. So 4 >=3 → sum_kept +=3. So 4-3=1. Correct.
Another sample, input 5: 2,4,1,4,2. count[1]=1 → add 1. count[2]=2 → add 2. count[4]=2 → add 0. sum is 3. 5-3=2. Correct.
The sample input 8: 2,7,1,8,2,8,1,8 → count for 1 is 2 → 2 >=1 → add 1. 2's count is 2 → add 2. 7's count 1 <7 → add 0. 8's count 3 <8 → add 0. sum_kept is 3. 8-3=5. Correct.
So the code should be fixed.
The original code written in the assistant's answer had a mistake. So the correct code is as above.
But in the initial code written by the assistant, the else clause added c. So why? Because during the transition from thinking to code, a mistake was made.
So here's the corrected code.
python
n = int(input())
a = list(map(int, input().split()))
from collections import Counter
count = Counter(a)
sum_kept = 0
for x, c in count.items():
if c >= x:
sum_kept += x
print(n - sum_kept)