#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, Q;
cin >> n >> Q;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<int> cnt(300001);
for (int i = 0; i < n; ++i) {
++cnt[a[i]];
}
vector<int> x;
for (int i = 1; i <= 300000; ++i) {
if (cnt[i] > 0)
x.push_back(i);
}
sort(x.rbegin(), x.rend());
int k = x.size();
deque<int> q;
for (int i = 0; i < k; ++i) {
if (i & 1)
q.push_front(x[i]);
else
q.push_back(x[i]);
}
x.clear();
for (int i : q)
x.push_back(i);
long long ans = n * 1ll * (n + 1) / 2 * k;
long long sum = 0;
for (int i = 0; i < k; ++i) {
ans -= sum * (sum + 1) / 2;
sum += x[i];
}
sum = 0;
for (int i = k - 1; i >= 0; --i) {
ans -= sum * (sum + 1) / 2;
sum += x[i];
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1568 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1472 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1568 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1472 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1568 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1472 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |