This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int nmax = 3e5 + 5;
int freq[nmax];
static ll solve(vector<int> val) {
ll total = 0;
for(auto x : val)
total += (ll)x * (x + 1) / 2;
ll sum = 0, int1 = 0;
for(auto x : val) {
int1 += sum;
total += (ll)x * (int1 + sum);
sum += x;
}
return total;
}
int main() {
int n, q;
cin >> n >> q;
for(int i = 0, x; i < n; i++) {
cin >> x;
freq[x]++;
}
vector<int> val;
for(auto x : freq)
if(x != 0)
val.push_back(x);
sort(val.begin(), val.end());
ll cst = solve(val);
for(int i = 1; i < n; i++) {
reverse(val.end() - i, val.end());
cst = min(cst, solve(val));
reverse(val.end() - i, val.end());
}
cout << cst << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |