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

Previous submission (verdict: Time limit exceeded on test 14):
#include <bits/stdc++.h>
using namespace std;

int assign_smallest_to_largest(vector<pair<int, int>> &lis, int k, int n){
    int i = 0;
    int current_most = lis.back().first;
    int cnt = 0;
    // cout << "current_most "<<current_most << "n:"<< n << endl;
    // for(int i = 0; i < lis.size(); i++){
    //     cout << "data before: "<<lis[i].first << " " << lis[i].second << endl;
    // }
    for (int i = 0; i < lis.size(); i++)
    {
        if(lis[i].first == current_most){
            continue;
        }
        if(lis[i].second <= k){
            lis.back().second += lis[i].second;
            k -= lis[i].second;
            lis[i].second = 0;
        }
        else{
            break;
        }
    }  
    
    for(int i = 0; i < lis.size(); i++){
        // cout << "data: "<<lis[i].first << " " << lis[i].second << endl;
        if(lis[i].second > 0){
            cnt++;
        }
    }
    return cnt;
}

unordered_map<int, int> m;
vector<pair<int, int>> lis;
int main(){
    ios::sync_with_stdio(false); // 取消同步流
    cin.tie(0); // 解绑cin与cout
    cout.tie(0); // 解绑cout与cin
    int t, n, k;
    cin >> t;

    for(int i = 0; i < t; i++){
        cin >> n >> k;
        for(int j = 0; j < n; j++){
            int x;
            cin >> x;
            m[x]++;
        }
        // cout << "msize: "<<m.size() << endl;
        lis.clear();
        for (auto i = m.begin(); i != m.end(); i++)
        {
            lis.push_back(make_pair(i->first, i->second));
            //cout << "data pushed: "<<i->first << " " << i->second << endl;
        }
        // cout << "data size: "<<lis.size() << endl;
        
        sort(lis.begin(), lis.end(), [](const pair<int, int> &a, const pair<int, int> &b){
            return a.second < b.second;
        });

        cout << assign_smallest_to_largest(lis, k, n) << endl;
        // cout << "lis size" << lis.size() << endl;
        m.clear();
    }



    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;

int assign_smallest_to_largest(vector<pair<int, int>> &lis, int k, int n){
    int i = 0;
    int current_most = lis.back().first;
    int cnt = 0;
    // cout << "current_most "<<current_most << "n:"<< n << endl;
    // for(int i = 0; i < lis.size(); i++){
    //     cout << "data before: "<<lis[i].first << " " << lis[i].second << endl;
    // }
    for (int i = 0; i < lis.size(); i++)
    {
        if(lis[i].first == current_most){
            continue;
        }
        if(lis[i].second <= k){
            lis.back().second += lis[i].second;
            k -= lis[i].second;
            lis[i].second = 0;
        }
        else{
            break;
        }
    }  
    
    for(int i = 0; i < lis.size(); i++){
        // cout << "data: "<<lis[i].first << " " << lis[i].second << endl;
        if(lis[i].second > 0){
            cnt++;
        }
    }
    return cnt;
}

map<int, int> m;
vector<pair<int, int>> lis;
int main(){
    ios::sync_with_stdio(false); // 取消同步流
    cin.tie(0); // 解绑cin与cout
    cout.tie(0); // 解绑cout与cin
    int t, n, k;
    cin >> t;

    for(int i = 0; i < t; i++){
        cin >> n >> k;
        for(int j = 0; j < n; j++){
            int x;
            cin >> x;
            m[x]++;
        }
        // cout << "msize: "<<m.size() << endl;
        lis.clear();
        for (auto i = m.begin(); i != m.end(); i++)
        {
            lis.push_back(make_pair(i->first, i->second));
            //cout << "data pushed: "<<i->first << " " << i->second << endl;
        }
        // cout << "data size: "<<lis.size() << endl;
        
        sort(lis.begin(), lis.end(), [](const pair<int, int> &a, const pair<int, int> &b){
            return a.second < b.second;
        });

        cout << assign_smallest_to_largest(lis, k, n) << endl;
        // cout << "lis size" << lis.size() << endl;
        m.clear();
    }



    return 0;
}