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

Previous submission (verdict: Time limit exceeded on test 13):
/*
Behind every successful person, there's a woman called Mom :))
*/
#include <bits/stdc++.h>
using namespace std;

#define Author ios_base::sync_with_stdio(false);
#define GOAT__ cin.tie(NULL);
#define int long long
#define vi vector<int>
#define pii pair<int, int>
#define endl "\n"
#define pb push_back
#define Pb pop_back
#define sz(a) static_cast<int>(a.size())
#define all(a) a.begin(), a.end()
#define double long double
#define que_max priority_queue<int>
#define que_min priority_queue<int, vi, greater<int>>
#define f first
#define s second
#define yess cout<<"YES\n";
#define noo cout<<"NO\n";
#define fori(a) for(auto &i: a)
#define forab(a, b) for(int i=a; i<=b; ++i)

template<typename T>
using vv = vector<T>;

template<typename T>
ostream& operator<< (ostream &os, vector<T> &nums) {
  for(auto &i: nums) os<<i<<" ";
  os<<endl;
  return os;
}

template<typename T>
istream& operator>> (istream &is, vector<T> &nums) {
  fori(nums) is>>i;
  return is;
}

const int mod = 1e9+7;

int n, m, k, x, y, z;
string t;

void solve() {
  cin>>n>>k;
  vi nums(n);
  cin>>nums;
  
  if(n==k) {
    cout<<"1\n";
    return;
  }

  unordered_map<int, int> track;
  fori(nums) track[i]++;

  if(k==0) {
    cout<<sz(track)<<endl;
    return;
  }

  vi freq;
  for(auto &[k, v]: track) freq.pb(v);
  sort(all(freq));

  vi sum(1, freq[0]);
  forab(1, sz(freq)-1) sum.pb(sum.back()+freq[i]);
  int ub = upper_bound(all(sum), k)-sum.begin()-1;
  cout<<max(sz(track)-ub-1, 1LL)<<endl;
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  Author GOAT__

  int tc = 1;
  cin>>tc;

  while(tc--) solve();

  return 0;
}

Submission (verdict: Accepted):
/*
Behind every successful person, there's a woman called Mom :))
*/
#include <bits/stdc++.h>
using namespace std;

#define Author ios_base::sync_with_stdio(false);
#define GOAT__ cin.tie(NULL);
#define int long long
#define vi vector<int>
#define pii pair<int, int>
#define endl "\n"
#define pb push_back
#define Pb pop_back
#define sz(a) static_cast<int>(a.size())
#define all(a) a.begin(), a.end()
#define double long double
#define que_max priority_queue<int>
#define que_min priority_queue<int, vi, greater<int>>
#define f first
#define s second
#define yess cout<<"YES\n";
#define noo cout<<"NO\n";
#define fori(a) for(auto &i: a)
#define forab(a, b) for(int i=a; i<=b; ++i)

template<typename T>
using vv = vector<T>;

template<typename T>
ostream& operator<< (ostream &os, vector<T> &nums) {
  for(auto &i: nums) os<<i<<" ";
  os<<endl;
  return os;
}

template<typename T>
istream& operator>> (istream &is, vector<T> &nums) {
  fori(nums) is>>i;
  return is;
}

const int mod = 1e9+7;

int n, m, k, x, y, z;
string t;

void solve() {
  cin>>n>>k;
  vi nums(n);
  cin>>nums;

  if(n==k) {
    cout<<"1\n";
    return;
  }

  map<int, int> track;
  fori(nums) track[i]++;

  if(k==0) {
    cout<<sz(track)<<endl;
    return;
  }

  vi freq;
  for(auto &[k, v]: track) freq.pb(v);
  sort(all(freq));

  vi sum(1, freq[0]);
  forab(1, sz(freq)-1) sum.pb(sum.back()+freq[i]);
  int ub = upper_bound(all(sum), k)-sum.begin()-1;
  cout<<max(sz(track)-ub-1, 1LL)<<endl;
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  Author GOAT__

  int tc = 1;
  cin>>tc;

  while(tc--) solve();

  return 0;
}