Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <iostream>
#include <algorithm> // Ensure __gcd is recognized
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <unordered_map>
#include <cstring>
#include <climits>
#include <iomanip>
#include <numeric>
#include <map>
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long ll;
const int MODD = 32768;
const int MOD = 1e9 + 7;
ll poww(int x, int n, int m)
{
    if (n == 0) return 1;
    ll u = poww(x, n / 2, m);
    u = (u * u) % m;
    if (n & 1) u = (u * x) % m;
    return u;
}
int fact(int n)
{
      if(n==0)
      return 1;
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = (res * i) % MOD;
    return res;
}
long nCr(int n, int r)
{
    return (poww(fact(n) / (fact(r) * fact(n - r)), MOD -1 , MOD));
}



void solve() {
  
    int n; cin >> n;
    int k; cin >> k;
    
    map<int, int> freq;
    for (int i = 0; i < n;i++)
    {
        int temp; cin >> temp;
        freq[temp]++;
    }
    int max_freq = 0;
    int total_freq = freq.size();
    for (auto &it: freq)
    {
        if (it.second <= k && k > 0)
        {
            total_freq--;
            k -= it.second;
        }
    }
    cout << max(total_freq, 1);
    label: cout << endl;
    
}
int main() {
    fast;
    int t = 1;
   cin >> t;

    while (t--) {
        solve();
    }
    return 0;
}


Submission (verdict: Accepted):
#include <iostream>
#include <algorithm> // Ensure __gcd is recognized
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <unordered_map>
#include <cstring>
#include <climits>
#include <iomanip>
#include <numeric>
#include <map>
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long ll;
const int MODD = 32768;
const int MOD = 1e9 + 7;
ll poww(int x, int n, int m)
{
    if (n == 0) return 1;
    ll u = poww(x, n / 2, m);
    u = (u * u) % m;
    if (n & 1) u = (u * x) % m;
    return u;
}
int fact(int n)
{
      if(n==0)
      return 1;
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = (res * i) % MOD;
    return res;
}
long nCr(int n, int r)
{
    return (poww(fact(n) / (fact(r) * fact(n - r)), MOD -1 , MOD));
}



void solve() {
  
    int n; cin >> n;
    int k; cin >> k;
    
    map<int, int> freq;
    for (int i = 0; i < n;i++)
    {
        int temp; cin >> temp;
        freq[temp]++;
    }
    int max_freq = 0;
    vector<pair<int, int>> pi;
    int pii = 0;
    int total_freq = freq.size();
    
    for (auto &it: freq)
    {
        pi.push_back({it.second, it.first});
       if (it.second > max_freq)
       {
            pii = it.first;
            max_freq = it.second;
       }
    }
    map<int, int> mp;
    mp[pii] = freq[pii];
    sort(pi.begin(), pi.end());
    for (auto &it: pi)
    {
        if (it.first <= k && k > 0 && pii != it.second)
        {
            mp[pii] += it.first;
            k -= it.first; 
        }
        else mp[it.second] = it.first;
    }
    cout << mp.size();
    label: cout << '\n';
    
}
int main() {
    fast;
    int t = 1;
   cin >> t;

    while (t--) {
        solve();
    }
    return 0;
}