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.
We are given a sequence of positive integers, length N up to 100k, a_i up to 1e9. We need to remove some elements to make the remaining sequence "good": For each element x in the sequence, the number of occurrences of x in the sequence must be exactly x. Note that the condition applies to each distinct element present. So if we have a multiset of numbers, each number v that appears must appear exactly v times. It's okay to have multiple different numbers each satisfying that.
We need to minimize removals, equivalently maximize the size of a subsequence (not necessarily contiguous) that is good. Since removal order doesn't matter, we just choose a subset of the original indices such that the multiset of chosen numbers satisfies the condition.
Goal: Compute maximum cardinality of subset S of indices such that for each distinct value v in S, count_S(v) = v. Then answer = N - max_size.
We need to compute this efficiently.
Let's denote the frequencies of each value in the original array: For each distinct integer x, let f_x = number of occurrences of x in the original array. For huge values up to 1e9, but N is only 100k, so we can compress.
Now, we need to decide for each x, how many copies we take from the available f_x into the final good sequence. Let t_x be the number of copies we take. The condition: For any x with t_x > 0, we must have t_x = x. For x with t_x = 0, it's fine. Also, t_x cannot exceed f_x. So we either take exactly x copies, or take 0 copies, provided that f_x >= x. Wait, if f_x < x, can we still take t_x? The condition for a good sequence: if we include an element of value x, then the number of occurrences of x must be exactly x. So if we include any x, we must have at least x copies. If we have less than x copies available, we cannot include any x because we cannot reach exactly x (we could only include less, but then the condition would fail for that x). So we have two possibilities: either we exclude x entirely (t_x=0) or we include exactly x copies (t_x = x) provided f_x >= x. But what if f_x > x? Then we can choose to take exactly x copies, discarding the rest. We cannot take more than x because then the count would exceed x and not satisfy condition. We could also take 0. So indeed, for each distinct value x, we have a binary choice: either we include the value in the good subsequence, in which case we must include exactly min(x, f_x?) Actually we need exactly x copies. But if f_x < x, we cannot include. So for x with f_x < x, the only viable option is to exclude all copies (t_x=0). For x with f_x >= x, we could either take exactly x copies (and discard the rest) or take 0.
Thus the problem reduces to: given a set of values x with f_x >= x, we can select a subset of these values to include, each inclusion yields a contribution of x to the total size of the good sequence, but we must ensure that we are not double-counting? Actually each value x we include adds x copies, and they are all distinct values? But note: if we include two different values, they don't interfere with each other. Because the condition is per value. So we can independently decide for each x whether to include it (provided we have enough copies). However, there is a subtlety: The elements we take for different x are distinct copies because each element has a specific value. So there's no conflict: we can simultaneously include multiple values as long as we have enough copies of each from the original array. Since we have f_x copies of value x, we can allocate up to f_x. If we decide to include x, we need exactly x copies, and f_x >= x ensures we can pick x. So we can include any subset of the values where f_x >= x. There's no overall limit except the original counts.
Thus the maximum total size is simply the sum over all x such that f_x >= x, of x? Wait, is it always optimal to include all such x? Possibly yes, because including x adds x to the total size, and doesn't affect other values because they are distinct. However, we must consider that there might be a scenario where including a value might conflict with something else? No, because each element belongs to a specific value. There's no overlap; we can independently choose for each value. But is there any constraint like total number of elements taken from original cannot exceed N (but that's always true) or we might run out of indices? No, because we have separate pools. So it seems we can take all values that satisfy f_x >= x. Then total size = sum_{x: f_x >= x} x. Then answer = N - sum_{x: f_x >= x} x.
But wait, let's test with examples.
Example 1: 4, a = [3,3,3,3]. f_3 = 4. x=3: f_x >= 3? Yes. So sum = 3. N=4, answer = 1. That matches.
Example 2: N=5, a=[2,4,1,4,2]. Compute frequencies: f_1=1, f_2=2, f_4=2. Check: x=1: f_1=1 >=1 => include? Then sum includes 1. But careful: if we include 1, we need exactly one copy of 1, which we have. That's fine. x=2: f_2=2 >=2 => include 2 => adds 2. x=4: f_4=2, but 4 requires 4 copies; f_4=2 <4 => cannot include. So sum = 1+2 = 3. N=5 => answer=2. That matches example (output 2). But wait, is the resulting sequence good? If we take all values that satisfy condition, we take one 1 and two 2's. The sequence would contain: one 1, two 2's. Check condition: For value 1, it occurs exactly 1 time; for value 2, it occurs exactly 2 times. Good. That's valid. So sum=3, answer=2.
Example 3: N=6, a=[1,2,2,3,3,3]. f_1=1 (>=1), f_2=2 (>=2), f_3=3 (>=3). Sum = 1+2+3 = 6, answer=0. matches.
Example 4: N=1, a=[1e9]. f_x = 1. x=1e9: f=1 < 1e9 => cannot include. So sum=0, answer=1. matches.
Example 5: N=8, a=[2,7,1,8,2,8,1,8]. Let's compute frequencies: 1:2? Actually list: 2,7,1,8,2,8,1,8. So frequencies: 1:2, 2:2, 7:1, 8:3? Wait count: 8 appears at positions 4,6,8 => 3 copies. So f_1=2, f_2=2, f_7=1, f_8=3. Check each:
x=1: f=2 >=1 -> include? Yes, we need exactly 1 copy. But careful: If we include 1, we need exactly 1 occurrence of 1. But we have 2 copies; we can choose to take only one of them. That's allowed because we remove the other. So including 1 is possible. But we must also check: The condition for the final sequence: each element x must occur exactly x times. For x=1, exactly 1 time. So we can take 1 copy of 1. So including 1 yields contribution 1.
x=2: f=2 >=2 -> include, contributes 2.
x=7: f=1 <7 -> cannot include.
x=8: f=3 <8 -> cannot include.
Sum = 1+2 = 3. So answer = 8-3 = 5. That matches example output: 5.
Thus the solution appears to be simply as described: answer = N - sum_{x: f_x >= x} x.
But is it always optimal to include all such x? Let's think more deeply. There might be a subtle conflict: what if including a value x uses up copies of some other value? No, because copies are distinct per value. However, consider a scenario where f_x is greater than x, but we have multiple values, maybe we could include a subset that yields more total elements by sometimes not including a value with small x but including something else? But since each included value adds x (its required count) and we can always include it if f_x >= x, it seems it's always beneficial to include it because it adds positive number of elements (x>=1) and doesn't hinder anything else. So indeed, including all is optimal.
But wait, is there any case where including a value might force us to discard copies that could be used for another value? No, because values are distinct; a copy of value x can only be used to satisfy x's own requirement. It cannot be used to satisfy a requirement of a different value. So no conflict.
Thus the problem reduces to computing frequencies, for each distinct value, if frequency >= value, add value to sum; else disregard. Then answer = N - sum.
But hold on: The condition says "For each element x in b, the value x occurs exactly x times in b." That implies that if b contains some value x, then count(x) must be x. But it does not say that every x that appears in b must satisfy that; it says for each element x in b, the count of that value is exactly x. Which is essentially the same as: for every value x that appears in b, its frequency must be x. So yes.
Thus it's straightforward. However, I'm suspicious because this seems too easy for a problem likely from AtCoder (maybe ABC or similar). Actually I recall a problem "Good Sequence" from AtCoder Beginner Contest, maybe indeed it's simple. But I'll check constraints: N up to 1e5, a_i up to 1e9. So we need to use map/dict to count frequencies.
Edge Cases: For large x (like 1e9) if f_x >= x, then we add x to sum, but f_x cannot be >= x because N <= 1e5, and x is huge. So we only need to consider x <= N because if x > N, then f_x (at most N) cannot be >= x, so we can skip. But careful: x could be up to 1e9, but N=100k, so indeed if x > N, f_x cannot be >= x because f_x <= N < x. So we only need to consider x <= N. So we can filter.
But we still need to count frequencies for all values in the array, including those > N, because they may appear and we need to know f_x to know if we ignore them (they will always be excluded since f_x < x). So we could just count all, but if we iterate over all distinct values, it's at most N distinct values, so it's fine.
Thus solution: read N, array a. Count frequencies using unordered_map or map (since up to 1e5, we could also sort and count). Then initialize sum = 0. For each key-value pair (x, freq), if freq >= x then sum += x; else sum unchanged. Then answer = N - sum. Output answer.
But is it always optimal? Let's think more: Consider a scenario where f_x > x. Could we include only part of the copies, but also use the extra copies as something else? No, because they are value x. They cannot become other values. So we must either include exactly x copies (and discard the rest) or include none. So no alternative.
Thus solution is correct.
But wait, what about values that appear exactly x times? Then they contribute x.
Thus answer as described.
Let's test additional cases manually.
Case: N=4, a=[2,2,2,2]. f_2=4 >=2, so sum +=2 => sum=2. answer=2. Is it possible to have a good sequence of size >2? Let's test possibilities: We could take all four 2's, but then count of 2's is 4, not 2, so fails. We could take three 2's -> count 3, fails. We could take two 2's -> good (since 2 appears twice). Could we also include something else? No other values. So max size =2, matches.
Case: N=5, a=[1,1,1,1,1]. f_1=5 >=1, sum +=1 => sum=1, answer=4. Is max good sequence size=1? Check: If we take one 1, it's good (1 appears once). Could we take more? If we take two 1's, then count=2, but 1 would need to appear exactly once, so fails. So yes, max=1.
Case: N=3, a=[1,2,2]. f_1=1 (>=1) => include 1. f_2=2 (>=2) => include 2. sum=1+2=3, answer=0. Good? Sequence with all three: [1,2,2] -> count(1)=1, count(2)=2 => good. Yes.
Case: N=3, a=[1,2,3]. f_1=1 (>=1) include 1; f_2=1 (<2) exclude; f_3=1 (<3) exclude. sum=1, answer=2. Good sequence: just [1]. Could we get size 2? Possibly [2,?] No. [1,2] has count 1 for 1 (good) and count 1 for 2 (needs 2) fails. [1,3] fails similarly. [2,3] fails. So max=1.
Case: N=5, a=[3,3,3,3,3] f_3=5>=3 => sum=3, answer=2. Good sequence: three 3's. Good.
Case: N=6, a=[2,2,2,2,2,2] f_2=6>=2 => sum=2? Wait, but if we include 2, we must have exactly 2 copies. That yields total size 2. But can we possibly get a better good sequence by including some other value? No, all are 2. So max=2. But answer would be 4. Is it possible to use some of the 2's to form a different number? No. So sum=2. However, consider that we have six 2's; maybe we could think about taking three 2's? That fails. So correct.
Case: N=7, a=[2,2,2,2,2,2,3] f_2=6>=2, f_3=1<3. So sum=2, answer=5. Could we include 3? No. Could we include both 2 and 3? We can include 2 (2 copies) and also include 3? But 3 requires 3 copies, we have only 1, so cannot include 3. So sum=2, answer=5. Check manually: best good sequence: two 2's. Yes.
Now, is there any scenario where we would want to exclude a value even though f_x >= x, because including it might prevent including another value? No, because they are independent. However, consider a value x that is large, say x=1000, and f_x=1000 (exactly). Including it adds 1000 to sum. That seems good. But what about the copies of other values? They are unaffected. So include.
What about a case where we have two values x and y such that x = y? They are same value, but we treat as one value.
Thus it's simple.
But wait: There's a nuance: The condition is "For each element x in b, the value x occurs exactly x times in b." This is a condition on the multiset b. If b is empty, it's vacuously true. So that's fine.
Now, double-check: Could there be a scenario where we include a value x but we have more than x copies, we can choose any x of them. That's fine.
Thus solution is correct.
But hold on, could we possibly get a larger total by not including some value that we could include, but instead using some of its copies as part of a different value? That doesn't make sense because values are fixed.
Thus problem solved.
Given the problem statement, maybe it's from AtCoder ABC 124 C? Actually no. But it's trivial.
But wait, I recall a problem "Good Sequence" from AtCoder ABC 124? Actually there's "Good Sequence" from ABC 113? Let me think: There is AtCoder problem "Good Sequence" (AGC??). Actually I'm aware of a problem: "Good Sequence" from AtCoder Beginner Contest 124? That problem was about choosing subsequence such that each number's count equals itself. The solution indeed is as we described. I think it's correct.
Thus answer is N - sum_{x: freq[x] >= x} x.
Now let's consider implementation details.
We need to compute frequencies for up to 1e5 numbers. Use a hash map (unordered_map) but careful: keys are up to 1e9, but only up to 1e5 distinct. So it's fine.
Alternatively, we could sort the array and iterate. Sorting and counting is also O(N log N) but simpler and maybe less overhead. But either works.
Algorithm steps:
Read N.
Read array a of length N.
Count frequencies: using map<int, int> freq.
Initialize total = 0.
For each (value, count) in freq:
if count >= value:
total += value
else:
total += 0? Actually if count < value, we may still be able to take some copies? But we cannot take exactly value. But maybe we could take count copies? Let's check: If count < value, we cannot have the value appear because if it appears, its count must equal value, which we can't achieve. But could we have a good sequence where that value appears count times? The condition says for each element x in b, the value x occurs exactly x times. So if we include any copy of x, we must have exactly x copies total. So we cannot have any copy if count < x. So we must remove all copies. Thus no contribution. So we add 0.
answer = N - total.
Print answer.
But wait, there is a subtle case: Suppose freq[x] > x. Then we include x, and we get exactly x copies in the good sequence. But we might also consider including x as something else? No.
Now, is there any possibility that we could include a value x with count less than x but we could combine multiple numbers to achieve a different value? No, because numbers cannot change.
Thus answer stands.
But let's verify a bit more by constructing a counterexample if we try to be greedy for all such x: maybe we can sometimes get more by discarding some values that would force us to remove extra copies that could be used for other values? That doesn't happen.
Consider: Suppose we have two values: x=3 with freq=4, and y=2 with freq=3. If we include both, total = 3+2 = 5, and we remove (4-3)+(3-2)=1+1=2, total removed =2, kept=5. If we skip x, we can include y only: total=2, keep=2, remove 4+3-2=5, which is worse. If we skip y only: total=3, remove (4-3)+(3) = 1+3=4, keep=3 worse. So including both is best.
Now consider x=100, freq=101, y=1 with freq=1. Include both gives total 101, keep 101? Actually sum = 100+1=101. N=102? Wait, total original N = 101+1=102? Actually freq: x 101, y 1 => N=102. If we include both, we take exactly 100 x's and 1 y => total keep = 101, removed = 102-101=1. That's good. If we skip y, keep 100, removed=2. If we skip x, keep 1, removed=101. So include both better.
Now consider if x=100, freq=100, y=1, freq=1, then include both yields keep=101, original N=101, removed=0, good. If skip y, keep=100, removed=1.
Thus always include both when possible.
Now, could there be a scenario where including a large x with freq >= x might consume copies that could be used to form a different number? No.
Thus it's optimal.
Given the simplicity, I suspect that the problem is indeed that. However, I recall a similar problem where we need to consider that if freq[x] > x, we might need to remove all freq[x] because maybe we cannot keep exactly x? But we can keep exactly x by removing the extra copies. So that's fine.
But wait: The problem statement: "remove some of the elements in a so that a will be a good sequence." This means we are deleting elements from the original sequence, not selecting a subset and rearranging? But order doesn't matter; we just need the resulting sequence to be good. We can delete any elements, so we can keep a subset. So yes.
Thus solution is trivial.
Nevertheless, maybe there is a nuance: The definition says "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." This condition must hold for all elements, but note that if b contains elements with different values, the condition applies to each such value. So it's as we used.
One more check: Is there any scenario where we could have a value that appears more than its value but we could also keep other values, but maybe we need to remove some copies of a value to maintain that for another value? No, because they are independent.
Thus answer is clear.
Now, let's think about potential pitfalls: For values that are huge, say a_i = 1e9, but freq is 1, then we cannot keep it. So we remove it. That's fine.
But what about values like x=0? The problem says positive integers, so all a_i >=1. So no zero.
Now, the implementation can be done with unordered_map. However, note that if we iterate over the map and add x when freq >= x, this includes x that might be > N, but freq >= x cannot happen if x > N because freq <= N. So we can also skip checking those. But it's fine to check.
Complexities: O(N) time and O(N) memory.
But is there any tricky part? Possibly some might think about when freq[x] > x, we might have to remove all copies? Because if we keep exactly x copies, that's allowed. So it's fine.
Double-check the third example: Input: 6; 1 2 2 3 3 3. sum = 1+2+3=6, answer=0. Good.
Now, test another case: N=4; a=[2,2,2,2]. sum=2, answer=2. Good.
Now, test case: N=2; a=[1,1]. f1=2 >=1, sum=1, answer=1. Good sequence: [1] works.
Now test: N=2; a=[1,2]. f1=1>=1 -> include 1; f2=1<2 exclude; sum=1, answer=1.
Now test: N=3; a=[1,1,2]. f1=2>=1? include 1? But careful: If we include 1, we must have exactly 1 copy of 1? Actually condition: if we include element x=1, then count of 1 must be 1. But if we also include the 2, we have to have exactly 2 copies of 2. We have only one 2, so cannot include 2. So we could either include 1 (taking one of the 1's) and ignore 2, or include 2? can't. Could we include both 1's? That would be count(1)=2, but condition for element 1 would require count(1)=1, so fails. So we must have exactly one 1 if we include 1. So we have two 1's, we can only keep one. So sum=1? But we could also decide not to include 1, then maybe include nothing? But empty is allowed. So max size = 1. According to our rule: f1=2>=1 -> include 1? But what does "include 1" mean? Our sum adds x=1, but that implies we take exactly one 1. That yields total kept=1. Good. So answer = 3-1=2. Let's see if we could do better by not including 1 but maybe including something else? No. So answer=2. But wait, is there a possibility to keep both 1's and also maybe add something? No, because that would violate condition. So answer=2. But is there any way to keep both 1's and also keep 2, but then modify something? No. So it's correct.
Now test: N=3; a=[1,1,1]. f1=3>=1 -> sum=1, answer=2. That seems correct: we can keep one 1, remove two.
Thus solution holds.
Now we should ensure we understand problem statement correctly: "remove some of the elements in a so that a will be a good sequence." That implies we are allowed to delete some elements, and the remaining elements (in original order?) The order doesn't matter for the condition, and deletions can be arbitrary. So we simply select a subset. So yes.
Thus final solution.
Now we need to output the minimum number of removals, which is N - total.
Implementation:
cpp
#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
int N;
cin >> N;
unordered_map<int, int> freq;
for (int i=0; i<N; ++i) {
int x;
cin >> x;
freq[x]++;
}
long long kept = 0; // may need long long because sum of x could be up to N? Actually each x <= N, sum of x over selected could be up to N? Suppose we include values like 1,2,... such that sum of x = N. But x can be up to 1e9 but if x <= N sum may exceed N? Actually if we include x > N, can't happen. So max sum of kept is at most N (since total kept cannot exceed N). However, sum of x over distinct selected values might exceed N? Wait, kept is the number of elements we keep, not the sum of the x values of selected types? Actually kept = total elements kept = sum_{selected} x. Since each selected value x contributes x elements, the total kept is sum x. This total cannot exceed N because we have only N elements total. So kept <= N. So int is fine for N <= 1e5. But x can be up to 1e9, but if we include such x, then N must be at least x, which is >1e5? Actually N <= 1e5, so x > 1e5 cannot be selected because f_x cannot be >= x. So we only include x <= N. Thus sum is at most N. So 32-bit int is fine. But we might use long long for safety.
for (auto& p : freq) {
int x = p.first;
int cnt = p.second;
if (cnt >= x) {
kept += x;
}
}
cout << N - kept << endl;
return 0;
}
But note: For values with cnt >= x, we add x, not cnt. That's the key.
One more nuance: What about values that appear exactly x times? Then we add x.
Thus answer is N - sum_{x: cnt >= x} x.
Now, is it possible that by removing some copies from a value with cnt > x, we could also satisfy a different value? For example, if we have many copies of a value that is not included, could we transform them? No.
Thus solution is final.
Given the simplicity, I'd like to double-check the problem statement from the original source if possible. I recall an AtCoder problem "Good Sequence" (maybe ABC 126 B? Not sure). But I think it's correct.
Thus final answer.
Now we should produce the answer in the required format. We'll output the integer.
Thus final solution is as described.
But wait, there might be an edge-case: what if there is a value x such that freq[x] > x, but we decide to keep zero copies of x, but we keep some other value y that requires copies? That's independent. So no.
Now, verify with a tricky scenario: Suppose we have values: 2 with freq=3, and 1 with freq=1. If we include both, we need exactly 2 copies of 2 and 1 copy of 1. But we have 3 copies of 2, so we can keep 2 of them, discard one. And keep the 1. So total kept = 3, original N = 4, removal =1. According to rule: include both (since freq2=3>=2, freq1=1>=1) sum=2+1=3, answer=1. Good.
What if we had values: 2 with freq=2, and 1 with freq=1. Include both: sum=2+1=3, kept=3, N=3, removal=0. Good.
Thus it's consistent.
Now, consider case where we have value 3 with freq=5, value 2 with freq=2. Include both: sum=3+2=5, N=7, removal=2. But could we possibly keep more than 5? For example, could we keep 3 copies of 3 and 2 copies of 2, that's 5. Could we keep some extra 3s? No, because then count of 3 would be >3, not allowed. Could we keep all 5 copies of 3? No, because then count of 3 would be 5, not 3. So 5 is max. So rule works.
Now, consider if we have value 3 with freq=4, value 1 with freq=2. Include both: sum=3+1=4, N=6, removal=2. Could we include only 3? then kept=3, removal=3. Could we include only 1? then kept=1, removal=5. So both best.
Now, consider if we have value 10 with freq=10, and many small values that also meet condition, sum of all x may exceed N? Wait, if we have many values each with freq >= x, sum of x (the kept count) could exceed N? But we cannot keep more than N elements because we only have N elements total. However, sum of x may be <= N? Actually is it guaranteed? Let's check: For each value x we want to keep exactly x copies. The total number of kept copies is sum over selected x of x. But we must have for each selected x, the original frequency f_x >= x. This doesn't guarantee that sum x <= N, because we might have overlapping? But they are distinct values, so the copies used for different x are distinct (they are different elements). So total kept = sum x must be <= total available elements = N, because we need to allocate distinct elements for each selected value. However, it's possible that the sum of x for selected values could exceed N even if each individually f_x >= x? Let's test: Suppose N=5, we have value 3 with freq=4, value 2 with freq=2. Then f_3=4>=3, f_2=2>=2, sum x=3+2=5, which equals N=5. That's okay. But what about value 4 with freq=4, value 2 with freq=2, N=6? That sum=4+2=6, okay. Could we have sum x > N? Let's try to construct: We need each f_x >= x. If we have two values x and y with x+y > N, can we have f_x >= x and f_y >= y simultaneously? Since f_x + f_y <= N (because they are counts of distinct values, but there could be other values too, but at least these two counts sum to <= N? Actually f_x + f_y <= N because the total number of elements with those two values cannot exceed N. If x+y > N, then it's impossible for both f_x >= x and f_y >= y because that would require f_x + f_y >= x+y > N, which is impossible. So it's impossible to have both conditions hold if the sum of those x's exceeds N. More generally, for any set of selected values S, sum_{x in S} x <= sum_{x in S} f_x <= N (since f_x are counts of distinct values, they sum to at most N). Actually the sum of f_x for all distinct values is N. For the selected S, sum_{x in S} f_x <= N. And if we select S, we require f_x >= x, so sum_{x in S} x <= sum_{x in S} f_x <= N. Therefore sum x <= N automatically holds. So no risk. So kept sum is always <= N.
Thus answer is non-negative.
Now, the solution appears straightforward.
But wait, there is another nuance: The definition: "For each element x in b, the value x occurs exactly x times in b." This could be interpreted as: For each position i in b, letting the element be x_i, the number of occurrences of the value x_i in b must be exactly x_i. That's what we used. So it's correct.
Thus we can produce final answer.
But I'm thinking: Might there be a scenario where it's beneficial to keep some but not all copies of a value even if we don't satisfy exactly x? For example, consider value 2 appears 1 time. Could we keep that single 2? Then we have one 2 in b. For element 2, we need exactly 2 occurrences of 2, but there is only 1, so condition fails. So not allowed.
Thus indeed each value either is completely excluded or appears exactly x times.
Thus solution.
Now, since the problem is given in a contest, maybe there is a twist: The values are up to 1e9, but we only care about frequencies. The answer is easy.
Thus final.
Now, I'll produce the final answer code.
But wait, there is a possible alternative: Some might think we need to consider also values with freq < x: we need to remove all of them. That's what we do.
Now, let's think about potential integer overflow: N up to 1e5, answer up to 1e5, so int fine. But in sum, we might use long long, but not needed.
Thus final code:
C++:
cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
unordered_map<int, int> mp;
for (int i=0; i<N; ++i) {
int a;
cin >> a;
mp[a]++;
}
ll kept = 0;
for (auto& p : mp) {
int x = p.first;
int cnt = p.second;
if (cnt >= x) {
kept += x;
}
}
cout << N - kept << endl;
return 0;
}
But note: if we use unordered_map, need to handle large keys; it's fine.
Alternatively, we can sort and count.
Thus final.
Now, we can output.
But before finalizing, I'll re-check the examples:
Example1: N=4, a=3,3,3,3. mp[3]=4 >=3 => kept=3 => 4-3=1 OK.
But wait: There is a potential bug: In the sum, we might count a value x multiple times? No, each distinct key once.
Now, confirm that we are not missing the case where there are duplicates of same value but we could also keep some of them as part of different values? No.
Thus final answer.
Now, I'll produce the final answer. The problem reduces to counting frequencies of each value. For a value x, if its frequency f_x is at least x, we can keep exactly x copies in the final good sequence; otherwise we must remove all copies of x. The maximum number of kept elements is therefore the sum of x over all distinct x with f_x ≥ x. The answer is the total number of elements N minus that sum.
cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
unordered_map<int, int> freq;
for (int i = 0; i < N; ++i) {
int a;
cin >> a;
freq[a]++;
}
long long kept = 0;
for (auto& p : freq) {
int x = p.first;
int cnt = p.second;
if (cnt >= x) kept += x;
}
cout << N - kept << '\n';
return 0;
}