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> all = {0};
int i = 1;
while (i <= n){
int j = i;
while (j <= n && a[i] == a[j]){
j++;
}
all.pb(j - i);
i = j;
}
ll out = 0, pr = 0;
for (int i: all) out += (1LL * i * (i + 1)) / 2;
n = (int) all.size() - 1;
int sum = 0;
for (int i = 1; i <= n; i++){
out += 1LL * all[i] * (1LL * i * sum - pr);
sum += all[i];
pr += 1LL * (i - 1) * all[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... |