Problem: 2057B - Gorilla and the Exam
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Time limit exceeded on test 9):
#define _CRT_SECURE_NO_WARNINGS
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>

using namespace std;

#pragma GCC optimize ("-O3")

#define ll long long
#define ull unsigned long long
#define wt int t; cin>>t; while(t--)
#define all(v) v.begin(),v.end()
#define max3(a,b,c) max(max(a,b),max(a,c))
#define min3(a,b,c) min(min(a,b),min(a,c))
#define pi 3.141592653

void a()
{
	int t;
	cin >> t;

	for (int i = 0;i < t;i++)
	{
		int n, k, maxNum = 0, maxCount = 0, cur;
		map<int, int> m;

		cin >> n >> k;

		for (int j = 0;j < n; j++)
		{
			cin >> cur;

			m[cur]++;

			if (m[cur] > maxCount || (m[cur] == maxCount && cur > maxNum))
			{
				maxCount = m[cur];
				maxNum = cur;
			}
		}

		while (k > 0 && m.size() > 1)
		{
			int minNum = 1e8, minCount = 1e8;

			for (auto& i : m)
			{
				if (i.second < minCount || (i.second == minCount && i.first < minNum))
				{
					minCount = i.second;
					minNum = i.first;
				}
			}

			// вычисляем, сколько чисел мы можем изменить на текущем шаге
			int delta = min(m[minNum], k);

			m[minNum] -= delta;
			k -= delta;

			if (m[minNum] == 0)
				m.erase(minNum);
		}

		cout << m.size() << '\n';
	}
}

void b()
{

}

void c()
{

}

void d()
{

}

void e()
{

}

void f()
{

}

void g()
{

}

int main()
{
    IOS

#ifdef ASD
    freopen("a.in", "r", stdin);
    freopen("a.out", "w", stdout);
#endif

#ifdef TEST
    freopen("a.in", "w", stdout);

    test();
#else
    a();
#endif

    return 0;
}

Submission (verdict: Wrong answer on test 1):
#define _CRT_SECURE_NO_WARNINGS
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>

using namespace std;

#pragma GCC optimize ("-O3")

#define ll long long
#define ull unsigned long long
#define wt int t; cin>>t; while(t--)
#define all(v) v.begin(),v.end()
#define max3(a,b,c) max(max(a,b),max(a,c))
#define min3(a,b,c) min(min(a,b),min(a,c))
#define pi 3.141592653

void a()
{
	int t;
	cin >> t;

	for (int i = 0;i < t;i++)
	{
		int n, k;
		
		cin >> n >> k;

		vector<int> a(n);

		for (int j = 0;j < n; j++)
			cin >> a[j];

		sort(all(a));

		vector<int> cnt = { 1 };

		for (int j = 1;j < n; j++)
		{
			if (a[j] == a[j - 1]) cnt.back()++;
			else cnt.emplace_back(1);
		}

		sort(all(cnt));

		while (cnt.size() > 1 && k > 0)
		{
			if (cnt.front() <= k)
			{
				k -= cnt.front();
				cnt.erase(cnt.begin());
			}
			else break;
		}

		cout << k << '\n';
	}
}

void b()
{

}

void c()
{

}

void d()
{

}

void e()
{

}

void f()
{

}

void g()
{

}

int main()
{
    IOS

#ifdef ASD
    freopen("a.in", "r", stdin);
    freopen("a.out", "w", stdout);
#endif

#ifdef TEST
    freopen("a.in", "w", stdout);

    test();
#else
    a();
#endif

    return 0;
}