1. Abridged Problem Statement  
Given n dumbbells, each with a mass mᵢ and cost cᵢ, you want to form as many “uniform” sets as possible. Each set must contain exactly k dumbbells of distinct masses, and every set must use the same k masses. First maximize the number of sets t; then, among all ways to achieve t sets, maximize the total cost of the selected dumbbells. Output t and that maximum total cost.

2. Detailed Editorial  
Let’s rephrase and solve step by step:

1. Group by Mass  
   Build a map from mass → list of costs. For each mass m, collect all costs of dumbbells of mass m.

2. Sort Costs Descending  
   For each mass, sort its cost list in descending order. If you decide to make t sets using mass m, you will pick the t most expensive dumbbells of that mass.

3. Compute Frequency List  
   Create a list of pairs (countₘ, m) where countₘ = number of dumbbells available of mass m. Sort that list descending by countₘ. This tells you which masses have the most supply.

4. Determine Maximum t  
   To form t sets you need at least k masses that each have ≥ t dumbbells. If you look at the sorted counts, the k-th largest count (say C) is exactly the maximum t. If there are fewer than k distinct masses, answer is t=0, cost=0.

5. Maximize Cost for t Sets  
   Consider all masses whose countₘ ≥ C. For each such mass m, take the sum of its top C costs; call that Sₘ. You need to pick exactly k masses out of these to maximize total cost, so sort the Sₘ values in descending order and sum the top k values. That sum is the secondary objective.

6. Output  
   Print t=C and the computed total cost.

Time complexity:  
– Grouping & sorting each mass’s cost list: ∑ₘ O(countₘ log countₘ) = O(n log n)  
– Sorting frequencies: O(M log M) where M ≤ n distinct masses  
– Summing and final sort of at most M sums: O(n + M log M)  
Fits easily under n, k ≤ 4000.

3. Provided C++ Solution with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // Read n (total dumbbells) and k (dumbbells per set)
    int n, k;
    cin >> n >> k;

    // Step 1: Group costs by mass
    // mass2costs[m] will be the vector of costs for dumbbells of mass m
    unordered_map<int, vector<int>> mass2costs;
    mass2costs.reserve(n);
    for (int i = 0; i < n; i++) {
        int m, c;
        cin >> m >> c;
        mass2costs[m].push_back(c);
    }

    // Step 2 & 3: Build a frequency list: (count, mass)
    // and sort each mass's costs descending
    vector<pair<int,int>> freq;  // pairs of (count, mass)
    freq.reserve(mass2costs.size());
    for (auto &entry : mass2costs) {
        int mass = entry.first;
        auto &costs = entry.second;
        // Sort costs in descending order so top costs come first
        sort(costs.begin(), costs.end(), greater<int>());
        // Record how many we have of this mass
        freq.emplace_back((int)costs.size(), mass);
    }

    // Sort masses by descending count
    sort(freq.begin(), freq.end(), greater<pair<int,int>>());

    // If fewer than k distinct masses, we can't form even one set
    if ((int)freq.size() < k) {
        cout << 0 << " " << 0 << "\n";
        return 0;
    }

    // Step 4: The maximum number of sets t is the k-th largest count
    // freq is 0-based, so index k-1 gives the k-th largest
    int t = freq[k-1].first;

    // Step 5: For all masses with count >= t, compute sum of top t costs
    vector<ll> sums;
    sums.reserve(freq.size());
    for (auto &p : freq) {
        int count = p.first;
        int mass = p.second;
        if (count < t) {
            // Since freq is sorted descending, we can break early
            break;
        }
        // Sum the highest t costs for this mass
        ll s = 0;
        auto &costs = mass2costs[mass];
        for (int i = 0; i < t; i++) {
            s += costs[i];
        }
        sums.push_back(s);
    }

    // Pick the top k sums to maximize total cost
    sort(sums.begin(), sums.end(), greater<ll>());

    // Sum the first k of them
    ll totalCost = 0;
    for (int i = 0; i < k; i++) {
        totalCost += sums[i];
    }

    // Step 6: Output t and the maximum total cost
    cout << t << " " << totalCost << "\n";
    return 0;
}
```

4. Python Solution with Detailed Comments  
```python
import sys
from collections import defaultdict

def solve():
    data = sys.stdin.read().split()
    n, k = map(int, data[:2])
    # Step 1: group costs by mass
    mass_to_costs = defaultdict(list)
    idx = 2
    for _ in range(n):
        m = int(data[idx]); c = int(data[idx+1])
        idx += 2
        mass_to_costs[m].append(c)

    # Step 2 & 3: build list of (count, mass) and sort each list of costs
    freq = []
    for m, costs in mass_to_costs.items():
        costs.sort(reverse=True)      # descending costs
        freq.append((len(costs), m))  # record how many of this mass

    # Sort masses by how many dumbbells they have, descending
    freq.sort(reverse=True)

    # If we have fewer than k masses total, no sets can be formed
    if len(freq) < k:
        print(0, 0)
        return

    # Step 4: t = maximum number of sets = k-th largest count
    t = freq[k-1][0]

    # Step 5: For each mass with at least t dumbbells,
    # sum its top t costs, collect those sums
    possible_sums = []
    for count, m in freq:
        if count < t:
            break  # remaining masses have too few
        # sum the t largest costs for mass m
        possible_sums.append(sum(mass_to_costs[m][:t]))

    # Choose the best k masses by these sums
    possible_sums.sort(reverse=True)
    total_cost = sum(possible_sums[:k])

    # Step 6: Output t and the maximal total cost
    print(t, total_cost)

if __name__ == "__main__":
    solve()
```

5. Compressed Editorial  
- Group dumbbells by mass and sort each group’s costs descending.  
- Let counts be the sizes of these groups; sort counts descending.  
- The number of sets t = the k-th largest group size.  
- For every group with size ≥ t, sum its top t costs; pick the k largest such sums and add them.  
- Output (t, total_cost).