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;
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second
const int maxn = 3e5;
int n, q;
int base_cnt[maxn + 1];
int ans;
vector<int> v;
void solve() {
cin >> n >> q;
for(int i = 1; i <= n; i++) {
int x;
cin >> x;
base_cnt[x]++;
}
for(int i = 1; i <= maxn; i++) {
if(base_cnt[i] != 0) {
int x = base_cnt[i];
ans += (x * (x + 1)) / 2;
v.pb(x);
}
}
n = v.size();
sort(v.rbegin(), v.rend());
int lsum = 0, rsum = 0, sum = 0, cnt = 0;
for(int cur : v) {
cnt++;
if(lsum < rsum) {
ans += cur * lsum;
lsum += sum;
lsum += 2 * cur;
rsum += (cnt + 1) * cur;
} else {
ans += cur * rsum;
rsum += sum;
rsum += 2 * cur;
lsum += (cnt + 1) * cur;
}
sum += cur;
}
cout << ans << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
/*
19 1
1 2 3 3 4 4 4 5 5 5 5 5 6 6 6 6 6 6 6
1 19
*/
# | 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... |