Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 125 ms):
/**
 *    author:  Mhd9011
 *    created: 12.25.2024 20:29:20
**/

#include <bits/stdc++.h>

using namespace std;

using ll = long long ;
using  ld = long double ;
using u32 = unsigned int ;
using u64 = unsigned long long ;
using i64 = int64_t;
using i32 = int32_t;
//using i128 = __int128_t;
//using u128 = unsigned __int128;
//using f128 = __float128;
typedef pair<int, int>	pii  ;
typedef pair<ll, ll>	pll  ;
typedef vector<pii>     vii  ;
typedef vector<int>     veci ;
typedef vector<i32>     veci ;
typedef vector<pll>     vll  ;
typedef vector<ll>      vecll;
typedef vector<i64>      veci64;


#define F		        first
#define S		        second
#define pb		        push_back
#define endl            '\n'
#define spc             ' '
#define Mp		        make_pair
#define all(x)          x.begin(), x.end()
#define allr(x)          x, x + (sizeof(x)/sizeof(x[0]))
#define debug(x)        cerr << #x << " = " << x << endl
#define set_dec(x)	    cout << fixed << setprecision(x);
#define fast_io         ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io         freopen("in.txt" , "r" , stdin) ; freopen("out.txt" , "w" , stdout);
#define lb              lower_bound
#define ub              upper_bound
#define sz(a)           a.size();
#define USET(a, b) unordered_set<a> b;
#define OSET(a, b) ordered_set<a> b;
#define QUEUE(a, b) queue<a> b;
#define MEMSET(a,b)  ::memset(a,b,sizeof(a));
#define RESIZE(a,b,c)  a.resize(b,c);
#define MIN         *min_element
#define MAX         *max_element
#define llc(a) ll a; cin >> a;
#define llc1(a) ll a; cin >> a;
#define llc2(a,b) ll a,b; cin >> a >> b;
#define llc3(a,b,c) ll a , b , c; cin >> a >> b >> c;
#define llc4(a,b,c,d) ll a , b , c , d; cin >> a >> b >> c >> d;
#define scl(a)    string a;cin>>a; ll len=a.size();
#define sc(a)    string a;cin>>a;
//#define fi              first
//#define se              second
#define yes             cout<<"YES"

#define for1(n)         for(ll i=1;i<=n;i++)
#define for0(n)         for(ll i=0;i<n;i++)
#define forn(n)         for(ll i=n;i>0;i--)
 #define FORO(i,a) for (ll i = 0; i < ll(a.size()); i++)
#define FFOR(a) for (ll i = 0; i < ll(a); i++)
#define FOR(i, a) for (ll i = 0; i < ll(a); i++)
#define FOR2(i, a, b) for (ll i = a; i < ll(b); i++)
#define FOR3(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a, b) for (ll i = (b)-1; i >= ll(a); i--)
#define FOR3_R(i, a, b, c) for (ll i = (b)-1; i >= ll(a); i -= c)
#define no             cout<<"NO"
#define lli long long int
#define minll(a,b)  min(ll(a), ll (b))
#define maxll(a,b)  max(ll(a), ll (b))
#define PAIR(a, b, c) pair<a, b> c;
#define UMAP(a, b, c) unordered_map<a, b> c;
#define MAP(a, b, c)  map<a, b> c;
#define USET(a, b) unordered_set<a> b;
#define OSET(a, b) ordered_set<a> b;
#define STACK(a, b) stack<a> b;
#define QUEUE(a, b) queue<a> b;

ll ceil( ll n , ll m)
{
    if(n%m==0)
        return n/m;
    return n/m+1;
}
bool isprime (ll a)
{
    if(a==1)
        return false ;
    bool flag = false ;
    for (ll i = 2; i <= ll(sqrt(a)) ; ++i) {
        if(a % i==0){
            flag = true ;
            break;
        }
    }
    if(flag)
        return false;
    return true;
}

constexpr int64_t inf = 5e18 + 100;
constexpr int64_t mod1 = 1e9+7;
constexpr int64_t mod2 = 998244353;
constexpr int64_t mod3 = 1e9+9;
constexpr int64_t mod4 = (1ll<<31) - 1;
//constexpr long double PI = 2*asinl(1.0);
bool SIF(const vector<int>& v1, const vector<int>& v2) {
    if( v1[2] < v2[2] ) return true;
    else if( v1[2] == v2[2] and v1[1] > v2[1]) return true;
    else return false;
}
template<typename T>
void SORT(T & A) {
    sort(A.begin(), A.end());
}
template<typename T>
void CIN(T& a, ll n = -1) {
    (n == -1) ? n = a.size() : n += 0;
    a.resize(n);
    FOR(i, n) cin >> a[i];
}
template<typename T>
void COUT(T & a, ll n = -1) {
    (n == -1) ? n = a.size() : n += 0;
    FOR(i, n) {
        cout << a[i] << ' ';
    }
}
template<typename T>
void EMPTY(T & a, ll t = 0) {
    FOR(i, a.size()) a[i] = t;
}
template<typename T>
ll SUM(T a , ll l = 0, ll r = -1) {
    (r == -1) ? r = a.size() : r += 0;
    ll s = 0;
    FOR2(i, l, r) s += a[i];
    return s;
}
template<typename T>
bool ISBINARY(T a) {
    FOR(i, a.size()) if(a[i] != '0' || a[i] != '1') return false;
    return true;
}



template<typename T>
ll FIND(T a, ll b) {
    FOR(i, a.size()){
        if(a[i] == b) return i;
    }
    return -1;
}

const int N=1e6+100,L=25,bs=701;
ll A[N],B[N],C[N],D[N],E[N],dp[N],pre[N];
vector<int>G[N];

void SOLVE() {
    i64 n,k,t=0;
    std::cin >> n >>k;
    vecll a(n), c; map <ll , ll> b;
    for0(n) {
        cin >> a[i];
        b[a[i]]++;
    }
    for (auto &d : b) {
        if(d.S)
            c.pb(d.S);
    }
    ll ans=0;
    sort(all(c),greater<>());
    for0(c.size())
    {ans++;
        t+=c[i];
//        cout<<c[i]<<spc;
        if(n-t<=k)
        {
            cout<<ans;
            return;
        }
    }
//    cout<<n;


}

int main() {
    fast_io
    llc1(TS);
    while (TS--) {
        SOLVE();
        cout<<endl;
    }

    return 0;
}



Solution 2 (runtime: 108 ms):
#include <bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int tc; cin >> tc;
    for(int t = 0; t < tc; t++){
        int n, m; cin >> n >> m;
        vector<int> nums;
        for(int i = 0; i < n; i++){ int x; cin >> x; nums.push_back(x); }
        vector<int> freq;
        sort(nums.begin(), nums.end());
        int last = -1, f = 0;
        for(int n : nums){ 
            if(last == -1){ last = n; f++; }
            else if(last == n){ f++; }
            else{ last = n; freq.push_back(f); f = 1;}
        }
        freq.push_back(f);
        sort(freq.begin(), freq.end());
        int i = 0;
        while(i < freq.size()-1){
            if(m < freq[i]){ break; }
            m -= freq[i]; i++;
        }
        cout << freq.size() - i << '\n';
    }
}