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;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q; cin>>n>>q;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++){
cin>>a[i];
}
sort(a.begin() + 1, a.end());
vector<int> b;
int i = 1;
while (i <= n){
int j = i;
while (j <= n && a[i] == a[j]){
j++;
}
b.pb(j - i);
i = j;
}
sort(b.begin(), b.end());
n = (int) b.size();
vector<int> x(n + 1);
for (int i = 0; i < n; i++){
if (i % 2){
x[n - i / 2] = b[i];
}
else {
x[i / 2 + 1] = b[i];
}
}
ll out = 0, pr = 0;
for (int i = 1; i <= n; i++) out += 1LL * x[i] * (x[i] + 1) / 2;
int sum = 0;
for (int i = 1; i <= n; i++){
out += 1LL * x[i] * (1LL * i * sum - pr);
sum += x[i];
pr += 1LL * (i - 1) * x[i];
}
cout<<out<<"\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... |