Problem: 2057B - Gorilla and the Exam
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 11):
//
// Created by bahab on 7/24/2024.
//
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#ifndef ONLINE_JUDGE
#endif

const long long mod=1e9+7;
const long long MOD=998244353;
#define ll long long
#define endl '\n'
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define F first
#define S second
#define INF 1e18+5
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define bahab(x) cout << x
#define lb cout << endl;
#define show(x) for (auto it : x) cout << it << " "

ll binpow(ll a,ll b){ll res=1;while(b>0){if(b&1)res=(res*a);a=((a)*(a));b/=2;}return res;}
ll gcd(ll a,ll b){return b==0 ? a:gcd(b,a%b);}
ll lcm(ll a,ll b){return (a/gcd(a,b))*b;}
ll comb(ll n, ll k) {
    if (k > n) return 0;
    if (k == 0 || k == n) return 1;
    if (k == 1) return n;
    ll result = 1;
    for (ll i = 1; i <= k; ++i) { result *= (n - i + 1) / i; }
    return result;
}
// -----------------------------------------------------------------------

void solve(){
    int n, k; cin >> n >> k;
    vector<ll> arr(n);
    vector<pair<int, ll>> p;
    unordered_map<ll,int> mp;
    for (int i = 0; i < n ; i++){
        cin >> arr[i];
        mp[arr[i]]++;
    }
    int sz = mp.size();
    vector<ll> pref(sz + 1);
    for (auto it : mp){
        p.emplace_back(it.second, it.first);
    }
    sort(all(p));
//    for (auto iter : p) cout << iter.F << " , " << iter.S << " || ";
//    cout << endl;
    for (int i = 1 ; i <= mp.size(); i ++){
        pref[i] = pref[i - 1] + p[i - 1].first;
    }
//    show(pref);
//    cout << endl;
    int ind = lower_bound(all(pref), k) - pref.begin();
    if (pref[ind] > k){
        ind--;
    }
    cout << max(sz - ind, 1) << endl;
}
// -----------------------------------------------------------------------

int main (){
    FAST;
    int tc = 1;
    cin >> tc;
    while (tc --)
        solve();
}

Submission (verdict: Accepted):
//
// Created by bahab on 7/24/2024.
//
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#ifndef ONLINE_JUDGE
#endif

const long long mod=1e9+7;
const long long MOD=998244353;
#define ll long long
#define endl '\n'
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define F first
#define S second
#define INF 1e18+5
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define bahab(x) cout << x
#define lb cout << endl;
#define show(x) for (auto it : x) cout << it << " "

ll binpow(ll a,ll b){ll res=1;while(b>0){if(b&1)res=(res*a);a=((a)*(a));b/=2;}return res;}
ll gcd(ll a,ll b){return b==0 ? a:gcd(b,a%b);}
ll lcm(ll a,ll b){return (a/gcd(a,b))*b;}
ll comb(ll n, ll k) {
    if (k > n) return 0;
    if (k == 0 || k == n) return 1;
    if (k == 1) return n;
    ll result = 1;
    for (ll i = 1; i <= k; ++i) { result *= (n - i + 1) / i; }
    return result;
}
// -----------------------------------------------------------------------

void solve(){
    int n, k; cin >> n >> k;
    vector<ll> arr(n);
    for (int i = 0 ; i < n ; i ++)
        cin >> arr[i];
    sort(all(arr));
    vector<int> mp = {1};
    for (int i = 1 ; i < n ; i ++){
        if (arr[i] == arr[i - 1])
            mp.back()++;
        else mp.pb(1);
    }
    sort(all(mp));
//    show(mp); cout << endl;
    int sz = mp.size(), ind = 0;
    for (int i = 0 ; i < sz ; i ++){
        if (mp[i] > k){
            break;
        }
        else {
            ind++;
            k -= mp[i];
        }
    }
    cout << max(sz - ind, 1) << endl;
}
// -----------------------------------------------------------------------

int main (){
    FAST;
    int tc = 1;
    cin >> tc;
    while (tc --)
        solve();
}