We need to understand the problem: Given an array a of length n. We want to remove some elements (minimum number) so that the resulting array is beautiful. Beautiful means: there exists a number C (a positive integer?) such that each number that appears in the array occurs either exactly 0 times (i.e., not present) or exactly C times. So all numbers that are present must appear the same number of times. Note that C can be any positive integer? Could C be 0? That would mean all numbers occur 0 times (empty array), but we could remove all elements. However, we want to minimize removals, so we want to keep as many as possible. So we want to find a subset of the array (by removing some elements) such that the frequencies of the remaining numbers are all equal to some C > 0 (maybe C=0 allowed? But then we would remove all elements, which is rarely minimal). So effectively, we need to choose a target frequency C and keep some numbers, each exactly C times (we can discard some occurrences of each number). For numbers not kept at all, we remove all occurrences. For numbers we keep, we must keep exactly C occurrences, so we may remove some of their occurrences if they occur more than C, and we cannot keep numbers that have fewer than C occurrences because we need to have exactly C occurrences. However, we could also decide to discard some numbers entirely. So we need to choose a non-negative integer C and a subset S of distinct numbers such that for each x in S, the original frequency f_x >= C, and we will keep exactly C occurrences of x (by removing f_x - C occurrences). For numbers not in S, we remove all f_x occurrences. The total kept elements = |S| * C. The total removals = n - |S| * C.
We want to minimize removals = maximize kept elements = maximize |S| * C subject to constraints: For each chosen x, f_x >= C. So given the frequencies of distinct numbers (we can compute counts of each value), we need to pick C and a set of frequencies that are >= C, and then keep exactly C from each, so effectively the kept = number of distinct numbers with f >= C times C. But note: we can also choose to not use some numbers even if f_x >= C? Because we might want to increase C, but then some numbers may not have f_x >= C, but we could still keep only numbers that do. The product = (# of numbers with f >= C) * C. However, could we sometimes choose to not include a number that has f >= C because we want to increase C? No, because if we increase C, the set of numbers with f >= C might be smaller, but we are free to include any subset of numbers that have f >= C. But including more numbers never hurts the product because if we include an additional number with f >= C, we add C kept elements. So it's optimal to include all numbers with f >= C. So indeed, for a given C, the maximum kept = (count of numbers with frequency >= C) * C. However, we need to consider that we can also choose C=0, but that yields kept=0. So we need to maximize over integer C >= 1 (or maybe C can be any positive integer) of: kept(C) = C * M(C), where M(C) = number of distinct values whose frequency f is at least C.
But wait: is there any scenario where we would not want to include all numbers with f >= C because we could keep fewer numbers but also maybe choose a different C? No, because for a fixed C, we want to maximize kept, so we include all possible. So the optimal solution is max_{C>=1} C * (# of f_i >= C). However, we also need to consider that we can choose any C, not necessarily dividing anything. So we need to compute frequencies and then consider all possible C that could be optimal. Since frequencies sum to n, the number of distinct frequencies is O(sqrt(n))? But we can iterate over possible C efficiently. Since n up to 2e5 per test case sum, we can try all possible C from 1 to n, but that would be O(n^2) overall if many test cases with large n? But total sum n <= 2e5, so we could iterate over all C from 1 to max freq? That would be O(max freq) per test case. Since total n sum is 2e5, the maximum n per test case could be 2e5, so iterating up to 2e5 for each test case could be up to 2e5 * t? But t can be up to 1e4, but total n sum is 2e5, so many test cases have small n. In worst case, we could have 2e5 test cases each with n=1. Then iterating up to n for each would be 1e5? Actually each n=1, max freq =1, so iterating up to 1 for each gives total 2e5 operations, okay. But if we have one test case with n=2e5, iterating up to 2e5 is okay. So total across all test cases iterating up to each n could be sum of n? Not exactly: if we for each test case iterate from 1 to max frequency, that might be O(max frequency) which could be up to n, but sum of n across test cases is 2e5, so sum of max frequencies is at most 2e5? Not exactly: In a test case with n=2e5, max frequency could be 2e5 (if all same number). That's one test case. So total iterations over C would be about 2e5 for that test case plus maybe small for others. That's fine.
But we need to compute M(C) = count of frequencies >= C. If we have frequencies f1, f2, ..., fk (k distinct values), we can sort frequencies descending. Then we can compute for each possible C, M(C) = number of f_i >= C. That's like a histogram. Instead of iterating all C from 1 to max f, we could consider only C values that are among the frequencies (or maybe all possible). However O(max f) is acceptable because max f <= n <= 2e5. So we can create an array cnt_freq: for each possible frequency value f, we have count of numbers that have that frequency. Actually easier: let freq_counts be a map from value to frequency. Then we have list of frequencies v. Sort v descending. Then for each C, M(C) can be computed as the number of elements in v with f >= C. That can be precomputed by iterating over C from max_f down to 1: maintain pointer. Or we can compute frequency_of_frequencies: freq_count array of size n+1, where freq_count[f] = how many numbers have exactly frequency f. Then we can compute suffix sums to get M(C). That's straightforward. Since n up to 2e5, we can allocate an array of size n+1 per test case and fill zero. Then for each distinct number, we get f, freq_count[f]++. Then we compute suffix sum: greater_or_equal[C] = sum_{x>=C} freq_count[x]. Then compute kept = C * greater_or_equal[C] for all C from 1 to n, and find max. Then answer = n - max_kept. That's O(n) per test case, which is fine.
But careful: The frequency array size is n+1, and sum of n is 2e5, so total memory across test cases (if we allocate new each time) okay.
Edge case: Could C be greater than n? No, because if C > n, no number can have frequency >= C, so product 0. So we consider up to n.
Also, could we consider C = 0? That would give kept = 0, which is worse than any positive C if there exists any number, but maybe if n=0? Not possible. So ignore.
Thus answer = n - max_{C>=1} (C * (# of f_i >= C)).
Now confirm with examples:
Example 1: n=6, a=[1,3,2,1,4,2]. Frequencies: 1:2, 2:2, 3:1, 4:1 => v = [2,2,1,1]. Compute:
C=1: # >=1 =4 => kept=4
C=2: # >=2 =2 => kept=4
C=3: # >=3 =0 => kept=0
max kept=4, answer=2. matches.
Example 2: n=4, a=[100,100,4,100]. Frequencies: 100:3, 4:1 => v=[3,1]
C=1: #>=1=2 => kept=2
C=2: #>=2=1 => kept=2
C=3: #>=3=1 => kept=3
C=4:0 =>0
max kept=3, answer=1. matches.
Example 3: n=8, a=[1,2,3,3,3,2,6,6] => frequencies: 1:1,2:2,3:3,6:2 => v=[3,2,2,1]? Actually: distinct: 1:1,2:2,3:3,6:2 => sorted: 3,2,2,1.
C=1: #>=1=4 => kept=4
C=2: #>=2=3 (3,2,2) => kept=6
C=3: #>=3=1 => kept=3
C=4:0
max kept=6, answer=2. matches.
Thus the solution is correct.
But we should think about potential pitfalls: The problem statement says "each number in the array occurs either zero or C times." That includes that numbers not present occur zero times, which is allowed. So yes.
Thus algorithm: For each test case:
- Read n, read array a.
- Count frequencies using unordered_map or map (since values up to 1e9, many distinct).
- Build freq_count array of size n+1 (or n+1) initialized to 0. For each frequency f from map, increment freq_count[f].
- Compute suffix sum: let ge = array of size n+2 perhaps, initialize ge[n+1]=0. For c from n down to 1: ge[c] = ge[c+1] + freq_count[c].
- Then iterate c from 1 to n: kept = c * ge[c]; track max_kept.
- Answer = n - max_kept.
- Print answer.
But note: There might be multiple distinct numbers with same frequency; the freq_count array will count them correctly.
Complexities: O(n) per test case.
Now we must consider if any nuance: Could we choose C that is not integer? No, frequencies are integer counts.
But we could also consider that we can choose C that is larger than some frequencies, but we already consider all C.
Potential alternative: Could we also consider that we may not need to take all numbers with f >= C, because maybe we could get a larger product by excluding some numbers with f >= C? Wait: If we have numbers with f >= C, we can include them and get C each. Including them increases kept by C. So it's always beneficial to include them. However, what if we exclude some numbers, then we could potentially choose a larger C? That's different: The optimization is over C; for each C, we consider keeping all numbers with f >= C. But is it possible that by selecting a subset of numbers (not necessarily all with f >= C), we could achieve a product larger than C * M(C) for some other C? For example, suppose frequencies: [5,3,3]. The M(C) function:
C=1: 31=3
C=2: #>=2 =3? Actually 5>=2,3>=2,3>=2 => 3, product=6
C=3: #>=3 =3? 5>=3,3>=3,3>=3 =>3, product=9
C=4: #>=4 =1 (5) => product=4
C=5: 1 =>5
max kept=9, answer n=11, removal=2. That seems plausible: keep each of the three numbers exactly 3 times? But 5 has 5 occurrences, so we need to remove 2 from it, and each 3 we keep all 3, so total kept=9, removals=2. That matches product CM(C). Now suppose we try a different strategy: Choose C=4, keep only the number with freq 5 (remove 1) and drop the two numbers with freq 3 entirely? That would keep 4, removals=11-4=7, worse. Or choose C=2, keep all numbers, each keep 2? For freq 5 keep 2 remove 3, freq3 each keep 2 remove 1, total kept=6, removal=5. So indeed best is 9.
Now consider scenario: frequencies: [4,4,4,1,1]. n=14. C=4: #>=4=3 => kept=12. C=3: #>=3=3 => kept=9? Actually #>=3 = 3 because all three 4s are >=3, 1s are not => 3, product=9. C=4 gives 12, removal=2. Could we do better by not taking all three 4s? What if we take C=5? #>=5=0. So no. So product 12 is best.
Now consider frequencies: [6,5,5]. n=16. C=5: #>=5=3 => kept=15. C=6: #>=6=1 => kept=6. So best kept=15, removal=1. That matches: Keep each number exactly 5 times? But 6 has 6 occurrences, so remove 1 from it; each 5 keep all 5; total kept 15. Good.
Now consider frequencies: [7,7,1,1,1]. n=17. C=7: #>=7=2 => kept=14. C=3: #>=3=2? Actually 7>=3 and 7>=3 => 2, kept=6. C=2: #>=2=2 => kept=4. C=4: #>=4=2 =>8. So best 14? Could we do better by selecting C=5? #>=5=2 =>10. So max 14. But what if we take C=3 and also include some of the 1's? The 1's are <3 so they cannot be included with C=3 because they would need exactly 3 occurrences but they only have 1. So we cannot include them. So indeed best is 14.
Now is there any scenario where we might want to include a number with f >= C but keep fewer than C occurrences? That would not satisfy beautiful condition because that number would appear less than C, not allowed; we could choose to drop that number entirely. So no.
Thus solution stands.
Edge Cases:
- All numbers distinct: frequencies all 1. Then for C=1, M(1)=number of distinct values = n, kept = n, removal 0. So answer 0.
- All numbers same: frequencies = [n]. Then for C=n, M(n)=1, kept=n, removal 0. Also for C=1, kept=1*1=1? Actually M(1)=1 because only one distinct value? Wait distinct values count is 1, so M(1)=1, kept=1, not n. But C=n gives kept=n. So answer 0.
Now we also need to handle multiple test cases, sum of n <= 2e5.
Implementation details:
- Use fast I/O (cin, cout with tie false) due to large input.
- For each test case, use unordered_map<int,int> freqMap to count occurrences of each value.
- Then create a vector freqCount(n+1, 0) (maybe using int array).
- For each pair in freqMap: freqCount[f]++.
- Then compute suffix sum: maybe keep a vector ge(n+2,0) but we can compute on the fly:
let ge = 0; iterate c from n down to 1:
ge += freqCount[c];
kept = c * ge; update max_kept.
- Because we need # of frequencies >= c, which is the cumulative from c to n. So we can maintain ge as we go down.
That's efficient.
Now ensure to use long for kept product because n up to 2e5, product c*ge up to n * n? Actually max kept = n (since cannot exceed n). c and ge are ints, product <= n, but we can use int for kept.
But caution: ge can be up to distinct count <= n, and c <= n, product <= n^2? Actually distinct count may be up to n, and c can be up to n, product may exceed 2e5? Example: n=2e5, distinct numbers = 2e5 (all 1). Then c=1, ge=2e5, product=2e5, within int. If n=2e5, and all numbers same, distinct=1, c could be 2e5, ge=1, product=2e5. So product at most n, because the kept elements cannot exceed n. Wait product c * M(C) is the number of kept elements, which is at most n. So it's safe as int. But the calculation c * ge could exceed n? ge = number of distinct values with freq >= c, which times c is exactly the number of kept elements if we take all such values and keep exactly c from each. But note that if we take all values with freq >= c and keep exactly c from each, the total kept is at most sum of frequencies = n. So indeed c * ge <= n. So we can use int.
But note: In the intermediate calculation, if we compute c * ge where c is up to n and ge is up to distinct count, it might be up to n * distinct count, but distinct count can be up to n, so product could be up to n^2 = 4e10, which doesn't fit int. However, in reality, if c is large, ge is small such that product <= n. But if we simply compute it, we could overflow if we use int with large numbers? Example: n=200000, distinct=200000 all frequencies 1, then ge at c=1 is 200000, product=200000 fits int. For c=100000, ge would be number of frequencies >=100000, which is 0 (since all 1). So product 0. Actually the only way product could exceed n is if we incorrectly compute ge as cumulative from c to n including numbers with frequency less than c? Wait, cumulative from c to n includes numbers with frequency exactly f where f>=c. That's correct. For c that is large, there will be few numbers with high frequency. So product <= sum of frequencies of those numbers? Actually c * ge is at most the sum of frequencies of those numbers because each number has at least c occurrences, but could be more. The sum of frequencies of those numbers is at most n. So product <= n. So integer overflow cannot happen if we compute product using int because product is at most n, which is 2e5, safe. However, if we compute c * ge using int, but ge is the count, not sum of frequencies, but we can still have c*ge > n? Let's test: suppose frequencies: [100, 1,1,1,...]. n=199? Actually if n=200, and we have one number with frequency 100, and 100 numbers with frequency 1. Then c=2, ge = number of frequencies >=2 = 1 (only the 100). product = 2, <= n. But what about c=50, ge=1, product=50, sum of frequencies for that number is 100, product 50 <= 100. In general, if we have a set S of numbers with frequencies f_i >= c, then sum_i f_i >= c * |S|, so c * |S| <= sum_i f_i <= n. So indeed product <= n. So safe.
Thus we can use int.
Now we must be careful about the frequency array: size n+1; indices from 0 to n. But we won't use index 0. We'll iterate c from n down to 1.
Alternatively, we could sort frequencies descending and then iterate over distinct C values only. But O(n) is fine.
Now let's think about potential pitfalls: The sum of n across test cases is 2e5, but we allocate an array of size n+1 each test case. That could cause repeated allocations if we use vector freqCount(n+1, 0) each test case; total memory allocation across all tests is O(sum n) which is okay.
But we can also reuse a global array of size max_n across tests? But simpler to allocate per test.
Implementation steps:
Pseudo:
int t; cin >> t;
while(t--){
int n; cin >> n;
vector a(n);
unordered_map<int,int> mp;
for(int i=0;i<n;++i){
cin>>a[i];
mp[a[i]]++;
}
vector cnt(n+1,0);
for(auto& p: mp){
int f = p.second;
cnt[f]++;
}
int ge = 0;
int max_kept = 0;
for(int c = n; c >= 1; --c){
ge += cnt[c];
int kept = c * ge;
if(kept > max_kept) max_kept = kept;
}
cout << n - max_kept << "\n";
}
Now confirm with examples.
Now we should also consider if there is any nuance: Could C be 0? If we choose C=0, then all numbers occur 0 times (i.e., empty array). That would give kept = 0, removal = n. That's not better than any positive C that yields kept >0. So no need.
Now think about a case: frequencies: [3,2,2,2]. n=9. C=2: #>=2=4 => kept=8, removal=1. C=3: #>=3=1 => kept=3. So best 8. But is it possible to achieve kept=8? We need to keep 2 from each of the four numbers. The number with frequency 3 can keep 2 and remove 1; the three numbers with frequency 2 keep all. So total kept = 2*4=8, removal=1. That's valid.
What about frequencies: [4,4,2]? n=10. C=4: #>=4=2 => kept=8, removal=2. C=2: #>=2=3 => kept=6. C=3: #>=3=2 => kept=6. So best 8. Check: Keep both 4's exactly 4 each? That would be 8, removal 2 from the 2's? Wait we need to keep only numbers with freq>=4? Actually if we choose C=4, we must include all numbers with freq>=4, i.e., the two 4's. We cannot include the number with freq=2 because it doesn't have 4. So kept = 24=8, removal = 10-8=2 (the two from the freq2 number). That's valid. Could we do better by including the freq2 number with C=2? That would be kept=32=6, worse.
Thus algorithm works.
Now consider if there might be a better solution that doesn't include all numbers with freq >= C? Suppose we have frequencies: [5,3,3]. We found best kept=9. If we include all numbers with freq>=3, kept=3*3=9. If we consider C=5: #>=5=1 =>5. So fine.
Consider [6,4,3,3] n=16. Compute:
C=3: #>=3=4 => kept=12
C=4: #>=4=2 => kept=8
C=5: #>=5=1 =>5
C=6: #>=6=1 =>6
C=2: #>=2=4 =>8? Actually #>=2=4 (all have >=2) => 8.
max kept=12, removal=4. Could we do better by not including all numbers with freq>=3? Suppose we take C=4, include the 6 and 4? kept=8. worse. If we take C=5, include 6? 5. So no.
Now consider [5,5,2] n=12. C=5: #>=5=2 =>10, removal=2. C=2: #>=2=3 =>6. So max 10. That seems best: keep both 5's exactly 5 each, drop the 2. So removal 2. Could we do better by keeping the 2 partially? No because C must be uniform.
Thus algorithm stands.
Now let's consider a subtle case: The problem statement says "each number in the array occurs either zero or C times". That means if a number appears, it appears exactly C times. So we cannot have a number appear multiple times but less than C. So we must either completely remove it or keep exactly C occurrences. So we must ensure that for numbers we keep, we have at least C occurrences; we can remove extras. So the product formula is correct.
Now check if there is any possibility to keep some numbers but not all occurrences of them and also keep numbers with frequency less than C? Only if we can increase frequency of a number? No, we cannot create new occurrences.
Thus done.
Now the solution is straightforward.
However, we need to be mindful of memory usage and speed. Using unordered_map with many distinct values could be fine. But we can also sort the array and compute frequencies via sorting to avoid hash map overhead? But unordered_map is fine; total n sum 2e5, so at most 2e5 insertions per test case.
Alternative: We can just sort the array, then compute frequencies by scanning. That's also O(n log n). But the O(n) with hash map may be slightly faster? Sorting is O(n log n) which might be okay but n is 2e5, sum is 2e5, so total O(N log N) is fine. But using hash map might be simpler. However, we need to be careful about worst-case collisions? But with 2e5 elements, unordered_map is usually fine. Could use map (tree) for safety; O(n log n) anyway. We'll use unordered_map.
Now we have the solution.
But wait, there is also an alternative approach: Group frequencies, then consider each possible C that appears as a frequency. Because the function kept(C) = C * (number of frequencies >= C) is piecewise constant in intervals between frequencies. But iterating from 1 to n is fine.
Now let's think about the constraints: t up to 1e4, sum n up to 2e5. For each test case we allocate an array of size n+1, which in worst case if many small n tests, total allocation is sum of (n+1) <= 2e5+1e4 ~ 2.1e5, okay. But we must ensure that we don't allocate a huge array for each test case based on n but we might have many test cases with n=0? Not possible.
Now confirm the sample.
Edge case: n=1. Then frequencies: [1]. cnt[1]=1. Loop: c=1: ge=1, kept=1, max_kept=1. answer=0. That's correct because array is already beautiful? For n=1, any number occurs 1 time. Choose C=1, each number occurs 0 or 1 times, the only number occurs 1 time, so beautiful. So removal 0.
Edge case: n=2 with distinct numbers: [1,2] => frequencies [1,1]. cnt[1]=2. c=2: ge=0, kept=0; c=1: ge=2, kept=2, max_kept=2, answer=0. Beautiful with C=1.
Now consider n=2 with same number: [1,1] => freq [2]. cnt[2]=1. Loop: c=2: ge=1, kept=2; c=1: ge+=cnt[1]? cnt[1]=0, ge=1 (since we added cnt[2] earlier). Actually careful: In descending loop, we start ge=0; c=2: ge += cnt[2]=1, kept=21=2; c=1: ge+=cnt[1]=0, kept=11=1; max_kept=2, answer=0. Good.
Now consider case where we need to remove some: already tested.
Thus solution is correct.
Now we will produce final answer.
But there is a nuance: The problem statement says "Polycarp wants to remove some elements from the array a to make it beautiful." It doesn't say he can reorder. He only removes elements. The remaining array keeps original order, but that doesn't affect the condition of frequencies, because beautiful condition only cares about counts of each number, not order. So fine.
Now final code.
But we need to make sure we flush output properly.
Let's also consider using faster I/O: ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
Now one more thought: Could the answer be improved by not including all numbers with freq >= C? For a given C, the kept count is C times the number of selected numbers. If we have numbers with frequencies f_i >= C, selecting them gives C each. But if we select a subset of them, the product would be less, so no reason. However, is there a scenario where we might want to choose a C that is not exactly some frequency threshold, but still keep only a subset of numbers that have freq >= C? That's covered by our M(C) definition: For a given C, the maximum kept from numbers with freq >= C is indeed C * M(C) if we include all. So for any C, the best we can do is C * M(C). So the overall optimum is max_{C} C * M(C). So it's correct.
We can also think of it as: After removal, the array will have some multiset of numbers each occurring exactly C times. So the set of numbers kept must be a subset of original distinct numbers, each with original frequency at least C. For each possible C, the maximum number of numbers we can keep is the number of distinct numbers with frequency >= C. So maximum kept is C times that number. So indeed it's optimal to keep all those numbers.
Now about the possibility of C being 0? Then we keep no numbers, kept = 0, which is less than any positive C if there is at least one element. So ignore.
Thus final solution.
Now we should also ensure we handle the case where there are many distinct numbers and frequencies up to n. The frequency count array is size n+1. We'll use a vector freqCount(n+1, 0). But note that if n=200000, vector is fine.
Now, to improve further, we could use a map for frequencies to avoid O(n) array if n is large? But array is simpler.
Now double-check: The sum of n across test cases <= 2e5, so total memory allocation for freqCount vectors across test cases is O(total n) if we allocate and deallocate each time, but that's fine.
Now code:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
unordered_map<int, int> mp;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
mp[x]++;
}
vector cnt(n + 1, 0);
for (auto& p : mp) {
int f = p.second;
cnt[f]++;
}
int ge = 0;
int max_kept = 0;
for (int c = n; c >= 1; --c) {
ge += cnt[c];
max_kept = max(max_kept, c * ge);
}
cout << n - max_kept << '\n';
}
return 0;
}
Test with given examples:
Input:
3
6
1 3 2 1 4 2
4
100 100 4 100
8
1 2 3 3 3 2 6 6
Our code:
Case1: frequencies: 2,2,1,1 -> cnt[1]=2, cnt[2]=2. Loop from c=6 down to 1:
c=6: ge=0, kept=0
c=5: ge=0, kept=0
c=4: ge=0, kept=0
c=3: ge=0, kept=0
c=2: ge += cnt[2]=2 -> ge=2, kept=4
c=1: ge += cnt[1]=2 -> ge=4, kept=4
max_kept=4, ans=2.
Case2: n=4, mp: 100->3, 4->1 => cnt[3]=1, cnt[1]=1. Loop:
c=4: ge=0, kept=0
c=3: ge+=cnt[3]=1, ge=1, kept=3
c=2: ge+=cnt[2]=0, ge=1, kept=2
c=1: ge+=cnt[1]=1, ge=2, kept=2
max_kept=3, ans=1.
Case3: n=8, mp: 1->1,2->2,3->3,6->2 => cnt[1]=1, cnt[2]=2, cnt[3]=1. Loop:
c=8..4: ge=0
c=3: ge+=cnt[3]=1, ge=1, kept=3
c=2: ge+=cnt[2]=2, ge=3, kept=6
c=1: ge+=cnt[1]=1, ge=4, kept=4
max_kept=6, ans=2.
Matches.
Now we should consider some random tests to ensure no off-by-one errors.
Suppose n=5, a=[1,1,1,1,1] -> mp: 1->5 -> cnt[5]=1. Loop:
c=5: ge=1, kept=5
c=4: ge=1, kept=4
...
c=1: ge=1, kept=1
max_kept=5, ans=0.
Suppose n=5, a=[1,2,3,4,5] -> all freq 1 => cnt[1]=5.
Loop: c=5..2 ge=0; c=1: ge=5, kept=5 -> ans=0.
Suppose n=5, a=[1,1,2,2,3] -> frequencies: 2,2,1 => cnt[2]=2, cnt[1]=1. Loop:
c=5..3: 0
c=2: ge+=2=2, kept=4
c=1: ge+=1=3, kept=3
max_kept=4, ans=1. Check if we can remove 1 element: We need all numbers to appear either 0 or C times. If we choose C=2, we need numbers appearing exactly 2. We have two numbers with frequency 2 (1 and 2) can keep both exactly 2, total kept 4, we remove the 3. That's removal 1. So correct.
Now consider n=5, a=[1,1,1,2,2] -> frequencies: 3,2. cnt[3]=1, cnt[2]=1. Loop:
c=3: ge=1, kept=3
c=2: ge+=1=2, kept=4
c=1: ge+=0? Actually cnt[1]=0, ge=2, kept=2
max_kept=4, ans=1. Is that possible? Choose C=2, keep both numbers? For number with freq 3, we can keep 2 (remove 1) and for number with freq 2 keep 2, total kept=4, removal=1. Works. Choose C=3: keep only the 3-occurrence number, remove the 2's, kept=3, removal=2. So best is 1 removal. Good.
Now consider n=6, a=[1,1,1,2,2,2] -> frequencies: 3,3. cnt[3]=2. Loop:
c=3: ge=2, kept=6
c=2: ge=2, kept=4
c=1: ge=2, kept=2
max_kept=6, ans=0. Indeed array already beautiful with C=3.
Now consider n=6, a=[1,1,1,2,2,3] -> frequencies: 3,2,1. cnt[3]=1, cnt[2]=1, cnt[1]=1.
c=3: ge=1, kept=3
c=2: ge+=1=2, kept=4
c=1: ge+=1=3, kept=3
max_kept=4, ans=2. Is it possible to remove 2? Choose C=2: keep numbers with freq >=2: 1 and 2. Keep exactly 2 of each: from 1 remove 1, from 2 keep both, drop 3 entirely. Keep total 4, removal 2. So answer 2.
Now consider a tricky case: frequencies: [5,4,4,4] n=17. cnt[5]=1, cnt[4]=3. Loop:
c=5: ge=1, kept=5
c=4: ge+=3=4, kept=16
c=3: ge=4 (since cnt[3]=0), kept=12
c=2: ge=4, kept=8
c=1: ge=4, kept=4
max_kept=16, ans=1. Indeed choose C=4, keep all four numbers? But one number has frequency 5, which is >=4, so we can keep 4 from it, and the three numbers with frequency 4 keep all 4 each, total kept=4*4=16, removal=1. So works.
Now consider frequencies: [10,9,9] n=28. cnt[10]=1, cnt[9]=2.
c=10: ge=1, kept=10
c=9: ge+=2=3, kept=27
c=8: ge=3, kept=24
...
max_kept=27, ans=1. So removal=1. Indeed choose C=9, keep the two 9's exactly 9 each (they have exactly 9), and the 10 keep 9 (remove 1). So kept=27, removal=1. Alternatively C=10, keep only the 10, kept=10, removal=18. So best is 27. So answer 1.
Now consider frequencies: [10,9,8] n=27. cnt[10]=1, cnt[9]=1, cnt[8]=1.
c=10: ge=1, kept=10
c=9: ge+=1=2, kept=18
c=8: ge+=1=3, kept=24
c=7: ge=3, kept=21
...
max_kept=24, ans=3. Check: C=8, keep all three numbers? For number with freq 10, keep 8 remove 2; 9 keep 8 remove 1; 8 keep 8; total kept 24, removal 3. Works. Could we do better with C=9? then only two numbers (10 and 9) can be kept: kept=18. C=10: only one number kept 10. So 24 is max. So answer 3.
Now consider case where frequencies: [6,5,5,5] n=21. cnt[6]=1, cnt[5]=3.
c=6: ge=1, kept=6
c=5: ge+=3=4, kept=20
c=4: ge=4, kept=16
max_kept=20, ans=1. Keep C=5: all four numbers, keep 5 from each: the 6 loses 1, each 5 keep all, total 20, removal=1.
Now consider frequencies: [6,5,4,4] n=19. cnt[6]=1, cnt[5]=1, cnt[4]=2.
c=6: ge=1, kept=6
c=5: ge+=1=2, kept=10
c=4: ge+=2=4, kept=16
c=3: ge=4, kept=12
c=2: ge=4, kept=8
max_kept=16, ans=3. Check C=4: keep all? 6 keep 4 (remove2), 5 keep4? but 5 >=4, can keep 4 (remove1), 4's keep all (each 4). total kept=4+4+4+4? Actually there are 2 numbers with freq 4, so total kept=4*4=16? Wait: we have 4 numbers: freq6, freq5, freq4, freq4. So M(4)=4 (since all have >=4). So kept = 4 * 4 =16. Removal = n - 16 = 3. Works.
Now we might consider if there is a case where best C is not among the frequencies but between them. For example, frequencies: [10, 1,1,1,... many 1's]. Suppose we have one number with freq 10, and 90 numbers with freq 1. n=100. cnt[10]=1, cnt[1]=90. Loop:
c=10: ge=1, kept=10
c=9: ge=1, kept=9
...
c=2: ge=1, kept=2
c=1: ge+=90=91, kept=91
max_kept=91, ans=9. So best C=1. That's among frequencies (1). So C=1 yields best.
Suppose frequencies: [5,4,4,1,1,1,1] n=?? 5+4+4+1+1+1+1=17. cnt[5]=1, cnt[4]=2, cnt[1]=4. Loop:
c=5: ge=1, kept=5
c=4: ge+=2=3, kept=12
c=3: ge=3, kept=9
c=2: ge=3, kept=6
c=1: ge+=4=7, kept=7
max_kept=12, ans=5. So C=4 is best. Among frequencies (4,5). So C among frequencies.
But could C not be a frequency? For instance, suppose frequencies: [5,3,3,3]. n=14. cnt[5]=1, cnt[3]=3.
c=5: ge=1, kept=5
c=4: ge=1, kept=4
c=3: ge+=3=4, kept=12
c=2: ge=4, kept=8
c=1: ge=4, kept=4
max_kept=12, C=3 works. Among frequencies.
What about frequencies: [4,4,2,2,2] n=14. cnt[4]=2, cnt[2]=3.
c=4: ge=2, kept=8
c=3: ge=2, kept=6
c=2: ge+=3=5, kept=10
c=1: ge=5, kept=5
max_kept=10, C=2 works. Among frequencies.
What about frequencies: [6,6,5] n=17. cnt[6]=2, cnt[5]=1.
c=6: ge=2, kept=12
c=5: ge+=1=3, kept=15
c=4: ge=3, kept=12
...
max_kept=15, C=5 works, which is a frequency.
Seems that optimal C will always be a frequency? Not necessarily? Consider frequencies: [3,3,2,2,2]. n=12. cnt[3]=2, cnt[2]=3.
c=3: ge=2, kept=6
c=2: ge+=3=5, kept=10
c=1: ge=5, kept=5
max_kept=10, C=2 works. That's a frequency.
What about [100,99,99]? c=99 yields 992=198, c=100 yields 1001=100. So C=99, which is a frequency.
Try to find a case where best C is not a frequency. Suppose frequencies: [10,6,6]. n=22. cnt[10]=1, cnt[6]=2.
c=10: ge=1, kept=10
c=9: ge=1, kept=9
c=8: ge=1, kept=8
c=7: ge=1, kept=7
c=6: ge+=2=3, kept=18
c=5: ge=3, kept=15
c=4: ge=3, kept=12
c=3: ge=3, kept=9
c=2: ge=3, kept=6
c=1: ge=3, kept=3
max_kept=18 at C=6 (frequency). So again.
Consider [7,5,5]? C=5: 52=10, C=7:71=7, best 10. C=5.
Consider [8,5,5]? C=5:10, C=8:8, best 10.
Consider [9,6,6]? C=6:12, C=9:9.
Now try frequencies: [5,4,4,4] we already did; best C=4.
Consider [5,4,4,4,1]? n=18? Actually 5+4+4+4+1=18. cnt[5]=1, cnt[4]=3, cnt[1]=1.
c=5: ge=1, kept=5
c=4: ge+=3=4, kept=16
c=3: ge=4, kept=12
c=2: ge=4, kept=8
c=1: ge+=1=5, kept=5
max=16 at C=4.
Consider [5,4,4,3,3]? n=19? 5+4+4+3+3=19. cnt[5]=1, cnt[4]=2, cnt[3]=2.
c=5:1->5
c=4:ge+=2=3, kept=12
c=3:ge+=2=5, kept=15
c=2:ge=5, kept=10
c=1:ge=5, kept=5
max=15 at C=3, which is a frequency.
Consider [6,4,4,3,3]? 6+4+4+3+3=20. cnt[6]=1, cnt[4]=2, cnt[3]=2.
c=6:1->6
c=5:1->5
c=4:ge+=2=3, kept=12
c=3:ge+=2=5, kept=15
c=2:5->10
max=15 at C=3.
Now try to see if there is a scenario where optimal C is between frequencies. Suppose we have many numbers with frequencies just above a certain number and then a drop. The product is C * M(C). M(C) is non-increasing step function as C increases. As C increases, M(C) stays constant between frequencies, then drops at frequencies that are present. The product C * M(C) may be maximized at a point between frequencies? Since M(C) is constant on intervals (c, next frequency], the product is linear in C on that interval. So the maximum on that interval occurs at the right endpoint (since increasing C increases product if M(C) is constant). For an interval (c1, c2] where M(C) = m constant (since for C in (c1, c2], the count of frequencies >= C is m, where m is # frequencies >= c2, and c2 is the next distinct frequency value? Actually careful: For C in (F_{k+1}, F_k] where F_k are sorted frequencies descending? Let's think: Suppose we have sorted frequencies f1 >= f2 >= ... >= fk. M(C) = number of f_i >= C. As C increases, M(C) decreases only when C passes a frequency value. For C in (f_{i+1}, f_i] (with f_i being distinct maybe) M(C) = i (if f_i distinct). So M(C) constant on intervals (f_{i+1}, f_i]. The product P(C) = C * i. Since i constant and C increases, P(C) is increasing linearly. So the maximum on that interval occurs at the maximum C within that interval, which is at C = f_i (the upper bound). So the maximum over all C will be achieved at some C equal to one of the frequencies. Also C can be > max f? Then M(C)=0, product 0. So the optimum C is among the distinct frequencies (or possibly 1? but 1 is a frequency if there is any value with freq >=1, which all frequencies are >=1, so 1 is always a candidate; but 1 may be the leftmost if smallest frequency is >1? Actually if smallest frequency is >1, then for C=1, M(1)=k, product = k; but we might also consider C=2? But 2 may not be a frequency if no number has exactly 2 but some have >=2. Then M(2) is the number of frequencies >=2, which might be less than k. The interval between 1 and the smallest frequency? Suppose smallest frequency is f_min > 1. Then for C in (1, f_min], M(C)=k (since all frequencies >= C because C <= f_min). So product = C * k is increasing in C, so maximum on that interval is at C = f_min. So again, optimum C is f_min (a frequency). So indeed the optimum C is always one of the frequencies present in the array (or possibly 1 if 1 is not a frequency but M(1) = k and product = k; but if smallest frequency >1, then C= f_min gives product = f_min * k, which is >= k * 1 since f_min >=2? Actually if f_min=2, product =2k, which is greater than k, so C=2 is better. But 2 may not be a frequency if no number appears exactly 2 times? But if the smallest frequency is 2, then all numbers appear at least 2 times. So there is a number with frequency exactly 2? Not necessarily; they could all have frequencies >=3, with min freq =3. Then C=3 is a frequency? Wait if all frequencies are >=3 but none exactly 3? That's possible: e.g., frequencies 5 and 4. The smallest frequency is 4, not 3. For C in (1,4], M(C)=2 (since both have >= C for C <=4). So product = C * 2. This is increasing on (1,4], so maximum occurs at C=4, which is a frequency (the min frequency). So again, the optimum C is the min frequency among numbers that are included? Not exactly; we need to consider all frequencies as candidates. Since the function is increasing on each interval, the maximum will be at some frequency value (specifically, at one of the distinct frequencies present). So we could just iterate over distinct frequency values to compute product. That would be more efficient if number of distinct frequencies is small. But our O(n) method is fine.
Thus algorithm is correct.
Now we must produce final answer.
But wait: There is one more nuance: The problem says "there exists a number C, such that each number in the array occurs either zero or C times." That C must be a positive integer? Usually yes. Could C be 0? That would mean all numbers occur zero times, i.e., empty array. But that's allowed? It would require removing all elements. But it's always worse than keeping some elements if any exist. So ignore.
Also, note that after removal, the array could be empty. Is that considered beautiful? Yes, because each number occurs zero times (all numbers). C could be any number? Actually if array is empty, then the condition "each number occurs either zero or C times" is vacuously true for any C, because there are no numbers to check. But we want to minimize removals, so we would not remove all unless no other choice (i.e., if n=0 but n>=1). So fine.
Now complexity: O(n) per test case. Good.
Now I'll produce the final solution.
But I recall a known Codeforces problem: "Make It Beautiful"? Actually it's from Codeforces Round #... maybe problem "1462C" or something. This looks like "Polycarp and the Day of Pi" no. I think it's "Make It Equal" or "Beautiful Array"? Actually problem is from Codeforces Round #... It's "1560D Make a Power of Two"? Not.
Anyway, it's fine.
Now let's test with random small cases to ensure no oversight:
We can brute force for small n to verify.
But I'm confident.
Potential pitfalls: The loop from c=n down to 1 uses ge variable cumulative. But we must ensure we consider c from n down to 1 and compute cnt[c] correctly. cnt is size n+1, index from 0 to n. We ignore index 0.
But what about frequencies that are 0? They are not in map, so cnt[0] is 0.
Now, is there any chance that c * ge could exceed int range? As argued, product <= n, but n <= 2e5, so int safe.
Now final code.
But before finalizing, consider using vector freqCount with size n+1, and after populating, we compute suffix sum.
One more thought: Could there be a scenario where C is larger than n? Then M(C)=0, product=0. So we can stop at n.
Now we need to output integer.
Now, we can also improve by using map for frequencies but it's fine.
Now final answer.
But I realize we can also solve using sorting frequencies and scanning, but no matter.
Now I'll produce the final answer.
However, I recall that sometimes Codeforces problems require long long for product, but as argued product <= n, but if we compute c * ge in int, c and ge are ints, and product might overflow if we mistakenly use larger numbers? For n up to 2e5, c up to 2e5, ge up to 2e5, product up to 4e10 which is > 2^31-1 (2.147e9). But earlier I argued that product is at most n. Let's re-evaluate: product = c * (# of frequencies >= c). But # of frequencies >= c is the number of distinct numbers that appear at least c times. Each such number contributes at least c occurrences, so the total number of elements among these numbers is at least c * (#). But that total is at most n. Indeed, the sum of frequencies of those numbers is at least c*(#) and at most n. So c * (#) <= sum of frequencies of those numbers <= n. Therefore product <= n. So product cannot exceed n. Good. So int is safe.
But careful: In our calculation, ge = count of distinct numbers with frequency >= c. But if we have numbers with frequency > c, then sum of frequencies is > cge. So cge <= sum <= n. So indeed it's bounded by n. So product within 2e5.
Thus int is safe.
Now let's consider the code again:
for (int c = n; c >= 1; --c) {
ge += cnt[c];
max_kept = max(max_kept, c * ge);
}
But note that if we start from c=n down to 1, ge accumulates counts of frequencies exactly c, then exactly c-1, etc. That's correct.
But is there a case where we need to consider c=0? No.
Now we should also consider that we might get max_kept = 0 if we never set it? That would happen only if n=0, but n>=1, so there is at least one element, so ge eventually becomes >0 at c=1 because all frequencies are at least 1, so cnt[1] plus others ensure ge>0, so max_kept>0.
Now confirm with n=1: cnt[1]=1; loop: c=1: ge=1, max_kept=1.
Now about memory: vector cnt(n+1) initializes to zero O(n). That's fine.
Now about time: For each test case, we iterate over map to fill cnt, then loop over c from n down to 1, O(n). So total O(n) per test case.
Now I think the solution is correct.
But wait, there might be a nuance: In the beautiful array definition, each number occurs either zero or C times. But what about numbers that appear exactly 0 times? That's all numbers not present. But consider the array after removal: It contains some numbers, each appears some number of times. The condition must hold for all numbers, including those not present? Typically the phrase "each number in the array" means each number that appears in the array. Usually it's interpreted as: for every value x that appears in the array, its frequency is exactly C. It does not impose condition on values not present. So it's okay.
Thus our interpretation stands.
Now double-check with a possible edge case: Suppose the array contains many distinct numbers, all with frequency 1. Then C must be 1, because if C>1, then any number that appears must appear exactly C times, but they appear only once, so they would violate. So we can choose C=1 and keep all (0 removals). That's product n. Works.
Now we are ready.
Now, I'll produce final answer with explanation.
But I'd like to think if there is any scenario where we might need to consider that we can also remove numbers partially, i.e., we could keep some but not all of a number, but we already account for that by selecting C and keeping exactly C occurrences from numbers with freq >= C.
Now answer.
Now I will produce final message with solution in C++.
Potential improvements: Use array instead of unordered_map for frequencies of values if values are up to 1e9 we cannot. Use unordered_map.
Now I'll write final code with fast I/O.
One more thought: Could there be an issue with multiple test cases and clearing the map and vector? Yes, we create new each iteration.
Now finalize.
But let's test with a random larger case via mental.
Also note: Since the sum of n over test cases is 2e5, we can allocate a vector each test case of size n+1, but if many test cases with small n, total memory allocations might be okay.
Alternatively, we could reuse a global vector and resize each test case. But not necessary.
Now final answer.
I'll produce the solution with explanation as required. Since the user only asked for "The first line contains one integer t", but they probably want code. So I'll output final solution.
Now the final ChatGPT answer: Provide explanation and code.
But the instruction: "You are an AI... provide answer." Probably expects code.
I'll provide solution in C++.
Now I'll output:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
unordered_map<int, int> freq;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
freq[x]++;
}
vector<int> cnt(n + 1, 0);
for (auto &p : freq) {
cnt[p.second]++;
}
int ge = 0, best = 0;
for (int c = n; c >= 1; --c) {
ge += cnt[c];
best = max(best, c * ge);
}
cout << n - best << "\n";
}
return 0;
}
I'll also include some comments.
Now also note: Use of unordered_map may cause collisions, but with n=2e5 it's fine. Could use map for deterministic O(n log n). But unordered_map is fine.
But there is a nuance: When we allocate vector cnt(n+1), we must ensure that we don't access out-of-bounds. Since frequencies can be at most n, we are fine.
Now confirm that we are reading t and then processing each test case.
Edge: t up to 1e4, sum n <= 2e5, so total input size is manageable.
Now I think it's done.
But I recall that there is a known solution: sort frequencies, then for each distinct frequency, compute product. But this is fine.
Now I'll produce final answer.
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
unordered_map<int, int> freq;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
++freq[x];
}
// cnt[f] = how many distinct numbers appear exactly f times
vector<int> cnt(n + 1, 0);
for (auto &p : freq) {
++cnt[p.second];
}
int ge = 0; // # of values with frequency >= current c
int best_keep = 0; // maximal number of kept elements
for (int c = n; c >= 1; --c) {
ge += cnt[c];
best_keep = max(best_keep, c * ge);
}
cout << n - best_keep << "\n";
}
return 0;
}