Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
The order of Valera's list is a distraction. What matters is how many times each fruit name appears.
If a fruit appears times and gets price , its contribution is . So the total is a sum over distinct fruits, not over list lines directly.
There may be more price tags than fruit types Valera needs. Since all counts are positive, for the minimum you only care about the cheapest tags, and for the maximum the most expensive tags, where is the number of distinct listed fruits.
Now it is just pairing frequencies with prices. To minimize, large frequencies should get small prices. To maximize, large frequencies should get large prices. This is the rearrangement inequality doing all the work while pretending to be greedy.
Count frequencies, sort them decreasing. Sort prices increasing. Then
and
The actual fruit names are mostly noise. We only need to know how many times each distinct fruit appears in Valera's list.
If some fruit appears times and Ashot assigns it price , then that fruit contributes
to the total cost.
So after counting the list, the problem becomes:
Given frequencies and price tags, assign one price to each of these fruits to minimize or maximize
Here is the number of distinct fruits in the list, and the statement guarantees .
For the minimum, every frequency is positive, so assigning a larger price instead of a smaller unused one can only make the sum worse. Therefore, the minimum uses the cheapest price tags.
For the maximum, similarly, it uses the most expensive price tags. The unused tags can be slapped onto fruits Valera does not buy. Ashot can do that; nobody cares.
For minimization, big frequencies should get small prices.
Suppose are two frequencies and are two prices. Compare the two pairings:
versus
Their difference is
So pairing the larger frequency with the smaller price is never worse. That proves the greedy pairing.
For maximization, flip the logic: pair large frequencies with large prices.
price[i].price[n - 1 - i].Formally:
Sorting dominates:
with , so this is hilariously fast. Memory usage is
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int n, m;
cin >> n >> m;
vector<int> price(n);
for (int &x : price) cin >> x;
map<string, int> cnt;
for (int i = 0; i < m; i++) {
string s;
cin >> s;
cnt[s]++;
}
vector<int> freq;
for (auto &it : cnt) freq.push_back(it.second);
sort(price.begin(), price.end());
sort(freq.begin(), freq.end(), greater<int>());
int d = (int)freq.size();
int mn = 0, mx = 0;
for (int i = 0; i < d; i++) {
mn += freq[i] * price[i];
mx += freq[i] * price[n - 1 - i];
}
cout << mn << ' ' << mx << '\n';
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int n, m;
cin >> n >> m;
vector<int> price(n);
for (int &x : price) cin >> x;
map<string, int> cnt;
for (int i = 0; i < m; i++) {
string s;
cin >> s;
cnt[s]++;
}
vector<int> freq;
for (auto &it : cnt) freq.push_back(it.second);
sort(price.begin(), price.end());
sort(freq.begin(), freq.end(), greater<int>());
int d = (int)freq.size();
int mn = 0, mx = 0;
for (int i = 0; i < d; i++) {
mn += freq[i] * price[i];
mx += freq[i] * price[n - 1 - i];
}
cout << mn << ' ' << mx << '\n';
return 0;
}