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

Previous submission (verdict: Wrong answer on test 1):
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int T; // Number of test cases
    cin >> T;

    while (T--) {
        int n, m;
        cin >> n >> m;

        vector<long long> a(n); // Input array
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }

        // Sort the array to group identical values together
        sort(a.begin(), a.end());

        // Calculate frequencies of each distinct element
        vector<long long> freq;
        long long count = 1; // Frequency counter
        for (int i = 1; i < n; i++) {
            if (a[i] == a[i - 1]) {
                count++; // Increment frequency if the same value continues
            } else {
                freq.push_back(count); // Store frequency
                count = 1;             // Reset frequency counter
            }
        }
        freq.push_back(count); // Add the last frequency

        // Sort the frequencies to handle smallest groups first
        sort(freq.begin(), freq.end());

        // Minimize the number of distinct groups
        int distinct_groups = freq.size(); // Start with all groups
        for (int i = 0; i < freq.size(); i++) {
            if (m >= freq[i]) {
                m -= freq[i];     // Use changes to eliminate this group
                distinct_groups--; // Reduce the count of distinct groups
            } else {
                break; // No more changes can be made
            }
        }

        // Output the result for this test case
        cout << distinct_groups << endl;
    }

    return 0;
}


Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;

int main() {
    int T; // Number of test cases
    cin >> T;

    while (T--) {
        long long n, m;
        cin >> n >> m;

        vector<long long> a(n); // Array to store input numbers
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }

        // Sort the array to group identical elements
        sort(a.begin(), a.end());

        vector<long long> freq; // Frequency array for unique elements
        long long count = 1; // Initialize frequency counter

        // Calculate frequencies
        for (int i = 1; i < n; i++) {
            if (a[i] != a[i - 1]) {
                freq.push_back(count); // Store frequency
                count = 1;             // Reset counter
            } else {
                count++;
            }
        }
        freq.push_back(count); // Add the last frequency

        // Sort the frequency array
        sort(freq.begin(), freq.end());

        // Start with all distinct groups
        long long distinct_groups = freq.size();

        // Use available changes to reduce distinct groups
        for (auto f : freq) {
            if (m >= f) {
                m -= f;               // Use changes for this group
                distinct_groups--;    // Reduce group count
            } else {
                break; // No more changes can be used
            }
        }

        // Output the result
        cout << distinct_groups << '\n';
    }

    return 0;
}