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. C++ Solution
---------------

```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n, k;
vector<pair<int, int>> dumbbells;

void read() {
    cin >> n >> k;
    dumbbells.resize(n);
    cin >> dumbbells;
}

void solve() {
    // Each set fixes a multiset of k distinct masses shared by every set, so a
    // mass can contribute one dumbbell to each of the t sets only if it has at
    // least t dumbbells. Group costs by mass; the number of sets t is limited
    // to k (k distinct masses per set) and to how many masses are frequent
    // enough, so the best t is the count of the k-th most frequent mass: with
    // that t we have at least k masses each owning >= t dumbbells.
    //
    // For each eligible mass (count >= t) take its t most expensive dumbbells
    // and sum them; we then pick the k masses with the largest such sums to
    // maximize total cost. If there are fewer than k distinct masses no set
    // can be formed and the answer is 0 0.

    map<int, vector<int>> mass_to_costs;
    for(auto [m, c]: dumbbells) {
        mass_to_costs[m].push_back(c);
    }

    vector<pair<int, int>> frequency;
    for(auto& [mass, costs]: mass_to_costs) {
        sort(costs.rbegin(), costs.rend());
        frequency.push_back({(int)costs.size(), mass});
    }

    sort(frequency.rbegin(), frequency.rend());

    if((int)frequency.size() < k) {
        cout << 0 << ' ' << 0 << '\n';
        return;
    }

    int t = frequency[k - 1].first;

    vector<int64_t> selected;
    for(auto [count, mass]: frequency) {
        if(count < t) {
            break;
        }

        int64_t sum_costs = 0;
        for(int i = 0; i < t; i++) {
            sum_costs += mass_to_costs[mass][i];
        }

        selected.push_back(sum_costs);
    }

    sort(selected.rbegin(), selected.rend());

    int64_t total = 0;
    for(int i = 0; i < k; i++) {
        total += selected[i];
    }

    cout << t << ' ' << total << '\n';
}

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

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}
```

4. Python Solution
------------------

```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).
