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

Previous submission (verdict: Runtime error on test 4):
#include <bits/stdc++.h>
using namespace std;

#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long

int main() {
    fast_io;
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        vector<int> arr(n);
        for (int i = 0; i < n; ++i) {
            cin >> arr[i];
        }

        // Step 1: Calculate frequencies
        int max_val = *max_element(arr.begin(), arr.end());
        vector<int> freq(max_val + 1, 0);
        for (int num : arr) {
            freq[num]++;
        }

        // Step 2: Remove zeros and sort frequencies
        vector<int> counts;
        for (int count : freq) {
            if (count > 0) {
                counts.push_back(count);
            }
        }
        sort(counts.begin(), counts.end()); // Sort by frequency (ascending)

        // Step 3: Use k to reduce frequencies
        for (int i = 0; i < counts.size() && k > 0; ++i) {
            if (k >= counts[i]) {
                k -= counts[i]; // Use all occurrences of this element
                counts[i] = 0;  // Mark as removed
            } else {
                counts[i] -= k; // Reduce remaining frequency
                k = 0;          // All of k is used
            }
        }

        // Step 4: Count remaining distinct elements
        int cnt = 0;
        for (int count : counts) {
            if (count > 0) {
                cnt++;
            }
        }

        // If no distinct elements are left, output 1
        cout << (cnt == 0 ? 1 : cnt) << endl;
    }

    return 0;
}


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

#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int main() {
    fast_io;
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        vector<int> arr(n);
        for (int i = 0; i < n; ++i) {
            cin >> arr[i];
        }

        // Step 1: Calculate frequencies
        unordered_map<int, int> freq;
        for (int num : arr) {
            freq[num]++;
        }

        // Step 2: Store frequencies in a vector and sort
        vector<int> counts;
        for (auto it : freq) {
            counts.push_back(it.second);
        }
        sort(counts.begin(), counts.end()); // Sort by frequency (ascending)

        // Step 3: Use k to reduce frequencies
        for (int i = 0; i < counts.size() && k > 0; ++i) {
            if (k >= counts[i]) {
                k -= counts[i]; // Remove all occurrences of this element
                counts[i] = 0;  // Mark as removed
            } else {
                counts[i] -= k; // Reduce the remaining frequency
                k = 0;          // All of k is used
            }
        }

        // Step 4: Count remaining distinct elements
        int distinct_count = 0;
        for (int count : counts) {
            if (count > 0) {
                distinct_count++;
            }
        }

        // Output the result
        cout << (distinct_count == 0 ? 1 : distinct_count) << "\n";
    }

    return 0;
}