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 i64 = long long;
#ifdef DEBUG
#include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
#define debug(...) void(23)
#endif
constexpr int max_A = int(3E5) + 5;
int cnt[max_A];
i64 calc(std::vector<i64> x) {
int n = int(x.size());
i64 ans = 0;
for (int i = 0; i < n; ++i) {
ans += x[i] * (x[i] + 1) / 2;
for (int j = i + 1; j < n; ++j) {
ans += x[i] * x[j] * (j - i + 1);
}
}
return ans;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, Q;
std::cin >> N >> Q;
std::vector<int> A(N);
for (int i = 0; i < N; ++i) {
std::cin >> A[i];
}
for (int i = 0; i < N; ++i) {
++cnt[A[i]];
}
std::vector<i64> x;
for (int i = 0; i < max_A; ++i) {
if (cnt[i] != 0) {
x.emplace_back(cnt[i]);
}
}
int n = int(x.size());
std::sort(x.begin(), x.end());
std::vector<i64> w(n);
int l = 0, r = n - 1;
for (int i = 0; i < n; ++i) {
if (~i & 1) w[i] = x[l++];
else w[i] = x[r--];
}
debug(w);
i64 ans = calc(w);
std::cout << ans << '\n';
return 0;
}
# | 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... |