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

Previous submission (verdict: Wrong answer on test 2):
#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
                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: 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;
}