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

Previous submission (verdict: Time limit exceeded on test 9):
#include<bits/stdc++.h>

using namespace std;

int nproblems;
int n;
int k;
int tmp;
map<int, int> mp;

int main(){
        cin >> nproblems;
        for(int i = 0; i< nproblems; i++){
                cin >> n;
                cin >> k;
                for(int j = 0; j <n; j++){
                        cin >> tmp; //current element of array
                        if(mp.count(tmp)){ //if key is allready in map
                                mp[tmp]++; //increment count
                        } else {
                                mp[tmp] = 1; //or add to map w/ count 1
                        }
                }
                if(mp.empty()){
                        mp = {};
                        cout << 0 << "\n";
                        continue;
                }
                map<int, int>::iterator it = mp.begin();

                //find number with maximum occurences
                int maxkey;
                it = mp.begin();
                maxkey = it -> first;
                it++;
                while(it != mp.end()){
                        if(it -> second >= mp[maxkey]){ //chose last max so that we dont collide w. min
                                maxkey = it -> first;
                        }
                        it++;
                }
                // remove k least frequent numbers
                if(mp.size() > 1){
                        while (k > 0){
                                //find least frequent number:
                                it = mp.begin();
                                int minkey = it->first;
                                it++;
                                while(it != mp.end()){
                                        if(it->second < mp[minkey]){
                                                minkey = it->first;
                                        }
                                        it++;
                                }
                                //now we have the smallest number of occurances
                                if(mp[minkey] <= k){ //if we have enough changes {k}
                                        //change all of the least frequent # to the most frequent
                                        mp[maxkey] += mp[minkey];
                                        k = k-mp[minkey];
                                        mp.erase(minkey);
                                } else { //we have less changes than the least frequent num
                                        //so changes wont reduce the number of moves.
                                        k = 0;
                                }

                        }
                }
                //do actual count of operations (# of distinct nums in list)
                cout << mp.size()<< "\n";
                mp = {}; //clear map at end of each loop
        }

        return 0;
}

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

using namespace std;

int nproblems;
int n;
int k;
int tmp;
int minkey;
map<int, int> mp;
map<int, int>::iterator it = mp.begin();
int get_minkey();

int main(){
        cin >> nproblems;
        for(int i = 0; i< nproblems; i++){
                cin >> n;
                cin >> k;
                for(int j = 0; j <n; j++){
                        cin >> tmp; //current element of array
                        if(mp.count(tmp)){ //if key is allready in map
                                mp[tmp]++; //increment count
                        } else {
                                mp[tmp] = 1; //or add to map w/ count 1
                        }
                }
                if(mp.empty()){
                        mp = {};
                        cout << 0 << "\n";
                        continue;
                }

                //find number with maximum occurences
                int maxkey;
                it = mp.begin();
                maxkey = it -> first;
                it++;
                while(it != mp.end()){
                        if(it -> second >= mp[maxkey]){ //chose last max so that we dont collide w. min
                                maxkey = it -> first;
                        }
                        it++;
                }
                // remove k least frequent numbers
                if(mp.size() > 1){
                        while (k > 0){
                                //find least frequent number:
                                minkey = get_minkey();
                                
                                //now we have the smallest number of occurances
                                if(mp[minkey] <= k){ //if we have enough changes {k}
                                        //change all of the least frequent # to the most frequent
                                        mp[maxkey] += mp[minkey];
                                        k = k-mp[minkey];
                                        mp.erase(minkey);
                                } else { //we have less changes than the least frequent num
                                        //so changes wont reduce the number of moves.
                                        k = 0;
                                }

                        }
                }
                //do actual count of operations (# of distinct nums in list)
                cout << mp.size()<< "\n";
                mp = {}; //clear map at end of each loop
        }

        return 0;
}

int get_minkey(){
        it = mp.begin();
        int mn;
        mn = it->first;
        it++;
        while(it != mp.end()){
                if(mp[mn] == 1){
                        return mn; //get out of the llop as soon as we get a single
                }
                if(it->second < mp[mn]){
                        mn = it->first;
                }
                it++;
        }

        return mn;
}